@cloud411716/fancy-webnovel 4.24.1 → 4.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/package.json +1 -1
- package/plugins/fancy-scan/index.js +36 -3
- package/plugins/fancy-scan/scripts/run-scan.js +14 -8
- package/plugins/fancy-scan/scripts/scrapers/fanqie-rank-scraper.js +58 -8
- package/plugins/fancy-scan/scripts/scrapers/jjwxc-rank-scraper.js +51 -9
- package/plugins/fancy-scan/scripts/scrapers/qimao-rank-scraper.js +46 -0
- package/plugins/fancy-scan/scripts/scrapers/selfcheck.js +715 -0
- package/plugins/fancy-scan/templates.js +10 -1
- package/plugins/fancy-scan/visual-selfcheck.js +536 -0
package/README.md
CHANGED
|
@@ -42,6 +42,12 @@ allowBuilds:
|
|
|
42
42
|
工作区 `tessdata` 和系统 tessdata 目录。DSH 沙箱里外网请求经常是 `fetch failed`,
|
|
43
43
|
把 `chi_sim.traineddata` 放到上述任一目录即可离线 OCR。
|
|
44
44
|
|
|
45
|
+
番茄 OCR 模式在采集每个榜单前会做一次可选的视觉自检:先截一张书籍卡片和一张
|
|
46
|
+
候选元素清单,由当前 LLM 判断书名/作者/状态/简介分别对应哪个候选编号,再把编号
|
|
47
|
+
还原成字段选择器。选择器在本页所有卡片上的命中率不足 80% 时自动回退默认选择器。
|
|
48
|
+
模型不支持视觉时会直接回退,不会改用 OCR 代替视觉自检。设置
|
|
49
|
+
`FANCY_SCAN_VISUAL_SELF_CHECK=0` 可关闭晋江、七猫和番茄 OCR 的视觉自检。
|
|
50
|
+
|
|
45
51
|
## 依赖解析
|
|
46
52
|
|
|
47
53
|
依赖声明在 `dependencies` / `peerDependencies` 中。用 `dsh plugin add` 从 npm 安装时,
|
package/package.json
CHANGED
|
@@ -24,6 +24,11 @@ import { captureFileState, verifyBlockWritten } from './scripts/report-validate.
|
|
|
24
24
|
import { inspectExistingBlock, EXISTING_BLOCK_STATUS } from './existing-block.js';
|
|
25
25
|
import { waitForAnalysisTurn } from './wait-turn.js';
|
|
26
26
|
import { deployScanTools, writeScanManifest } from './tool-deploy.js';
|
|
27
|
+
import {
|
|
28
|
+
shouldRunSelfCheck,
|
|
29
|
+
selfCheckDirFor,
|
|
30
|
+
startSelfCheckService,
|
|
31
|
+
} from './visual-selfcheck.js';
|
|
27
32
|
import {
|
|
28
33
|
buildVisionPrompt,
|
|
29
34
|
mergeVisionCategory,
|
|
@@ -542,6 +547,8 @@ export async function apply(ctx) {
|
|
|
542
547
|
const receiptDates = new Set();
|
|
543
548
|
let lastDate = '';
|
|
544
549
|
let processedRanks = 0;
|
|
550
|
+
const selfCheckDisabled = /^(0|false|off|no)$/i.test(String(process.env.FANCY_SCAN_VISUAL_SELF_CHECK || ''));
|
|
551
|
+
let selfCheckUnsupported = false;
|
|
545
552
|
for (const rankId of rankIds) {
|
|
546
553
|
// 用户按停止后不要再启动下一个榜单的采集(每个榜单可能跑几十分钟)。
|
|
547
554
|
if (signal?.aborted) {
|
|
@@ -555,11 +562,30 @@ export async function apply(ctx) {
|
|
|
555
562
|
`(${processedRanks}/${rankIds.length}:${rankLabel})`
|
|
556
563
|
);
|
|
557
564
|
|
|
565
|
+
let selfCheckDir = '';
|
|
566
|
+
let selfCheck = null;
|
|
567
|
+
if (!selfCheckDisabled && !selfCheckUnsupported && shouldRunSelfCheck(platform, scanMode || DEFAULT_SCAN_MODE)) {
|
|
568
|
+
selfCheckDir = selfCheckDirFor(projectRoot, platform, rankId);
|
|
569
|
+
mkdirSync(selfCheckDir, { recursive: true });
|
|
570
|
+
selfCheck = startSelfCheckService({
|
|
571
|
+
dir: selfCheckDir,
|
|
572
|
+
projectRoot,
|
|
573
|
+
invocation,
|
|
574
|
+
tasks,
|
|
575
|
+
signal,
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
const scanArgs = [
|
|
580
|
+
join(SCAN_SCRIPTS_DIR, 'run-scan.js'),
|
|
581
|
+
'--project-root', projectRoot, '--platform', platform, '--rank', rankId,
|
|
582
|
+
'--scan-mode', scanMode || DEFAULT_SCAN_MODE,
|
|
583
|
+
];
|
|
584
|
+
if (selfCheckDir) scanArgs.push('--self-check-dir', selfCheckDir);
|
|
585
|
+
|
|
558
586
|
const scriptResult = await spawnScript(
|
|
559
587
|
'node',
|
|
560
|
-
|
|
561
|
-
'--project-root', projectRoot, '--platform', platform, '--rank', rankId,
|
|
562
|
-
'--scan-mode', scanMode || DEFAULT_SCAN_MODE],
|
|
588
|
+
scanArgs,
|
|
563
589
|
// 这一层是兜底预算,必须大于 run-scan.js 内部的 scraper 预算
|
|
564
590
|
// (25 分钟,见 run-scan.js 的 SCRAPER_TIMEOUT_MS)。
|
|
565
591
|
// 若两层等长,外层会先 δ(node 启动耗时)触发:外层杀的是
|
|
@@ -569,6 +595,13 @@ export async function apply(ctx) {
|
|
|
569
595
|
{ timeout: 1800000 }
|
|
570
596
|
);
|
|
571
597
|
|
|
598
|
+
if (selfCheck) {
|
|
599
|
+
const selfCheckResult = await selfCheck.stop();
|
|
600
|
+
if (selfCheckResult && selfCheckResult.unsupported) {
|
|
601
|
+
selfCheckUnsupported = true;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
572
605
|
if (!scriptResult.ok) {
|
|
573
606
|
const innerErr = (typeof scriptResult.error === 'object' && scriptResult.error !== null)
|
|
574
607
|
? (scriptResult.error.message || JSON.stringify(scriptResult.error))
|
|
@@ -19,7 +19,7 @@ import { spawnScript, killProcessTree } from '#infra';
|
|
|
19
19
|
import { EC, errorResponse } from '#error-codes';
|
|
20
20
|
import { atomicWriteFile, atomicWriteJSON } from '#atomic-write';
|
|
21
21
|
import { isSensitiveProjectPath, isValidProjectPath } from '#path-guard';
|
|
22
|
-
import { localDateStamp, normalizeBookIntro, normalizeScanMode, DEFAULT_SCAN_MODE, renderIntroLines, sanitizeBookFields } from '#scraper-utils';
|
|
22
|
+
import { localDateStamp, normalizeBookIntro, normalizeScanMode, DEFAULT_SCAN_MODE, SCAN_MODE_OCR, renderIntroLines, sanitizeBookFields } from '#scraper-utils';
|
|
23
23
|
import { readDeployedState, PHASE_ORDER, DEPLOYED_STATUS } from '#deployed-state';
|
|
24
24
|
|
|
25
25
|
// 调用 scraper 的超时预算。
|
|
@@ -379,7 +379,7 @@ function renderQidianMarkdown(rankLabel, url, books) {
|
|
|
379
379
|
// scraper 参数构造(platform → 调哪个 scraper)
|
|
380
380
|
// ---------------------------------------------------------------------------
|
|
381
381
|
|
|
382
|
-
function scraperArgs(platform, rank, scanMode) {
|
|
382
|
+
function scraperArgs(platform, rank, scanMode, selfCheckDir = '') {
|
|
383
383
|
switch (platform) {
|
|
384
384
|
case 'fanqie': {
|
|
385
385
|
const [channel, type] = rank.split('_');
|
|
@@ -388,13 +388,16 @@ function scraperArgs(platform, rank, scanMode) {
|
|
|
388
388
|
// --scan-mode 决定番茄用明文接口还是截图 OCR 取书名/作者/简介。
|
|
389
389
|
// 非法值在 normalizeScanMode 里已回落到默认,不会透传给 scraper。
|
|
390
390
|
extra: ['--channel', channel, '--type', type, '--top', '10',
|
|
391
|
-
'--scan-mode', normalizeScanMode(scanMode)
|
|
391
|
+
'--scan-mode', normalizeScanMode(scanMode),
|
|
392
|
+
...(selfCheckDir && normalizeScanMode(scanMode) === SCAN_MODE_OCR
|
|
393
|
+
? ['--self-check-dir', selfCheckDir]
|
|
394
|
+
: [])],
|
|
392
395
|
};
|
|
393
396
|
}
|
|
394
397
|
case 'jinjiang':
|
|
395
398
|
return {
|
|
396
399
|
script: 'jjwxc-rank-scraper.js',
|
|
397
|
-
extra: ['--type', rank],
|
|
400
|
+
extra: ['--type', rank, ...(selfCheckDir ? ['--self-check-dir', selfCheckDir] : [])],
|
|
398
401
|
};
|
|
399
402
|
case 'qimao': {
|
|
400
403
|
const parts = rank.split('/');
|
|
@@ -403,7 +406,8 @@ function scraperArgs(platform, rank, scanMode) {
|
|
|
403
406
|
const period = parts[2] || 'day';
|
|
404
407
|
return {
|
|
405
408
|
script: 'qimao-rank-scraper.js',
|
|
406
|
-
extra: ['--channel', channel, '--type', type, '--period', period
|
|
409
|
+
extra: ['--channel', channel, '--type', type, '--period', period,
|
|
410
|
+
...(selfCheckDir ? ['--self-check-dir', selfCheckDir] : [])],
|
|
407
411
|
};
|
|
408
412
|
}
|
|
409
413
|
default:
|
|
@@ -428,8 +432,8 @@ function humanizeSpawnError(raw) {
|
|
|
428
432
|
return `采集超时(超过 ${Math.round(Number(t[1]) / 60000)} 分钟未完成)`;
|
|
429
433
|
}
|
|
430
434
|
|
|
431
|
-
async function runScraper(platform, rank, outDir, scanMode) {
|
|
432
|
-
const args = scraperArgs(platform, rank, scanMode);
|
|
435
|
+
async function runScraper(platform, rank, outDir, scanMode, selfCheckDir = '') {
|
|
436
|
+
const args = scraperArgs(platform, rank, scanMode, selfCheckDir);
|
|
433
437
|
if (!args.script) {
|
|
434
438
|
return { ok: false, error: `平台 ${platform} 暂不支持采集` };
|
|
435
439
|
}
|
|
@@ -533,12 +537,14 @@ async function main() {
|
|
|
533
537
|
// 扫描方式(取数路径)。仅番茄使用;其他平台忽略。
|
|
534
538
|
// 缺失或非法一律按默认(api)处理,绝不把未知值透传给 scraper。
|
|
535
539
|
let scanMode = DEFAULT_SCAN_MODE;
|
|
540
|
+
let selfCheckDir = '';
|
|
536
541
|
|
|
537
542
|
for (let i = 0; i < args.length; i++) {
|
|
538
543
|
if (args[i] === '--project-root') projectRoot = args[i + 1] || '';
|
|
539
544
|
if (args[i] === '--platform') platform = args[i + 1] || '';
|
|
540
545
|
if (args[i] === '--rank') rank = args[i + 1] || '';
|
|
541
546
|
if (args[i] === '--scan-mode') scanMode = args[i + 1] || '';
|
|
547
|
+
if (args[i] === '--self-check-dir') selfCheckDir = args[i + 1] || '';
|
|
542
548
|
}
|
|
543
549
|
|
|
544
550
|
if (!projectRoot || !platform || !rank) {
|
|
@@ -594,7 +600,7 @@ async function main() {
|
|
|
594
600
|
// 调用 scraper
|
|
595
601
|
const r = platform === 'qidian'
|
|
596
602
|
? await scrapeQidianRank(rank, scanDir)
|
|
597
|
-
: await runScraper(platform, rank, scanDir, scanMode);
|
|
603
|
+
: await runScraper(platform, rank, scanDir, scanMode, selfCheckDir);
|
|
598
604
|
|
|
599
605
|
if (!r.ok) {
|
|
600
606
|
throw new Error(r.error);
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
* → 按扫描方式取文本:
|
|
13
13
|
* api —— 番茄明文接口(不截图)
|
|
14
14
|
* ocr —— 截图 title/author/status/desc 四个区域 + tesseract 识别
|
|
15
|
+
* (可选前置视觉自检:候选编号 → 字段选择器)
|
|
15
16
|
* vision —— 每本书截一张完整卡片图,写进 manifest,交给 LLM 识别
|
|
16
17
|
* 5. 失败时写结构化诊断到 stderr(即便进程被 OOM 杀,前面阶段的进度也能保留)
|
|
17
18
|
* 6. 完成后输出 JSON receipt 到 stdout(被 run-scan.js 解析)
|
|
@@ -35,12 +36,18 @@ import {
|
|
|
35
36
|
runCli,
|
|
36
37
|
normalizeScanMode,
|
|
37
38
|
SCAN_MODE_API,
|
|
39
|
+
SCAN_MODE_OCR,
|
|
38
40
|
SCAN_MODE_VISION,
|
|
39
41
|
renderIntroLines,
|
|
40
42
|
sanitizeBookFields,
|
|
41
43
|
sanitizeMarkdownInline,
|
|
42
44
|
resolveTessdata,
|
|
43
45
|
} from '#scraper-utils';
|
|
46
|
+
import {
|
|
47
|
+
FANQIE_DEFAULT_SELECTORS,
|
|
48
|
+
runFanqieOcrSelfCheck,
|
|
49
|
+
validateFanqieSelectors,
|
|
50
|
+
} from './selfcheck.js';
|
|
44
51
|
|
|
45
52
|
// 捕获未处理的 promise 错误——只写 stderr,让上层重试逻辑处理,不要直接退出
|
|
46
53
|
process.on('unhandledRejection', (reason) => {
|
|
@@ -57,6 +64,7 @@ const OUTDIR = getArg(args, "--outdir") || ".";
|
|
|
57
64
|
const CHANNEL = getArg(args, "--channel") || "1";
|
|
58
65
|
const TYPE = getArg(args, "--type") || "2";
|
|
59
66
|
const TOP = parseInt(getArg(args, "--top") || "10", 10);
|
|
67
|
+
const SELF_CHECK_DIR = getArg(args, "--self-check-dir") || '';
|
|
60
68
|
|
|
61
69
|
// 扫描方式(取数路径):
|
|
62
70
|
// api —— 书名/作者/简介走番茄明文接口 /api/book/info(零 OCR 误差)
|
|
@@ -615,7 +623,7 @@ export function parseTags(categoryV2) {
|
|
|
615
623
|
return [...main, ...rest].map(nameOf).filter(Boolean);
|
|
616
624
|
}
|
|
617
625
|
|
|
618
|
-
async function scrapeCategory(page, catHref, catName, ch, type, screenshotBase) {
|
|
626
|
+
async function scrapeCategory(page, catHref, catName, ch, type, screenshotBase, selfCheckState = null) {
|
|
619
627
|
await page.goto('https://fanqienovel.com' + catHref, { waitUntil: 'commit', timeout: 60000 });
|
|
620
628
|
await page.waitForTimeout(3000);
|
|
621
629
|
|
|
@@ -627,6 +635,46 @@ async function scrapeCategory(page, catHref, catName, ch, type, screenshotBase)
|
|
|
627
635
|
if (SCAN_MODE !== SCAN_MODE_API) {
|
|
628
636
|
await prepareOcrFriendlyStyles(page);
|
|
629
637
|
}
|
|
638
|
+
|
|
639
|
+
// OCR 路径:每个榜单(channel+type)只做一次视觉自检,后续题材页
|
|
640
|
+
// 复用同一套选择器,并在本页验证命中率;不匹配就回退默认选择器。
|
|
641
|
+
let fieldSelectors = FANQIE_DEFAULT_SELECTORS;
|
|
642
|
+
if (SCAN_MODE === SCAN_MODE_OCR && selfCheckState) {
|
|
643
|
+
if (!selfCheckState.attempted && SELF_CHECK_DIR) {
|
|
644
|
+
selfCheckState.attempted = true;
|
|
645
|
+
try {
|
|
646
|
+
const self = await runFanqieOcrSelfCheck(page, {
|
|
647
|
+
dir: SELF_CHECK_DIR,
|
|
648
|
+
rank: `${ch}_${type}`,
|
|
649
|
+
category: catName,
|
|
650
|
+
url: `https://fanqienovel.com${catHref}`,
|
|
651
|
+
});
|
|
652
|
+
if (self) {
|
|
653
|
+
selfCheckState.mapping = self;
|
|
654
|
+
console.error(` ✓ 番茄视觉自检选择器:${JSON.stringify(self.selectors)}`);
|
|
655
|
+
} else {
|
|
656
|
+
console.error(' ⚠ 番茄视觉自检未取得可用选择器,使用默认选择器');
|
|
657
|
+
}
|
|
658
|
+
} catch (e) {
|
|
659
|
+
console.error(` ⚠ 番茄视觉自检失败,使用默认选择器:${e.message}`);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
if (selfCheckState.mapping) {
|
|
663
|
+
try {
|
|
664
|
+
const verified = await validateFanqieSelectors(page, selfCheckState.mapping, {
|
|
665
|
+
threshold: 0.8,
|
|
666
|
+
});
|
|
667
|
+
if (verified) {
|
|
668
|
+
fieldSelectors = { ...FANQIE_DEFAULT_SELECTORS, ...verified.selectors };
|
|
669
|
+
console.error(` ✓ 番茄本页选择器命中:${JSON.stringify(verified.selectors)}`);
|
|
670
|
+
} else {
|
|
671
|
+
console.error(' ⚠ 番茄本页与自检选择器不匹配,回退默认选择器');
|
|
672
|
+
}
|
|
673
|
+
} catch (e) {
|
|
674
|
+
console.error(` ⚠ 番茄本页选择器验证失败,回退默认选择器:${e.message}`);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
}
|
|
630
678
|
if (IS_VISION) {
|
|
631
679
|
// fullPage + clip 按文档坐标截图,先把页面归零,避免吸附头部影响
|
|
632
680
|
// 第一张卡的坐标。
|
|
@@ -712,12 +760,8 @@ async function scrapeCategory(page, catHref, catName, ch, type, screenshotBase)
|
|
|
712
760
|
}
|
|
713
761
|
|
|
714
762
|
const ocr = {};
|
|
715
|
-
for (const [
|
|
716
|
-
[
|
|
717
|
-
['author', '.book-item-text .author'],
|
|
718
|
-
['status', '.book-item-footer .book-item-footer-status'],
|
|
719
|
-
['desc', '.book-item-text .desc'],
|
|
720
|
-
]) {
|
|
763
|
+
for (const field of ['title', 'author', 'status', 'desc']) {
|
|
764
|
+
const sel = fieldSelectors[field] || FANQIE_DEFAULT_SELECTORS[field];
|
|
721
765
|
const fp = path.join(bd, `${bookId}_${field}.jpg`);
|
|
722
766
|
try {
|
|
723
767
|
await screenshotWithRetry(item.locator(sel), fp, 3);
|
|
@@ -879,6 +923,10 @@ async function scrapeChannel(browserRef, ch, type) {
|
|
|
879
923
|
let okCategories = 0;
|
|
880
924
|
|
|
881
925
|
// 4. 逐个分类采集(每个 category 用独立 browser context)
|
|
926
|
+
// 视觉自检只在第一个成功打开的题材页执行一次,之后复用选择器。
|
|
927
|
+
const selfCheckState = (SCAN_MODE === SCAN_MODE_OCR && SELF_CHECK_DIR)
|
|
928
|
+
? { attempted: false, mapping: null }
|
|
929
|
+
: null;
|
|
882
930
|
for (let ci = 0; ci < categories.length; ci++) {
|
|
883
931
|
const cat = categories[ci];
|
|
884
932
|
const screenshotBase = path.join(screenshotBasePrefix, safePathSegment(cat.name, '未分类'));
|
|
@@ -893,7 +941,9 @@ async function scrapeChannel(browserRef, ch, type) {
|
|
|
893
941
|
});
|
|
894
942
|
const page = await ctx.newPage();
|
|
895
943
|
|
|
896
|
-
const { books, failures } = await scrapeCategory(
|
|
944
|
+
const { books, failures } = await scrapeCategory(
|
|
945
|
+
page, cat.href, cat.name, ch, type, screenshotBase, selfCheckState
|
|
946
|
+
);
|
|
897
947
|
if (IS_VISION) visionCategories.push({ name: cat.name, books });
|
|
898
948
|
else allLines.push(buildMarkdown(cat.name, books));
|
|
899
949
|
// 单本明文取不到不算"品类失败",但必须让用户看见:
|
|
@@ -23,6 +23,7 @@ import path from 'path';
|
|
|
23
23
|
import { pathToFileURL } from 'url';
|
|
24
24
|
import { chromium } from 'playwright-core';
|
|
25
25
|
import { getArg, localDateStamp, runCli, renderIntroLines, sanitizeBookFields, stripThousands } from '#scraper-utils';
|
|
26
|
+
import { runVisualSelfCheck, inferJinjiangColumns } from './selfcheck.js';
|
|
26
27
|
|
|
27
28
|
const BASE_URL = "https://www.jjwxc.net/topten.php";
|
|
28
29
|
|
|
@@ -102,6 +103,7 @@ const args = process.argv.slice(2);
|
|
|
102
103
|
const RANKTYPE = getArg(args, "--type") || "12";
|
|
103
104
|
const LIST_ONLY = args.includes("--list-only");
|
|
104
105
|
const OUTDIR = getArg(args, "--outdir") || '.';
|
|
106
|
+
const SELF_CHECK_DIR = getArg(args, "--self-check-dir") || '';
|
|
105
107
|
|
|
106
108
|
// ---------------------------------------------------------------------------
|
|
107
109
|
// 列表页提取(使用 Playwright)
|
|
@@ -110,8 +112,9 @@ const OUTDIR = getArg(args, "--outdir") || '.';
|
|
|
110
112
|
/**
|
|
111
113
|
* 提取晋江榜单数据(从表格提取)
|
|
112
114
|
*/
|
|
113
|
-
async function extractRankData(page) {
|
|
114
|
-
return await page.evaluate(() => {
|
|
115
|
+
async function extractRankData(page, columns = {}) {
|
|
116
|
+
return await page.evaluate((columns) => {
|
|
117
|
+
var C = Object.assign({ rank: 0, author: 1, title: 2, type: 3, status: 4, words: 5, score: 6 }, columns || {});
|
|
115
118
|
var result = { channels: [{ name: '全站', books: [] }] };
|
|
116
119
|
|
|
117
120
|
// 从表格提取书籍数据
|
|
@@ -125,16 +128,17 @@ async function extractRankData(page) {
|
|
|
125
128
|
if (cells.length < 6) continue;
|
|
126
129
|
|
|
127
130
|
// 检查是否是书籍行(第一列是数字序号)
|
|
128
|
-
var rankCell = cells[
|
|
131
|
+
var rankCell = cells[C.rank];
|
|
132
|
+
if (!rankCell) continue;
|
|
129
133
|
var rankNum = parseInt(rankCell.innerText.trim(), 10);
|
|
130
134
|
if (isNaN(rankNum)) continue;
|
|
131
135
|
|
|
132
|
-
var authorCell = cells[1];
|
|
133
|
-
var titleCell = cells[2];
|
|
134
|
-
var typeCell = cells[3];
|
|
135
|
-
var statusCell = cells[4];
|
|
136
|
-
var wordsCell = cells[5];
|
|
137
|
-
var scoreCell = cells[6] || null;
|
|
136
|
+
var authorCell = cells[C.author] || cells[1];
|
|
137
|
+
var titleCell = cells[C.title] || cells[2];
|
|
138
|
+
var typeCell = cells[C.type] || cells[3];
|
|
139
|
+
var statusCell = cells[C.status] || cells[4];
|
|
140
|
+
var wordsCell = cells[C.words] || cells[5];
|
|
141
|
+
var scoreCell = cells[C.score] || cells[6] || null;
|
|
138
142
|
|
|
139
143
|
var author = (authorCell.innerText || '').trim();
|
|
140
144
|
var title = (titleCell.innerText || '').trim();
|
|
@@ -288,6 +292,44 @@ async function scrapeRank(browser, rankTypeId, channelId) {
|
|
|
288
292
|
}
|
|
289
293
|
}
|
|
290
294
|
|
|
295
|
+
if (SELF_CHECK_DIR && data?.channels?.length) {
|
|
296
|
+
try {
|
|
297
|
+
const tableIndex = await page.evaluate(() => {
|
|
298
|
+
const tables = [...document.querySelectorAll('table')];
|
|
299
|
+
let best = 0;
|
|
300
|
+
let bestRows = -1;
|
|
301
|
+
tables.forEach((table, index) => {
|
|
302
|
+
const rows = table.querySelectorAll('tr').length;
|
|
303
|
+
if (rows > bestRows) { bestRows = rows; best = index; }
|
|
304
|
+
});
|
|
305
|
+
return best;
|
|
306
|
+
});
|
|
307
|
+
const self = await runVisualSelfCheck(page, {
|
|
308
|
+
dir: SELF_CHECK_DIR,
|
|
309
|
+
platform: 'jinjiang',
|
|
310
|
+
rank: rankTypeId,
|
|
311
|
+
url,
|
|
312
|
+
screenshotLocator: page.locator('table').nth(tableIndex),
|
|
313
|
+
maxHeight: 1000,
|
|
314
|
+
});
|
|
315
|
+
if (self) {
|
|
316
|
+
const inferred = await inferJinjiangColumns(page, self.books);
|
|
317
|
+
if (inferred) {
|
|
318
|
+
const mapped = await extractRankData(page, inferred.columns);
|
|
319
|
+
let mappedTotal = 0;
|
|
320
|
+
for (const ch of mapped?.channels || []) mappedTotal += ch.books.length;
|
|
321
|
+
if (mappedTotal > 0) {
|
|
322
|
+
data = mapped;
|
|
323
|
+
totalBooks = mappedTotal;
|
|
324
|
+
console.error(` ✓ 视觉自检列映射:${Object.entries(inferred.columns).map(([k, v]) => k + '=' + v).join(', ')}`);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
} catch (e) {
|
|
329
|
+
console.error(` ⚠ 晋江视觉自检失败,使用默认列:${e.message}`);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
291
333
|
data.channels.forEach((ch) => {
|
|
292
334
|
const authors = new Set(ch.books.map((b) => b.author));
|
|
293
335
|
if (ch.books.length >= 5 && authors.size / ch.books.length < 0.2) {
|
|
@@ -19,6 +19,7 @@ import path from 'path';
|
|
|
19
19
|
import { pathToFileURL } from 'url';
|
|
20
20
|
import { chromium } from 'playwright-core';
|
|
21
21
|
import { getArg, localDateStamp, runCli, renderIntroLines, sanitizeBookFields, sanitizeMarkdownInline } from '#scraper-utils';
|
|
22
|
+
import { runVisualSelfCheck, inferQimaoSelectors, extractQimaoByMapping } from './selfcheck.js';
|
|
22
23
|
|
|
23
24
|
const RANK_URL = "https://www.qimao.com/paihang";
|
|
24
25
|
|
|
@@ -31,6 +32,7 @@ const OUTDIR = getArg(args, "--outdir") || ".";
|
|
|
31
32
|
const CHANNEL = getArg(args, "--channel") || "boy";
|
|
32
33
|
const RANKTYPE = getArg(args, "--type") || "hot";
|
|
33
34
|
const PERIOD = getArg(args, "--period") || "day";
|
|
35
|
+
const SELF_CHECK_DIR = getArg(args, "--self-check-dir") || '';
|
|
34
36
|
|
|
35
37
|
const CHANNELS = [
|
|
36
38
|
{ id: "boy", label: "男生", tab: "男生榜", path: "boy" },
|
|
@@ -463,6 +465,50 @@ async function scrapeRank(browser, channelId, rankTypeId, periodId) {
|
|
|
463
465
|
const matched = urls.find((u) => norm(u.title) === norm(b.title));
|
|
464
466
|
if (matched) b.url = matched.url;
|
|
465
467
|
}
|
|
468
|
+
|
|
469
|
+
// 前置视觉自检:只在父进程给了临时目录时执行。
|
|
470
|
+
// 同一个 page 内截图、等 LLM 样例值、反查 DOM;失败静默回退文本解析。
|
|
471
|
+
if (SELF_CHECK_DIR) {
|
|
472
|
+
try {
|
|
473
|
+
const self = await runVisualSelfCheck(page, {
|
|
474
|
+
dir: SELF_CHECK_DIR,
|
|
475
|
+
platform: 'qimao',
|
|
476
|
+
rank: `${channelId}/${rankTypeId}/${periodId || ''}`,
|
|
477
|
+
url,
|
|
478
|
+
screenshotLocator: page.locator('ul.rank-list').first(),
|
|
479
|
+
maxHeight: 2400,
|
|
480
|
+
});
|
|
481
|
+
if (self) {
|
|
482
|
+
const mapping = await inferQimaoSelectors(page, self.books);
|
|
483
|
+
if (mapping) {
|
|
484
|
+
const mapped = await extractQimaoByMapping(page, mapping);
|
|
485
|
+
const required = Math.max(5, Math.ceil(books.length * 0.8));
|
|
486
|
+
if (Array.isArray(mapped) && mapped.length >= required) {
|
|
487
|
+
const mappedByTitle = new Map(mapped.map((m) => [norm(m.title), m]));
|
|
488
|
+
const merged = books.map((textBook) => {
|
|
489
|
+
const m = mappedByTitle.get(norm(textBook.title));
|
|
490
|
+
if (!m) return textBook;
|
|
491
|
+
mappedByTitle.delete(norm(textBook.title));
|
|
492
|
+
return {
|
|
493
|
+
...textBook,
|
|
494
|
+
rank: m.rank || textBook.rank || '',
|
|
495
|
+
title: m.title,
|
|
496
|
+
author: m.author || textBook.author || '',
|
|
497
|
+
heat: m.heat || textBook.heat || '',
|
|
498
|
+
desc: m.desc || textBook.desc || '',
|
|
499
|
+
url: m.url || textBook.url || '',
|
|
500
|
+
};
|
|
501
|
+
});
|
|
502
|
+
for (const m of mappedByTitle.values()) merged.push(m);
|
|
503
|
+
books = merged;
|
|
504
|
+
console.error(` ✓ 视觉自检选择器:${mapping.cardSelector} ${JSON.stringify(mapping.selectors)}`);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
} catch (e) {
|
|
509
|
+
console.error(` ⚠ 七猫视觉自检失败,使用文本解析:${e.message}`);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
466
512
|
} catch (err) {
|
|
467
513
|
console.error(`❌ 七猫-${ch.label}${rt.label}${periodLabel}采集失败:${err.message}`);
|
|
468
514
|
await page.close();
|