@cloud411716/fancy-webnovel 4.8.2 → 4.10.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 +1 -1
- package/docs/douyin-plugin.md +32 -6
- package/docs/workspace-mcp.md +7 -5
- package/package.json +1 -1
- package/plugins/fancy-bootstrap/index.js +1 -1
- package/plugins/fancy-douyin/deploy.js +5 -4
- package/plugins/fancy-douyin/index.js +409 -129
- package/plugins/fancy-douyin/prompts/03-article-prompt.md +94 -0
- package/plugins/fancy-douyin/scripts/crawl-article.cjs +146 -0
- package/plugins/fancy-douyin/scripts/scan-account.cjs +32 -8
- package/plugins/fancy-douyin/state.js +45 -9
- package/plugins/fancy-douyin/subagents.js +70 -0
- package/plugins/fancy-douyin/templates.js +40 -4
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
你正在分析一篇抖音长文/文章作品,把文章里的设定、素材、灵感和写作方法拆成可以直接进入网文素材库的小积木。
|
|
2
|
+
|
|
3
|
+
## 作品信息
|
|
4
|
+
|
|
5
|
+
- 账号主页:{{ACCOUNT_URL}}
|
|
6
|
+
- 文章地址:{{ARTICLE_URL}}
|
|
7
|
+
- 文章标题:{{ARTICLE_TITLE}}
|
|
8
|
+
- 发布日期:{{ARTICLE_DATE}}
|
|
9
|
+
- 阅读时长:{{ARTICLE_READING_TIME}}
|
|
10
|
+
- 正文字数:约 {{ARTICLE_LENGTH}} 字
|
|
11
|
+
- 正文文件:`{{ARTICLE_FILE}}`
|
|
12
|
+
|
|
13
|
+
## 文章正文
|
|
14
|
+
|
|
15
|
+
{{ARTICLE_BODY}}
|
|
16
|
+
|
|
17
|
+
## 任务
|
|
18
|
+
|
|
19
|
+
分析文章内容。如果文章与小说网文创作的设定、素材、写作方法等无关,写 skip 标记并结束。有关则把文章拆成原子素材,不要只写一篇摘要。
|
|
20
|
+
|
|
21
|
+
### 提取规则(必须遵守)
|
|
22
|
+
|
|
23
|
+
1. **先按文章结构拆小节**:一级标题、二级标题、编号列表都是天然的分类依据。小节标题要进入 `subkind` 和 `tags`。
|
|
24
|
+
2. **世界观 / 设定大纲 / 图鉴 / 列表**:
|
|
25
|
+
- 每个独立条目单独成一条素材:世界层级、境界、灵根、宗门、资源、丹药、法器、规则、角色、地点等分别输出。
|
|
26
|
+
- `name` 原样保留文章里的名称或短语:例如“引气境”“筑基境”“凡界”“灵界”“极品灵根”“九转成丹”。
|
|
27
|
+
- 不要把多个条目合并成一条,不要改写成意象句,不要只挑一部分。文章里有多少个独立设定,就输出多少条。
|
|
28
|
+
- 分类标题进入 `subkind` 和 `tags`。例如【二、完整修炼体系】→ subkind 写“修炼体系”,tags 至少包含 `修炼体系`、`修仙`。
|
|
29
|
+
3. **长段落 / 分析 / 写作技巧**:抽象成可以独立取用的素材,不要整段抄写。机制、代价、限制、反制、冲突、反转、写法分开成条。
|
|
30
|
+
4. `kind` 必须写中文主题,例如 `修仙境界`、`世界层级`、`灵根`、`宗门`、`功法`、`丹药`、`法器`、`世界规则`、`写作技巧`。不要写 `world_rule`、`color_word` 这类英文内部标识。
|
|
31
|
+
5. `subkind` 可选,是 `kind` 下的中文二级分类,最多 40 字;文章有二级标题就原样写。
|
|
32
|
+
6. `tags` 3–8 个:包含文章主题、章节分类、用途、气质等维度。
|
|
33
|
+
7. `genres` 1–5 个:根据内容判断,例如仙侠、玄幻、修真、西幻、悬疑、都市异能。
|
|
34
|
+
8. `note` 只补充一句使用场景、限制或来源,最多 300 字;不要把 `name` 再改写一遍。
|
|
35
|
+
9. **禁止**把长文只概括成少量创作句;文章里的独立设定必须逐条保留。
|
|
36
|
+
|
|
37
|
+
### 命名风格提炼与同风格扩写(每篇必做)
|
|
38
|
+
|
|
39
|
+
提取完全部原始条目后,先提炼这篇文章的命名方式、特色和吸睛原因,再据此扩写 10 条新素材:
|
|
40
|
+
|
|
41
|
+
1. **先提炼,再动笔**:想清楚文章的命名方式(如“境界名 + 能力边界”“地名 + 地域气质”“宗门名 + 行事规则”)、语言气质和它为什么适合网文。
|
|
42
|
+
2. **选一组 kind + subkind**:从本文已提取的条目里选最有代表性的一组,扩写的 10 条必须全部使用这同一个 `kind` 和同一个 `subkind`;如果文章没有二级分类,10 条保持同 `kind` 即可。
|
|
43
|
+
3. **保持风格一致**:新素材要沿用文章的长度、词性、意象密度和命名节奏;读起来必须像同一篇设定大纲里会出现的条目。
|
|
44
|
+
4. **必须是新条目**:不能与文章原始条目、也不能与数据库中已有素材同名或近义重复。
|
|
45
|
+
5. **同样写全字段**:每条都要有 `tags`、`genres`、`note`;质量与原始条目一致。
|
|
46
|
+
6. **写在同一个文件**:10 条新素材接在原始条目后面,`id` 序号继续递增,仍用 `dy_<文章ID>_<序号>`。
|
|
47
|
+
7. **数量**:原始条目全部保留,另加 10 条新素材;如果文章有多个 subkind,只选其中一组扩写 10 条。
|
|
48
|
+
|
|
49
|
+
## 输出文件
|
|
50
|
+
|
|
51
|
+
把 JSONL 写到:
|
|
52
|
+
|
|
53
|
+
```text
|
|
54
|
+
{{TARGET_FILE}}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
一行一个 JSON 对象,不要 Markdown 代码围栏、标题、解释、前后缀。
|
|
58
|
+
|
|
59
|
+
字段只能是:
|
|
60
|
+
|
|
61
|
+
```text
|
|
62
|
+
id / kind / subkind / name / tags / genres / note / source
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
- `source` 固定写 `other`。
|
|
66
|
+
- `name`:设定/图鉴/列表条目必须原样抄录;长段落才抽象。
|
|
67
|
+
- `kind` 必须写中文主题。
|
|
68
|
+
- `subkind` 可选,是 `kind` 下的中文二级分类,最多 40 字。
|
|
69
|
+
- `tags` 3–8 个短标签;必须包含文章主题和章节分类。
|
|
70
|
+
- `genres` 1–5 个题材;不要全部写 `["通用"]`。
|
|
71
|
+
- `note` 可写一句补充,没有就写空字符串,最多 300 字。
|
|
72
|
+
- `id` 用小写字母、数字、下划线,格式建议:`dy_<文章ID>_<序号>`,全局唯一。
|
|
73
|
+
|
|
74
|
+
如果判断文章与网文创作无关:不要写任何素材行,只把下面的对象写进同一个文件,然后结束本轮:
|
|
75
|
+
|
|
76
|
+
```json
|
|
77
|
+
{"skip": true, "reason": "与网文创作无关:<一句话原因>"}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
生成前可以查询已有素材,避免同名:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
node '{{QUERY_SCRIPT}}' --workspace '{{WORKSPACE}}' --kind <kind> --name '<候选名>' --json
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
写完运行格式校验:
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
node '{{VALIDATE_SCRIPT}}' '{{TARGET_FILE}}'
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
校验失败就按错误修正,直到通过。
|
|
93
|
+
|
|
94
|
+
写完后只回复:`已写入 {{TARGET_FILE}}`。
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Open one Douyin /article/ long-form work, extract its title, metadata and
|
|
5
|
+
* body text, and write article.json + article.md. stdout is one JSON object;
|
|
6
|
+
* Chrome stays open.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const fs = require('fs');
|
|
10
|
+
const path = require('path');
|
|
11
|
+
const {
|
|
12
|
+
log, parseArgs, connect, installExternalProtocolGuard, restoreCookies, saveCookies,
|
|
13
|
+
safeGateResult, printJson, fail, ensureDir,
|
|
14
|
+
} = require('./lib.cjs');
|
|
15
|
+
|
|
16
|
+
function articleId(url) {
|
|
17
|
+
const match = String(url).match(/\/article\/(\d+)/);
|
|
18
|
+
if (match) return match[1];
|
|
19
|
+
return String(url).replace(/[?#].*$/, '').split('/').filter(Boolean).pop() || '';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function readArticle(page) {
|
|
23
|
+
return await page.evaluate(() => {
|
|
24
|
+
const text = (el) => (el && el.innerText ? el.innerText.trim() : '');
|
|
25
|
+
const titleEl = document.querySelector('.ZuAUZjxr');
|
|
26
|
+
const metaEls = [...document.querySelectorAll('.lXa17VN5 .uKJCzxhH')];
|
|
27
|
+
let bodyEl = document.querySelector('.vZyRhR3L');
|
|
28
|
+
if (!bodyEl || text(bodyEl).length < 50) {
|
|
29
|
+
const candidates = [...document.querySelectorAll('div, article, section')]
|
|
30
|
+
.map((el) => ({
|
|
31
|
+
el,
|
|
32
|
+
len: text(el).length,
|
|
33
|
+
paragraphs: el.querySelectorAll('p').length,
|
|
34
|
+
}))
|
|
35
|
+
.filter((item) => item.paragraphs >= 3 && item.len >= 100)
|
|
36
|
+
.sort((a, b) => b.len - a.len);
|
|
37
|
+
if (candidates.length) bodyEl = candidates[0].el;
|
|
38
|
+
}
|
|
39
|
+
const paragraphs = bodyEl ? [...bodyEl.querySelectorAll('p')].map((p) => text(p)).filter(Boolean) : [];
|
|
40
|
+
const rawBody = text(bodyEl);
|
|
41
|
+
const meta = metaEls.map((el) => text(el)).filter(Boolean);
|
|
42
|
+
const date = meta.find((value) => /^\d{4}-\d{2}-\d{2}$/.test(value)) || '';
|
|
43
|
+
const readingTime = meta.find((value) => /阅读需要/.test(value)) || '';
|
|
44
|
+
return {
|
|
45
|
+
title: text(titleEl) || document.title.replace(/\s*-\s*抖音$/, ''),
|
|
46
|
+
date,
|
|
47
|
+
reading_time: readingTime,
|
|
48
|
+
body: paragraphs.length ? paragraphs.join('\n\n') : rawBody,
|
|
49
|
+
paragraphs,
|
|
50
|
+
body_length: rawBody.length,
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function waitForArticle(page) {
|
|
56
|
+
let article = null;
|
|
57
|
+
for (let attempt = 0; attempt < 20; attempt += 1) {
|
|
58
|
+
article = await readArticle(page);
|
|
59
|
+
if (article && article.title && article.body && article.body.length >= 100) return article;
|
|
60
|
+
await page.waitForTimeout(700);
|
|
61
|
+
}
|
|
62
|
+
return article;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function main() {
|
|
66
|
+
const args = parseArgs(process.argv.slice(2));
|
|
67
|
+
const workspace = path.resolve(String(args.workspace || process.cwd()));
|
|
68
|
+
const articleUrl = String(args.article || '');
|
|
69
|
+
const cdp = String(args.cdp || 'http://127.0.0.1:9222');
|
|
70
|
+
const outDir = path.resolve(String(args.out || path.join(workspace, '.fancy', 'douyin', 'output', 'article')));
|
|
71
|
+
const loginState = path.resolve(String(args['login-state'] || path.join(workspace, '.fancy', 'douyin', 'login-state.json')));
|
|
72
|
+
if (!/\/article\//.test(articleUrl)) throw new Error('不是抖音文章作品地址:' + articleUrl);
|
|
73
|
+
ensureDir(outDir);
|
|
74
|
+
|
|
75
|
+
const { context } = await connect(cdp);
|
|
76
|
+
let page = null;
|
|
77
|
+
let keepPage = false;
|
|
78
|
+
try {
|
|
79
|
+
await restoreCookies(context, loginState);
|
|
80
|
+
page = await context.newPage();
|
|
81
|
+
await installExternalProtocolGuard(page);
|
|
82
|
+
await page.goto(articleUrl, { waitUntil: 'domcontentloaded', timeout: 60000 });
|
|
83
|
+
await page.waitForTimeout(3500);
|
|
84
|
+
|
|
85
|
+
const firstGate = await safeGateResult(page, outDir);
|
|
86
|
+
if (firstGate) {
|
|
87
|
+
keepPage = true;
|
|
88
|
+
await saveCookies(context, loginState, page.url());
|
|
89
|
+
printJson(firstGate);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const article = await waitForArticle(page);
|
|
94
|
+
if (!article || !article.title || !article.body || article.body.length < 50) {
|
|
95
|
+
throw new Error('文章正文没有加载出来');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const afterGate = await safeGateResult(page, outDir);
|
|
99
|
+
if (afterGate) {
|
|
100
|
+
keepPage = true;
|
|
101
|
+
await saveCookies(context, loginState, page.url());
|
|
102
|
+
printJson(afterGate);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
await saveCookies(context, loginState, page.url());
|
|
107
|
+
const bodyFile = path.join(outDir, 'article.md');
|
|
108
|
+
const lines = ['# ' + article.title, ''];
|
|
109
|
+
if (article.date) lines.push('- 发布日期:' + article.date);
|
|
110
|
+
if (article.reading_time) lines.push('- ' + article.reading_time);
|
|
111
|
+
if (article.date || article.reading_time) lines.push('');
|
|
112
|
+
lines.push(article.body, '');
|
|
113
|
+
fs.writeFileSync(bodyFile, lines.join('\n'), 'utf8');
|
|
114
|
+
|
|
115
|
+
const record = {
|
|
116
|
+
id: articleId(articleUrl),
|
|
117
|
+
url: articleUrl,
|
|
118
|
+
final_url: page.url(),
|
|
119
|
+
type: 'article',
|
|
120
|
+
title: article.title,
|
|
121
|
+
date: article.date,
|
|
122
|
+
reading_time: article.reading_time,
|
|
123
|
+
body: article.body,
|
|
124
|
+
body_length: article.body.length,
|
|
125
|
+
body_file: bodyFile,
|
|
126
|
+
crawled_at: new Date().toISOString(),
|
|
127
|
+
dir: outDir,
|
|
128
|
+
};
|
|
129
|
+
const articleFile = path.join(outDir, 'article.json');
|
|
130
|
+
fs.writeFileSync(articleFile, JSON.stringify(record, null, 2) + '\n', 'utf8');
|
|
131
|
+
printJson({
|
|
132
|
+
ok: true,
|
|
133
|
+
login_required: false,
|
|
134
|
+
article: record,
|
|
135
|
+
article_file: articleFile,
|
|
136
|
+
});
|
|
137
|
+
} finally {
|
|
138
|
+
if (page && !keepPage) {
|
|
139
|
+
try { await page.close(); } catch { /* ignore */ }
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
main()
|
|
145
|
+
.then(() => process.exit(0))
|
|
146
|
+
.catch((err) => { fail(err); process.exit(0); });
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Scan one Douyin account homepage and return its /note/ image-text works
|
|
5
|
-
*
|
|
4
|
+
* Scan one Douyin account homepage and return its /note/ image-text works,
|
|
5
|
+
* /video/ video works and /article/ long-form articles.
|
|
6
6
|
*
|
|
7
7
|
* stdout is one JSON object. Progress goes to stderr. The script never closes
|
|
8
8
|
* Chrome; it closes only its own page, so login state stays in the profile.
|
|
@@ -20,13 +20,31 @@ function normalize(url) {
|
|
|
20
20
|
return String(url);
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
async function readProfile(page) {
|
|
24
|
+
return await page.evaluate(() => {
|
|
25
|
+
const title = String(document.title || '')
|
|
26
|
+
.replace(/\s*-\s*抖音\s*$/, '')
|
|
27
|
+
.replace(/的抖音.*$/, '')
|
|
28
|
+
.trim();
|
|
29
|
+
const body = document.body && document.body.innerText ? document.body.innerText : '';
|
|
30
|
+
const idMatch = body.match(/抖音号[::]\s*([A-Za-z0-9_.-]+)/);
|
|
31
|
+
const secMatch = location.pathname.match(/\/user\/([^/?#]+)/);
|
|
32
|
+
return {
|
|
33
|
+
name: title || '',
|
|
34
|
+
douyin_id: idMatch ? idMatch[1] : '',
|
|
35
|
+
sec_uid: secMatch ? secMatch[1] : '',
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
23
40
|
async function collectCards(page) {
|
|
24
41
|
const cards = new Map();
|
|
25
42
|
let stable = 0;
|
|
26
43
|
for (let round = 1; round <= 30; round += 1) {
|
|
27
44
|
const batch = await page.evaluate(() => {
|
|
28
45
|
const rows = [];
|
|
29
|
-
|
|
46
|
+
const root = document.querySelector('[data-e2e="user-post-list"]') || document;
|
|
47
|
+
for (const a of root.querySelectorAll('a[href*="/note/"], a[href*="/video/"], a[href*="/article/"]')) {
|
|
30
48
|
// 只收「作品列表」里的卡片:必须是 li > a,并且卡片里有封面图。
|
|
31
49
|
// 账号页底部还有 footer 推荐/友情链接,它们也是 <a href="/note/...">,
|
|
32
50
|
// 但没有 li/img —— 之前会把「这裸考成绩…」这种页脚推荐当成作品。
|
|
@@ -34,13 +52,14 @@ async function collectCards(page) {
|
|
|
34
52
|
if (!li) continue;
|
|
35
53
|
if (a.closest('footer, nav, [role="navigation"]')) continue;
|
|
36
54
|
const img = li.querySelector('img');
|
|
37
|
-
|
|
55
|
+
const isArticle = a.href.includes('/article/');
|
|
56
|
+
if (!img && !isArticle) continue;
|
|
38
57
|
rows.push({
|
|
39
58
|
href: a.href,
|
|
40
|
-
kind: a.href.includes('/note/') ? 'note' : 'video',
|
|
59
|
+
kind: isArticle ? 'article' : (a.href.includes('/note/') ? 'note' : 'video'),
|
|
41
60
|
text: li.innerText ? li.innerText.trim().slice(0, 300) : '',
|
|
42
|
-
alt: img.alt ? img.alt : '',
|
|
43
|
-
cover: img.src ? img.src : '',
|
|
61
|
+
alt: img && img.alt ? img.alt : '',
|
|
62
|
+
cover: img && img.src ? img.src : '',
|
|
44
63
|
});
|
|
45
64
|
}
|
|
46
65
|
return rows;
|
|
@@ -49,7 +68,8 @@ async function collectCards(page) {
|
|
|
49
68
|
for (const card of batch) {
|
|
50
69
|
const href = normalize(card.href);
|
|
51
70
|
// 第二层保险:页脚/相关推荐的裸链没有封面或 alt,直接丢弃。
|
|
52
|
-
|
|
71
|
+
// 文章卡片可能没有封面图,但必须有文本。
|
|
72
|
+
if (!card.alt && !card.cover && !(card.kind === 'article' && card.text)) continue;
|
|
53
73
|
if (/[?&]source=Baiduspider/i.test(href)) continue;
|
|
54
74
|
if (!cards.has(href)) cards.set(href, { ...card, href });
|
|
55
75
|
}
|
|
@@ -158,13 +178,17 @@ async function main() {
|
|
|
158
178
|
|
|
159
179
|
const notes = cards.filter((card) => card.kind === 'note');
|
|
160
180
|
const videos = cards.filter((card) => card.kind === 'video');
|
|
181
|
+
const articles = cards.filter((card) => card.kind === 'article');
|
|
182
|
+
const profile = await readProfile(page);
|
|
161
183
|
printJson({
|
|
162
184
|
ok: true,
|
|
163
185
|
login_required: false,
|
|
164
186
|
account,
|
|
165
187
|
title: await page.title(),
|
|
188
|
+
profile,
|
|
166
189
|
notes,
|
|
167
190
|
videos,
|
|
191
|
+
articles,
|
|
168
192
|
cards_file: cardsFile,
|
|
169
193
|
});
|
|
170
194
|
} finally {
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* live under <workspace>/.fancy/douyin. The deployed crawler scripts live under
|
|
6
6
|
* <workspace>/.fancy-tools/fancy-douyin.
|
|
7
7
|
*
|
|
8
|
-
* The crawl history is keyed by the
|
|
9
|
-
* /
|
|
8
|
+
* The crawl history is keyed by the work id parsed from /note/<id>, /video/<id>
|
|
9
|
+
* or /article/<id>. A work is written only after its analysis has been imported into
|
|
10
10
|
* the material library, or after it was confirmed irrelevant/long and skipped,
|
|
11
11
|
* so failed works are retried on the next run.
|
|
12
12
|
*/
|
|
@@ -98,6 +98,10 @@ export function readAccounts(root) {
|
|
|
98
98
|
.map((item) => ({
|
|
99
99
|
url: item.url,
|
|
100
100
|
added_at: item.added_at || '',
|
|
101
|
+
name: typeof item.name === 'string' ? item.name : '',
|
|
102
|
+
douyin_id: typeof item.douyin_id === 'string' ? item.douyin_id : '',
|
|
103
|
+
sec_uid: typeof item.sec_uid === 'string' ? item.sec_uid : '',
|
|
104
|
+
last_scan_at: typeof item.last_scan_at === 'string' ? item.last_scan_at : '',
|
|
101
105
|
}));
|
|
102
106
|
}
|
|
103
107
|
|
|
@@ -106,6 +110,7 @@ export function writeAccounts(root, accounts) {
|
|
|
106
110
|
atomicWriteJson(paths.accounts, {
|
|
107
111
|
version: 1,
|
|
108
112
|
accounts: accounts.map((item) => ({
|
|
113
|
+
...item,
|
|
109
114
|
url: item.url,
|
|
110
115
|
added_at: item.added_at || new Date().toISOString(),
|
|
111
116
|
})),
|
|
@@ -128,6 +133,31 @@ export function addAccount(root, rawUrl) {
|
|
|
128
133
|
return { added: true, url, accounts };
|
|
129
134
|
}
|
|
130
135
|
|
|
136
|
+
/**
|
|
137
|
+
* Record the profile fields discovered while scanning an account homepage.
|
|
138
|
+
* Existing accounts keep all other state; unknown accounts are ignored.
|
|
139
|
+
*
|
|
140
|
+
* @param {string} root workspace directory
|
|
141
|
+
* @param {string} url normalized account URL
|
|
142
|
+
* @param {{name?: string, douyin_id?: string, sec_uid?: string, last_scan_at?: string}} profile
|
|
143
|
+
* @returns {object|null} updated account
|
|
144
|
+
*/
|
|
145
|
+
export function updateAccountProfile(root, url, profile) {
|
|
146
|
+
const accounts = readAccounts(root);
|
|
147
|
+
const index = accounts.findIndex((item) => item.url === url);
|
|
148
|
+
if (index < 0) return null;
|
|
149
|
+
const next = {
|
|
150
|
+
...accounts[index],
|
|
151
|
+
...(profile && typeof profile.name === 'string' && profile.name.trim() ? { name: profile.name.trim() } : {}),
|
|
152
|
+
...(profile && typeof profile.douyin_id === 'string' && profile.douyin_id.trim() ? { douyin_id: profile.douyin_id.trim() } : {}),
|
|
153
|
+
...(profile && typeof profile.sec_uid === 'string' && profile.sec_uid.trim() ? { sec_uid: profile.sec_uid.trim() } : {}),
|
|
154
|
+
last_scan_at: new Date().toISOString(),
|
|
155
|
+
};
|
|
156
|
+
accounts[index] = next;
|
|
157
|
+
writeAccounts(root, accounts);
|
|
158
|
+
return next;
|
|
159
|
+
}
|
|
160
|
+
|
|
131
161
|
/**
|
|
132
162
|
* Remove an account by 1-based list index or by homepage URL.
|
|
133
163
|
*
|
|
@@ -188,11 +218,17 @@ export function accountListText(root) {
|
|
|
188
218
|
if (!item || typeof item !== 'object' || !item.account_url) continue;
|
|
189
219
|
counts.set(item.account_url, (counts.get(item.account_url) || 0) + 1);
|
|
190
220
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
221
|
+
const lines = ['已记录的抖音账号:'];
|
|
222
|
+
accounts.forEach((item, index) => {
|
|
223
|
+
const count = counts.get(item.url) || 0;
|
|
224
|
+
if (item.name) {
|
|
225
|
+
const idText = item.douyin_id ? '抖音号:' + item.douyin_id : '抖音号未获取';
|
|
226
|
+
lines.push(`${index + 1}. ${item.name}(${idText})`);
|
|
227
|
+
lines.push(' ' + item.url);
|
|
228
|
+
} else {
|
|
229
|
+
lines.push(`${index + 1}. ${item.url}(未获取账号名/抖音号)`);
|
|
230
|
+
}
|
|
231
|
+
lines.push(` 已采集 ${count} 篇`);
|
|
232
|
+
});
|
|
233
|
+
return lines.join('\n');
|
|
198
234
|
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers for the optional DSH subagent analysis pool.
|
|
3
|
+
*
|
|
4
|
+
* The Douyin crawler still runs in the parent process. Only the LLM analysis
|
|
5
|
+
* of already-prepared works is delegated to child agents. That keeps Chrome,
|
|
6
|
+
* `crawled.json`, and material imports single-writer while allowing several
|
|
7
|
+
* image/video/article analyses to run at once.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export const DEFAULT_SUBAGENT_WORKERS = 3;
|
|
11
|
+
export const MAX_SUBAGENT_WORKERS = 6;
|
|
12
|
+
|
|
13
|
+
export function subagentWorkerCount(env = process.env) {
|
|
14
|
+
const raw = env && env.FANCY_DOUYIN_SUBAGENTS;
|
|
15
|
+
if (raw === undefined || raw === null || String(raw).trim() === '') return DEFAULT_SUBAGENT_WORKERS;
|
|
16
|
+
const value = Number(String(raw).trim());
|
|
17
|
+
if (!Number.isFinite(value) || value <= 0) return 0;
|
|
18
|
+
return Math.max(1, Math.min(MAX_SUBAGENT_WORKERS, Math.floor(value)));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Resolve the DSH subagent runtime from a plugin context. The service is
|
|
23
|
+
* optional: older hosts or a profile without subagents simply fall back to the
|
|
24
|
+
* existing single-agent path.
|
|
25
|
+
*/
|
|
26
|
+
export function getSubagentRuntime(ctx, provider = 'spawn') {
|
|
27
|
+
if (!ctx) return null;
|
|
28
|
+
let service = null;
|
|
29
|
+
try {
|
|
30
|
+
service = typeof ctx.get === 'function' ? ctx.get('subagents') : ctx.subagents;
|
|
31
|
+
} catch {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
if (!service || typeof service.startActivation !== 'function') return null;
|
|
35
|
+
if (typeof service.getProvider === 'function' && !service.getProvider(provider)) return null;
|
|
36
|
+
return service;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Run a fixed list of jobs through at most `concurrency` workers.
|
|
41
|
+
* `worker` receives each item and is awaited before that worker takes another.
|
|
42
|
+
*/
|
|
43
|
+
export async function runJobPool(items, concurrency, worker) {
|
|
44
|
+
const list = Array.isArray(items) ? items : [];
|
|
45
|
+
const limit = Math.max(1, Math.min(Number(concurrency) || 1, list.length || 1));
|
|
46
|
+
let cursor = 0;
|
|
47
|
+
const runners = Array.from({ length: limit }, async () => {
|
|
48
|
+
while (true) {
|
|
49
|
+
const index = cursor;
|
|
50
|
+
cursor += 1;
|
|
51
|
+
if (index >= list.length) return;
|
|
52
|
+
await worker(list[index], index);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
await Promise.all(runners);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A small async mutex. Finalization is deliberately serialized because it
|
|
60
|
+
* writes crawled.json and opens the material database; the LLM work itself is
|
|
61
|
+
* already parallel.
|
|
62
|
+
*/
|
|
63
|
+
export function createSerialLock() {
|
|
64
|
+
let chain = Promise.resolve();
|
|
65
|
+
return function withLock(fn) {
|
|
66
|
+
const next = chain.then(fn, fn);
|
|
67
|
+
chain = next.catch(() => {});
|
|
68
|
+
return next;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
@@ -9,23 +9,28 @@ import { fileURLToPath } from 'url';
|
|
|
9
9
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
10
10
|
const PROMPT_FILE = path.join(__dirname, 'prompts', '01-analysis-prompt.md');
|
|
11
11
|
const VIDEO_PROMPT_FILE = path.join(__dirname, 'prompts', '02-video-analysis-prompt.md');
|
|
12
|
+
const ARTICLE_PROMPT_FILE = path.join(__dirname, 'prompts', '03-article-prompt.md');
|
|
12
13
|
const CAPABILITY_PROMPT_FILE = path.join(__dirname, 'prompts', '00-capability-prompt.md');
|
|
13
14
|
|
|
14
15
|
export function helpText() {
|
|
15
16
|
return `🎬 抖音素材采集 用法
|
|
16
17
|
|
|
17
18
|
/fancy-douyin add <主页地址> 记录一个抖音账号主页
|
|
18
|
-
/fancy-douyin list
|
|
19
|
+
/fancy-douyin list 查看账号名、抖音号和已采集数量
|
|
19
20
|
/fancy-douyin remove <序号|地址> 删除已记录的账号
|
|
20
|
-
/fancy-douyin
|
|
21
|
+
/fancy-douyin 依次采集所有账号下未处理的图文、视频和文章作品
|
|
21
22
|
/fancy-douyin help 显示本帮助
|
|
22
23
|
|
|
23
24
|
说明:
|
|
24
25
|
- 先让 LLM 自检图片识别和视频识别能力:
|
|
25
26
|
- 有图片识别能力:分析 /note/ 图文作品。
|
|
26
27
|
- 有视频识别能力:分析 /video/ 视频作品。
|
|
27
|
-
-
|
|
28
|
+
- /article/ 长文不需要视觉能力,始终直接读取正文分析。
|
|
29
|
+
- 图片和视频能力都没有时,跳过图文/视频,只处理文章。
|
|
28
30
|
- 图文作品会下载轮播原图并转成 JPG;如果轮播里混有视频页,会下载该页短视频,用场景变化检测 + 固定间隔兜底抽取关键帧拼图。
|
|
31
|
+
- 文章作品直接提取标题、发布日期、阅读时长和正文,按长文设定拆成原子素材。
|
|
32
|
+
- 采集完成后可由 DSH 子 Agent 并行分析作品:父进程负责采集、校验、入库和写 crawled.json,子 Agent 只写各作品的 materials.jsonl。
|
|
33
|
+
- 子 Agent 并发数由 FANCY_DOUYIN_SUBAGENTS 控制(默认 3,0 关闭);DSH 没有 subagent 服务或 spawn provider 时自动回退单 Agent 串行。
|
|
29
34
|
- 视频作品先判断时长:超过 1 分钟直接跳过;不超过 1 分钟的先看前 5 秒,
|
|
30
35
|
与网文创作无关就跳过,有关才继续分析关键帧。
|
|
31
36
|
- 如果内容与网文创作无关,LLM 会写入 skip 标记,该作品记为已处理并跳过。
|
|
@@ -167,6 +172,31 @@ export function videoRelevancePrompt({
|
|
|
167
172
|
return lines.join('\n') + '\n';
|
|
168
173
|
}
|
|
169
174
|
|
|
175
|
+
export function articlePrompt({
|
|
176
|
+
accountUrl,
|
|
177
|
+
article,
|
|
178
|
+
targetFile,
|
|
179
|
+
workspace,
|
|
180
|
+
queryScript,
|
|
181
|
+
validateScript,
|
|
182
|
+
}) {
|
|
183
|
+
const body = String(article && article.body ? article.body : '').slice(0, 60000);
|
|
184
|
+
return renderTemplate(ARTICLE_PROMPT_FILE, {
|
|
185
|
+
ACCOUNT_URL: read(accountUrl),
|
|
186
|
+
ARTICLE_URL: read(article && article.url),
|
|
187
|
+
ARTICLE_TITLE: read(article && article.title, '(无标题)'),
|
|
188
|
+
ARTICLE_DATE: read(article && article.date, '未知'),
|
|
189
|
+
ARTICLE_READING_TIME: read(article && article.reading_time, '未知'),
|
|
190
|
+
ARTICLE_LENGTH: String((article && (article.body_length || article.body)) ? (article.body_length || article.body.length) : 0),
|
|
191
|
+
ARTICLE_FILE: read(article && article.body_file, ''),
|
|
192
|
+
ARTICLE_BODY: body || '(文章正文为空)',
|
|
193
|
+
TARGET_FILE: targetFile,
|
|
194
|
+
QUERY_SCRIPT: queryScript,
|
|
195
|
+
VALIDATE_SCRIPT: validateScript,
|
|
196
|
+
WORKSPACE: workspace,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
170
200
|
export function videoAnalysisPrompt({
|
|
171
201
|
accountUrl,
|
|
172
202
|
video,
|
|
@@ -194,9 +224,11 @@ export function summaryText({
|
|
|
194
224
|
scannedAccounts,
|
|
195
225
|
foundNotes,
|
|
196
226
|
foundVideos = 0,
|
|
227
|
+
foundArticles = 0,
|
|
197
228
|
skippedNotesByCapability = 0,
|
|
198
229
|
skippedVideosByCapability = 0,
|
|
199
230
|
skippedNotes,
|
|
231
|
+
skippedArticles = 0,
|
|
200
232
|
processedNotes,
|
|
201
233
|
skippedIrrelevant = 0,
|
|
202
234
|
skippedLongVideos = 0,
|
|
@@ -207,14 +239,18 @@ export function summaryText({
|
|
|
207
239
|
stateFile,
|
|
208
240
|
aborted,
|
|
209
241
|
mode,
|
|
242
|
+
analysisMode = 'single',
|
|
243
|
+
subagentWorkers = 0,
|
|
210
244
|
}) {
|
|
211
245
|
const lines = [
|
|
212
246
|
aborted ? '⚠️ 抖音采集已中断' : '✅ 抖音采集完成',
|
|
213
247
|
'账号:' + accounts + ' 个(本次扫描 ' + scannedAccounts + ' 个)',
|
|
214
248
|
'浏览器:' + (mode === 'headless' ? '无头' : '有头'),
|
|
249
|
+
'分析模式:' + (analysisMode === 'subagent' ? '子 Agent 并行(' + subagentWorkers + ' 个)' : '单 Agent 串行'),
|
|
215
250
|
'发现图文:' + foundNotes + ' 篇',
|
|
216
251
|
'发现视频:' + foundVideos + ' 个',
|
|
217
|
-
'
|
|
252
|
+
'发现文章:' + foundArticles + ' 篇',
|
|
253
|
+
'已处理过并跳过:' + (skippedNotes + skippedArticles) + ' 篇',
|
|
218
254
|
'能力不支持跳过:图文 ' + skippedNotesByCapability + ' 篇 / 视频 ' + skippedVideosByCapability + ' 个',
|
|
219
255
|
'本次处理:' + processedNotes + ' 篇',
|
|
220
256
|
'无关跳过:' + skippedIrrelevant + ' 篇',
|