tt-help-cli-ycl 1.3.48 → 1.3.50

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 (64) hide show
  1. package/README.md +33 -33
  2. package/cli.js +9 -9
  3. package/package.json +52 -52
  4. package/scripts/run-explore copy.bat +101 -101
  5. package/scripts/run-explore.bat +134 -134
  6. package/scripts/run-explore.ps1 +159 -159
  7. package/scripts/run-explore.sh +121 -121
  8. package/scripts/test-captcha-lib.mjs +68 -0
  9. package/scripts/test-captcha.mjs +81 -0
  10. package/scripts/test-incognito-lib.mjs +36 -0
  11. package/scripts/test-login-state.mjs +128 -0
  12. package/scripts/test-safe-click.mjs +45 -0
  13. package/scripts/test-watch-db-smoke.mjs +246 -0
  14. package/src/cli/attach.js +331 -331
  15. package/src/cli/auto.js +265 -265
  16. package/src/cli/comments.js +620 -620
  17. package/src/cli/config.js +170 -170
  18. package/src/cli/db-import.js +51 -51
  19. package/src/cli/explore.js +555 -555
  20. package/src/cli/open.js +109 -111
  21. package/src/cli/progress.js +111 -111
  22. package/src/cli/refresh.js +288 -288
  23. package/src/cli/scrape.js +47 -47
  24. package/src/cli/utils.js +18 -18
  25. package/src/cli/videos.js +41 -41
  26. package/src/cli/videostats.js +196 -196
  27. package/src/cli/watch.js +30 -30
  28. package/src/lib/api-interceptor.js +161 -161
  29. package/src/lib/args.js +809 -809
  30. package/src/lib/browser/anti-detect.js +23 -23
  31. package/src/lib/browser/cdp.js +261 -261
  32. package/src/lib/browser/health-checker.js +114 -114
  33. package/src/lib/browser/launch.js +43 -43
  34. package/src/lib/browser/page.js +184 -184
  35. package/src/lib/constants.js +297 -297
  36. package/src/lib/delay.js +54 -54
  37. package/src/lib/explore-fetch.js +118 -118
  38. package/src/lib/fetcher.js +45 -45
  39. package/src/lib/filter.js +66 -66
  40. package/src/lib/io.js +54 -54
  41. package/src/lib/output.js +80 -80
  42. package/src/lib/page-error-detector.js +109 -109
  43. package/src/lib/parse-ssr.mjs +69 -69
  44. package/src/lib/parser.js +47 -47
  45. package/src/lib/retry.js +45 -45
  46. package/src/lib/scrape.js +90 -90
  47. package/src/lib/target-locations.js +61 -61
  48. package/src/lib/tiktok-scraper.mjs +98 -61
  49. package/src/lib/url.js +52 -52
  50. package/src/main.js +73 -73
  51. package/src/npm-main.js +70 -70
  52. package/src/results/user-videos-bar.lar.lar.moeta.json +37 -0
  53. package/src/scraper/auto-core.js +203 -203
  54. package/src/scraper/core.js +255 -255
  55. package/src/scraper/explore-core.js +208 -208
  56. package/src/scraper/modules/captcha-handler.js +114 -114
  57. package/src/scraper/modules/follow-extractor.js +250 -250
  58. package/src/scraper/modules/guess-extractor.js +51 -51
  59. package/src/scraper/modules/page-helpers.js +48 -48
  60. package/src/scraper/refresh-core.js +213 -213
  61. package/src/videos/core.js +143 -143
  62. package/src/watch/data-store.js +2980 -2980
  63. package/src/watch/public/index.html +2355 -2355
  64. package/src/watch/server.js +727 -727
@@ -1,288 +1,288 @@
1
- import {
2
- getOrCreatePage,
3
- isBrowserClosedError,
4
- relaunchBrowser,
5
- } from "../lib/browser/page.js";
6
- import { delay, setDelayConfig } from "../scraper/modules/page-helpers.js";
7
- import { userId as configuredUserId, saveUserId } from "../lib/constants.js";
8
- import { getMacOrUuid } from "../lib/mac-or-uuid.js";
9
- import { ensureBrowserReady as ensureBrowserReadyCDP } from "../lib/browser/cdp.js";
10
- import { processRefresh } from "../scraper/refresh-core.js";
11
- import path from "path";
12
- import os from "os";
13
-
14
- async function withRetry(fn, maxRetries = 5) {
15
- for (let attempt = 1; attempt <= maxRetries; attempt++) {
16
- try {
17
- return await fn();
18
- } catch (e) {
19
- if (attempt < maxRetries) {
20
- const waitTime =
21
- attempt <= 2
22
- ? 5000 + Math.random() * 5000
23
- : attempt <= 4
24
- ? 10000 + Math.random() * 10000
25
- : 20000 + Math.random() * 10000;
26
- console.error(
27
- ` [网络] 请求失败 (${attempt}/${maxRetries}),${Math.round(waitTime / 1000)}s 后重试...`,
28
- );
29
- await delay(waitTime / 1000, waitTime / 1000);
30
- } else {
31
- throw e;
32
- }
33
- }
34
- }
35
- }
36
-
37
- async function apiGet(url) {
38
- const resp = await fetch(url);
39
- if (!resp.ok) throw new Error(`HTTP ${resp.status}: ${resp.statusText}`);
40
- return resp.json();
41
- }
42
-
43
- async function apiPost(url, body) {
44
- const resp = await fetch(url, {
45
- method: "POST",
46
- headers: { "Content-Type": "application/json" },
47
- body: JSON.stringify(body),
48
- });
49
- if (!resp.ok) throw new Error(`HTTP ${resp.status}: ${resp.statusText}`);
50
- return resp.json();
51
- }
52
-
53
- export async function handleRefresh(options) {
54
- const {
55
- explorePreset,
56
- explorePort,
57
- exploreProfile,
58
- exploreUserId,
59
- serverUrl,
60
- } = options;
61
- let browser = null;
62
- let shuttingDown = false;
63
-
64
- const shutdown = async (signal) => {
65
- if (shuttingDown) return;
66
- shuttingDown = true;
67
- console.error(`\n[Refresh] 收到 ${signal},正在关闭浏览器...`);
68
- await browser?.close().catch(() => {});
69
- console.error("[Refresh] 已退出");
70
- process.exit(0);
71
- };
72
-
73
- const onSigint = () => {
74
- void shutdown("SIGINT");
75
- };
76
- const onSigterm = () => {
77
- void shutdown("SIGTERM");
78
- };
79
-
80
- process.once("SIGINT", onSigint);
81
- process.once("SIGTERM", onSigterm);
82
-
83
- try {
84
- let userId = exploreUserId || configuredUserId;
85
- if (!userId) {
86
- userId = await getMacOrUuid();
87
- saveUserId(userId);
88
- console.error(`[初始化] 未检测到本地用户编号,已生成并使用: ${userId}`);
89
- }
90
-
91
- setDelayConfig(explorePreset);
92
-
93
- console.error(`\n=== Refresh 模式 ===`);
94
- console.error(`服务器: ${serverUrl}`);
95
- console.error(`CDP 端口: ${explorePort || 9222}, 用户编号: ${userId}`);
96
- if (exploreProfile) console.error(`浏览器配置: ${exploreProfile}`);
97
- console.error(`刷新: 视频 100 + 关注 100 + 粉丝 100`);
98
- console.error(`新用户探索: 评论 + 猜你喜欢 + 关注/粉丝`);
99
-
100
- const cdpOptions = {};
101
- if (explorePort) cdpOptions.port = explorePort;
102
- if (exploreProfile) {
103
- cdpOptions.userDataDir = path.join(
104
- os.homedir(),
105
- "Library",
106
- "Application Support",
107
- `Microsoft Edge For Testing_${exploreProfile}`,
108
- );
109
- }
110
-
111
- browser = await ensureBrowserReadyCDP(cdpOptions);
112
- const page = await getOrCreatePage(browser);
113
-
114
- let processedCount = 0;
115
- let errorCount = 0;
116
- let consecutiveNetworkErrors = 0;
117
-
118
- console.error(`\n开始循环刷新任务...\n`);
119
-
120
- while (!shuttingDown) {
121
- try {
122
- const jobData = await withRetry(() =>
123
- apiGet(`${serverUrl}/api/redo-job?userId=${userId}`),
124
- );
125
-
126
- if (!jobData.hasJob) {
127
- console.error(`\n[空闲] 暂无 redo 任务,30s 后重试...`);
128
- await delay(30000, 30000);
129
- continue;
130
- }
131
-
132
- const { uniqueId, nickname } = jobData.user;
133
- consecutiveNetworkErrors = 0;
134
- processedCount++;
135
-
136
- console.error(
137
- `\n[${processedCount}] 刷新 @${uniqueId} (${nickname || "未知"})...`,
138
- );
139
-
140
- const result = await processRefresh(
141
- page,
142
- uniqueId,
143
- serverUrl,
144
- {
145
- maxFollowing: 100,
146
- maxFollowers: 100,
147
- maxVideos: 100,
148
- },
149
- console.error,
150
- );
151
-
152
- if (result.restricted) {
153
- console.error(` @${uniqueId} 页面受限,跳过`);
154
- await apiPost(`${serverUrl}/api/redo-job/${uniqueId}`, {
155
- restricted: true,
156
- userInfo: result.userInfo || {},
157
- });
158
- continue;
159
- }
160
-
161
- if (result.error) {
162
- // 浏览器关闭检测
163
- if (isBrowserClosedError(new Error(result.error))) {
164
- const newBrowser = await relaunchBrowser(
165
- cdpOptions,
166
- explorePort || 9222,
167
- );
168
- browser = newBrowser;
169
- const newPage = await getOrCreatePage(browser);
170
- Object.assign(page, newPage);
171
- // 重试当前用户
172
- const retryResult = await processRefresh(
173
- page,
174
- uniqueId,
175
- serverUrl,
176
- {
177
- maxFollowing: 100,
178
- maxFollowers: 100,
179
- maxVideos: 100,
180
- },
181
- console.error,
182
- );
183
- Object.assign(result, retryResult);
184
- // 继续下方逻辑,检查重试后的 result
185
- } else {
186
- consecutiveNetworkErrors++;
187
- errorCount++;
188
- console.error(` [错误] ${result.error}`);
189
-
190
- if (consecutiveNetworkErrors >= 3) {
191
- console.error(
192
- ` [警告] 连续 ${consecutiveNetworkErrors} 次错误,等待 60s 后重试...`,
193
- );
194
- await delay(60000, 60000);
195
- consecutiveNetworkErrors = 0;
196
- }
197
-
198
- await apiPost(`${serverUrl}/api/redo-job/${uniqueId}`, {
199
- error: result.error,
200
- userInfo: result.userInfo || {},
201
- });
202
- const errorType =
203
- consecutiveNetworkErrors > 1 ? "network" : "other";
204
- apiPost(`${serverUrl}/api/error-report`, {
205
- userId,
206
- username: uniqueId,
207
- errorType,
208
- errorMessage: result.error,
209
- stage: "process",
210
- errorStack: result.errorStack || "",
211
- }).catch(() => {});
212
- continue;
213
- }
214
- }
215
-
216
- if (result.captchaDetected) {
217
- await apiPost(`${serverUrl}/api/error-report`, {
218
- userId,
219
- username: uniqueId,
220
- errorType: "captcha",
221
- errorMessage: result.captchaMessage || "页面出现验证码",
222
- stage: result.captchaStage || "video-page",
223
- errorStack: "",
224
- });
225
- }
226
-
227
- consecutiveNetworkErrors = 0;
228
-
229
- const guessedLocation = result.locationCreated || null;
230
-
231
- await apiPost(`${serverUrl}/api/redo-job/${uniqueId}`, {
232
- userInfo: result.userInfo || {},
233
- discoveredVideoAuthors: (result.discoveredVideoAuthors || []).map(
234
- (item) =>
235
- typeof item === "object" ? { ...item, guessedLocation } : item,
236
- ),
237
- discoveredCommentAuthors: (result.discoveredCommentAuthors || []).map(
238
- (author) => ({ author, guessedLocation }),
239
- ),
240
- discoveredGuessAuthors: (result.discoveredGuessAuthors || []).map(
241
- (author) => ({ author, guessedLocation }),
242
- ),
243
- discoveredFollowing: (result.discoveredFollowing || []).map((f) => ({
244
- handle: Array.isArray(f) ? f[0] : f,
245
- displayName: Array.isArray(f) ? f[1] : null,
246
- guessedLocation,
247
- })),
248
- discoveredFollowers: (result.discoveredFollowers || []).map((f) => ({
249
- handle: Array.isArray(f) ? f[0] : f,
250
- displayName: Array.isArray(f) ? f[1] : null,
251
- guessedLocation,
252
- })),
253
- newUsersAdded: result.newUsersAdded || 0,
254
- collectedVideos: result.collectedVideos || 0,
255
- });
256
-
257
- console.error(
258
- ` [完成] 视频: ${result.collectedVideos}, 评论作者: ${result.discoveredCommentAuthors?.length || 0}, 关注: ${result.discoveredFollowing?.length || 0}, 粉丝: ${result.discoveredFollowers?.length || 0}, 新增用户: ${result.newUsersAdded}`,
259
- );
260
-
261
- await delay(3000, 5000);
262
- } catch (e) {
263
- consecutiveNetworkErrors++;
264
- errorCount++;
265
- console.error(`\n[错误] ${e.message}`);
266
-
267
- if (consecutiveNetworkErrors >= 3) {
268
- console.error(
269
- `[警告] 连续 ${consecutiveNetworkErrors} 次网络异常,等待 60s 后重试...`,
270
- );
271
- await delay(60000, 60000);
272
- consecutiveNetworkErrors = 0;
273
- } else {
274
- const waitTime =
275
- consecutiveNetworkErrors <= 2
276
- ? 5000 + Math.random() * 5000
277
- : 10000 + Math.random() * 10000;
278
- console.error(` 等待 ${Math.round(waitTime / 1000)}s 后重试...`);
279
- await delay(waitTime / 1000, waitTime / 1000);
280
- }
281
- }
282
- }
283
- } finally {
284
- process.removeListener("SIGINT", onSigint);
285
- process.removeListener("SIGTERM", onSigterm);
286
- await browser?.close().catch(() => {});
287
- }
288
- }
1
+ import {
2
+ getOrCreatePage,
3
+ isBrowserClosedError,
4
+ relaunchBrowser,
5
+ } from "../lib/browser/page.js";
6
+ import { delay, setDelayConfig } from "../scraper/modules/page-helpers.js";
7
+ import { userId as configuredUserId, saveUserId } from "../lib/constants.js";
8
+ import { getMacOrUuid } from "../lib/mac-or-uuid.js";
9
+ import { ensureBrowserReady as ensureBrowserReadyCDP } from "../lib/browser/cdp.js";
10
+ import { processRefresh } from "../scraper/refresh-core.js";
11
+ import path from "path";
12
+ import os from "os";
13
+
14
+ async function withRetry(fn, maxRetries = 5) {
15
+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
16
+ try {
17
+ return await fn();
18
+ } catch (e) {
19
+ if (attempt < maxRetries) {
20
+ const waitTime =
21
+ attempt <= 2
22
+ ? 5000 + Math.random() * 5000
23
+ : attempt <= 4
24
+ ? 10000 + Math.random() * 10000
25
+ : 20000 + Math.random() * 10000;
26
+ console.error(
27
+ ` [网络] 请求失败 (${attempt}/${maxRetries}),${Math.round(waitTime / 1000)}s 后重试...`,
28
+ );
29
+ await delay(waitTime / 1000, waitTime / 1000);
30
+ } else {
31
+ throw e;
32
+ }
33
+ }
34
+ }
35
+ }
36
+
37
+ async function apiGet(url) {
38
+ const resp = await fetch(url);
39
+ if (!resp.ok) throw new Error(`HTTP ${resp.status}: ${resp.statusText}`);
40
+ return resp.json();
41
+ }
42
+
43
+ async function apiPost(url, body) {
44
+ const resp = await fetch(url, {
45
+ method: "POST",
46
+ headers: { "Content-Type": "application/json" },
47
+ body: JSON.stringify(body),
48
+ });
49
+ if (!resp.ok) throw new Error(`HTTP ${resp.status}: ${resp.statusText}`);
50
+ return resp.json();
51
+ }
52
+
53
+ export async function handleRefresh(options) {
54
+ const {
55
+ explorePreset,
56
+ explorePort,
57
+ exploreProfile,
58
+ exploreUserId,
59
+ serverUrl,
60
+ } = options;
61
+ let browser = null;
62
+ let shuttingDown = false;
63
+
64
+ const shutdown = async (signal) => {
65
+ if (shuttingDown) return;
66
+ shuttingDown = true;
67
+ console.error(`\n[Refresh] 收到 ${signal},正在关闭浏览器...`);
68
+ await browser?.close().catch(() => {});
69
+ console.error("[Refresh] 已退出");
70
+ process.exit(0);
71
+ };
72
+
73
+ const onSigint = () => {
74
+ void shutdown("SIGINT");
75
+ };
76
+ const onSigterm = () => {
77
+ void shutdown("SIGTERM");
78
+ };
79
+
80
+ process.once("SIGINT", onSigint);
81
+ process.once("SIGTERM", onSigterm);
82
+
83
+ try {
84
+ let userId = exploreUserId || configuredUserId;
85
+ if (!userId) {
86
+ userId = await getMacOrUuid();
87
+ saveUserId(userId);
88
+ console.error(`[初始化] 未检测到本地用户编号,已生成并使用: ${userId}`);
89
+ }
90
+
91
+ setDelayConfig(explorePreset);
92
+
93
+ console.error(`\n=== Refresh 模式 ===`);
94
+ console.error(`服务器: ${serverUrl}`);
95
+ console.error(`CDP 端口: ${explorePort || 9222}, 用户编号: ${userId}`);
96
+ if (exploreProfile) console.error(`浏览器配置: ${exploreProfile}`);
97
+ console.error(`刷新: 视频 100 + 关注 100 + 粉丝 100`);
98
+ console.error(`新用户探索: 评论 + 猜你喜欢 + 关注/粉丝`);
99
+
100
+ const cdpOptions = {};
101
+ if (explorePort) cdpOptions.port = explorePort;
102
+ if (exploreProfile) {
103
+ cdpOptions.userDataDir = path.join(
104
+ os.homedir(),
105
+ "Library",
106
+ "Application Support",
107
+ `Microsoft Edge For Testing_${exploreProfile}`,
108
+ );
109
+ }
110
+
111
+ browser = await ensureBrowserReadyCDP(cdpOptions);
112
+ const page = await getOrCreatePage(browser);
113
+
114
+ let processedCount = 0;
115
+ let errorCount = 0;
116
+ let consecutiveNetworkErrors = 0;
117
+
118
+ console.error(`\n开始循环刷新任务...\n`);
119
+
120
+ while (!shuttingDown) {
121
+ try {
122
+ const jobData = await withRetry(() =>
123
+ apiGet(`${serverUrl}/api/redo-job?userId=${userId}`),
124
+ );
125
+
126
+ if (!jobData.hasJob) {
127
+ console.error(`\n[空闲] 暂无 redo 任务,30s 后重试...`);
128
+ await delay(30000, 30000);
129
+ continue;
130
+ }
131
+
132
+ const { uniqueId, nickname } = jobData.user;
133
+ consecutiveNetworkErrors = 0;
134
+ processedCount++;
135
+
136
+ console.error(
137
+ `\n[${processedCount}] 刷新 @${uniqueId} (${nickname || "未知"})...`,
138
+ );
139
+
140
+ const result = await processRefresh(
141
+ page,
142
+ uniqueId,
143
+ serverUrl,
144
+ {
145
+ maxFollowing: 100,
146
+ maxFollowers: 100,
147
+ maxVideos: 100,
148
+ },
149
+ console.error,
150
+ );
151
+
152
+ if (result.restricted) {
153
+ console.error(` @${uniqueId} 页面受限,跳过`);
154
+ await apiPost(`${serverUrl}/api/redo-job/${uniqueId}`, {
155
+ restricted: true,
156
+ userInfo: result.userInfo || {},
157
+ });
158
+ continue;
159
+ }
160
+
161
+ if (result.error) {
162
+ // 浏览器关闭检测
163
+ if (isBrowserClosedError(new Error(result.error))) {
164
+ const newBrowser = await relaunchBrowser(
165
+ cdpOptions,
166
+ explorePort || 9222,
167
+ );
168
+ browser = newBrowser;
169
+ const newPage = await getOrCreatePage(browser);
170
+ Object.assign(page, newPage);
171
+ // 重试当前用户
172
+ const retryResult = await processRefresh(
173
+ page,
174
+ uniqueId,
175
+ serverUrl,
176
+ {
177
+ maxFollowing: 100,
178
+ maxFollowers: 100,
179
+ maxVideos: 100,
180
+ },
181
+ console.error,
182
+ );
183
+ Object.assign(result, retryResult);
184
+ // 继续下方逻辑,检查重试后的 result
185
+ } else {
186
+ consecutiveNetworkErrors++;
187
+ errorCount++;
188
+ console.error(` [错误] ${result.error}`);
189
+
190
+ if (consecutiveNetworkErrors >= 3) {
191
+ console.error(
192
+ ` [警告] 连续 ${consecutiveNetworkErrors} 次错误,等待 60s 后重试...`,
193
+ );
194
+ await delay(60000, 60000);
195
+ consecutiveNetworkErrors = 0;
196
+ }
197
+
198
+ await apiPost(`${serverUrl}/api/redo-job/${uniqueId}`, {
199
+ error: result.error,
200
+ userInfo: result.userInfo || {},
201
+ });
202
+ const errorType =
203
+ consecutiveNetworkErrors > 1 ? "network" : "other";
204
+ apiPost(`${serverUrl}/api/error-report`, {
205
+ userId,
206
+ username: uniqueId,
207
+ errorType,
208
+ errorMessage: result.error,
209
+ stage: "process",
210
+ errorStack: result.errorStack || "",
211
+ }).catch(() => {});
212
+ continue;
213
+ }
214
+ }
215
+
216
+ if (result.captchaDetected) {
217
+ await apiPost(`${serverUrl}/api/error-report`, {
218
+ userId,
219
+ username: uniqueId,
220
+ errorType: "captcha",
221
+ errorMessage: result.captchaMessage || "页面出现验证码",
222
+ stage: result.captchaStage || "video-page",
223
+ errorStack: "",
224
+ });
225
+ }
226
+
227
+ consecutiveNetworkErrors = 0;
228
+
229
+ const guessedLocation = result.locationCreated || null;
230
+
231
+ await apiPost(`${serverUrl}/api/redo-job/${uniqueId}`, {
232
+ userInfo: result.userInfo || {},
233
+ discoveredVideoAuthors: (result.discoveredVideoAuthors || []).map(
234
+ (item) =>
235
+ typeof item === "object" ? { ...item, guessedLocation } : item,
236
+ ),
237
+ discoveredCommentAuthors: (result.discoveredCommentAuthors || []).map(
238
+ (author) => ({ author, guessedLocation }),
239
+ ),
240
+ discoveredGuessAuthors: (result.discoveredGuessAuthors || []).map(
241
+ (author) => ({ author, guessedLocation }),
242
+ ),
243
+ discoveredFollowing: (result.discoveredFollowing || []).map((f) => ({
244
+ handle: Array.isArray(f) ? f[0] : f,
245
+ displayName: Array.isArray(f) ? f[1] : null,
246
+ guessedLocation,
247
+ })),
248
+ discoveredFollowers: (result.discoveredFollowers || []).map((f) => ({
249
+ handle: Array.isArray(f) ? f[0] : f,
250
+ displayName: Array.isArray(f) ? f[1] : null,
251
+ guessedLocation,
252
+ })),
253
+ newUsersAdded: result.newUsersAdded || 0,
254
+ collectedVideos: result.collectedVideos || 0,
255
+ });
256
+
257
+ console.error(
258
+ ` [完成] 视频: ${result.collectedVideos}, 评论作者: ${result.discoveredCommentAuthors?.length || 0}, 关注: ${result.discoveredFollowing?.length || 0}, 粉丝: ${result.discoveredFollowers?.length || 0}, 新增用户: ${result.newUsersAdded}`,
259
+ );
260
+
261
+ await delay(3000, 5000);
262
+ } catch (e) {
263
+ consecutiveNetworkErrors++;
264
+ errorCount++;
265
+ console.error(`\n[错误] ${e.message}`);
266
+
267
+ if (consecutiveNetworkErrors >= 3) {
268
+ console.error(
269
+ `[警告] 连续 ${consecutiveNetworkErrors} 次网络异常,等待 60s 后重试...`,
270
+ );
271
+ await delay(60000, 60000);
272
+ consecutiveNetworkErrors = 0;
273
+ } else {
274
+ const waitTime =
275
+ consecutiveNetworkErrors <= 2
276
+ ? 5000 + Math.random() * 5000
277
+ : 10000 + Math.random() * 10000;
278
+ console.error(` 等待 ${Math.round(waitTime / 1000)}s 后重试...`);
279
+ await delay(waitTime / 1000, waitTime / 1000);
280
+ }
281
+ }
282
+ }
283
+ } finally {
284
+ process.removeListener("SIGINT", onSigint);
285
+ process.removeListener("SIGTERM", onSigterm);
286
+ await browser?.close().catch(() => {});
287
+ }
288
+ }
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.js');
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.js');
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
+ }