feat: 更新角色数据,优化表格获取方法,多语言别名匹配更正,图片获取改 Curl 形式

This commit is contained in:
2026-10-19 01:31:32 +08:00
parent a7addc13a7
commit ee21802602
8 changed files with 781 additions and 69 deletions
+34 -13
View File
@@ -1,5 +1,6 @@
#!/usr/bin/env node
const { execFile } = require('child_process');
const fs = require('fs');
const path = require('path');
const cheerio = require('cheerio');
@@ -10,6 +11,7 @@ const {
buildParseApiUrl,
fetchParseHtml,
resolveImageUrl,
resolveSiteOrigin,
getFileBaseNameFromUrl,
listLocalCharacters,
randomSleep,
@@ -177,21 +179,40 @@ function extractImages(html, config) {
return results;
}
async function downloadImage(url, filePath) {
await randomSleep();
const response = await fetch(url, {
headers: {
accept: 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
'user-agent': 'Mozilla/5.0 (compatible; download-character-img/1.0)',
},
});
function curlDownload(url, filePath, referer) {
const args = [
'-fsSL',
'--noproxy', '*',
'--connect-timeout', '15',
'--max-time', '60',
'-A', 'Mozilla/5.0',
'-o', filePath,
url,
];
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
if (referer) {
args.splice(args.length - 1, 0, '-e', referer);
}
const buffer = Buffer.from(await response.arrayBuffer());
fs.writeFileSync(filePath, buffer);
return new Promise((resolve, reject) => {
execFile('curl.exe', args, (error, _stdout, stderr) => {
if (!error) {
resolve();
return;
}
fs.rmSync(filePath, { force: true });
const detail = stderr?.toString().trim();
reject(new Error(detail || error.message));
});
});
}
async function downloadImage(url, filePath, referer) {
await randomSleep();
// static.wikia 会挑战 Node fetch 的 TLS 指纹,走不走 7897 都是 403。
// curl 直连可以拿到图片,所以这里强制不走代理。
await curlDownload(url, filePath, referer);
}
async function downloadWiki(config) {
@@ -228,7 +249,7 @@ async function downloadWiki(config) {
console.log(` 来源: ${image.url}`);
try {
await downloadImage(image.url, filePath);
await downloadImage(image.url, filePath, `${resolveSiteOrigin(config)}/`);
downloaded += 1;
} catch (error) {
console.error(` 失败: ${error.message}`);