tt-help-cli-ycl 1.3.32 → 1.3.34

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 (59) hide show
  1. package/README.md +17 -17
  2. package/cli.js +9 -9
  3. package/package.json +47 -47
  4. package/scripts/run-explore copy.bat +101 -68
  5. package/scripts/run-explore.bat +132 -97
  6. package/scripts/run-explore.ps1 +157 -107
  7. package/scripts/run-explore.sh +119 -98
  8. package/src/cli/attach.js +180 -180
  9. package/src/cli/auto.js +240 -240
  10. package/src/cli/config.js +152 -152
  11. package/src/cli/explore.js +488 -478
  12. package/src/cli/info.js +88 -88
  13. package/src/cli/open.js +111 -111
  14. package/src/cli/progress.js +111 -111
  15. package/src/cli/refresh.js +216 -216
  16. package/src/cli/scrape.js +47 -47
  17. package/src/cli/utils.js +18 -18
  18. package/src/cli/videos.js +41 -41
  19. package/src/cli/watch.js +31 -31
  20. package/src/lib/api-interceptor.js +44 -130
  21. package/src/lib/args.js +722 -718
  22. package/src/lib/browser/anti-detect.js +23 -23
  23. package/src/lib/browser/cdp.js +261 -261
  24. package/src/lib/browser/health-checker.js +114 -114
  25. package/src/lib/browser/launch.js +43 -43
  26. package/src/lib/browser/page.js +183 -183
  27. package/src/lib/constants.js +216 -213
  28. package/src/lib/delay.js +54 -54
  29. package/src/lib/explore-fetch.js +118 -118
  30. package/src/lib/fetcher.js +45 -45
  31. package/src/lib/filter.js +66 -66
  32. package/src/lib/io.js +54 -54
  33. package/src/lib/output.js +80 -80
  34. package/src/lib/page-error-detector.js +105 -105
  35. package/src/lib/parse-ssr.mjs +69 -69
  36. package/src/lib/parser.js +47 -47
  37. package/src/lib/retry.js +45 -45
  38. package/src/lib/scrape.js +89 -89
  39. package/src/lib/tiktok-scraper.mjs +194 -194
  40. package/src/lib/url.js +52 -52
  41. package/src/main.js +48 -48
  42. package/src/scraper/auto-core.js +203 -203
  43. package/src/scraper/core.js +211 -211
  44. package/src/scraper/explore-core.js +167 -167
  45. package/src/scraper/modules/captcha-handler.js +114 -114
  46. package/src/scraper/modules/follow-extractor.js +194 -118
  47. package/src/scraper/modules/guess-extractor.js +51 -51
  48. package/src/scraper/modules/page-helpers.js +48 -48
  49. package/src/scraper/refresh-core.js +179 -179
  50. package/src/videos/core.js +125 -125
  51. package/src/watch/data-store.js +1030 -855
  52. package/src/watch/public/index.html +753 -753
  53. package/src/watch/server.js +933 -734
  54. package/scripts/test-captcha-lib.mjs +0 -68
  55. package/scripts/test-captcha.mjs +0 -81
  56. package/scripts/test-incognito-lib.mjs +0 -36
  57. package/scripts/test-login-state.mjs +0 -128
  58. package/scripts/test-safe-click.mjs +0 -45
  59. package/src/results/user-videos-bar.lar.lar.moeta.json +0 -37
@@ -1,478 +1,488 @@
1
- import {
2
- getOrCreatePage,
3
- isBrowserClosedError,
4
- relaunchBrowser,
5
- } from "../lib/browser/page.js";
6
- import {
7
- delay,
8
- getDelayConfig,
9
- setDelayConfig,
10
- } from "../scraper/modules/page-helpers.js";
11
- import {
12
- detectCaptcha,
13
- closeCaptcha,
14
- } from "../scraper/modules/captcha-handler.js";
15
- import { userId as configuredUserId, saveUserId } from "../lib/constants.js";
16
- import { getMacOrUuid } from "../lib/mac-or-uuid.js";
17
- import {
18
- ensureBrowserReady as ensureBrowserReadyCDP,
19
- switchAccount,
20
- } from "../lib/browser/cdp.js";
21
- import { showResourceUsage } from "../utils/index.js";
22
- import { HealthChecker } from "../lib/browser/health-checker.js";
23
- import path from "path";
24
- import os from "os";
25
-
26
- const MAX_RETRY_WAIT = 5 * 60 * 1000;
27
-
28
- async function withRetry(label, fn) {
29
- let backoff = 1000;
30
- while (true) {
31
- try {
32
- return await fn();
33
- } catch (err) {
34
- console.error(
35
- `[连接] ${label} 失败: ${err.message},${backoff / 1000}秒后重试...`,
36
- );
37
- await new Promise((r) => setTimeout(r, backoff));
38
- if (backoff < MAX_RETRY_WAIT) backoff *= 2;
39
- }
40
- }
41
- }
42
-
43
- async function apiPost(url, body) {
44
- return withRetry(`POST ${url}`, async () => {
45
- const res = await fetch(url, {
46
- method: "POST",
47
- headers: { "Content-Type": "application/json" },
48
- body: JSON.stringify(body),
49
- });
50
- return res.json();
51
- });
52
- }
53
-
54
- async function apiGet(url) {
55
- return withRetry(`GET ${url}`, async () => {
56
- const res = await fetch(url);
57
- return res.json();
58
- });
59
- }
60
-
61
- export async function handleExplore(options) {
62
- const {
63
- exploreUsernames,
64
- explorePreset,
65
- exploreMaxComments,
66
- exploreMaxGuess,
67
- exploreEnableFollow,
68
- exploreMaxFollowing,
69
- exploreMaxFollowers,
70
- exploreLocation,
71
- exploreMaxUsers,
72
- serverUrl,
73
- explorePort,
74
- exploreBasePort,
75
- explorePortCount,
76
- exploreUserId,
77
- exploreMaxVideos,
78
- } = options;
79
-
80
- let userId = exploreUserId || configuredUserId;
81
- if (!userId) {
82
- userId = await getMacOrUuid();
83
- saveUserId(userId);
84
- console.error(`[初始化] 未检测到本地用户编号,已生成并使用: ${userId}`);
85
- }
86
-
87
- setDelayConfig(explorePreset);
88
-
89
- await apiGet(`${serverUrl}/api/stats`);
90
-
91
- if (exploreUsernames && exploreUsernames.length > 0) {
92
- const { added, skipped } = await apiPost(`${serverUrl}/api/users`, {
93
- usernames: exploreUsernames,
94
- });
95
- console.error(`种子用户: ${added} 个新增, ${skipped} 个已存在`);
96
- }
97
-
98
- console.error(`\n国家筛选: ${exploreLocation}`);
99
- console.error(`视频采集: ${exploreMaxVideos || 1}`);
100
- console.error(`关注/粉丝: ${exploreEnableFollow ? "启用" : "禁用"}`);
101
- console.error(`服务器: ${serverUrl}(断开会自动重连)`);
102
- if (exploreMaxUsers > 0) console.error(`上限: ${exploreMaxUsers} 个用户`);
103
-
104
- const healthChecker = new HealthChecker({
105
- basePort: exploreBasePort,
106
- totalAccounts: explorePortCount,
107
- });
108
- let currentAccount = healthChecker.getCurrentAccount();
109
-
110
- const cdpOptions = {};
111
- if (explorePort) {
112
- // 固定端口模式(调试用,关闭自动轮换)
113
- cdpOptions.port = explorePort;
114
- cdpOptions.userDataDir = path.join(
115
- os.homedir(),
116
- "Library",
117
- "Application Support",
118
- `Microsoft Edge For Testing_p${explorePort}`,
119
- );
120
- } else {
121
- cdpOptions.port = currentAccount.port;
122
- cdpOptions.userDataDir = currentAccount.userDataDir;
123
- }
124
-
125
- console.error(`CDP 端口: ${cdpOptions.port}, 用户编号: ${userId}`);
126
- console.error(`浏览器配置: ${path.basename(cdpOptions.userDataDir)}`);
127
- if (!explorePort) {
128
- const portRange = `${currentAccount.port}-${currentAccount.port + healthChecker.accounts.length - 1}`;
129
- console.error(
130
- `端口轮换范围: ${portRange}(共 ${healthChecker.accounts.length} 个)`,
131
- );
132
- }
133
-
134
- let browser = await ensureBrowserReadyCDP(cdpOptions);
135
- const { processExplore } = await import("../scraper/explore-core.js");
136
- const { isLoggedIn } = await import("../lib/browser/page.js");
137
-
138
- const page = await getOrCreatePage(browser);
139
-
140
- // 先导航到 TikTok 首页,再检测登录状态
141
- await page.goto("https://www.tiktok.com", { waitUntil: "domcontentloaded" });
142
-
143
- // 检测登录状态(启动时只检测一次)
144
- let loggedIn = await isLoggedIn(page);
145
- console.error(`登录状态: ${loggedIn ? "已登录" : "未登录"}`);
146
-
147
- // 全局拦截图片资源,减少内存占用和加载时间
148
- await page.route("**/*", (route) => {
149
- const resourceType = route.request().resourceType();
150
- if (resourceType === "image" || resourceType === "stylesheet") {
151
- route.abort();
152
- } else {
153
- route.continue();
154
- }
155
- });
156
-
157
- // 账户切换后,重新初始化 page
158
- async function setupNewPage(newBrowser) {
159
- const newPage = await getOrCreatePage(newBrowser);
160
- await newPage.route("**/*", (route) => {
161
- const resourceType = route.request().resourceType();
162
- if (resourceType === "image" || resourceType === "stylesheet") {
163
- route.abort();
164
- } else {
165
- route.continue();
166
- }
167
- });
168
- return newPage;
169
- }
170
-
171
- async function handleAccountSwitch(reason) {
172
- console.error(`\n[健康检查] ${reason},切换到下一个账户...`);
173
- const oldAccount = currentAccount;
174
- const nextAccount = healthChecker.getNextAccount();
175
- currentAccount = nextAccount;
176
- const newBrowser = await switchAccount(
177
- { port: oldAccount.port, userDataDir: oldAccount.userDataDir },
178
- { port: nextAccount.port, userDataDir: nextAccount.userDataDir },
179
- );
180
- browser = newBrowser;
181
- const newPage = await setupNewPage(browser);
182
- Object.assign(page, newPage);
183
- Object.assign(cdpOptions, {
184
- port: nextAccount.port,
185
- userDataDir: nextAccount.userDataDir,
186
- });
187
- console.error(`[健康检查] 已切换到端口 ${nextAccount.port}`);
188
- // 切换账户后先导航到 TikTok,再重新检测登录状态
189
- await page.goto("https://www.tiktok.com", {
190
- waitUntil: "domcontentloaded",
191
- });
192
- loggedIn = await isLoggedIn(page);
193
- console.error(
194
- `[健康检查] 新账户登录状态: ${loggedIn ? "已登录" : "未登录"}`,
195
- );
196
- lastFollowSuccessTime = Date.now(); // 切换账户后重置
197
- captchaCount = 0; // 重置验证码计数
198
- consecutiveNetworkErrors = 0; // 重置网络错误计数
199
- }
200
-
201
- let processedCount = 0;
202
- let errorCount = 0;
203
- let consecutiveNetworkErrors = 0;
204
- let captchaCount = 0; // 验证码累计计数
205
- let lastFollowSuccessTime = Date.now(); // 最近一次成功获取关注/粉丝的时间
206
- const FOLLOW_BLOCK_THRESHOLD = 10 * 60 * 1000; // 10分钟
207
-
208
- while (true) {
209
- const job = await apiGet(
210
- `${serverUrl}/api/job?userId=${encodeURIComponent(userId)}`,
211
- );
212
- if (!job.hasJob) break;
213
-
214
- const username = job.user.uniqueId;
215
- processedCount++;
216
-
217
- if (processedCount % 10 === 0) {
218
- showResourceUsage();
219
- }
220
-
221
- const checkResult = healthChecker.check();
222
- if (checkResult.shouldSwitch) {
223
- await handleAccountSwitch(checkResult.reason);
224
- } else if (checkResult.info && processedCount % 10 === 0) {
225
- // 关注/粉丝封禁检测状态(仅登录状态)
226
- let followStatus = "";
227
- if (loggedIn && exploreEnableFollow) {
228
- const followElapsed = Math.round(
229
- (Date.now() - lastFollowSuccessTime) / 60000,
230
- );
231
- const followRemaining = Math.max(
232
- 0,
233
- Math.round(FOLLOW_BLOCK_THRESHOLD / 60000) - followElapsed,
234
- );
235
- followStatus = ` | 关注/粉丝上次成功 ${followElapsed} 分钟前 | 封禁检测 ${followRemaining} 分钟后`;
236
- }
237
- console.error(`\n[健康检查] ${checkResult.info}${followStatus}`);
238
- }
239
-
240
- // 切换任务前检测验证码
241
- const switchCaptcha = await detectCaptcha(page);
242
- if (switchCaptcha && switchCaptcha.visible) {
243
- console.error(`\n[验证码] 切换任务前检测到验证码,等待3分钟...`);
244
- captchaCount++;
245
- console.error(` [验证码] 累计 ${captchaCount} 次`);
246
-
247
- // 等待3分钟
248
- await new Promise((r) => setTimeout(r, 3 * 60 * 1000));
249
-
250
- // 尝试关闭
251
- const closeResult = await closeCaptcha(page);
252
- if (closeResult.success) {
253
- console.error(` [验证码] 已关闭`);
254
- } else {
255
- console.error(` [验证码] 关闭失败: ${closeResult.reason}`);
256
- }
257
-
258
- // 上报
259
- await withRetry("report captcha", () =>
260
- apiPost(`${serverUrl}/api/error-report`, {
261
- userId,
262
- username: "unknown",
263
- errorType: "captcha",
264
- errorMessage: "切换任务前检测到验证码",
265
- stage: "between-tasks",
266
- errorStack: "",
267
- }),
268
- ).catch(() => {});
269
-
270
- // 累计2次切换账户
271
- if (captchaCount >= 2) {
272
- await handleAccountSwitch(`验证码累计 ${captchaCount} 次`);
273
- captchaCount = 0;
274
- }
275
- }
276
-
277
- // 关注/粉丝封禁检测移到健康检查中(仅登录状态下)
278
- if (loggedIn && exploreEnableFollow) {
279
- const followElapsed = Date.now() - lastFollowSuccessTime;
280
- if (followElapsed > FOLLOW_BLOCK_THRESHOLD) {
281
- const minutes = Math.round(followElapsed / 60000);
282
- console.error(
283
- `\n[封禁检测] 关注/粉丝功能已 ${minutes} 分钟未成功获取,切换到下一个账户...`,
284
- );
285
- await handleAccountSwitch(`关注/粉丝功能被封`);
286
- }
287
- }
288
-
289
- if (consecutiveNetworkErrors > 0) {
290
- const waitTime =
291
- consecutiveNetworkErrors <= 2
292
- ? 0
293
- : consecutiveNetworkErrors <= 5
294
- ? 30000
295
- : 300000;
296
- if (waitTime > 0) {
297
- console.error(
298
- ` [网络] 连续 ${consecutiveNetworkErrors} 次网络异常,等待 ${waitTime / 1000}s 后重试...`,
299
- );
300
- await new Promise((r) => setTimeout(r, waitTime));
301
- }
302
- }
303
-
304
- console.error(`\n[${processedCount}] 探索 @${username}...`);
305
-
306
- const { switchMax } = getDelayConfig();
307
- await delay(switchMax, switchMax * 3);
308
-
309
- let result = await processExplore(
310
- page,
311
- username,
312
- {
313
- maxVideos: exploreMaxVideos,
314
- enableFollow: exploreEnableFollow,
315
- loggedIn, // 传入登录状态,避免每次调用 isLoggedIn(page)
316
- maxFollowing: exploreMaxFollowing,
317
- maxFollowers: exploreMaxFollowers,
318
- location: exploreLocation,
319
- browser,
320
- },
321
- console.error,
322
- );
323
-
324
- // 浏览器关闭检测:processExplore 内部 catch 了异常,需要从 result.error 判断
325
- if (result.error && isBrowserClosedError(new Error(result.error))) {
326
- const newBrowser = await relaunchBrowser(
327
- cdpOptions,
328
- cdpOptions.port || 9222,
329
- );
330
- browser = newBrowser;
331
- const newPage = await setupNewPage(browser);
332
- Object.assign(page, newPage);
333
- // 重试当前用户
334
- result = await processExplore(
335
- page,
336
- username,
337
- {
338
- maxVideos: exploreMaxVideos,
339
- enableFollow: exploreEnableFollow,
340
- loggedIn, // 传入登录状态
341
- maxFollowing: exploreMaxFollowing,
342
- maxFollowers: exploreMaxFollowers,
343
- location: exploreLocation,
344
- browser,
345
- },
346
- console.error,
347
- );
348
- }
349
-
350
- if (result.restricted) {
351
- consecutiveNetworkErrors = 0;
352
- await apiPost(`${serverUrl}/api/job/${username}`, {
353
- restricted: true,
354
- userInfo: result.userInfo || {},
355
- });
356
- if (exploreMaxUsers > 0 && processedCount >= exploreMaxUsers) {
357
- console.error(`\n已达上限 ${exploreMaxUsers} 个用户,停止处理`);
358
- break;
359
- }
360
- continue;
361
- }
362
-
363
- if (result.error) {
364
- consecutiveNetworkErrors++;
365
- errorCount++;
366
- await apiPost(`${serverUrl}/api/job/${username}`, {
367
- error: result.error,
368
- });
369
- const errorType = result.error.startsWith("被封:")
370
- ? "被封"
371
- : consecutiveNetworkErrors > 1
372
- ? "network"
373
- : "other";
374
- await withRetry("report error", () =>
375
- apiPost(`${serverUrl}/api/error-report`, {
376
- userId,
377
- username,
378
- errorType,
379
- errorMessage: result.error,
380
- stage: "process",
381
- errorStack: result.errorStack || "",
382
- }),
383
- ).catch(() => {});
384
- if (errorType === "被封") {
385
- await handleAccountSwitch("账号被封");
386
- continue;
387
- }
388
- if (exploreMaxUsers > 0 && processedCount >= exploreMaxUsers) {
389
- console.error(`\n已达上限 ${exploreMaxUsers} 个用户,停止处理`);
390
- break;
391
- }
392
- continue;
393
- }
394
-
395
- if (result.captchaDetected) {
396
- captchaCount++;
397
- console.error(` [验证码] 累计 ${captchaCount} 次`);
398
-
399
- await withRetry("report captcha", () =>
400
- apiPost(`${serverUrl}/api/error-report`, {
401
- userId,
402
- username,
403
- errorType: "captcha",
404
- errorMessage: result.captchaMessage || "页面出现验证码",
405
- stage: result.captchaStage || "video-page",
406
- errorStack: "",
407
- }),
408
- ).catch(() => {});
409
-
410
- // 累计2次验证码,切换账户
411
- if (captchaCount >= 2) {
412
- await handleAccountSwitch(`验证码累计 ${captchaCount} 次`);
413
- captchaCount = 0;
414
- }
415
- }
416
-
417
- consecutiveNetworkErrors = 0;
418
-
419
- // 更新关注/粉丝成功时间(仅登录状态下)
420
- if (result.hasFollowData && result.keepFollow) {
421
- const totalFollows =
422
- (result.discoveredFollowing || []).length +
423
- (result.discoveredFollowers || []).length;
424
- if (totalFollows > 0) {
425
- lastFollowSuccessTime = Date.now();
426
- }
427
- }
428
-
429
- const guessedLocation = result.locationCreated || null;
430
-
431
- const payload = {
432
- userInfo: result.userInfo || {},
433
- discoveredFollowing: (result.discoveredFollowing || []).map((f) => ({
434
- handle: Array.isArray(f) ? f[0] : f,
435
- displayName: Array.isArray(f) ? f[1] : null,
436
- guessedLocation,
437
- })),
438
- discoveredFollowers: (result.discoveredFollowers || []).map((f) => ({
439
- handle: Array.isArray(f) ? f[0] : f,
440
- displayName: Array.isArray(f) ? f[1] : null,
441
- guessedLocation,
442
- })),
443
- processed: result.processed,
444
- hasFollowData: result.hasFollowData,
445
- keepFollow: result.keepFollow,
446
- locationCreated: result.locationCreated,
447
- noVideo: result.noVideo,
448
- collectedVideos: result.collectedVideos,
449
- };
450
- await apiPost(`${serverUrl}/api/job/${username}`, payload);
451
-
452
- // 视频登记
453
- if (result.videoList && result.videoList.length > 0) {
454
- const { registered, skipped } = await apiPost(`${serverUrl}/api/videos`, {
455
- sourceUser: username,
456
- videoList: result.videoList,
457
- locationCreated: result.locationCreated,
458
- ttSeller: result.userInfo?.ttSeller || false,
459
- });
460
- console.error(` 视频登记: ${registered} 条新增, ${skipped} 条已存在`);
461
- }
462
-
463
- console.error(" 已提交");
464
-
465
- if (exploreMaxUsers > 0 && processedCount >= exploreMaxUsers) {
466
- console.error(`\n已达上限 ${exploreMaxUsers} 个用户,停止处理`);
467
- break;
468
- }
469
- }
470
-
471
- const stats = await apiGet(`${serverUrl}/api/stats`);
472
- console.error(`\n完成: ${processedCount} 个用户处理, ${errorCount} 个出错`);
473
- console.error(
474
- ` 总用户: ${stats.totalUsers}, 已完成: ${stats.processedUsers}, 待处理: ${stats.pendingUsers}, 错误: ${stats.errorUsers}`,
475
- );
476
-
477
- await browser.close().catch(() => {});
478
- }
1
+ import {
2
+ getOrCreatePage,
3
+ isBrowserClosedError,
4
+ relaunchBrowser,
5
+ } from "../lib/browser/page.js";
6
+ import {
7
+ delay,
8
+ getDelayConfig,
9
+ setDelayConfig,
10
+ } from "../scraper/modules/page-helpers.js";
11
+ import {
12
+ detectCaptcha,
13
+ closeCaptcha,
14
+ } from "../scraper/modules/captcha-handler.js";
15
+ import { userId as configuredUserId, saveUserId } from "../lib/constants.js";
16
+ import { getMacOrUuid } from "../lib/mac-or-uuid.js";
17
+ import {
18
+ ensureBrowserReady as ensureBrowserReadyCDP,
19
+ switchAccount,
20
+ } from "../lib/browser/cdp.js";
21
+ import { showResourceUsage } from "../utils/index.js";
22
+ import { HealthChecker } from "../lib/browser/health-checker.js";
23
+ import path from "path";
24
+ import os from "os";
25
+
26
+ const MAX_RETRY_WAIT = 5 * 60 * 1000;
27
+
28
+ async function withRetry(label, fn) {
29
+ let backoff = 1000;
30
+ while (true) {
31
+ try {
32
+ return await fn();
33
+ } catch (err) {
34
+ console.error(
35
+ `[连接] ${label} 失败: ${err.message},${backoff / 1000}秒后重试...`,
36
+ );
37
+ await new Promise((r) => setTimeout(r, backoff));
38
+ if (backoff < MAX_RETRY_WAIT) backoff *= 2;
39
+ }
40
+ }
41
+ }
42
+
43
+ async function apiPost(url, body) {
44
+ return withRetry(`POST ${url}`, async () => {
45
+ const res = await fetch(url, {
46
+ method: "POST",
47
+ headers: { "Content-Type": "application/json" },
48
+ body: JSON.stringify(body),
49
+ });
50
+ return res.json();
51
+ });
52
+ }
53
+
54
+ async function apiGet(url) {
55
+ return withRetry(`GET ${url}`, async () => {
56
+ const res = await fetch(url);
57
+ return res.json();
58
+ });
59
+ }
60
+
61
+ export async function handleExplore(options) {
62
+ const {
63
+ exploreUsernames,
64
+ explorePreset,
65
+ exploreMaxComments,
66
+ exploreMaxGuess,
67
+ exploreEnableFollow,
68
+ exploreMaxFollowing,
69
+ exploreMaxFollowers,
70
+ exploreLocation,
71
+ exploreJobLocations,
72
+ exploreMaxUsers,
73
+ serverUrl,
74
+ explorePort,
75
+ exploreBasePort,
76
+ explorePortCount,
77
+ exploreUserId,
78
+ exploreMaxVideos,
79
+ } = options;
80
+
81
+ let userId = exploreUserId || configuredUserId;
82
+ if (!userId) {
83
+ userId = await getMacOrUuid();
84
+ saveUserId(userId);
85
+ console.error(`[初始化] 未检测到本地用户编号,已生成并使用: ${userId}`);
86
+ }
87
+
88
+ setDelayConfig(explorePreset);
89
+
90
+ await apiGet(`${serverUrl}/api/stats`);
91
+
92
+ if (exploreUsernames && exploreUsernames.length > 0) {
93
+ const { added, skipped } = await apiPost(`${serverUrl}/api/users`, {
94
+ usernames: exploreUsernames,
95
+ });
96
+ console.error(`种子用户: ${added} 个新增, ${skipped} 个已存在`);
97
+ }
98
+
99
+ console.error(`\n国家筛选: ${exploreLocation}`);
100
+ if (exploreJobLocations) console.error(`任务国家: ${exploreJobLocations}`);
101
+ console.error(`视频采集: ${exploreMaxVideos || 1}`);
102
+ console.error(`关注/粉丝: ${exploreEnableFollow ? "启用" : "禁用"}`);
103
+ console.error(`服务器: ${serverUrl}(断开会自动重连)`);
104
+ if (exploreMaxUsers > 0) console.error(`上限: ${exploreMaxUsers} 个用户`);
105
+
106
+ const healthChecker = new HealthChecker({
107
+ basePort: exploreBasePort,
108
+ totalAccounts: explorePortCount,
109
+ });
110
+ let currentAccount = healthChecker.getCurrentAccount();
111
+
112
+ const cdpOptions = {};
113
+ if (explorePort) {
114
+ // 固定端口模式(调试用,关闭自动轮换)
115
+ cdpOptions.port = explorePort;
116
+ cdpOptions.userDataDir = path.join(
117
+ os.homedir(),
118
+ "Library",
119
+ "Application Support",
120
+ `Microsoft Edge For Testing_p${explorePort}`,
121
+ );
122
+ } else {
123
+ cdpOptions.port = currentAccount.port;
124
+ cdpOptions.userDataDir = currentAccount.userDataDir;
125
+ }
126
+
127
+ console.error(`CDP 端口: ${cdpOptions.port}, 用户编号: ${userId}`);
128
+ console.error(`浏览器配置: ${path.basename(cdpOptions.userDataDir)}`);
129
+ if (!explorePort) {
130
+ const portRange = `${currentAccount.port}-${currentAccount.port + healthChecker.accounts.length - 1}`;
131
+ console.error(
132
+ `端口轮换范围: ${portRange}(共 ${healthChecker.accounts.length} 个)`,
133
+ );
134
+ }
135
+
136
+ let browser = await ensureBrowserReadyCDP(cdpOptions);
137
+ const { processExplore } = await import("../scraper/explore-core.js");
138
+ const { isLoggedIn } = await import("../lib/browser/page.js");
139
+
140
+ const page = await getOrCreatePage(browser);
141
+
142
+ // 先导航到 TikTok 首页,再检测登录状态
143
+ await page.goto("https://www.tiktok.com", { waitUntil: "domcontentloaded" });
144
+
145
+ // 检测登录状态(启动时只检测一次)
146
+ let loggedIn = await isLoggedIn(page);
147
+ console.error(`登录状态: ${loggedIn ? "已登录" : "未登录"}`);
148
+
149
+ // 全局拦截图片资源,减少内存占用和加载时间
150
+ await page.route("**/*", (route) => {
151
+ const resourceType = route.request().resourceType();
152
+ if (resourceType === "image" || resourceType === "stylesheet") {
153
+ route.abort();
154
+ } else {
155
+ route.continue();
156
+ }
157
+ });
158
+
159
+ // 账户切换后,重新初始化 page
160
+ async function setupNewPage(newBrowser) {
161
+ const newPage = await getOrCreatePage(newBrowser);
162
+ await newPage.route("**/*", (route) => {
163
+ const resourceType = route.request().resourceType();
164
+ if (resourceType === "image" || resourceType === "stylesheet") {
165
+ route.abort();
166
+ } else {
167
+ route.continue();
168
+ }
169
+ });
170
+ return newPage;
171
+ }
172
+
173
+ async function handleAccountSwitch(reason) {
174
+ console.error(`\n[健康检查] ${reason},切换到下一个账户...`);
175
+ const oldAccount = currentAccount;
176
+ const nextAccount = healthChecker.getNextAccount();
177
+ currentAccount = nextAccount;
178
+ const newBrowser = await switchAccount(
179
+ { port: oldAccount.port, userDataDir: oldAccount.userDataDir },
180
+ { port: nextAccount.port, userDataDir: nextAccount.userDataDir },
181
+ );
182
+ browser = newBrowser;
183
+ const newPage = await setupNewPage(browser);
184
+ Object.assign(page, newPage);
185
+ Object.assign(cdpOptions, {
186
+ port: nextAccount.port,
187
+ userDataDir: nextAccount.userDataDir,
188
+ });
189
+ console.error(`[健康检查] 已切换到端口 ${nextAccount.port}`);
190
+ // 切换账户后先导航到 TikTok,再重新检测登录状态
191
+ await page.goto("https://www.tiktok.com", {
192
+ waitUntil: "domcontentloaded",
193
+ });
194
+ loggedIn = await isLoggedIn(page);
195
+ console.error(
196
+ `[健康检查] 新账户登录状态: ${loggedIn ? "已登录" : "未登录"}`,
197
+ );
198
+ lastFollowSuccessTime = Date.now(); // 切换账户后重置
199
+ captchaCount = 0; // 重置验证码计数
200
+ blockedCount = 0; // 重置被封计数
201
+ consecutiveNetworkErrors = 0; // 重置网络错误计数
202
+ }
203
+
204
+ let processedCount = 0;
205
+ let errorCount = 0;
206
+ let consecutiveNetworkErrors = 0;
207
+ let captchaCount = 0; // 验证码累计计数
208
+ let blockedCount = 0; // 被封累计计数
209
+ let lastFollowSuccessTime = Date.now(); // 最近一次成功获取关注/粉丝的时间
210
+ const FOLLOW_BLOCK_THRESHOLD = 10 * 60 * 1000; // 10分钟
211
+
212
+ while (true) {
213
+ const jobQuery = exploreJobLocations
214
+ ? `${serverUrl}/api/job?userId=${encodeURIComponent(userId)}&locations=${encodeURIComponent(exploreJobLocations)}`
215
+ : `${serverUrl}/api/job?userId=${encodeURIComponent(userId)}`;
216
+ const job = await apiGet(jobQuery);
217
+ if (!job.hasJob) break;
218
+
219
+ const username = job.user.uniqueId;
220
+ processedCount++;
221
+
222
+ if (processedCount % 10 === 0) {
223
+ showResourceUsage();
224
+ }
225
+
226
+ const checkResult = healthChecker.check();
227
+ if (checkResult.shouldSwitch) {
228
+ await handleAccountSwitch(checkResult.reason);
229
+ } else if (checkResult.info && processedCount % 10 === 0) {
230
+ // 关注/粉丝封禁检测状态(仅登录状态)
231
+ let followStatus = "";
232
+ if (loggedIn && exploreEnableFollow) {
233
+ const followElapsed = Math.round(
234
+ (Date.now() - lastFollowSuccessTime) / 60000,
235
+ );
236
+ const followRemaining = Math.max(
237
+ 0,
238
+ Math.round(FOLLOW_BLOCK_THRESHOLD / 60000) - followElapsed,
239
+ );
240
+ followStatus = ` | 关注/粉丝上次成功 ${followElapsed} 分钟前 | 封禁检测 ${followRemaining} 分钟后`;
241
+ }
242
+ console.error(`\n[健康检查] ${checkResult.info}${followStatus}`);
243
+ }
244
+
245
+ // 切换任务前检测验证码
246
+ const switchCaptcha = await detectCaptcha(page);
247
+ if (switchCaptcha && switchCaptcha.visible) {
248
+ console.error(`\n[验证码] 切换任务前检测到验证码,等待3分钟...`);
249
+ captchaCount++;
250
+ console.error(` [验证码] 累计 ${captchaCount} 次`);
251
+
252
+ // 等待3分钟
253
+ await new Promise((r) => setTimeout(r, 3 * 60 * 1000));
254
+
255
+ // 尝试关闭
256
+ const closeResult = await closeCaptcha(page);
257
+ if (closeResult.success) {
258
+ console.error(` [验证码] 已关闭`);
259
+ } else {
260
+ console.error(` [验证码] 关闭失败: ${closeResult.reason}`);
261
+ }
262
+
263
+ // 上报
264
+ await withRetry("report captcha", () =>
265
+ apiPost(`${serverUrl}/api/error-report`, {
266
+ userId,
267
+ username: "unknown",
268
+ errorType: "captcha",
269
+ errorMessage: "切换任务前检测到验证码",
270
+ stage: "between-tasks",
271
+ errorStack: "",
272
+ }),
273
+ ).catch(() => {});
274
+
275
+ // 累计2次切换账户
276
+ if (captchaCount >= 2) {
277
+ await handleAccountSwitch(`验证码累计 ${captchaCount} 次`);
278
+ captchaCount = 0;
279
+ }
280
+ }
281
+
282
+ // 关注/粉丝封禁检测移到健康检查中(仅登录状态下)
283
+ if (loggedIn && exploreEnableFollow) {
284
+ const followElapsed = Date.now() - lastFollowSuccessTime;
285
+ if (followElapsed > FOLLOW_BLOCK_THRESHOLD) {
286
+ const minutes = Math.round(followElapsed / 60000);
287
+ console.error(
288
+ `\n[封禁检测] 关注/粉丝功能已 ${minutes} 分钟未成功获取,切换到下一个账户...`,
289
+ );
290
+ await handleAccountSwitch(`关注/粉丝功能被封`);
291
+ }
292
+ }
293
+
294
+ if (consecutiveNetworkErrors > 0) {
295
+ const waitTime =
296
+ consecutiveNetworkErrors <= 2
297
+ ? 0
298
+ : consecutiveNetworkErrors <= 5
299
+ ? 30000
300
+ : 300000;
301
+ if (waitTime > 0) {
302
+ console.error(
303
+ ` [网络] 连续 ${consecutiveNetworkErrors} 次网络异常,等待 ${waitTime / 1000}s 后重试...`,
304
+ );
305
+ await new Promise((r) => setTimeout(r, waitTime));
306
+ }
307
+ }
308
+
309
+ console.error(`\n[${processedCount}] 探索 @${username}...`);
310
+
311
+ const { switchMax } = getDelayConfig();
312
+ await delay(switchMax, switchMax * 3);
313
+
314
+ let result = await processExplore(
315
+ page,
316
+ username,
317
+ {
318
+ maxVideos: exploreMaxVideos,
319
+ enableFollow: exploreEnableFollow,
320
+ loggedIn, // 传入登录状态,避免每次调用 isLoggedIn(page)
321
+ maxFollowing: exploreMaxFollowing,
322
+ maxFollowers: exploreMaxFollowers,
323
+ location: exploreLocation,
324
+ browser,
325
+ },
326
+ console.error,
327
+ );
328
+
329
+ // 浏览器关闭检测:processExplore 内部 catch 了异常,需要从 result.error 判断
330
+ if (result.error && isBrowserClosedError(new Error(result.error))) {
331
+ const newBrowser = await relaunchBrowser(
332
+ cdpOptions,
333
+ cdpOptions.port || 9222,
334
+ );
335
+ browser = newBrowser;
336
+ const newPage = await setupNewPage(browser);
337
+ Object.assign(page, newPage);
338
+ // 重试当前用户
339
+ result = await processExplore(
340
+ page,
341
+ username,
342
+ {
343
+ maxVideos: exploreMaxVideos,
344
+ enableFollow: exploreEnableFollow,
345
+ loggedIn, // 传入登录状态
346
+ maxFollowing: exploreMaxFollowing,
347
+ maxFollowers: exploreMaxFollowers,
348
+ location: exploreLocation,
349
+ browser,
350
+ },
351
+ console.error,
352
+ );
353
+ }
354
+
355
+ if (result.restricted) {
356
+ consecutiveNetworkErrors = 0;
357
+ await apiPost(`${serverUrl}/api/job/${username}`, {
358
+ restricted: true,
359
+ userInfo: result.userInfo || {},
360
+ });
361
+ if (exploreMaxUsers > 0 && processedCount >= exploreMaxUsers) {
362
+ console.error(`\n已达上限 ${exploreMaxUsers} 个用户,停止处理`);
363
+ break;
364
+ }
365
+ continue;
366
+ }
367
+
368
+ if (result.error) {
369
+ consecutiveNetworkErrors++;
370
+ errorCount++;
371
+ await apiPost(`${serverUrl}/api/job/${username}`, {
372
+ error: result.error,
373
+ });
374
+ const errorType = result.error.startsWith("被封:")
375
+ ? "被封"
376
+ : consecutiveNetworkErrors > 1
377
+ ? "network"
378
+ : "other";
379
+ await withRetry("report error", () =>
380
+ apiPost(`${serverUrl}/api/error-report`, {
381
+ userId,
382
+ username,
383
+ errorType,
384
+ errorMessage: result.error,
385
+ stage: "process",
386
+ errorStack: result.errorStack || "",
387
+ }),
388
+ ).catch(() => {});
389
+ if (errorType === "被封") {
390
+ blockedCount++;
391
+ console.error(` [被封] 累计 ${blockedCount} 次`);
392
+ if (blockedCount >= 3) {
393
+ await handleAccountSwitch(`账号被封累计 ${blockedCount} 次`);
394
+ blockedCount = 0;
395
+ }
396
+ continue;
397
+ }
398
+ if (exploreMaxUsers > 0 && processedCount >= exploreMaxUsers) {
399
+ console.error(`\n已达上限 ${exploreMaxUsers} 个用户,停止处理`);
400
+ break;
401
+ }
402
+ continue;
403
+ }
404
+
405
+ if (result.captchaDetected) {
406
+ captchaCount++;
407
+ console.error(` [验证码] 累计 ${captchaCount} 次`);
408
+
409
+ await withRetry("report captcha", () =>
410
+ apiPost(`${serverUrl}/api/error-report`, {
411
+ userId,
412
+ username,
413
+ errorType: "captcha",
414
+ errorMessage: result.captchaMessage || "页面出现验证码",
415
+ stage: result.captchaStage || "video-page",
416
+ errorStack: "",
417
+ }),
418
+ ).catch(() => {});
419
+
420
+ // 累计2次验证码,切换账户
421
+ if (captchaCount >= 2) {
422
+ await handleAccountSwitch(`验证码累计 ${captchaCount} 次`);
423
+ captchaCount = 0;
424
+ }
425
+ }
426
+
427
+ consecutiveNetworkErrors = 0;
428
+
429
+ // 更新关注/粉丝成功时间(仅登录状态下)
430
+ if (result.hasFollowData && result.keepFollow) {
431
+ const totalFollows =
432
+ (result.discoveredFollowing || []).length +
433
+ (result.discoveredFollowers || []).length;
434
+ if (totalFollows > 0) {
435
+ lastFollowSuccessTime = Date.now();
436
+ }
437
+ }
438
+
439
+ const guessedLocation = result.locationCreated || null;
440
+
441
+ const payload = {
442
+ userInfo: result.userInfo || {},
443
+ discoveredFollowing: (result.discoveredFollowing || []).map((f) => ({
444
+ handle: Array.isArray(f) ? f[0] : f,
445
+ displayName: Array.isArray(f) ? f[1] : null,
446
+ guessedLocation,
447
+ })),
448
+ discoveredFollowers: (result.discoveredFollowers || []).map((f) => ({
449
+ handle: Array.isArray(f) ? f[0] : f,
450
+ displayName: Array.isArray(f) ? f[1] : null,
451
+ guessedLocation,
452
+ })),
453
+ processed: result.processed,
454
+ hasFollowData: result.hasFollowData,
455
+ keepFollow: result.keepFollow,
456
+ locationCreated: result.locationCreated,
457
+ noVideo: result.noVideo,
458
+ collectedVideos: result.collectedVideos,
459
+ };
460
+ await apiPost(`${serverUrl}/api/job/${username}`, payload);
461
+
462
+ // 视频登记
463
+ if (result.videoList && result.videoList.length > 0) {
464
+ const { registered, skipped } = await apiPost(`${serverUrl}/api/videos`, {
465
+ sourceUser: username,
466
+ videoList: result.videoList,
467
+ locationCreated: result.locationCreated,
468
+ ttSeller: result.userInfo?.ttSeller || false,
469
+ });
470
+ console.error(` 视频登记: ${registered} 条新增, ${skipped} 条已存在`);
471
+ }
472
+
473
+ console.error(" 已提交");
474
+
475
+ if (exploreMaxUsers > 0 && processedCount >= exploreMaxUsers) {
476
+ console.error(`\n已达上限 ${exploreMaxUsers} 个用户,停止处理`);
477
+ break;
478
+ }
479
+ }
480
+
481
+ const stats = await apiGet(`${serverUrl}/api/stats`);
482
+ console.error(`\n完成: ${processedCount} 个用户处理, ${errorCount} 个出错`);
483
+ console.error(
484
+ ` 总用户: ${stats.totalUsers}, 已完成: ${stats.processedUsers}, 待处理: ${stats.pendingUsers}, 错误: ${stats.errorUsers}`,
485
+ );
486
+
487
+ await browser.close().catch(() => {});
488
+ }