tt-help-cli-ycl 1.3.6 → 1.3.8

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.
Files changed (46) hide show
  1. package/README.md +17 -17
  2. package/cli.js +9 -9
  3. package/package.json +45 -45
  4. package/src/cli/auto.js +131 -121
  5. package/src/cli/explore.js +147 -138
  6. package/src/cli/progress.js +111 -111
  7. package/src/cli/scrape.js +47 -47
  8. package/src/cli/utils.js +18 -18
  9. package/src/cli/videos.js +41 -41
  10. package/src/cli/watch.js +31 -31
  11. package/src/lib/args.js +391 -391
  12. package/src/lib/browser/anti-detect.js +23 -23
  13. package/src/lib/browser/cdp.js +142 -142
  14. package/src/lib/browser/launch.js +43 -43
  15. package/src/lib/browser/page.js +87 -87
  16. package/src/lib/constants.js +109 -95
  17. package/src/lib/delay.js +54 -54
  18. package/src/lib/explore-fetch.js +118 -118
  19. package/src/lib/fetcher.js +45 -45
  20. package/src/lib/filter.js +66 -66
  21. package/src/lib/io.js +54 -54
  22. package/src/lib/mac-or-uuid.js +82 -0
  23. package/src/lib/output.js +80 -80
  24. package/src/lib/parser.js +47 -47
  25. package/src/lib/retry.js +44 -44
  26. package/src/lib/scrape.js +40 -40
  27. package/src/lib/url.js +52 -52
  28. package/src/main.mjs +221 -221
  29. package/src/scraper/auto-core.mjs +185 -185
  30. package/src/scraper/core.mjs +190 -190
  31. package/src/scraper/explore-core.mjs +162 -162
  32. package/src/scraper/modules/captcha-handler.mjs +114 -114
  33. package/src/scraper/modules/comment-extractor.mjs +69 -69
  34. package/src/scraper/modules/follow-extractor.mjs +121 -121
  35. package/src/scraper/modules/guess-extractor.mjs +51 -51
  36. package/src/scraper/modules/page-error-detector.mjs +70 -70
  37. package/src/scraper/modules/page-helpers.mjs +48 -48
  38. package/src/scraper/modules/scroll-collector.mjs +189 -189
  39. package/src/test-auto-follow.cjs +109 -0
  40. package/src/test-extractors.cjs +75 -0
  41. package/src/test-follow.cjs +41 -0
  42. package/src/videos/core.mjs +126 -126
  43. package/src/watch/data-store.mjs +258 -261
  44. package/src/watch/public/index.html +580 -464
  45. package/src/watch/server.mjs +308 -281
  46. package/src/results/user-videos-bar.lar.lar.moeta.json +0 -37
@@ -1,138 +1,147 @@
1
- import { getOrCreatePage } from '../lib/browser/page.js';
2
- import { delay, getDelayConfig, setDelayConfig } from '../scraper/modules/page-helpers.mjs';
3
-
4
- const MAX_RETRY_WAIT = 5 * 60 * 1000;
5
-
6
- async function withRetry(label, fn) {
7
- let backoff = 1000;
8
- while (true) {
9
- try {
10
- return await fn();
11
- } catch (err) {
12
- console.error(`[连接] ${label} 失败: ${err.message},${backoff / 1000}秒后重试...`);
13
- await new Promise(r => setTimeout(r, backoff));
14
- if (backoff < MAX_RETRY_WAIT) backoff *= 2;
15
- }
16
- }
17
- }
18
-
19
- async function apiPost(url, body) {
20
- return withRetry(`POST ${url}`, async () => {
21
- const res = await fetch(url, {
22
- method: 'POST',
23
- headers: { 'Content-Type': 'application/json' },
24
- body: JSON.stringify(body),
25
- });
26
- return res.json();
27
- });
28
- }
29
-
30
- async function apiGet(url) {
31
- return withRetry(`GET ${url}`, async () => {
32
- const res = await fetch(url);
33
- return res.json();
34
- });
35
- }
36
-
37
- export async function handleExplore(options) {
38
- const {
39
- exploreUsernames, explorePreset, exploreMaxComments, exploreMaxGuess,
40
- exploreEnableFollow, exploreMaxFollowing, exploreMaxFollowers,
41
- exploreLocation, exploreMaxUsers, serverUrl,
42
- } = options;
43
-
44
- setDelayConfig(explorePreset);
45
-
46
- await apiGet(`${serverUrl}/api/stats`);
47
-
48
- if (exploreUsernames && exploreUsernames.length > 0) {
49
- const { added, skipped } = await apiPost(`${serverUrl}/api/users`, { usernames: exploreUsernames });
50
- console.error(`种子用户: ${added} 个新增, ${skipped} 个已存在`);
51
- }
52
-
53
- console.error(`\n国家筛选: ${exploreLocation}`);
54
- console.error(`评论: ${exploreMaxComments}, 猜你喜欢: ${exploreMaxGuess}`);
55
- console.error(`关注/粉丝: ${exploreEnableFollow ? '启用' : '禁用'}`);
56
- console.error(`服务器: ${serverUrl}(断开会自动重连)`);
57
- if (exploreMaxUsers > 0) console.error(`上限: ${exploreMaxUsers} 个用户`);
58
-
59
- const { ensureBrowserReady, processExplore } = await import('../scraper/explore-core.mjs');
60
- const browser = await ensureBrowserReady();
61
-
62
- const page = await getOrCreatePage(browser);
63
-
64
- let processedCount = 0;
65
- let errorCount = 0;
66
-
67
- while (true) {
68
- const job = await apiGet(`${serverUrl}/api/job`);
69
- if (!job.hasJob) break;
70
-
71
- const username = job.user.uniqueId;
72
- processedCount++;
73
- let proxyRetry = 0;
74
-
75
- while (true) {
76
- console.error(`\n[${processedCount}] 探索 @${username}...${proxyRetry > 0 ? ` (代理重试 ${proxyRetry})` : ''}`);
77
-
78
- const { switchMax } = getDelayConfig();
79
- await delay(switchMax, switchMax * 3);
80
-
81
- const result = await processExplore(page, username, {
82
- maxComments: exploreMaxComments,
83
- maxGuess: exploreMaxGuess,
84
- enableFollow: exploreEnableFollow,
85
- maxFollowing: exploreMaxFollowing,
86
- maxFollowers: exploreMaxFollowers,
87
- location: exploreLocation,
88
- browser,
89
- }, console.error);
90
-
91
- if (result.restricted) {
92
- await apiPost(`${serverUrl}/api/job/${username}`, { restricted: true, userInfo: result.userInfo || {} });
93
- break;
94
- }
95
-
96
- if (result.error && result.error.includes('代理错误')) {
97
- proxyRetry++;
98
- console.error(` [代理错误] ${result.error},等待 10s 后重试...`);
99
- await new Promise(r => setTimeout(r, 10000));
100
- continue;
101
- }
102
-
103
- if (result.error) {
104
- errorCount++;
105
- await apiPost(`${serverUrl}/api/job/${username}`, { error: result.error });
106
- break;
107
- }
108
-
109
- const payload = {
110
- userInfo: result.userInfo || {},
111
- discoveredVideoAuthors: result.discoveredVideoAuthors || [],
112
- discoveredCommentAuthors: result.discoveredCommentAuthors || [],
113
- discoveredGuessAuthors: result.discoveredGuessAuthors || [],
114
- discoveredFollowing: result.discoveredFollowing || [],
115
- discoveredFollowers: result.discoveredFollowers || [],
116
- processed: result.processed,
117
- hasFollowData: result.hasFollowData,
118
- keepFollow: result.keepFollow,
119
- locationCreated: result.locationCreated,
120
- noVideo: result.noVideo,
121
- };
122
- await apiPost(`${serverUrl}/api/job/${username}`, payload);
123
- console.error(' 已提交');
124
- break;
125
- }
126
-
127
- if (exploreMaxUsers > 0 && processedCount >= exploreMaxUsers) {
128
- console.error(`\n已达上限 ${exploreMaxUsers} 个用户,停止处理`);
129
- break;
130
- }
131
- }
132
-
133
- const stats = await apiGet(`${serverUrl}/api/stats`);
134
- console.error(`\n完成: ${processedCount} 个用户处理, ${errorCount} 个出错`);
135
- console.error(` 总用户: ${stats.totalUsers}, 已完成: ${stats.processedUsers}, 待处理: ${stats.pendingUsers}, 错误: ${stats.errorUsers}`);
136
-
137
- await browser.close().catch(() => {});
138
- }
1
+ import { getOrCreatePage } from '../lib/browser/page.js';
2
+ import { delay, getDelayConfig, setDelayConfig } from '../scraper/modules/page-helpers.mjs';
3
+ import { userId as configuredUserId, saveUserId } from '../lib/constants.js';
4
+ import { getMacOrUuid } from '../lib/mac-or-uuid.js';
5
+
6
+ const MAX_RETRY_WAIT = 5 * 60 * 1000;
7
+
8
+ async function withRetry(label, fn) {
9
+ let backoff = 1000;
10
+ while (true) {
11
+ try {
12
+ return await fn();
13
+ } catch (err) {
14
+ console.error(`[连接] ${label} 失败: ${err.message},${backoff / 1000}秒后重试...`);
15
+ await new Promise(r => setTimeout(r, backoff));
16
+ if (backoff < MAX_RETRY_WAIT) backoff *= 2;
17
+ }
18
+ }
19
+ }
20
+
21
+ async function apiPost(url, body) {
22
+ return withRetry(`POST ${url}`, async () => {
23
+ const res = await fetch(url, {
24
+ method: 'POST',
25
+ headers: { 'Content-Type': 'application/json' },
26
+ body: JSON.stringify(body),
27
+ });
28
+ return res.json();
29
+ });
30
+ }
31
+
32
+ async function apiGet(url) {
33
+ return withRetry(`GET ${url}`, async () => {
34
+ const res = await fetch(url);
35
+ return res.json();
36
+ });
37
+ }
38
+
39
+ export async function handleExplore(options) {
40
+ const {
41
+ exploreUsernames, explorePreset, exploreMaxComments, exploreMaxGuess,
42
+ exploreEnableFollow, exploreMaxFollowing, exploreMaxFollowers,
43
+ exploreLocation, exploreMaxUsers, serverUrl,
44
+ } = options;
45
+
46
+ let userId = configuredUserId;
47
+ if (!userId) {
48
+ userId = await getMacOrUuid();
49
+ saveUserId(userId);
50
+ console.error(`[初始化] 未检测到本地用户编号,已生成并使用: ${userId}`);
51
+ }
52
+
53
+ setDelayConfig(explorePreset);
54
+
55
+ await apiGet(`${serverUrl}/api/stats`);
56
+
57
+ if (exploreUsernames && exploreUsernames.length > 0) {
58
+ const { added, skipped } = await apiPost(`${serverUrl}/api/users`, { usernames: exploreUsernames });
59
+ console.error(`种子用户: ${added} 个新增, ${skipped} 个已存在`);
60
+ }
61
+
62
+ console.error(`\n国家筛选: ${exploreLocation}`);
63
+ console.error(`评论: ${exploreMaxComments}, 猜你喜欢: ${exploreMaxGuess}`);
64
+ console.error(`关注/粉丝: ${exploreEnableFollow ? '启用' : '禁用'}`);
65
+ console.error(`服务器: ${serverUrl}(断开会自动重连)`);
66
+ if (exploreMaxUsers > 0) console.error(`上限: ${exploreMaxUsers} 个用户`);
67
+
68
+ const { ensureBrowserReady, processExplore } = await import('../scraper/explore-core.mjs');
69
+ const browser = await ensureBrowserReady();
70
+
71
+ const page = await getOrCreatePage(browser);
72
+
73
+ let processedCount = 0;
74
+ let errorCount = 0;
75
+
76
+ while (true) {
77
+ const job = await apiGet(`${serverUrl}/api/job?userId=${encodeURIComponent(userId)}`);
78
+ if (!job.hasJob) break;
79
+
80
+ const username = job.user.uniqueId;
81
+ processedCount++;
82
+ let proxyRetry = 0;
83
+
84
+ while (true) {
85
+ console.error(`\n[${processedCount}] 探索 @${username}...${proxyRetry > 0 ? ` (代理重试 ${proxyRetry})` : ''}`);
86
+
87
+ const { switchMax } = getDelayConfig();
88
+ await delay(switchMax, switchMax * 3);
89
+
90
+ const result = await processExplore(page, username, {
91
+ maxComments: exploreMaxComments,
92
+ maxGuess: exploreMaxGuess,
93
+ enableFollow: exploreEnableFollow,
94
+ maxFollowing: exploreMaxFollowing,
95
+ maxFollowers: exploreMaxFollowers,
96
+ location: exploreLocation,
97
+ browser,
98
+ }, console.error);
99
+
100
+ if (result.restricted) {
101
+ await apiPost(`${serverUrl}/api/job/${username}`, { restricted: true, userInfo: result.userInfo || {} });
102
+ break;
103
+ }
104
+
105
+ if (result.error && result.error.includes('代理错误')) {
106
+ proxyRetry++;
107
+ console.error(` [代理错误] ${result.error},等待 10s 后重试...`);
108
+ await new Promise(r => setTimeout(r, 10000));
109
+ continue;
110
+ }
111
+
112
+ if (result.error) {
113
+ errorCount++;
114
+ await apiPost(`${serverUrl}/api/job/${username}`, { error: result.error });
115
+ break;
116
+ }
117
+
118
+ const payload = {
119
+ userInfo: result.userInfo || {},
120
+ discoveredVideoAuthors: result.discoveredVideoAuthors || [],
121
+ discoveredCommentAuthors: result.discoveredCommentAuthors || [],
122
+ discoveredGuessAuthors: result.discoveredGuessAuthors || [],
123
+ discoveredFollowing: result.discoveredFollowing || [],
124
+ discoveredFollowers: result.discoveredFollowers || [],
125
+ processed: result.processed,
126
+ hasFollowData: result.hasFollowData,
127
+ keepFollow: result.keepFollow,
128
+ locationCreated: result.locationCreated,
129
+ noVideo: result.noVideo,
130
+ };
131
+ await apiPost(`${serverUrl}/api/job/${username}`, payload);
132
+ console.error(' 已提交');
133
+ break;
134
+ }
135
+
136
+ if (exploreMaxUsers > 0 && processedCount >= exploreMaxUsers) {
137
+ console.error(`\n已达上限 ${exploreMaxUsers} 个用户,停止处理`);
138
+ break;
139
+ }
140
+ }
141
+
142
+ const stats = await apiGet(`${serverUrl}/api/stats`);
143
+ console.error(`\n完成: ${processedCount} 个用户处理, ${errorCount} 个出错`);
144
+ console.error(` 总用户: ${stats.totalUsers}, 已完成: ${stats.processedUsers}, 待处理: ${stats.pendingUsers}, 错误: ${stats.errorUsers}`);
145
+
146
+ await browser.close().catch(() => {});
147
+ }
@@ -1,111 +1,111 @@
1
- import { writeFileSync } from 'fs';
2
- import { formatOutput } from '../lib/output.js';
3
- import { deduplicate } from '../lib/output.js';
4
- import { applyFilter, formatFilterDescription } from '../lib/filter.js';
5
- import { calculateConcurrency, createMultiProgressBars, renderMultiProgressBars, clearProgressBars } from '../lib/io.js';
6
- import { randomDelay } from '../lib/delay.js';
7
-
8
- export async function processUrlsWithProgress({
9
- urls,
10
- proxyUrl,
11
- outputFile,
12
- outputFormat,
13
- filter,
14
- processFn,
15
- label = '数据',
16
- }) {
17
- const allResults = [];
18
- const errors = [];
19
-
20
- if (urls.length === 0) {
21
- console.error('\n未获取到数据');
22
- if (outputFile) writeFileSync(outputFile, '[]', 'utf-8');
23
- return;
24
- }
25
-
26
- const concurrency = calculateConcurrency(urls.length);
27
- const bars = createMultiProgressBars(concurrency);
28
-
29
- const slots = Array.from({ length: concurrency }, () => []);
30
- urls.forEach((url, i) => slots[i % concurrency].push(url));
31
-
32
- bars.forEach((bar, i) => {
33
- bar.total = slots[i].length;
34
- bar.status = slots[i].length > 0 ? 'running' : 'done';
35
- });
36
-
37
- renderMultiProgressBars(bars);
38
-
39
- const workers = slots.map(async (slotUrls, slotIndex) => {
40
- for (const url of slotUrls) {
41
- bars[slotIndex].url = url;
42
- renderMultiProgressBars(bars);
43
-
44
- await randomDelay();
45
-
46
- try {
47
- const results = await processFn(url, proxyUrl);
48
- allResults.push(...results);
49
- bars[slotIndex].current++;
50
- bars[slotIndex].status = 'running';
51
- } catch (err) {
52
- errors.push({ url, message: err.message });
53
- bars[slotIndex].current++;
54
- bars[slotIndex].status = 'error';
55
- }
56
-
57
- renderMultiProgressBars(bars);
58
- }
59
- bars[slotIndex].status = bars[slotIndex].current === bars[slotIndex].total ? 'done' : 'error';
60
- renderMultiProgressBars(bars);
61
- });
62
-
63
- await Promise.all(workers);
64
- clearProgressBars();
65
-
66
- const uniqueResults = deduplicate(allResults);
67
- const filteredResults = applyFilter(uniqueResults, filter);
68
-
69
- if (errors.length > 0) {
70
- const firstMsg = errors[0].message;
71
- const isProxyError = ['不可用', '连接被拒绝', '连接中断', '超时', '无法解析']
72
- .some(kw => firstMsg.includes(kw));
73
-
74
- if (filteredResults.length === 0) {
75
- if (isProxyError) {
76
- console.error(` 所有请求失败,请检查代理: ${proxyUrl}\n`);
77
- } else {
78
- const show = errors.slice(0, 5);
79
- for (const e of show) console.error(` ✗ ${e.url}: ${e.message}\n`);
80
- if (errors.length > 5) console.error(` ... 还有 ${errors.length - 5} 个失败\n`);
81
- }
82
- console.error('未获取到数据');
83
- if (outputFile) writeFileSync(outputFile, '[]', 'utf-8');
84
- return;
85
- } else {
86
- if (isProxyError) {
87
- console.error(` ${errors.length} 个请求失败,请检查代理: ${proxyUrl}\n`);
88
- } else {
89
- console.error(` ${errors.length} 个失败:`);
90
- const show = errors.slice(0, 5);
91
- for (const e of show) console.error(` ✗ ${e.url}: ${e.message}`);
92
- if (errors.length > 5) console.error(` ... 还有 ${errors.length - 5} 个`);
93
- }
94
- }
95
- }
96
-
97
- const output = formatOutput(filteredResults, outputFormat);
98
-
99
- if (outputFile) {
100
- writeFileSync(outputFile, output, 'utf-8');
101
- console.log(`\n结果已写入: ${outputFile}`);
102
- } else {
103
- process.stdout.write(output + '\n');
104
- }
105
-
106
- if (filter) {
107
- console.log(`\n共 ${uniqueResults.length} 个${label},过滤后 ${filteredResults.length} 个(过滤条件: ${formatFilterDescription(filter)})`);
108
- } else {
109
- console.log(`\n共 ${filteredResults.length} 个${label}`);
110
- }
111
- }
1
+ import { writeFileSync } from 'fs';
2
+ import { formatOutput } from '../lib/output.js';
3
+ import { deduplicate } from '../lib/output.js';
4
+ import { applyFilter, formatFilterDescription } from '../lib/filter.js';
5
+ import { calculateConcurrency, createMultiProgressBars, renderMultiProgressBars, clearProgressBars } from '../lib/io.js';
6
+ import { randomDelay } from '../lib/delay.js';
7
+
8
+ export async function processUrlsWithProgress({
9
+ urls,
10
+ proxyUrl,
11
+ outputFile,
12
+ outputFormat,
13
+ filter,
14
+ processFn,
15
+ label = '数据',
16
+ }) {
17
+ const allResults = [];
18
+ const errors = [];
19
+
20
+ if (urls.length === 0) {
21
+ console.error('\n未获取到数据');
22
+ if (outputFile) writeFileSync(outputFile, '[]', 'utf-8');
23
+ return;
24
+ }
25
+
26
+ const concurrency = calculateConcurrency(urls.length);
27
+ const bars = createMultiProgressBars(concurrency);
28
+
29
+ const slots = Array.from({ length: concurrency }, () => []);
30
+ urls.forEach((url, i) => slots[i % concurrency].push(url));
31
+
32
+ bars.forEach((bar, i) => {
33
+ bar.total = slots[i].length;
34
+ bar.status = slots[i].length > 0 ? 'running' : 'done';
35
+ });
36
+
37
+ renderMultiProgressBars(bars);
38
+
39
+ const workers = slots.map(async (slotUrls, slotIndex) => {
40
+ for (const url of slotUrls) {
41
+ bars[slotIndex].url = url;
42
+ renderMultiProgressBars(bars);
43
+
44
+ await randomDelay();
45
+
46
+ try {
47
+ const results = await processFn(url, proxyUrl);
48
+ allResults.push(...results);
49
+ bars[slotIndex].current++;
50
+ bars[slotIndex].status = 'running';
51
+ } catch (err) {
52
+ errors.push({ url, message: err.message });
53
+ bars[slotIndex].current++;
54
+ bars[slotIndex].status = 'error';
55
+ }
56
+
57
+ renderMultiProgressBars(bars);
58
+ }
59
+ bars[slotIndex].status = bars[slotIndex].current === bars[slotIndex].total ? 'done' : 'error';
60
+ renderMultiProgressBars(bars);
61
+ });
62
+
63
+ await Promise.all(workers);
64
+ clearProgressBars();
65
+
66
+ const uniqueResults = deduplicate(allResults);
67
+ const filteredResults = applyFilter(uniqueResults, filter);
68
+
69
+ if (errors.length > 0) {
70
+ const firstMsg = errors[0].message;
71
+ const isProxyError = ['不可用', '连接被拒绝', '连接中断', '超时', '无法解析']
72
+ .some(kw => firstMsg.includes(kw));
73
+
74
+ if (filteredResults.length === 0) {
75
+ if (isProxyError) {
76
+ console.error(` 所有请求失败,请检查代理: ${proxyUrl}\n`);
77
+ } else {
78
+ const show = errors.slice(0, 5);
79
+ for (const e of show) console.error(` ✗ ${e.url}: ${e.message}\n`);
80
+ if (errors.length > 5) console.error(` ... 还有 ${errors.length - 5} 个失败\n`);
81
+ }
82
+ console.error('未获取到数据');
83
+ if (outputFile) writeFileSync(outputFile, '[]', 'utf-8');
84
+ return;
85
+ } else {
86
+ if (isProxyError) {
87
+ console.error(` ${errors.length} 个请求失败,请检查代理: ${proxyUrl}\n`);
88
+ } else {
89
+ console.error(` ${errors.length} 个失败:`);
90
+ const show = errors.slice(0, 5);
91
+ for (const e of show) console.error(` ✗ ${e.url}: ${e.message}`);
92
+ if (errors.length > 5) console.error(` ... 还有 ${errors.length - 5} 个`);
93
+ }
94
+ }
95
+ }
96
+
97
+ const output = formatOutput(filteredResults, outputFormat);
98
+
99
+ if (outputFile) {
100
+ writeFileSync(outputFile, output, 'utf-8');
101
+ console.log(`\n结果已写入: ${outputFile}`);
102
+ } else {
103
+ process.stdout.write(output + '\n');
104
+ }
105
+
106
+ if (filter) {
107
+ console.log(`\n共 ${uniqueResults.length} 个${label},过滤后 ${filteredResults.length} 个(过滤条件: ${formatFilterDescription(filter)})`);
108
+ } else {
109
+ console.log(`\n共 ${filteredResults.length} 个${label}`);
110
+ }
111
+ }
package/src/cli/scrape.js CHANGED
@@ -1,47 +1,47 @@
1
- import { writeFileSync } from 'fs';
2
-
3
- export async function handleScrape(options) {
4
- const { scrapeUrl, scrapePreset, scrapeMaxVideos, scrapeMaxComments, scrapeMaxGuess, scrapeSwitchDelay, scrapeCommentDelay, outputFile } = options;
5
-
6
- if (!scrapeUrl) {
7
- console.error('用法: tt-help scrape <视频URL> [preset] [最大视频数] [最大评论数] [-o 输出路径]');
8
- console.error('预设: fast, normal, slow, stealth');
9
- console.error('选项: -o, --output <路径> 输出到文件(默认输出到 stdout)');
10
- console.error(' --switch-delay <ms> 视频切换延迟(毫秒)');
11
- console.error(' --comment-delay <ms> 评论滚动延迟(毫秒)');
12
- process.exit(1);
13
- }
14
-
15
- const { runScrape } = await import('../scraper/core.mjs');
16
-
17
- let browser;
18
- try {
19
- const { output, browser: b } = await runScrape({
20
- videoUrl: scrapeUrl,
21
- maxVideos: scrapeMaxVideos,
22
- maxComments: scrapeMaxComments,
23
- maxGuess: scrapeMaxGuess,
24
- preset: scrapePreset,
25
- switchMax: scrapeSwitchDelay,
26
- commentMax: scrapeCommentDelay,
27
- log: console.error,
28
- });
29
- browser = b;
30
-
31
- const json = JSON.stringify(output, null, 2);
32
- if (outputFile) {
33
- writeFileSync(outputFile, json, 'utf-8');
34
- console.error(`结果已写入: ${outputFile}`);
35
- } else {
36
- process.stdout.write(json + '\n');
37
- }
38
-
39
- const stats = output.stats;
40
- console.error(`\n共 ${stats.totalVideos} 个视频, ${stats.uniqueVideoAuthors} 个视频作者, ${stats.uniqueCommentAuthors} 个评论作者, ${stats.uniqueGuessAuthors} 个猜你喜欢作者`);
41
- } catch (err) {
42
- console.error(`浏览器抓取失败: ${err.message}`);
43
- process.exit(1);
44
- } finally {
45
- if (browser) await browser.close().catch(() => {});
46
- }
47
- }
1
+ import { writeFileSync } from 'fs';
2
+
3
+ export async function handleScrape(options) {
4
+ const { scrapeUrl, scrapePreset, scrapeMaxVideos, scrapeMaxComments, scrapeMaxGuess, scrapeSwitchDelay, scrapeCommentDelay, outputFile } = options;
5
+
6
+ if (!scrapeUrl) {
7
+ console.error('用法: tt-help scrape <视频URL> [preset] [最大视频数] [最大评论数] [-o 输出路径]');
8
+ console.error('预设: fast, normal, slow, stealth');
9
+ console.error('选项: -o, --output <路径> 输出到文件(默认输出到 stdout)');
10
+ console.error(' --switch-delay <ms> 视频切换延迟(毫秒)');
11
+ console.error(' --comment-delay <ms> 评论滚动延迟(毫秒)');
12
+ process.exit(1);
13
+ }
14
+
15
+ const { runScrape } = await import('../scraper/core.mjs');
16
+
17
+ let browser;
18
+ try {
19
+ const { output, browser: b } = await runScrape({
20
+ videoUrl: scrapeUrl,
21
+ maxVideos: scrapeMaxVideos,
22
+ maxComments: scrapeMaxComments,
23
+ maxGuess: scrapeMaxGuess,
24
+ preset: scrapePreset,
25
+ switchMax: scrapeSwitchDelay,
26
+ commentMax: scrapeCommentDelay,
27
+ log: console.error,
28
+ });
29
+ browser = b;
30
+
31
+ const json = JSON.stringify(output, null, 2);
32
+ if (outputFile) {
33
+ writeFileSync(outputFile, json, 'utf-8');
34
+ console.error(`结果已写入: ${outputFile}`);
35
+ } else {
36
+ process.stdout.write(json + '\n');
37
+ }
38
+
39
+ const stats = output.stats;
40
+ console.error(`\n共 ${stats.totalVideos} 个视频, ${stats.uniqueVideoAuthors} 个视频作者, ${stats.uniqueCommentAuthors} 个评论作者, ${stats.uniqueGuessAuthors} 个猜你喜欢作者`);
41
+ } catch (err) {
42
+ console.error(`浏览器抓取失败: ${err.message}`);
43
+ process.exit(1);
44
+ } finally {
45
+ if (browser) await browser.close().catch(() => {});
46
+ }
47
+ }
package/src/cli/utils.js CHANGED
@@ -1,18 +1,18 @@
1
- import { writeFileSync } from 'fs';
2
-
3
- export function cleanError(msg) {
4
- return msg
5
- .replace(/\x1b\[[0-9;]*m/g, '')
6
- .replace(/\s*- navigating to.*/s, '')
7
- .replace(/\s*Call log:/s, '')
8
- .trim();
9
- }
10
-
11
- export function writeJson(data, outputFile) {
12
- const json = JSON.stringify(data, null, 2);
13
- if (outputFile) {
14
- writeFileSync(outputFile, json, 'utf-8');
15
- } else {
16
- process.stdout.write(json + '\n');
17
- }
18
- }
1
+ import { writeFileSync } from 'fs';
2
+
3
+ export function cleanError(msg) {
4
+ return msg
5
+ .replace(/\x1b\[[0-9;]*m/g, '')
6
+ .replace(/\s*- navigating to.*/s, '')
7
+ .replace(/\s*Call log:/s, '')
8
+ .trim();
9
+ }
10
+
11
+ export function writeJson(data, outputFile) {
12
+ const json = JSON.stringify(data, null, 2);
13
+ if (outputFile) {
14
+ writeFileSync(outputFile, json, 'utf-8');
15
+ } else {
16
+ process.stdout.write(json + '\n');
17
+ }
18
+ }