@cloud411716/fancy-webnovel 4.9.0 → 4.11.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/docs/douyin-plugin.md +47 -3
- package/docs/material-library/README.md +2 -0
- package/docs/material-library/schema.surql +48 -1
- package/docs/workspace-mcp.md +2 -2
- package/lib/material-db.js +7 -2
- package/package.json +1 -1
- package/plugins/fancy-bootstrap/index.js +1 -1
- package/plugins/fancy-db/index.js +1 -1
- package/plugins/fancy-douyin/index.js +824 -276
- package/plugins/fancy-douyin/jobs.js +184 -0
- package/plugins/fancy-douyin/scripts/scan-account.cjs +19 -0
- package/plugins/fancy-douyin/state.js +54 -15
- package/plugins/fancy-douyin/subagents.js +106 -0
- package/plugins/fancy-douyin/templates.js +9 -2
|
@@ -31,8 +31,14 @@ import {
|
|
|
31
31
|
readAccounts,
|
|
32
32
|
readCrawled,
|
|
33
33
|
removeAccount,
|
|
34
|
+
updateAccountProfile,
|
|
34
35
|
} from './state.js';
|
|
35
36
|
import { analysisPrompt, articlePrompt, capabilityPrompt, helpText, loginQuestion, summaryText, videoAnalysisPrompt, videoRelevancePrompt } from './templates.js';
|
|
37
|
+
import { createJobQueue, createSerialLock, getSubagentRuntime, subagentWorkerCount } from './subagents.js';
|
|
38
|
+
import {
|
|
39
|
+
countJobsByAccount, createJob, jobKeyFor, jobRelDir,
|
|
40
|
+
listJobsForAccount, migrateCrawledJobs, openJobStore, updateJob,
|
|
41
|
+
} from './jobs.js';
|
|
36
42
|
|
|
37
43
|
export const inject = ['commands', 'userQuestions'];
|
|
38
44
|
|
|
@@ -455,6 +461,36 @@ async function detectCapabilities(ctx, invocation, session, taskList, root, runD
|
|
|
455
461
|
return { ok: false, error: 'LLM 没有返回可解析的能力自检结果' };
|
|
456
462
|
}
|
|
457
463
|
|
|
464
|
+
async function finalizeMaterialFile({ root, target, title, materialTools }) {
|
|
465
|
+
const skip = readSkipMarker(target);
|
|
466
|
+
if (skip) {
|
|
467
|
+
return {
|
|
468
|
+
ok: true,
|
|
469
|
+
skipped: true,
|
|
470
|
+
reason: skip.reason,
|
|
471
|
+
inserted: 0,
|
|
472
|
+
skipped: 0,
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
try {
|
|
476
|
+
normalizeOtherFile(target);
|
|
477
|
+
} catch (err) {
|
|
478
|
+
return { ok: false, detail: err && err.message ? err.message : String(err) };
|
|
479
|
+
}
|
|
480
|
+
const checked = await validateOtherFile(target, materialTools.validateScript);
|
|
481
|
+
if (!checked.ok) return { ok: false, detail: checked.detail };
|
|
482
|
+
const kindProblem = kindLanguageProblem(target);
|
|
483
|
+
if (kindProblem) return { ok: false, detail: kindProblem };
|
|
484
|
+
const coverage = coverageProblem(target, title);
|
|
485
|
+
if (coverage) return { ok: false, detail: coverage };
|
|
486
|
+
try {
|
|
487
|
+
const result = await importOtherMaterials(root, target);
|
|
488
|
+
return { ok: true, ...result };
|
|
489
|
+
} catch (err) {
|
|
490
|
+
return { ok: false, error: '导入数据库失败:' + (err && err.message ? err.message : String(err)) };
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
458
494
|
async function analyzeMaterials(ctx, invocation, session, {
|
|
459
495
|
root, prompt, target, title, materialTools, taskList,
|
|
460
496
|
}) {
|
|
@@ -497,44 +533,10 @@ async function analyzeMaterials(ctx, invocation, session, {
|
|
|
497
533
|
continue;
|
|
498
534
|
}
|
|
499
535
|
|
|
500
|
-
const
|
|
501
|
-
if (
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
skipped: true,
|
|
505
|
-
reason: skip.reason,
|
|
506
|
-
inserted: 0,
|
|
507
|
-
skipped: 0,
|
|
508
|
-
};
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
try {
|
|
512
|
-
normalizeOtherFile(target);
|
|
513
|
-
} catch (err) {
|
|
514
|
-
detail = err && err.message ? err.message : String(err);
|
|
515
|
-
continue;
|
|
516
|
-
}
|
|
517
|
-
const checked = await validateOtherFile(target, materialTools.validateScript);
|
|
518
|
-
if (!checked.ok) {
|
|
519
|
-
detail = checked.detail;
|
|
520
|
-
continue;
|
|
521
|
-
}
|
|
522
|
-
const kindProblem = kindLanguageProblem(target);
|
|
523
|
-
if (kindProblem) {
|
|
524
|
-
detail = kindProblem;
|
|
525
|
-
continue;
|
|
526
|
-
}
|
|
527
|
-
const coverage = coverageProblem(target, title);
|
|
528
|
-
if (coverage) {
|
|
529
|
-
detail = coverage;
|
|
530
|
-
continue;
|
|
531
|
-
}
|
|
532
|
-
try {
|
|
533
|
-
const result = await importOtherMaterials(root, target);
|
|
534
|
-
return { ok: true, ...result };
|
|
535
|
-
} catch (err) {
|
|
536
|
-
return { ok: false, error: '导入数据库失败:' + (err && err.message ? err.message : String(err)) };
|
|
537
|
-
}
|
|
536
|
+
const finalized = await finalizeMaterialFile({ root, target, title, materialTools });
|
|
537
|
+
if (finalized.ok) return finalized;
|
|
538
|
+
if (finalized.error) return finalized;
|
|
539
|
+
detail = finalized.detail;
|
|
538
540
|
}
|
|
539
541
|
return { ok: false, error: '素材没有通过校验:' + detail };
|
|
540
542
|
}
|
|
@@ -652,6 +654,162 @@ async function analyzeArticle(ctx, invocation, session, {
|
|
|
652
654
|
});
|
|
653
655
|
}
|
|
654
656
|
|
|
657
|
+
function analysisPromptForJob(job, root, materialTools) {
|
|
658
|
+
if (job.type === 'note') {
|
|
659
|
+
return analysisPrompt({
|
|
660
|
+
accountUrl: job.accountUrl,
|
|
661
|
+
note: job.note,
|
|
662
|
+
screenshots: job.screenshots,
|
|
663
|
+
targetFile: job.target,
|
|
664
|
+
workspace: root,
|
|
665
|
+
queryScript: materialTools.queryScript,
|
|
666
|
+
validateScript: materialTools.validateScript,
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
if (job.type === 'video') {
|
|
670
|
+
return videoAnalysisPrompt({
|
|
671
|
+
accountUrl: job.accountUrl,
|
|
672
|
+
video: job.video,
|
|
673
|
+
frames: job.video.frames || [],
|
|
674
|
+
targetFile: job.target,
|
|
675
|
+
workspace: root,
|
|
676
|
+
queryScript: materialTools.queryScript,
|
|
677
|
+
validateScript: materialTools.validateScript,
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
return articlePrompt({
|
|
681
|
+
accountUrl: job.accountUrl,
|
|
682
|
+
article: job.article,
|
|
683
|
+
targetFile: job.target,
|
|
684
|
+
workspace: root,
|
|
685
|
+
queryScript: materialTools.queryScript,
|
|
686
|
+
validateScript: materialTools.validateScript,
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
function subagentTaskPrompt(job, root, materialTools) {
|
|
691
|
+
return [
|
|
692
|
+
'你是 fancy-douyin 的分析子 Agent,只处理下面这一篇作品。',
|
|
693
|
+
'不要扫描账号,不要启动或操作 Chrome,不要修改 crawled.json,不要运行 import.js 或直接写素材数据库。',
|
|
694
|
+
'只读取给定文件,按提示词提取素材,写入目标 JSONL,运行 validate.js 后结束。',
|
|
695
|
+
'',
|
|
696
|
+
analysisPromptForJob(job, root, materialTools),
|
|
697
|
+
].join('\n');
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
async function runSubagentAnalysisJob({
|
|
701
|
+
subagents, provider, invocation, job, root, materialTools, signal,
|
|
702
|
+
}) {
|
|
703
|
+
const prompt = subagentTaskPrompt(job, root, materialTools);
|
|
704
|
+
let activation;
|
|
705
|
+
try {
|
|
706
|
+
activation = await subagents.startActivation({
|
|
707
|
+
provider,
|
|
708
|
+
label: 'fancy-douyin ' + job.type + ' ' + job.id,
|
|
709
|
+
parent: invocation.agent,
|
|
710
|
+
prompt: [{ type: 'text', text: prompt }],
|
|
711
|
+
signal,
|
|
712
|
+
delivery: 'caller',
|
|
713
|
+
});
|
|
714
|
+
} catch (err) {
|
|
715
|
+
return { ok: false, error: '子 Agent 启动失败:' + (err && err.message ? err.message : String(err)) };
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
let result;
|
|
719
|
+
let timer = null;
|
|
720
|
+
try {
|
|
721
|
+
result = await Promise.race([
|
|
722
|
+
activation.result,
|
|
723
|
+
new Promise((_, reject) => {
|
|
724
|
+
timer = setTimeout(() => reject(new Error('子 Agent 分析超时')), ANALYSIS_TIMEOUT_MS);
|
|
725
|
+
if (timer && typeof timer.unref === 'function') timer.unref();
|
|
726
|
+
}),
|
|
727
|
+
]);
|
|
728
|
+
} catch (err) {
|
|
729
|
+
return { ok: false, error: '子 Agent 执行失败:' + (err && err.message ? err.message : String(err)) };
|
|
730
|
+
} finally {
|
|
731
|
+
if (timer) clearTimeout(timer);
|
|
732
|
+
try { await activation.dispose(); } catch { /* best effort */ }
|
|
733
|
+
}
|
|
734
|
+
if (!fs.existsSync(job.target)) {
|
|
735
|
+
const stop = result && result.stopReason ? '(' + result.stopReason + ')' : '';
|
|
736
|
+
return { ok: false, error: '子 Agent 没有写入目标文件' + stop };
|
|
737
|
+
}
|
|
738
|
+
// 文件已经写出,由父进程在串行锁内统一校验、导入和写 crawled.json。
|
|
739
|
+
return { ok: true, needsFinalize: true };
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
async function runLocalAnalysisJob(ctx, invocation, session, job, {
|
|
743
|
+
root, materialTools, paths, taskList,
|
|
744
|
+
}) {
|
|
745
|
+
if (job.type === 'note') {
|
|
746
|
+
return analyzeNote(ctx, invocation, session, {
|
|
747
|
+
root,
|
|
748
|
+
note: job.note,
|
|
749
|
+
screenshots: job.screenshots,
|
|
750
|
+
target: job.target,
|
|
751
|
+
materialTools,
|
|
752
|
+
paths,
|
|
753
|
+
taskList,
|
|
754
|
+
});
|
|
755
|
+
}
|
|
756
|
+
if (job.type === 'video') {
|
|
757
|
+
return analyzeVideo(ctx, invocation, session, {
|
|
758
|
+
root,
|
|
759
|
+
accountUrl: job.accountUrl,
|
|
760
|
+
video: job.video,
|
|
761
|
+
target: job.target,
|
|
762
|
+
materialTools,
|
|
763
|
+
taskList,
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
return analyzeArticle(ctx, invocation, session, {
|
|
767
|
+
root,
|
|
768
|
+
accountUrl: job.accountUrl,
|
|
769
|
+
article: job.article,
|
|
770
|
+
target: job.target,
|
|
771
|
+
materialTools,
|
|
772
|
+
taskList,
|
|
773
|
+
});
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
function readJsonArtifact(dir, name) {
|
|
777
|
+
try {
|
|
778
|
+
return JSON.parse(fs.readFileSync(path.join(dir, name), 'utf8'));
|
|
779
|
+
} catch {
|
|
780
|
+
return null;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
function reusableNote(dir) {
|
|
785
|
+
const note = readJsonArtifact(dir, 'note.json');
|
|
786
|
+
if (!note || !Array.isArray(note.screenshots) || !note.screenshots.length) return null;
|
|
787
|
+
if (!note.screenshots.every((shot) => shot && shot.file && fs.existsSync(shot.file))) return null;
|
|
788
|
+
return note;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
function reusableVideo(dir) {
|
|
792
|
+
const video = readJsonArtifact(dir, 'video.json');
|
|
793
|
+
if (!video || !Array.isArray(video.frames) || !video.frames.length) return null;
|
|
794
|
+
if (!video.frames.every((frame) => frame && frame.file && fs.existsSync(frame.file))) return null;
|
|
795
|
+
return video;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
function reusableArticle(dir) {
|
|
799
|
+
const article = readJsonArtifact(dir, 'article.json');
|
|
800
|
+
if (!article || !article.body || String(article.body).trim().length < 50) return null;
|
|
801
|
+
return article;
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
function materialFileReady(dir) {
|
|
805
|
+
const file = path.join(dir, 'materials.jsonl');
|
|
806
|
+
try {
|
|
807
|
+
return fs.statSync(file).size > 0;
|
|
808
|
+
} catch {
|
|
809
|
+
return false;
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
|
|
655
813
|
async function runCrawl(ctx, invocation, session, taskList) {
|
|
656
814
|
const root = workspaceOf(session);
|
|
657
815
|
const paths = ensureDouyinDir(root);
|
|
@@ -662,6 +820,7 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
662
820
|
|
|
663
821
|
const runDir = path.join(paths.output, stampOfNow());
|
|
664
822
|
fs.mkdirSync(runDir, { recursive: true });
|
|
823
|
+
fs.mkdirSync(paths.jobs, { recursive: true });
|
|
665
824
|
|
|
666
825
|
// 先做能力自检:如果模型既不能看图也不能看视频,直接退出,
|
|
667
826
|
// 不启动 Chrome,也不扫描账号。
|
|
@@ -686,9 +845,21 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
686
845
|
return notify(session, 'error', '❌ 部署素材校验脚本失败:\n' + (materialTools.problems.join('\n') || '未知错误'));
|
|
687
846
|
}
|
|
688
847
|
|
|
848
|
+
let jobStore;
|
|
849
|
+
let queue = null;
|
|
850
|
+
let workerPromises = [];
|
|
851
|
+
try {
|
|
852
|
+
jobStore = await openJobStore(root);
|
|
853
|
+
await migrateCrawledJobs(jobStore.db, root);
|
|
854
|
+
} catch (err) {
|
|
855
|
+
if (jobStore) await jobStore.close();
|
|
856
|
+
return notify(session, 'error', '❌ 初始化抖音任务状态表失败:' + (err && err.message ? err.message : String(err)));
|
|
857
|
+
}
|
|
858
|
+
|
|
689
859
|
const mode = hasDouyinLogin(paths) ? 'headless' : 'headed';
|
|
690
860
|
const started = await startChrome(root, tools, paths, mode);
|
|
691
861
|
if (!started.ok) {
|
|
862
|
+
await jobStore.close();
|
|
692
863
|
return notify(session, 'error', '❌ 启动 Chrome 失败:' + started.error);
|
|
693
864
|
}
|
|
694
865
|
const browser = { cdp: started.cdp, mode: started.mode };
|
|
@@ -698,6 +869,85 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
698
869
|
try {
|
|
699
870
|
const crawled = readCrawled(root);
|
|
700
871
|
const failures = [];
|
|
872
|
+
const jobs = [];
|
|
873
|
+
const queued = new Set();
|
|
874
|
+
const subagentRuntime = getSubagentRuntime(ctx);
|
|
875
|
+
const subagentWorkers = subagentRuntime ? subagentWorkerCount() : 0;
|
|
876
|
+
const useSubagents = Boolean(subagentRuntime && subagentWorkers > 0);
|
|
877
|
+
const withStateLock = createSerialLock();
|
|
878
|
+
const abortController = new AbortController();
|
|
879
|
+
if (invocation.signal) {
|
|
880
|
+
if (invocation.signal.aborted) abortController.abort();
|
|
881
|
+
else invocation.signal.addEventListener('abort', () => abortController.abort(), { once: true });
|
|
882
|
+
}
|
|
883
|
+
const jobSignal = abortController.signal;
|
|
884
|
+
const markJobFailedLocked = async (job, reason, { crawl = false } = {}) => {
|
|
885
|
+
if (!job || !job.id) return;
|
|
886
|
+
markCrawled(root, job.id, {
|
|
887
|
+
account_url: job.accountUrl,
|
|
888
|
+
url: job.url || (job.card && job.card.href) || '',
|
|
889
|
+
title: job.title || (job.card && job.card.text) || '',
|
|
890
|
+
type: job.type,
|
|
891
|
+
duration: job.duration,
|
|
892
|
+
screenshots: job.type === 'note' && Array.isArray(job.screenshots)
|
|
893
|
+
? job.screenshots.map((shot) => shot.file)
|
|
894
|
+
: undefined,
|
|
895
|
+
body_file: job.article && job.article.body_file,
|
|
896
|
+
reading_time: job.article && job.article.reading_time,
|
|
897
|
+
body_length: job.article && job.article.body_length,
|
|
898
|
+
dir: job.dir,
|
|
899
|
+
inserted: 0,
|
|
900
|
+
skipped: 0,
|
|
901
|
+
skip: true,
|
|
902
|
+
failed: true,
|
|
903
|
+
reason,
|
|
904
|
+
});
|
|
905
|
+
crawled[job.id] = true;
|
|
906
|
+
if (job.jobKey) {
|
|
907
|
+
await updateJob(jobStore.db, job.jobKey, {
|
|
908
|
+
...(crawl ? { crawl_status: 'failed' } : { analysis_status: 'failed' }),
|
|
909
|
+
import_status: 'failed',
|
|
910
|
+
reason,
|
|
911
|
+
error: reason,
|
|
912
|
+
});
|
|
913
|
+
}
|
|
914
|
+
};
|
|
915
|
+
const markJobFailed = async (job, reason, options) => withStateLock(
|
|
916
|
+
() => markJobFailedLocked(job, reason, options),
|
|
917
|
+
);
|
|
918
|
+
|
|
919
|
+
const markJobSkippedLocked = async (job, reason, { crawl = false } = {}) => {
|
|
920
|
+
if (!job || !job.id) return;
|
|
921
|
+
markCrawled(root, job.id, {
|
|
922
|
+
account_url: job.accountUrl,
|
|
923
|
+
url: job.url || (job.card && job.card.href) || '',
|
|
924
|
+
title: job.title || (job.card && job.card.text) || '',
|
|
925
|
+
type: job.type,
|
|
926
|
+
duration: job.duration,
|
|
927
|
+
dir: job.dir,
|
|
928
|
+
inserted: 0,
|
|
929
|
+
skipped: 0,
|
|
930
|
+
skip: true,
|
|
931
|
+
reason,
|
|
932
|
+
});
|
|
933
|
+
crawled[job.id] = true;
|
|
934
|
+
if (job.jobKey) {
|
|
935
|
+
await updateJob(jobStore.db, job.jobKey, {
|
|
936
|
+
...(crawl ? { crawl_status: 'crawled' } : {}),
|
|
937
|
+
analysis_status: 'analyzed',
|
|
938
|
+
import_status: 'skipped',
|
|
939
|
+
reason,
|
|
940
|
+
error: '',
|
|
941
|
+
imported_at: new Date(),
|
|
942
|
+
});
|
|
943
|
+
}
|
|
944
|
+
};
|
|
945
|
+
const markJobSkipped = async (job, reason, options) => withStateLock(
|
|
946
|
+
() => markJobSkippedLocked(job, reason, options),
|
|
947
|
+
);
|
|
948
|
+
|
|
949
|
+
queue = useSubagents ? createJobQueue() : null;
|
|
950
|
+
const queueStats = { queued: 0, finished: 0 };
|
|
701
951
|
let scannedAccounts = 0;
|
|
702
952
|
let foundNotes = 0;
|
|
703
953
|
let foundVideos = 0;
|
|
@@ -713,26 +963,169 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
713
963
|
let skippedMaterials = 0;
|
|
714
964
|
let aborted = false;
|
|
715
965
|
|
|
966
|
+
const progressText = () => queue
|
|
967
|
+
? `子 Agent 并行分析(${queueStats.finished}/${queueStats.queued},并发 ${subagentWorkers})`
|
|
968
|
+
: `LLM 整理素材(${queueStats.finished}/${jobs.length})`;
|
|
969
|
+
|
|
970
|
+
const jobRecord = (job, extra = {}) => ({
|
|
971
|
+
account_url: job.accountUrl,
|
|
972
|
+
url: job.url,
|
|
973
|
+
title: job.title || (job.card && job.card.text) || '',
|
|
974
|
+
type: job.type,
|
|
975
|
+
duration: job.duration,
|
|
976
|
+
screenshots: job.type === 'note' && Array.isArray(job.screenshots)
|
|
977
|
+
? job.screenshots.map((shot) => shot.file)
|
|
978
|
+
: undefined,
|
|
979
|
+
body_file: job.article && job.article.body_file,
|
|
980
|
+
reading_time: job.article && job.article.reading_time,
|
|
981
|
+
body_length: job.article && job.article.body_length,
|
|
982
|
+
dir: job.dir,
|
|
983
|
+
...extra,
|
|
984
|
+
});
|
|
985
|
+
|
|
986
|
+
const finalizeJob = async (job, analyzed) => {
|
|
987
|
+
await withStateLock(async () => {
|
|
988
|
+
if (analyzed && analyzed.needsFinalize) {
|
|
989
|
+
analyzed = await finalizeMaterialFile({
|
|
990
|
+
root,
|
|
991
|
+
target: job.target,
|
|
992
|
+
title: job.title,
|
|
993
|
+
materialTools,
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
if (analyzed && analyzed.aborted) {
|
|
997
|
+
aborted = true;
|
|
998
|
+
if (job.jobKey) {
|
|
999
|
+
await updateJob(jobStore.db, job.jobKey, { analysis_status: 'pending' });
|
|
1000
|
+
}
|
|
1001
|
+
return;
|
|
1002
|
+
}
|
|
1003
|
+
if (!analyzed || !analyzed.ok) {
|
|
1004
|
+
const reason = (analyzed && (analyzed.error || analyzed.detail)) || '素材没有通过校验';
|
|
1005
|
+
failures.push({ id: job.id, url: job.url, reason });
|
|
1006
|
+
await markJobFailedLocked(job, reason);
|
|
1007
|
+
queueStats.finished += 1;
|
|
1008
|
+
taskList.progress(progressText());
|
|
1009
|
+
return;
|
|
1010
|
+
}
|
|
1011
|
+
if (analyzed.skipped) {
|
|
1012
|
+
skippedIrrelevant += 1;
|
|
1013
|
+
markCrawled(root, job.id, jobRecord(job, {
|
|
1014
|
+
inserted: 0,
|
|
1015
|
+
skipped: 0,
|
|
1016
|
+
skip: true,
|
|
1017
|
+
reason: analyzed.reason || '与网文创作无关',
|
|
1018
|
+
}));
|
|
1019
|
+
if (job.jobKey) {
|
|
1020
|
+
await updateJob(jobStore.db, job.jobKey, {
|
|
1021
|
+
analysis_status: 'analyzed',
|
|
1022
|
+
import_status: 'skipped',
|
|
1023
|
+
material_count: 0,
|
|
1024
|
+
reason: analyzed.reason || '与网文创作无关',
|
|
1025
|
+
error: '',
|
|
1026
|
+
imported_at: new Date(),
|
|
1027
|
+
});
|
|
1028
|
+
}
|
|
1029
|
+
} else {
|
|
1030
|
+
inserted += analyzed.inserted || 0;
|
|
1031
|
+
skippedMaterials += analyzed.skipped || 0;
|
|
1032
|
+
markCrawled(root, job.id, jobRecord(job, {
|
|
1033
|
+
inserted: analyzed.inserted || 0,
|
|
1034
|
+
skipped: analyzed.skipped || 0,
|
|
1035
|
+
}));
|
|
1036
|
+
if (job.jobKey) {
|
|
1037
|
+
await updateJob(jobStore.db, job.jobKey, {
|
|
1038
|
+
analysis_status: 'analyzed',
|
|
1039
|
+
import_status: 'imported',
|
|
1040
|
+
material_count: analyzed.inserted || 0,
|
|
1041
|
+
reason: '',
|
|
1042
|
+
error: '',
|
|
1043
|
+
imported_at: new Date(),
|
|
1044
|
+
});
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
crawled[job.id] = true;
|
|
1048
|
+
queueStats.finished += 1;
|
|
1049
|
+
taskList.progress(progressText());
|
|
1050
|
+
});
|
|
1051
|
+
};
|
|
1052
|
+
|
|
1053
|
+
const enqueueJob = async (job) => {
|
|
1054
|
+
if (job.jobKey) {
|
|
1055
|
+
await updateJob(jobStore.db, job.jobKey, {
|
|
1056
|
+
crawl_status: 'crawled',
|
|
1057
|
+
analysis_status: job.finalizeOnly ? 'analyzed' : 'pending',
|
|
1058
|
+
dir_rel: job.dirRel,
|
|
1059
|
+
title: job.title || '',
|
|
1060
|
+
work_url: job.url,
|
|
1061
|
+
error: '',
|
|
1062
|
+
reason: '',
|
|
1063
|
+
});
|
|
1064
|
+
}
|
|
1065
|
+
if (queue) {
|
|
1066
|
+
queueStats.queued += 1;
|
|
1067
|
+
queue.push(job);
|
|
1068
|
+
taskList.progress(progressText());
|
|
1069
|
+
} else {
|
|
1070
|
+
jobs.push(job);
|
|
1071
|
+
}
|
|
1072
|
+
};
|
|
1073
|
+
|
|
1074
|
+
const runWorker = async () => {
|
|
1075
|
+
while (true) {
|
|
1076
|
+
if (abortRequested(invocation)) { aborted = true; return; }
|
|
1077
|
+
const job = await queue.next();
|
|
1078
|
+
if (!job) return;
|
|
1079
|
+
let analyzed;
|
|
1080
|
+
if (job.finalizeOnly) {
|
|
1081
|
+
analyzed = { ok: true, needsFinalize: true };
|
|
1082
|
+
} else {
|
|
1083
|
+
try {
|
|
1084
|
+
analyzed = await runSubagentAnalysisJob({
|
|
1085
|
+
subagents: subagentRuntime,
|
|
1086
|
+
provider: 'spawn',
|
|
1087
|
+
invocation,
|
|
1088
|
+
job,
|
|
1089
|
+
root,
|
|
1090
|
+
materialTools,
|
|
1091
|
+
signal: jobSignal,
|
|
1092
|
+
});
|
|
1093
|
+
} catch (err) {
|
|
1094
|
+
analyzed = {
|
|
1095
|
+
ok: false,
|
|
1096
|
+
error: '子 Agent 分析失败:' + (err && err.message ? err.message : String(err)),
|
|
1097
|
+
};
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
await finalizeJob(job, analyzed);
|
|
1101
|
+
}
|
|
1102
|
+
};
|
|
1103
|
+
|
|
1104
|
+
workerPromises = queue
|
|
1105
|
+
? Array.from({ length: subagentWorkers }, () => runWorker())
|
|
1106
|
+
: [];
|
|
1107
|
+
if (queue) {
|
|
1108
|
+
taskList.progress(progressText());
|
|
1109
|
+
}
|
|
1110
|
+
|
|
716
1111
|
outer:
|
|
717
1112
|
for (const account of accounts) {
|
|
718
1113
|
if (abortRequested(invocation)) { aborted = true; break; }
|
|
719
1114
|
const accountDir = path.join(runDir, accountSlug(account.url));
|
|
720
|
-
const markFailed = (id, card, data, reason) => {
|
|
1115
|
+
const markFailed = async (id, card, data, reason) => {
|
|
721
1116
|
if (!id) return;
|
|
722
|
-
|
|
723
|
-
|
|
1117
|
+
const job = {
|
|
1118
|
+
id,
|
|
1119
|
+
jobKey: jobKeyFor(account.url, id),
|
|
1120
|
+
accountUrl: account.url,
|
|
724
1121
|
url: data.url || card.href,
|
|
725
1122
|
title: data.title || card.text || '',
|
|
726
1123
|
type: data.type,
|
|
727
1124
|
duration: data.duration,
|
|
728
1125
|
dir: data.dir,
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
failed: true,
|
|
733
|
-
reason,
|
|
734
|
-
});
|
|
735
|
-
crawled[id] = true;
|
|
1126
|
+
card,
|
|
1127
|
+
};
|
|
1128
|
+
await markJobFailed(job, reason, { crawl: true });
|
|
736
1129
|
};
|
|
737
1130
|
const scan = await runWithLogin(
|
|
738
1131
|
ctx, invocation, session, root, tools.scripts.scan,
|
|
@@ -752,6 +1145,43 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
752
1145
|
}
|
|
753
1146
|
|
|
754
1147
|
scannedAccounts += 1;
|
|
1148
|
+
if (scan.json.profile) {
|
|
1149
|
+
updateAccountProfile(root, account.url, scan.json.profile);
|
|
1150
|
+
account.name = scan.json.profile.name || account.name || '';
|
|
1151
|
+
account.douyin_id = scan.json.profile.douyin_id || account.douyin_id || '';
|
|
1152
|
+
account.sec_uid = scan.json.profile.sec_uid || account.sec_uid || '';
|
|
1153
|
+
}
|
|
1154
|
+
const existingJobs = new Map((await listJobsForAccount(jobStore.db, account.url))
|
|
1155
|
+
.map((job) => [String(job.work_id), job]));
|
|
1156
|
+
const ensureJob = async (workId, type, card, dirRel, title = '') => {
|
|
1157
|
+
const key = jobKeyFor(account.url, workId);
|
|
1158
|
+
let row = existingJobs.get(String(workId)) || null;
|
|
1159
|
+
if (!row) {
|
|
1160
|
+
row = await createJob(jobStore.db, {
|
|
1161
|
+
job_key: key,
|
|
1162
|
+
account_url: account.url,
|
|
1163
|
+
account_name: account.name || '',
|
|
1164
|
+
douyin_id: account.douyin_id || '',
|
|
1165
|
+
account_sec_uid: account.sec_uid || '',
|
|
1166
|
+
work_id: String(workId),
|
|
1167
|
+
work_type: type,
|
|
1168
|
+
work_url: card.href,
|
|
1169
|
+
title: title || card.text || '',
|
|
1170
|
+
dir_rel: dirRel,
|
|
1171
|
+
});
|
|
1172
|
+
existingJobs.set(String(workId), row);
|
|
1173
|
+
} else {
|
|
1174
|
+
await updateJob(jobStore.db, row.job_key || key, {
|
|
1175
|
+
account_name: account.name || '',
|
|
1176
|
+
douyin_id: account.douyin_id || '',
|
|
1177
|
+
account_sec_uid: account.sec_uid || '',
|
|
1178
|
+
title: title || row.title || '',
|
|
1179
|
+
work_url: card.href,
|
|
1180
|
+
dir_rel: dirRel,
|
|
1181
|
+
});
|
|
1182
|
+
}
|
|
1183
|
+
return row.job_key || key;
|
|
1184
|
+
};
|
|
755
1185
|
const notes = Array.isArray(scan.json.notes) ? scan.json.notes : [];
|
|
756
1186
|
const videos = Array.isArray(scan.json.videos) ? scan.json.videos : [];
|
|
757
1187
|
const articles = Array.isArray(scan.json.articles) ? scan.json.articles : [];
|
|
@@ -762,90 +1192,120 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
762
1192
|
|
|
763
1193
|
for (const card of notes) {
|
|
764
1194
|
if (abortRequested(invocation)) { aborted = true; break outer; }
|
|
765
|
-
if (!capabilities.image_supported) {
|
|
766
|
-
skippedNotesByCapability += 1;
|
|
767
|
-
continue;
|
|
768
|
-
}
|
|
769
1195
|
const noteId = noteIdFromUrl(card.href);
|
|
770
1196
|
if (!noteId) continue;
|
|
771
|
-
|
|
1197
|
+
const existing = existingJobs.get(String(noteId)) || null;
|
|
1198
|
+
if (existing && existing.import_status !== 'pending') {
|
|
772
1199
|
skippedNotes += 1;
|
|
773
1200
|
continue;
|
|
774
1201
|
}
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
ctx, invocation, session, root, tools.scripts.note,
|
|
778
|
-
['--workspace', root, '--note', card.href, '--cdp', browser.cdp,
|
|
779
|
-
'--out', noteDir, '--login-state', paths.loginState],
|
|
780
|
-
noteDir,
|
|
781
|
-
paths,
|
|
782
|
-
'采集图文作品',
|
|
783
|
-
tools.nodePath,
|
|
784
|
-
browser,
|
|
785
|
-
tools,
|
|
786
|
-
);
|
|
787
|
-
if (!noteResult.ok) {
|
|
788
|
-
failures.push({ id: noteId, url: card.href, reason: noteResult.error });
|
|
789
|
-
if (noteResult.cancelled || authFailureText(noteResult.error)) { aborted = true; break outer; }
|
|
790
|
-
markFailed(noteId, card, { type: 'note', dir: noteDir }, noteResult.error);
|
|
1202
|
+
if ((!existing && crawled[noteId]) || queued.has(noteId)) {
|
|
1203
|
+
skippedNotes += 1;
|
|
791
1204
|
continue;
|
|
792
1205
|
}
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
1206
|
+
|
|
1207
|
+
const dirRel = existing && existing.dir_rel ? existing.dir_rel : jobRelDir(account.url, noteId);
|
|
1208
|
+
const noteDir = path.join(paths.jobs, dirRel);
|
|
1209
|
+
const jobKey = await ensureJob(noteId, 'note', card, dirRel, existing && existing.title);
|
|
1210
|
+
const target = path.join(noteDir, 'materials.jsonl');
|
|
1211
|
+
|
|
1212
|
+
if (materialFileReady(noteDir)) {
|
|
1213
|
+
processedWorks += 1;
|
|
1214
|
+
await enqueueJob({
|
|
1215
|
+
type: 'note',
|
|
1216
|
+
id: noteId,
|
|
1217
|
+
jobKey,
|
|
1218
|
+
card,
|
|
1219
|
+
accountUrl: account.url,
|
|
1220
|
+
url: (existing && existing.work_url) || card.href,
|
|
1221
|
+
dir: noteDir,
|
|
1222
|
+
dirRel,
|
|
1223
|
+
target,
|
|
1224
|
+
title: (existing && existing.title) || card.text || '',
|
|
1225
|
+
screenshots: [],
|
|
1226
|
+
finalizeOnly: true,
|
|
1227
|
+
});
|
|
1228
|
+
queued.add(noteId);
|
|
1229
|
+
if (NOTE_DELAY_MIN_MS < NOTE_DELAY_MAX_MS) {
|
|
1230
|
+
const delay = NOTE_DELAY_MIN_MS + Math.floor(Math.random() * (NOTE_DELAY_MAX_MS - NOTE_DELAY_MIN_MS));
|
|
1231
|
+
await sleep(delay);
|
|
1232
|
+
}
|
|
797
1233
|
continue;
|
|
798
1234
|
}
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
taskList.advance(4);
|
|
803
|
-
taskList.progress(`LLM 整理图文素材(${note.title || noteId})`);
|
|
804
|
-
const analyzed = await analyzeNote(ctx, invocation, session, {
|
|
805
|
-
root,
|
|
806
|
-
note,
|
|
807
|
-
screenshots: note.screenshots,
|
|
808
|
-
target,
|
|
809
|
-
materialTools,
|
|
810
|
-
paths,
|
|
811
|
-
taskList,
|
|
812
|
-
});
|
|
813
|
-
if (!analyzed.ok) {
|
|
814
|
-
failures.push({ id: noteId, url: card.href, reason: analyzed.error });
|
|
815
|
-
if (analyzed.aborted) { aborted = true; break outer; }
|
|
816
|
-
if (analyzed.fatal) break outer;
|
|
817
|
-
markFailed(noteId, card, { type: 'note', dir: noteDir, title: note.title }, analyzed.error);
|
|
1235
|
+
|
|
1236
|
+
if (!capabilities.image_supported) {
|
|
1237
|
+
skippedNotesByCapability += 1;
|
|
818
1238
|
continue;
|
|
819
1239
|
}
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
1240
|
+
|
|
1241
|
+
let note = reusableNote(noteDir);
|
|
1242
|
+
if (note) {
|
|
1243
|
+
note.account_url = account.url;
|
|
1244
|
+
await updateJob(jobStore.db, jobKey, {
|
|
1245
|
+
crawl_status: 'crawled',
|
|
1246
|
+
analysis_status: 'pending',
|
|
1247
|
+
dir_rel: dirRel,
|
|
825
1248
|
title: note.title || card.text || '',
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
dir: noteDir,
|
|
829
|
-
inserted: 0,
|
|
830
|
-
skipped: 0,
|
|
831
|
-
skip: true,
|
|
832
|
-
reason: analyzed.reason || '与网文创作无关',
|
|
1249
|
+
work_url: note.url || card.href,
|
|
1250
|
+
error: '',
|
|
833
1251
|
});
|
|
834
1252
|
} else {
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
1253
|
+
await updateJob(jobStore.db, jobKey, {
|
|
1254
|
+
crawl_status: 'crawling',
|
|
1255
|
+
analysis_status: 'pending',
|
|
1256
|
+
dir_rel: dirRel,
|
|
1257
|
+
attempts: (Number(existing && existing.attempts) || 0) + 1,
|
|
1258
|
+
error: '',
|
|
1259
|
+
});
|
|
1260
|
+
const noteResult = await runWithLogin(
|
|
1261
|
+
ctx, invocation, session, root, tools.scripts.note,
|
|
1262
|
+
['--workspace', root, '--note', card.href, '--cdp', browser.cdp,
|
|
1263
|
+
'--out', noteDir, '--login-state', paths.loginState],
|
|
1264
|
+
noteDir,
|
|
1265
|
+
paths,
|
|
1266
|
+
'采集图文作品',
|
|
1267
|
+
tools.nodePath,
|
|
1268
|
+
browser,
|
|
1269
|
+
tools,
|
|
1270
|
+
);
|
|
1271
|
+
if (!noteResult.ok) {
|
|
1272
|
+
failures.push({ id: noteId, url: card.href, reason: noteResult.error });
|
|
1273
|
+
if (noteResult.cancelled || authFailureText(noteResult.error)) { aborted = true; break outer; }
|
|
1274
|
+
await markFailed(noteId, card, { type: 'note', dir: noteDir }, noteResult.error);
|
|
1275
|
+
continue;
|
|
1276
|
+
}
|
|
1277
|
+
note = noteResult.json.note;
|
|
1278
|
+
if (!note || !Array.isArray(note.screenshots) || !note.screenshots.length) {
|
|
1279
|
+
failures.push({ id: noteId, url: card.href, reason: '没有截图' });
|
|
1280
|
+
await markFailed(noteId, card, { type: 'note', dir: noteDir }, '没有截图');
|
|
1281
|
+
continue;
|
|
1282
|
+
}
|
|
1283
|
+
await updateJob(jobStore.db, jobKey, {
|
|
1284
|
+
crawl_status: 'crawled',
|
|
1285
|
+
title: note.title || '',
|
|
1286
|
+
work_url: note.url || card.href,
|
|
1287
|
+
dir_rel: dirRel,
|
|
1288
|
+
error: '',
|
|
846
1289
|
});
|
|
847
1290
|
}
|
|
848
|
-
|
|
1291
|
+
|
|
1292
|
+
processedWorks += 1;
|
|
1293
|
+
note.account_url = account.url;
|
|
1294
|
+
await enqueueJob({
|
|
1295
|
+
type: 'note',
|
|
1296
|
+
id: noteId,
|
|
1297
|
+
jobKey,
|
|
1298
|
+
card,
|
|
1299
|
+
accountUrl: account.url,
|
|
1300
|
+
url: note.url || card.href,
|
|
1301
|
+
dir: noteDir,
|
|
1302
|
+
dirRel,
|
|
1303
|
+
note,
|
|
1304
|
+
screenshots: note.screenshots,
|
|
1305
|
+
target,
|
|
1306
|
+
title: note.title,
|
|
1307
|
+
});
|
|
1308
|
+
queued.add(noteId);
|
|
849
1309
|
if (NOTE_DELAY_MIN_MS < NOTE_DELAY_MAX_MS) {
|
|
850
1310
|
const delay = NOTE_DELAY_MIN_MS + Math.floor(Math.random() * (NOTE_DELAY_MAX_MS - NOTE_DELAY_MIN_MS));
|
|
851
1311
|
await sleep(delay);
|
|
@@ -854,62 +1314,122 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
854
1314
|
|
|
855
1315
|
for (const card of videos) {
|
|
856
1316
|
if (abortRequested(invocation)) { aborted = true; break outer; }
|
|
857
|
-
if (!capabilities.video_supported) {
|
|
858
|
-
skippedVideosByCapability += 1;
|
|
859
|
-
continue;
|
|
860
|
-
}
|
|
861
1317
|
const videoId = noteIdFromUrl(card.href);
|
|
862
1318
|
if (!videoId) continue;
|
|
863
|
-
|
|
1319
|
+
const existing = existingJobs.get(String(videoId)) || null;
|
|
1320
|
+
if (existing && existing.import_status !== 'pending') {
|
|
864
1321
|
skippedNotes += 1;
|
|
865
1322
|
continue;
|
|
866
1323
|
}
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
ctx, invocation, session, root, tools.scripts.video,
|
|
870
|
-
['--workspace', root, '--video', card.href, '--cdp', browser.cdp,
|
|
871
|
-
'--out', videoDir, '--login-state', paths.loginState,
|
|
872
|
-
'--max-seconds', String(MAX_VIDEO_SECONDS)],
|
|
873
|
-
videoDir,
|
|
874
|
-
paths,
|
|
875
|
-
'采集视频作品',
|
|
876
|
-
tools.nodePath,
|
|
877
|
-
browser,
|
|
878
|
-
tools,
|
|
879
|
-
VIDEO_SCRIPT_TIMEOUT_MS,
|
|
880
|
-
);
|
|
881
|
-
if (!videoResult.ok) {
|
|
882
|
-
failures.push({ id: videoId, url: card.href, reason: videoResult.error });
|
|
883
|
-
if (videoResult.cancelled || authFailureText(videoResult.error)) { aborted = true; break outer; }
|
|
884
|
-
markFailed(videoId, card, { type: 'video', dir: videoDir }, videoResult.error);
|
|
1324
|
+
if ((!existing && crawled[videoId]) || queued.has(videoId)) {
|
|
1325
|
+
skippedNotes += 1;
|
|
885
1326
|
continue;
|
|
886
1327
|
}
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
1328
|
+
|
|
1329
|
+
const dirRel = existing && existing.dir_rel ? existing.dir_rel : jobRelDir(account.url, videoId);
|
|
1330
|
+
const videoDir = path.join(paths.jobs, dirRel);
|
|
1331
|
+
const jobKey = await ensureJob(videoId, 'video', card, dirRel, existing && existing.title);
|
|
1332
|
+
const target = path.join(videoDir, 'materials.jsonl');
|
|
1333
|
+
|
|
1334
|
+
if (materialFileReady(videoDir)) {
|
|
1335
|
+
processedWorks += 1;
|
|
1336
|
+
await enqueueJob({
|
|
894
1337
|
type: 'video',
|
|
895
|
-
|
|
1338
|
+
id: videoId,
|
|
1339
|
+
jobKey,
|
|
1340
|
+
card,
|
|
1341
|
+
accountUrl: account.url,
|
|
1342
|
+
url: (existing && existing.work_url) || card.href,
|
|
896
1343
|
dir: videoDir,
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
1344
|
+
dirRel,
|
|
1345
|
+
target,
|
|
1346
|
+
title: (existing && existing.title) || card.text || '',
|
|
1347
|
+
duration: existing && existing.duration,
|
|
1348
|
+
video: null,
|
|
1349
|
+
finalizeOnly: true,
|
|
901
1350
|
});
|
|
902
|
-
|
|
1351
|
+
queued.add(videoId);
|
|
1352
|
+
if (NOTE_DELAY_MIN_MS < NOTE_DELAY_MAX_MS) {
|
|
1353
|
+
const delay = NOTE_DELAY_MIN_MS + Math.floor(Math.random() * (NOTE_DELAY_MAX_MS - NOTE_DELAY_MIN_MS));
|
|
1354
|
+
await sleep(delay);
|
|
1355
|
+
}
|
|
903
1356
|
continue;
|
|
904
1357
|
}
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
1358
|
+
|
|
1359
|
+
if (!capabilities.video_supported) {
|
|
1360
|
+
skippedVideosByCapability += 1;
|
|
908
1361
|
continue;
|
|
909
1362
|
}
|
|
1363
|
+
|
|
1364
|
+
let video = reusableVideo(videoDir);
|
|
1365
|
+
if (!video) {
|
|
1366
|
+
await updateJob(jobStore.db, jobKey, {
|
|
1367
|
+
crawl_status: 'crawling',
|
|
1368
|
+
analysis_status: 'pending',
|
|
1369
|
+
dir_rel: dirRel,
|
|
1370
|
+
attempts: (Number(existing && existing.attempts) || 0) + 1,
|
|
1371
|
+
error: '',
|
|
1372
|
+
});
|
|
1373
|
+
const videoResult = await runWithLogin(
|
|
1374
|
+
ctx, invocation, session, root, tools.scripts.video,
|
|
1375
|
+
['--workspace', root, '--video', card.href, '--cdp', browser.cdp,
|
|
1376
|
+
'--out', videoDir, '--login-state', paths.loginState,
|
|
1377
|
+
'--max-seconds', String(MAX_VIDEO_SECONDS)],
|
|
1378
|
+
videoDir,
|
|
1379
|
+
paths,
|
|
1380
|
+
'采集视频作品',
|
|
1381
|
+
tools.nodePath,
|
|
1382
|
+
browser,
|
|
1383
|
+
tools,
|
|
1384
|
+
VIDEO_SCRIPT_TIMEOUT_MS,
|
|
1385
|
+
);
|
|
1386
|
+
if (!videoResult.ok) {
|
|
1387
|
+
failures.push({ id: videoId, url: card.href, reason: videoResult.error });
|
|
1388
|
+
if (videoResult.cancelled || authFailureText(videoResult.error)) { aborted = true; break outer; }
|
|
1389
|
+
await markFailed(videoId, card, { type: 'video', dir: videoDir }, videoResult.error);
|
|
1390
|
+
continue;
|
|
1391
|
+
}
|
|
1392
|
+
video = videoResult.json.video || {};
|
|
1393
|
+
if (videoResult.json.skip) {
|
|
1394
|
+
skippedLongVideos += 1;
|
|
1395
|
+
await markJobSkipped({
|
|
1396
|
+
id: videoId,
|
|
1397
|
+
jobKey,
|
|
1398
|
+
accountUrl: account.url,
|
|
1399
|
+
url: video.url || card.href,
|
|
1400
|
+
title: video.title || card.text || '',
|
|
1401
|
+
type: 'video',
|
|
1402
|
+
duration: video.duration,
|
|
1403
|
+
dir: videoDir,
|
|
1404
|
+
card,
|
|
1405
|
+
}, videoResult.json.reason || '视频时长超过 1 分钟', { crawl: true });
|
|
1406
|
+
continue;
|
|
1407
|
+
}
|
|
1408
|
+
if (!video || !Array.isArray(video.frames) || !video.frames.length) {
|
|
1409
|
+
failures.push({ id: videoId, url: card.href, reason: '没有视频关键帧' });
|
|
1410
|
+
await markFailed(videoId, card, { type: 'video', dir: videoDir, title: video.title }, '没有视频关键帧');
|
|
1411
|
+
continue;
|
|
1412
|
+
}
|
|
1413
|
+
await updateJob(jobStore.db, jobKey, {
|
|
1414
|
+
crawl_status: 'crawled',
|
|
1415
|
+
title: video.title || '',
|
|
1416
|
+
work_url: video.url || card.href,
|
|
1417
|
+
dir_rel: dirRel,
|
|
1418
|
+
error: '',
|
|
1419
|
+
});
|
|
1420
|
+
} else {
|
|
1421
|
+
await updateJob(jobStore.db, jobKey, {
|
|
1422
|
+
crawl_status: 'crawled',
|
|
1423
|
+
analysis_status: 'pending',
|
|
1424
|
+
dir_rel: dirRel,
|
|
1425
|
+
title: video.title || '',
|
|
1426
|
+
work_url: video.url || card.href,
|
|
1427
|
+
error: '',
|
|
1428
|
+
});
|
|
1429
|
+
}
|
|
1430
|
+
|
|
910
1431
|
processedWorks += 1;
|
|
911
1432
|
video.account_url = account.url;
|
|
912
|
-
const target = path.join(videoDir, 'materials.jsonl');
|
|
913
1433
|
taskList.advance(4);
|
|
914
1434
|
taskList.progress(`LLM 判断视频前 5 秒(${video.title || videoId})`);
|
|
915
1435
|
const relevance = await analyzeVideoRelevance(ctx, invocation, session, {
|
|
@@ -922,72 +1442,40 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
922
1442
|
if (!relevance.ok) {
|
|
923
1443
|
failures.push({ id: videoId, url: card.href, reason: relevance.error });
|
|
924
1444
|
if (relevance.aborted) { aborted = true; break outer; }
|
|
925
|
-
markFailed(videoId, card, { type: 'video', dir: videoDir, title: video.title, duration: video.duration }, relevance.error);
|
|
1445
|
+
await markFailed(videoId, card, { type: 'video', dir: videoDir, title: video.title, duration: video.duration }, relevance.error);
|
|
926
1446
|
continue;
|
|
927
1447
|
}
|
|
928
1448
|
if (relevance.skip) {
|
|
929
1449
|
skippedIrrelevant += 1;
|
|
930
|
-
|
|
931
|
-
|
|
1450
|
+
await markJobSkipped({
|
|
1451
|
+
id: videoId,
|
|
1452
|
+
jobKey,
|
|
1453
|
+
accountUrl: account.url,
|
|
932
1454
|
url: video.url || card.href,
|
|
933
1455
|
title: video.title || card.text || '',
|
|
934
1456
|
type: 'video',
|
|
935
1457
|
duration: video.duration,
|
|
936
1458
|
dir: videoDir,
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
skip: true,
|
|
940
|
-
reason: relevance.reason || '前 5 秒与网文创作无关',
|
|
941
|
-
});
|
|
942
|
-
crawled[videoId] = true;
|
|
1459
|
+
card,
|
|
1460
|
+
}, relevance.reason || '前 5 秒与网文创作无关');
|
|
943
1461
|
continue;
|
|
944
1462
|
}
|
|
945
1463
|
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
1464
|
+
await enqueueJob({
|
|
1465
|
+
type: 'video',
|
|
1466
|
+
id: videoId,
|
|
1467
|
+
jobKey,
|
|
1468
|
+
card,
|
|
949
1469
|
accountUrl: account.url,
|
|
1470
|
+
url: video.url || card.href,
|
|
1471
|
+
dir: videoDir,
|
|
1472
|
+
dirRel,
|
|
950
1473
|
video,
|
|
951
1474
|
target,
|
|
952
|
-
|
|
953
|
-
|
|
1475
|
+
title: video.title,
|
|
1476
|
+
duration: video.duration,
|
|
954
1477
|
});
|
|
955
|
-
|
|
956
|
-
failures.push({ id: videoId, url: card.href, reason: analyzed.error });
|
|
957
|
-
if (analyzed.aborted) { aborted = true; break outer; }
|
|
958
|
-
if (analyzed.fatal) break outer;
|
|
959
|
-
markFailed(videoId, card, { type: 'video', dir: videoDir, title: video.title, duration: video.duration }, analyzed.error);
|
|
960
|
-
continue;
|
|
961
|
-
}
|
|
962
|
-
if (analyzed.skipped) {
|
|
963
|
-
skippedIrrelevant += 1;
|
|
964
|
-
markCrawled(root, videoId, {
|
|
965
|
-
account_url: account.url,
|
|
966
|
-
url: video.url || card.href,
|
|
967
|
-
title: video.title || card.text || '',
|
|
968
|
-
type: 'video',
|
|
969
|
-
duration: video.duration,
|
|
970
|
-
dir: videoDir,
|
|
971
|
-
inserted: 0,
|
|
972
|
-
skipped: 0,
|
|
973
|
-
skip: true,
|
|
974
|
-
reason: analyzed.reason || '与网文创作无关',
|
|
975
|
-
});
|
|
976
|
-
} else {
|
|
977
|
-
inserted += analyzed.inserted;
|
|
978
|
-
skippedMaterials += analyzed.skipped;
|
|
979
|
-
markCrawled(root, videoId, {
|
|
980
|
-
account_url: account.url,
|
|
981
|
-
url: video.url || card.href,
|
|
982
|
-
title: video.title || card.text || '',
|
|
983
|
-
type: 'video',
|
|
984
|
-
duration: video.duration,
|
|
985
|
-
dir: videoDir,
|
|
986
|
-
inserted: analyzed.inserted,
|
|
987
|
-
skipped: analyzed.skipped,
|
|
988
|
-
});
|
|
989
|
-
}
|
|
990
|
-
crawled[videoId] = true;
|
|
1478
|
+
queued.add(videoId);
|
|
991
1479
|
if (NOTE_DELAY_MIN_MS < NOTE_DELAY_MAX_MS) {
|
|
992
1480
|
const delay = NOTE_DELAY_MIN_MS + Math.floor(Math.random() * (NOTE_DELAY_MAX_MS - NOTE_DELAY_MIN_MS));
|
|
993
1481
|
await sleep(delay);
|
|
@@ -998,87 +1486,111 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
998
1486
|
if (abortRequested(invocation)) { aborted = true; break outer; }
|
|
999
1487
|
const articleId = noteIdFromUrl(card.href);
|
|
1000
1488
|
if (!articleId) continue;
|
|
1001
|
-
|
|
1489
|
+
const existing = existingJobs.get(String(articleId)) || null;
|
|
1490
|
+
if (existing && existing.import_status !== 'pending') {
|
|
1002
1491
|
skippedArticles += 1;
|
|
1003
1492
|
continue;
|
|
1004
1493
|
}
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
ctx, invocation, session, root, tools.scripts.article,
|
|
1008
|
-
['--workspace', root, '--article', card.href, '--cdp', browser.cdp,
|
|
1009
|
-
'--out', articleDir, '--login-state', paths.loginState],
|
|
1010
|
-
articleDir,
|
|
1011
|
-
paths,
|
|
1012
|
-
'采集文章',
|
|
1013
|
-
tools.nodePath,
|
|
1014
|
-
browser,
|
|
1015
|
-
tools,
|
|
1016
|
-
);
|
|
1017
|
-
if (!articleResult.ok) {
|
|
1018
|
-
failures.push({ id: articleId, url: card.href, reason: articleResult.error });
|
|
1019
|
-
if (articleResult.cancelled || authFailureText(articleResult.error)) { aborted = true; break outer; }
|
|
1020
|
-
markFailed(articleId, card, { type: 'article', dir: articleDir }, articleResult.error);
|
|
1494
|
+
if ((!existing && crawled[articleId]) || queued.has(articleId)) {
|
|
1495
|
+
skippedArticles += 1;
|
|
1021
1496
|
continue;
|
|
1022
1497
|
}
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1498
|
+
|
|
1499
|
+
const dirRel = existing && existing.dir_rel ? existing.dir_rel : jobRelDir(account.url, articleId);
|
|
1500
|
+
const articleDir = path.join(paths.jobs, dirRel);
|
|
1501
|
+
const jobKey = await ensureJob(articleId, 'article', card, dirRel, existing && existing.title);
|
|
1502
|
+
const target = path.join(articleDir, 'materials.jsonl');
|
|
1503
|
+
|
|
1504
|
+
if (materialFileReady(articleDir)) {
|
|
1505
|
+
processedWorks += 1;
|
|
1506
|
+
await enqueueJob({
|
|
1507
|
+
type: 'article',
|
|
1508
|
+
id: articleId,
|
|
1509
|
+
jobKey,
|
|
1510
|
+
card,
|
|
1511
|
+
accountUrl: account.url,
|
|
1512
|
+
url: (existing && existing.work_url) || card.href,
|
|
1513
|
+
dir: articleDir,
|
|
1514
|
+
dirRel,
|
|
1515
|
+
target,
|
|
1516
|
+
title: (existing && existing.title) || card.text || '',
|
|
1517
|
+
article: null,
|
|
1518
|
+
finalizeOnly: true,
|
|
1519
|
+
});
|
|
1520
|
+
queued.add(articleId);
|
|
1521
|
+
if (NOTE_DELAY_MIN_MS < NOTE_DELAY_MAX_MS) {
|
|
1522
|
+
const delay = NOTE_DELAY_MIN_MS + Math.floor(Math.random() * (NOTE_DELAY_MAX_MS - NOTE_DELAY_MIN_MS));
|
|
1523
|
+
await sleep(delay);
|
|
1524
|
+
}
|
|
1027
1525
|
continue;
|
|
1028
1526
|
}
|
|
1527
|
+
|
|
1528
|
+
let article = reusableArticle(articleDir);
|
|
1529
|
+
if (!article) {
|
|
1530
|
+
await updateJob(jobStore.db, jobKey, {
|
|
1531
|
+
crawl_status: 'crawling',
|
|
1532
|
+
analysis_status: 'pending',
|
|
1533
|
+
dir_rel: dirRel,
|
|
1534
|
+
attempts: (Number(existing && existing.attempts) || 0) + 1,
|
|
1535
|
+
error: '',
|
|
1536
|
+
});
|
|
1537
|
+
const articleResult = await runWithLogin(
|
|
1538
|
+
ctx, invocation, session, root, tools.scripts.article,
|
|
1539
|
+
['--workspace', root, '--article', card.href, '--cdp', browser.cdp,
|
|
1540
|
+
'--out', articleDir, '--login-state', paths.loginState],
|
|
1541
|
+
articleDir,
|
|
1542
|
+
paths,
|
|
1543
|
+
'采集文章',
|
|
1544
|
+
tools.nodePath,
|
|
1545
|
+
browser,
|
|
1546
|
+
tools,
|
|
1547
|
+
);
|
|
1548
|
+
if (!articleResult.ok) {
|
|
1549
|
+
failures.push({ id: articleId, url: card.href, reason: articleResult.error });
|
|
1550
|
+
if (articleResult.cancelled || authFailureText(articleResult.error)) { aborted = true; break outer; }
|
|
1551
|
+
await markFailed(articleId, card, { type: 'article', dir: articleDir }, articleResult.error);
|
|
1552
|
+
continue;
|
|
1553
|
+
}
|
|
1554
|
+
article = articleResult.json.article || {};
|
|
1555
|
+
if (!article || !article.body || String(article.body).trim().length < 50) {
|
|
1556
|
+
failures.push({ id: articleId, url: card.href, reason: '文章正文为空' });
|
|
1557
|
+
await markFailed(articleId, card, { type: 'article', dir: articleDir, title: article.title }, '文章正文为空');
|
|
1558
|
+
continue;
|
|
1559
|
+
}
|
|
1560
|
+
await updateJob(jobStore.db, jobKey, {
|
|
1561
|
+
crawl_status: 'crawled',
|
|
1562
|
+
title: article.title || '',
|
|
1563
|
+
work_url: article.url || card.href,
|
|
1564
|
+
dir_rel: dirRel,
|
|
1565
|
+
error: '',
|
|
1566
|
+
});
|
|
1567
|
+
} else {
|
|
1568
|
+
await updateJob(jobStore.db, jobKey, {
|
|
1569
|
+
crawl_status: 'crawled',
|
|
1570
|
+
analysis_status: 'pending',
|
|
1571
|
+
dir_rel: dirRel,
|
|
1572
|
+
title: article.title || '',
|
|
1573
|
+
work_url: article.url || card.href,
|
|
1574
|
+
error: '',
|
|
1575
|
+
});
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1029
1578
|
processedWorks += 1;
|
|
1030
1579
|
article.account_url = account.url;
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1580
|
+
await enqueueJob({
|
|
1581
|
+
type: 'article',
|
|
1582
|
+
id: articleId,
|
|
1583
|
+
jobKey,
|
|
1584
|
+
card,
|
|
1036
1585
|
accountUrl: account.url,
|
|
1586
|
+
url: article.url || card.href,
|
|
1587
|
+
dir: articleDir,
|
|
1588
|
+
dirRel,
|
|
1037
1589
|
article,
|
|
1038
1590
|
target,
|
|
1039
|
-
|
|
1040
|
-
taskList,
|
|
1591
|
+
title: article.title,
|
|
1041
1592
|
});
|
|
1042
|
-
|
|
1043
|
-
failures.push({ id: articleId, url: card.href, reason: analyzed.error });
|
|
1044
|
-
if (analyzed.aborted) { aborted = true; break outer; }
|
|
1045
|
-
if (analyzed.fatal) break outer;
|
|
1046
|
-
markFailed(articleId, card, { type: 'article', dir: articleDir, title: article.title }, analyzed.error);
|
|
1047
|
-
continue;
|
|
1048
|
-
}
|
|
1049
|
-
if (analyzed.skipped) {
|
|
1050
|
-
skippedIrrelevant += 1;
|
|
1051
|
-
markCrawled(root, articleId, {
|
|
1052
|
-
account_url: account.url,
|
|
1053
|
-
url: article.url || card.href,
|
|
1054
|
-
title: article.title || card.text || '',
|
|
1055
|
-
type: 'article',
|
|
1056
|
-
body_file: article.body_file,
|
|
1057
|
-
reading_time: article.reading_time,
|
|
1058
|
-
body_length: article.body_length,
|
|
1059
|
-
dir: articleDir,
|
|
1060
|
-
inserted: 0,
|
|
1061
|
-
skipped: 0,
|
|
1062
|
-
skip: true,
|
|
1063
|
-
reason: analyzed.reason || '与网文创作无关',
|
|
1064
|
-
});
|
|
1065
|
-
} else {
|
|
1066
|
-
inserted += analyzed.inserted;
|
|
1067
|
-
skippedMaterials += analyzed.skipped;
|
|
1068
|
-
markCrawled(root, articleId, {
|
|
1069
|
-
account_url: account.url,
|
|
1070
|
-
url: article.url || card.href,
|
|
1071
|
-
title: article.title || card.text || '',
|
|
1072
|
-
type: 'article',
|
|
1073
|
-
body_file: article.body_file,
|
|
1074
|
-
reading_time: article.reading_time,
|
|
1075
|
-
body_length: article.body_length,
|
|
1076
|
-
dir: articleDir,
|
|
1077
|
-
inserted: analyzed.inserted,
|
|
1078
|
-
skipped: analyzed.skipped,
|
|
1079
|
-
});
|
|
1080
|
-
}
|
|
1081
|
-
crawled[articleId] = true;
|
|
1593
|
+
queued.add(articleId);
|
|
1082
1594
|
if (NOTE_DELAY_MIN_MS < NOTE_DELAY_MAX_MS) {
|
|
1083
1595
|
const delay = NOTE_DELAY_MIN_MS + Math.floor(Math.random() * (NOTE_DELAY_MAX_MS - NOTE_DELAY_MIN_MS));
|
|
1084
1596
|
await sleep(delay);
|
|
@@ -1086,6 +1598,25 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
1086
1598
|
}
|
|
1087
1599
|
}
|
|
1088
1600
|
|
|
1601
|
+
if (queue) {
|
|
1602
|
+
queue.close();
|
|
1603
|
+
await Promise.all(workerPromises);
|
|
1604
|
+
} else if (jobs.length) {
|
|
1605
|
+
taskList.advance(4);
|
|
1606
|
+
taskList.progress(progressText());
|
|
1607
|
+
for (const job of jobs) {
|
|
1608
|
+
if (abortRequested(invocation)) { aborted = true; break; }
|
|
1609
|
+
const analyzed = await runLocalAnalysisJob(ctx, invocation, session, job, {
|
|
1610
|
+
root,
|
|
1611
|
+
materialTools,
|
|
1612
|
+
paths,
|
|
1613
|
+
taskList,
|
|
1614
|
+
});
|
|
1615
|
+
await finalizeJob(job, analyzed);
|
|
1616
|
+
if (analyzed && analyzed.fatal) { aborted = true; break; }
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1089
1620
|
taskList.advance(5);
|
|
1090
1621
|
return notify(session, (aborted || failures.length) ? 'error' : 'success', summaryText({
|
|
1091
1622
|
accounts: accounts.length,
|
|
@@ -1107,9 +1638,16 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
1107
1638
|
stateFile: paths.crawled,
|
|
1108
1639
|
aborted,
|
|
1109
1640
|
mode: browser.mode,
|
|
1641
|
+
analysisMode: useSubagents ? 'subagent' : 'single',
|
|
1642
|
+
subagentWorkers,
|
|
1110
1643
|
}));
|
|
1111
1644
|
} finally {
|
|
1645
|
+
if (queue) {
|
|
1646
|
+
queue.close();
|
|
1647
|
+
await Promise.allSettled(workerPromises);
|
|
1648
|
+
}
|
|
1112
1649
|
await stopChrome(root, tools, paths);
|
|
1650
|
+
if (jobStore) await jobStore.close();
|
|
1113
1651
|
}
|
|
1114
1652
|
}
|
|
1115
1653
|
|
|
@@ -1131,7 +1669,17 @@ async function runAdd(session, rawUrl) {
|
|
|
1131
1669
|
|
|
1132
1670
|
async function runList(session) {
|
|
1133
1671
|
const root = workspaceOf(session);
|
|
1134
|
-
|
|
1672
|
+
let counts = null;
|
|
1673
|
+
try {
|
|
1674
|
+
const store = await openJobStore(root);
|
|
1675
|
+
try {
|
|
1676
|
+
await migrateCrawledJobs(store.db, root);
|
|
1677
|
+
counts = await countJobsByAccount(store.db);
|
|
1678
|
+
} finally {
|
|
1679
|
+
await store.close();
|
|
1680
|
+
}
|
|
1681
|
+
} catch { /* fall back to local crawled.json */ }
|
|
1682
|
+
return notify(session, 'success', accountListText(root, counts));
|
|
1135
1683
|
}
|
|
1136
1684
|
|
|
1137
1685
|
async function runRemove(session, selector, taskList) {
|
|
@@ -1157,7 +1705,7 @@ function douyinTaskTitles(invocation) {
|
|
|
1157
1705
|
'LLM 能力自检(图片 / 视频)',
|
|
1158
1706
|
'扫描账号主页',
|
|
1159
1707
|
'采集图文 / 视频 / 文章作品',
|
|
1160
|
-
'
|
|
1708
|
+
'分析作品素材',
|
|
1161
1709
|
'写入素材库',
|
|
1162
1710
|
];
|
|
1163
1711
|
}
|