feat: 新增角色数据收集功能,支持多语言角色信息和地区提取(9.3 修改
This commit is contained in:
@@ -8,11 +8,13 @@ const {
|
||||
generateImageName,
|
||||
toOutputBaseName,
|
||||
buildParseApiUrl,
|
||||
fetchParseHtml,
|
||||
resolveImageUrl,
|
||||
getFileBaseNameFromUrl,
|
||||
listLocalCharacters,
|
||||
sleep,
|
||||
randomSleep,
|
||||
} = require('./utils');
|
||||
const { collectCharacterData, extractRowMeta } = require('./character-data');
|
||||
|
||||
/**
|
||||
* @typedef {Object} WikiConfig
|
||||
@@ -26,8 +28,10 @@ const {
|
||||
* @property {string} imageSelector 图片 CSS 选择器(相对于 scope 或整页)
|
||||
* @property {string} [imageUrlAttr='src'] 图片 URL 所在属性,如 src、data-src
|
||||
* @property {(name: string) => string} [nameTransform] 文件名二次处理
|
||||
* @property {number} [downloadDelayMs] 下载间隔,默认 500ms
|
||||
* @property {number} [downloadDelayMs] 请求最小间隔,默认走随机 400~1600ms
|
||||
* @property {string} [verifyApiUrl] 摄影集页面 API,配置后自动验证服务器已录入角色
|
||||
* @property {string} [regionHeader] 列表表格中国家/地区列头,如 Region
|
||||
* @property {string} [regionSelector] 详情页 infobox 中国家选择器
|
||||
*/
|
||||
|
||||
/** @type {WikiConfig[]} */
|
||||
@@ -43,6 +47,8 @@ const wikiConfigs = [
|
||||
imageUrlAttr: 'data-src',
|
||||
nameTransform: (name) => name.replace(/-icon$/, ''),
|
||||
verifyApiUrl: 'https://paul.ren/api/page/get?slug=photo-collection-genshin',
|
||||
regionHeader: 'Region',
|
||||
regionSelector: '[data-source="region"] .pi-data-value a',
|
||||
},
|
||||
{
|
||||
name: 'sr',
|
||||
@@ -55,6 +61,7 @@ const wikiConfigs = [
|
||||
imageUrlAttr: 'data-src',
|
||||
nameTransform: (name) => name.replace(/character-/, '').replace(/-icon$/, ''),
|
||||
verifyApiUrl: 'https://paul.ren/api/page/get?slug=photo-collection-sr',
|
||||
regionSelector: '[data-source="world"] .pi-data-value a',
|
||||
},
|
||||
{
|
||||
name: 'zzz',
|
||||
@@ -67,6 +74,8 @@ const wikiConfigs = [
|
||||
imageUrlAttr: 'src',
|
||||
nameTransform: (name) => name.replace(/agent-/, '').replace(/-icon$/, ''),
|
||||
verifyApiUrl: 'https://paul.ren/api/page/get?slug=photo-collection-zzz',
|
||||
regionHeader: 'Faction',
|
||||
regionSelector: '[data-source="faction"] .pi-data-value a',
|
||||
},
|
||||
{
|
||||
name: 'nte',
|
||||
@@ -78,6 +87,7 @@ const wikiConfigs = [
|
||||
imageUrlAttr: 'data-src',
|
||||
nameTransform: (name) => name.replace(/-icon$/, ''),
|
||||
verifyApiUrl: 'https://paul.ren/api/page/get?slug=photo-collection-nte',
|
||||
regionSelector: '[data-source="affiliation"] .pi-data-value a',
|
||||
},
|
||||
{
|
||||
name: 'ww',
|
||||
@@ -87,6 +97,7 @@ const wikiConfigs = [
|
||||
imageSelector: 'table.article-table img[data-relevant="1"]',
|
||||
nameTransform: (name) => name.replace(/^resonator-/, ''),
|
||||
verifyApiUrl: 'https://paul.ren/api/page/get?slug=photo-collection-ww',
|
||||
regionSelector: '[data-source="nation"] .pi-data-value a',
|
||||
},
|
||||
// {
|
||||
// name: 'blue-archive',
|
||||
@@ -111,26 +122,7 @@ function resolveApiUrl(config) {
|
||||
async function fetchWikiHtml(config) {
|
||||
const apiUrl = resolveApiUrl(config);
|
||||
console.log('apiUrl', apiUrl);
|
||||
|
||||
const response = await fetch(apiUrl, {
|
||||
headers: {
|
||||
'user-agent': 'Mozilla/5.0 (compatible; download-character-img/1.0)',
|
||||
accept: 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`[${config.name}] 请求失败 ${response.status}: ${apiUrl}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const html = data?.parse?.text?.['*'];
|
||||
|
||||
if (!html) {
|
||||
throw new Error(`[${config.name}] API 响应中未找到页面 HTML`);
|
||||
}
|
||||
|
||||
return html;
|
||||
return fetchParseHtml(apiUrl, config.name);
|
||||
}
|
||||
|
||||
function extractImages(html, config) {
|
||||
@@ -165,14 +157,19 @@ function extractImages(html, config) {
|
||||
|
||||
const cleanedUrl = cleanImageUrl(rawUrl);
|
||||
const rawName = generateImageName(cleanedUrl);
|
||||
console.log('rawName', rawName);
|
||||
|
||||
if (!rawName || seenNames.has(rawName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
seenNames.add(rawName);
|
||||
results.push({ url: cleanedUrl, rawName });
|
||||
|
||||
const { wikiHref, listRegion } = extractRowMeta(
|
||||
$,
|
||||
$img,
|
||||
config.regionHeader
|
||||
);
|
||||
results.push({ url: cleanedUrl, rawName, wikiHref, listRegion });
|
||||
});
|
||||
|
||||
console.log('已去重结果数量:', results.length);
|
||||
@@ -181,6 +178,7 @@ function extractImages(html, config) {
|
||||
}
|
||||
|
||||
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',
|
||||
@@ -197,7 +195,6 @@ async function downloadImage(url, filePath) {
|
||||
}
|
||||
|
||||
async function downloadWiki(config) {
|
||||
const delay = config.downloadDelayMs ?? 500;
|
||||
const nameTransform = config.nameTransform ?? ((name) => name);
|
||||
|
||||
console.log(`\n=== ${config.name} ===`);
|
||||
@@ -237,15 +234,13 @@ async function downloadWiki(config) {
|
||||
console.error(` 失败: ${error.message}`);
|
||||
failed += 1;
|
||||
}
|
||||
|
||||
if (index < images.length - 1) {
|
||||
await sleep(delay);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(
|
||||
`完成: 下载 ${downloaded},跳过 ${skipped},失败 ${failed}`
|
||||
);
|
||||
|
||||
await collectCharacterData(config, images, nameTransform);
|
||||
}
|
||||
|
||||
async function fetchServerCharacters(verifyApiUrl) {
|
||||
|
||||
Reference in New Issue
Block a user