tt-help-cli-ycl 1.3.35 → 1.3.36

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.
@@ -79,87 +79,100 @@ export async function handleExplore(options) {
79
79
  } = options;
80
80
 
81
81
  let userId = exploreUserId || configuredUserId;
82
- if (!userId) {
83
- userId = await getMacOrUuid();
84
- saveUserId(userId);
85
- console.error(`[初始化] 未检测到本地用户编号,已生成并使用: ${userId}`);
86
- }
82
+ let browser = null;
83
+ let shuttingDown = false;
84
+
85
+ const shutdown = async (signal) => {
86
+ if (shuttingDown) return;
87
+ shuttingDown = true;
88
+ console.error(`\n[Explore] 收到 ${signal},正在关闭浏览器...`);
89
+ await browser?.close().catch(() => {});
90
+ console.error("[Explore] 已退出");
91
+ process.exit(0);
92
+ };
93
+
94
+ const onSigint = () => {
95
+ void shutdown("SIGINT");
96
+ };
97
+ const onSigterm = () => {
98
+ void shutdown("SIGTERM");
99
+ };
100
+
101
+ process.once("SIGINT", onSigint);
102
+ process.once("SIGTERM", onSigterm);
103
+
104
+ try {
105
+ if (!userId) {
106
+ userId = await getMacOrUuid();
107
+ saveUserId(userId);
108
+ console.error(`[初始化] 未检测到本地用户编号,已生成并使用: ${userId}`);
109
+ }
87
110
 
88
- setDelayConfig(explorePreset);
111
+ setDelayConfig(explorePreset);
89
112
 
90
- await apiGet(`${serverUrl}/api/stats`);
113
+ await apiGet(`${serverUrl}/api/stats`);
91
114
 
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
- }
115
+ if (exploreUsernames && exploreUsernames.length > 0) {
116
+ const { added, skipped } = await apiPost(`${serverUrl}/api/users`, {
117
+ usernames: exploreUsernames,
118
+ });
119
+ console.error(`种子用户: ${added} 个新增, ${skipped} 个已存在`);
120
+ }
98
121
 
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} 个用户`);
122
+ console.error(`\n国家筛选: ${exploreLocation}`);
123
+ if (exploreJobLocations) console.error(`任务国家: ${exploreJobLocations}`);
124
+ console.error(`视频采集: ${exploreMaxVideos || 1}`);
125
+ console.error(`关注/粉丝: ${exploreEnableFollow ? "启用" : "禁用"}`);
126
+ console.error(`服务器: ${serverUrl}(断开会自动重连)`);
127
+ if (exploreMaxUsers > 0) console.error(`上限: ${exploreMaxUsers} 个用户`);
105
128
 
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
- }
129
+ const healthChecker = new HealthChecker({
130
+ basePort: exploreBasePort,
131
+ totalAccounts: explorePortCount,
132
+ });
133
+ let currentAccount = healthChecker.getCurrentAccount();
134
+
135
+ const cdpOptions = {};
136
+ if (explorePort) {
137
+ // 固定端口模式(调试用,关闭自动轮换)
138
+ cdpOptions.port = explorePort;
139
+ cdpOptions.userDataDir = path.join(
140
+ os.homedir(),
141
+ "Library",
142
+ "Application Support",
143
+ `Microsoft Edge For Testing_p${explorePort}`,
144
+ );
145
+ } else {
146
+ cdpOptions.port = currentAccount.port;
147
+ cdpOptions.userDataDir = currentAccount.userDataDir;
148
+ }
135
149
 
136
- let browser = await ensureBrowserReadyCDP(cdpOptions);
137
- const { processExplore } = await import("../scraper/explore-core.js");
138
- const { isLoggedIn } = await import("../lib/browser/page.js");
150
+ console.error(`CDP 端口: ${cdpOptions.port}, 用户编号: ${userId}`);
151
+ console.error(`浏览器配置: ${path.basename(cdpOptions.userDataDir)}`);
152
+ if (!explorePort) {
153
+ const portRange = `${currentAccount.port}-${currentAccount.port + healthChecker.accounts.length - 1}`;
154
+ console.error(
155
+ `端口轮换范围: ${portRange}(共 ${healthChecker.accounts.length} 个)`,
156
+ );
157
+ }
139
158
 
140
- const page = await getOrCreatePage(browser);
159
+ browser = await ensureBrowserReadyCDP(cdpOptions);
160
+ const { processExplore } = await import("../scraper/explore-core.js");
161
+ const { isLoggedIn } = await import("../lib/browser/page.js");
141
162
 
142
- // 先导航到 TikTok 首页,再检测登录状态
143
- await page.goto("https://www.tiktok.com", { waitUntil: "domcontentloaded" });
163
+ const page = await getOrCreatePage(browser);
144
164
 
145
- // 检测登录状态(启动时只检测一次)
146
- let loggedIn = await isLoggedIn(page);
147
- console.error(`登录状态: ${loggedIn ? "已登录" : "未登录"}`);
165
+ // 先导航到 TikTok 首页,再检测登录状态
166
+ await page.goto("https://www.tiktok.com", {
167
+ waitUntil: "domcontentloaded",
168
+ });
148
169
 
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
- });
170
+ // 检测登录状态(启动时只检测一次)
171
+ let loggedIn = await isLoggedIn(page);
172
+ console.error(`登录状态: ${loggedIn ? "已登录" : "未登录"}`);
158
173
 
159
- // 账户切换后,重新初始化 page
160
- async function setupNewPage(newBrowser) {
161
- const newPage = await getOrCreatePage(newBrowser);
162
- await newPage.route("**/*", (route) => {
174
+ // 全局拦截图片资源,减少内存占用和加载时间
175
+ await page.route("**/*", (route) => {
163
176
  const resourceType = route.request().resourceType();
164
177
  if (resourceType === "image" || resourceType === "stylesheet") {
165
178
  route.abort();
@@ -167,182 +180,169 @@ export async function handleExplore(options) {
167
180
  route.continue();
168
181
  }
169
182
  });
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
183
 
212
- while (true) {
213
- const jobQuery = exploreJobLocations
214
- ? `${serverUrl}/api/job?userId=${encodeURIComponent(userId)}&locations=${encodeURIComponent(exploreJobLocations)}&loggedIn=${loggedIn}`
215
- : `${serverUrl}/api/job?userId=${encodeURIComponent(userId)}&loggedIn=${loggedIn}`;
216
- const job = await apiGet(jobQuery);
217
- if (!job.hasJob) break;
218
-
219
- const username = job.user.uniqueId;
220
- processedCount++;
184
+ // 账户切换后,重新初始化 page
185
+ async function setupNewPage(newBrowser) {
186
+ const newPage = await getOrCreatePage(newBrowser);
187
+ await newPage.route("**/*", (route) => {
188
+ const resourceType = route.request().resourceType();
189
+ if (resourceType === "image" || resourceType === "stylesheet") {
190
+ route.abort();
191
+ } else {
192
+ route.continue();
193
+ }
194
+ });
195
+ return newPage;
196
+ }
221
197
 
222
- if (processedCount % 10 === 0) {
223
- showResourceUsage();
198
+ async function handleAccountSwitch(reason) {
199
+ console.error(`\n[健康检查] ${reason},切换到下一个账户...`);
200
+ const oldAccount = currentAccount;
201
+ const nextAccount = healthChecker.getNextAccount();
202
+ currentAccount = nextAccount;
203
+ const newBrowser = await switchAccount(
204
+ { port: oldAccount.port, userDataDir: oldAccount.userDataDir },
205
+ { port: nextAccount.port, userDataDir: nextAccount.userDataDir },
206
+ );
207
+ browser = newBrowser;
208
+ const newPage = await setupNewPage(browser);
209
+ Object.assign(page, newPage);
210
+ Object.assign(cdpOptions, {
211
+ port: nextAccount.port,
212
+ userDataDir: nextAccount.userDataDir,
213
+ });
214
+ console.error(`[健康检查] 已切换到端口 ${nextAccount.port}`);
215
+ // 切换账户后先导航到 TikTok,再重新检测登录状态
216
+ await page.goto("https://www.tiktok.com", {
217
+ waitUntil: "domcontentloaded",
218
+ });
219
+ loggedIn = await isLoggedIn(page);
220
+ console.error(
221
+ `[健康检查] 新账户登录状态: ${loggedIn ? "已登录" : "未登录"}`,
222
+ );
223
+ lastFollowSuccessTime = Date.now(); // 切换账户后重置
224
+ captchaCount = 0; // 重置验证码计数
225
+ blockedCount = 0; // 重置被封计数
226
+ consecutiveNetworkErrors = 0; // 重置网络错误计数
224
227
  }
225
228
 
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} 分钟后`;
229
+ let processedCount = 0;
230
+ let errorCount = 0;
231
+ let consecutiveNetworkErrors = 0;
232
+ let captchaCount = 0; // 验证码累计计数
233
+ let blockedCount = 0; // 被封累计计数
234
+ let lastFollowSuccessTime = Date.now(); // 最近一次成功获取关注/粉丝的时间
235
+ const FOLLOW_BLOCK_THRESHOLD = 10 * 60 * 1000; // 10分钟
236
+
237
+ while (!shuttingDown) {
238
+ const jobQuery = exploreJobLocations
239
+ ? `${serverUrl}/api/job?userId=${encodeURIComponent(userId)}&locations=${encodeURIComponent(exploreJobLocations)}&loggedIn=${loggedIn}`
240
+ : `${serverUrl}/api/job?userId=${encodeURIComponent(userId)}&loggedIn=${loggedIn}`;
241
+ const job = await apiGet(jobQuery);
242
+ if (!job.hasJob) break;
243
+
244
+ const username = job.user.uniqueId;
245
+ processedCount++;
246
+
247
+ if (processedCount % 10 === 0) {
248
+ showResourceUsage();
241
249
  }
242
- console.error(`\n[健康检查] ${checkResult.info}${followStatus}`);
243
- }
244
250
 
245
- // 切换任务前检测验证码
246
- const switchCaptcha = await detectCaptcha(page);
247
- if (switchCaptcha && switchCaptcha.visible) {
248
- console.error(`\n[验证码] 切换任务前检测到验证码,等待3分钟...`);
249
- captchaCount++;
250
- console.error(` [验证码] 累计 ${captchaCount} 次`);
251
+ const checkResult = healthChecker.check();
252
+ if (checkResult.shouldSwitch) {
253
+ await handleAccountSwitch(checkResult.reason);
254
+ } else if (checkResult.info && processedCount % 10 === 0) {
255
+ // 关注/粉丝封禁检测状态(仅登录状态)
256
+ let followStatus = "";
257
+ if (loggedIn && exploreEnableFollow) {
258
+ const followElapsed = Math.round(
259
+ (Date.now() - lastFollowSuccessTime) / 60000,
260
+ );
261
+ const followRemaining = Math.max(
262
+ 0,
263
+ Math.round(FOLLOW_BLOCK_THRESHOLD / 60000) - followElapsed,
264
+ );
265
+ followStatus = ` | 关注/粉丝上次成功 ${followElapsed} 分钟前 | 封禁检测 ${followRemaining} 分钟后`;
266
+ }
267
+ console.error(`\n[健康检查] ${checkResult.info}${followStatus}`);
268
+ }
251
269
 
252
- // 等待3分钟
253
- await new Promise((r) => setTimeout(r, 3 * 60 * 1000));
270
+ // 切换任务前检测验证码
271
+ const switchCaptcha = await detectCaptcha(page);
272
+ if (switchCaptcha && switchCaptcha.visible) {
273
+ console.error(`\n[验证码] 切换任务前检测到验证码,等待3分钟...`);
274
+ captchaCount++;
275
+ console.error(` [验证码] 累计 ${captchaCount} 次`);
276
+
277
+ // 等待3分钟
278
+ await new Promise((r) => setTimeout(r, 3 * 60 * 1000));
279
+
280
+ // 尝试关闭
281
+ const closeResult = await closeCaptcha(page);
282
+ if (closeResult.success) {
283
+ console.error(` [验证码] 已关闭`);
284
+ } else {
285
+ console.error(` [验证码] 关闭失败: ${closeResult.reason}`);
286
+ }
254
287
 
255
- // 尝试关闭
256
- const closeResult = await closeCaptcha(page);
257
- if (closeResult.success) {
258
- console.error(` [验证码] 已关闭`);
259
- } else {
260
- console.error(` [验证码] 关闭失败: ${closeResult.reason}`);
288
+ // 上报
289
+ await withRetry("report captcha", () =>
290
+ apiPost(`${serverUrl}/api/error-report`, {
291
+ userId,
292
+ username: "unknown",
293
+ errorType: "captcha",
294
+ errorMessage: "切换任务前检测到验证码",
295
+ stage: "between-tasks",
296
+ errorStack: "",
297
+ }),
298
+ ).catch(() => {});
299
+
300
+ // 累计2次切换账户
301
+ if (captchaCount >= 2) {
302
+ await handleAccountSwitch(`验证码累计 ${captchaCount} 次`);
303
+ captchaCount = 0;
304
+ }
261
305
  }
262
306
 
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;
307
+ // 关注/粉丝封禁检测移到健康检查中(仅登录状态下)
308
+ if (loggedIn && exploreEnableFollow) {
309
+ const followElapsed = Date.now() - lastFollowSuccessTime;
310
+ if (followElapsed > FOLLOW_BLOCK_THRESHOLD) {
311
+ const minutes = Math.round(followElapsed / 60000);
312
+ console.error(
313
+ `\n[封禁检测] 关注/粉丝功能已 ${minutes} 分钟未成功获取,切换到下一个账户...`,
314
+ );
315
+ await handleAccountSwitch(`关注/粉丝功能被封`);
316
+ }
279
317
  }
280
- }
281
318
 
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(`关注/粉丝功能被封`);
319
+ if (consecutiveNetworkErrors > 0) {
320
+ const waitTime =
321
+ consecutiveNetworkErrors <= 2
322
+ ? 0
323
+ : consecutiveNetworkErrors <= 5
324
+ ? 30000
325
+ : 300000;
326
+ if (waitTime > 0) {
327
+ console.error(
328
+ ` [网络] 连续 ${consecutiveNetworkErrors} 次网络异常,等待 ${waitTime / 1000}s 后重试...`,
329
+ );
330
+ await new Promise((r) => setTimeout(r, waitTime));
331
+ }
291
332
  }
292
- }
293
333
 
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
- }
334
+ console.error(`\n[${processedCount}] 探索 @${username}...`);
308
335
 
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
- );
336
+ const { switchMax } = getDelayConfig();
337
+ await delay(switchMax, switchMax * 3);
328
338
 
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(
339
+ let result = await processExplore(
340
340
  page,
341
341
  username,
342
342
  {
343
343
  maxVideos: exploreMaxVideos,
344
344
  enableFollow: exploreEnableFollow,
345
- loggedIn, // 传入登录状态
345
+ loggedIn, // 传入登录状态,避免每次调用 isLoggedIn(page)
346
346
  maxFollowing: exploreMaxFollowing,
347
347
  maxFollowers: exploreMaxFollowers,
348
348
  location: exploreLocation,
@@ -350,139 +350,170 @@ export async function handleExplore(options) {
350
350
  },
351
351
  console.error,
352
352
  );
353
- }
354
353
 
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;
354
+ // 浏览器关闭检测:processExplore 内部 catch 了异常,需要从 result.error 判断
355
+ if (result.error && isBrowserClosedError(new Error(result.error))) {
356
+ const newBrowser = await relaunchBrowser(
357
+ cdpOptions,
358
+ cdpOptions.port || 9222,
359
+ );
360
+ browser = newBrowser;
361
+ const newPage = await setupNewPage(browser);
362
+ Object.assign(page, newPage);
363
+ // 重试当前用户
364
+ result = await processExplore(
365
+ page,
366
+ username,
367
+ {
368
+ maxVideos: exploreMaxVideos,
369
+ enableFollow: exploreEnableFollow,
370
+ loggedIn, // 传入登录状态
371
+ maxFollowing: exploreMaxFollowing,
372
+ maxFollowers: exploreMaxFollowers,
373
+ location: exploreLocation,
374
+ browser,
375
+ },
376
+ console.error,
377
+ );
364
378
  }
365
- continue;
366
- }
367
379
 
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;
380
+ if (result.restricted) {
381
+ consecutiveNetworkErrors = 0;
382
+ await apiPost(`${serverUrl}/api/job/${username}`, {
383
+ restricted: true,
384
+ userInfo: result.userInfo || {},
385
+ });
386
+ if (exploreMaxUsers > 0 && processedCount >= exploreMaxUsers) {
387
+ console.error(`\n已达上限 ${exploreMaxUsers} 个用户,停止处理`);
388
+ break;
395
389
  }
396
390
  continue;
397
391
  }
398
- if (exploreMaxUsers > 0 && processedCount >= exploreMaxUsers) {
399
- console.error(`\n已达上限 ${exploreMaxUsers} 个用户,停止处理`);
400
- break;
401
- }
402
- continue;
403
- }
404
392
 
405
- if (result.captchaDetected) {
406
- captchaCount++;
407
- console.error(` [验证码] 累计 ${captchaCount} 次`);
393
+ if (result.error) {
394
+ consecutiveNetworkErrors++;
395
+ errorCount++;
396
+ await apiPost(`${serverUrl}/api/job/${username}`, {
397
+ error: result.error,
398
+ });
399
+ const errorType = result.error.startsWith("被封:")
400
+ ? "被封"
401
+ : consecutiveNetworkErrors > 1
402
+ ? "network"
403
+ : "other";
404
+ await withRetry("report error", () =>
405
+ apiPost(`${serverUrl}/api/error-report`, {
406
+ userId,
407
+ username,
408
+ errorType,
409
+ errorMessage: result.error,
410
+ stage: "process",
411
+ errorStack: result.errorStack || "",
412
+ }),
413
+ ).catch(() => {});
414
+ if (errorType === "被封") {
415
+ blockedCount++;
416
+ console.error(` [被封] 累计 ${blockedCount} 次`);
417
+ if (blockedCount >= 3) {
418
+ await handleAccountSwitch(`账号被封累计 ${blockedCount} 次`);
419
+ blockedCount = 0;
420
+ }
421
+ continue;
422
+ }
423
+ if (exploreMaxUsers > 0 && processedCount >= exploreMaxUsers) {
424
+ console.error(`\n已达上限 ${exploreMaxUsers} 个用户,停止处理`);
425
+ break;
426
+ }
427
+ continue;
428
+ }
408
429
 
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;
430
+ if (result.captchaDetected) {
431
+ captchaCount++;
432
+ console.error(` [验证码] 累计 ${captchaCount} 次`);
433
+
434
+ await withRetry("report captcha", () =>
435
+ apiPost(`${serverUrl}/api/error-report`, {
436
+ userId,
437
+ username,
438
+ errorType: "captcha",
439
+ errorMessage: result.captchaMessage || "页面出现验证码",
440
+ stage: result.captchaStage || "video-page",
441
+ errorStack: "",
442
+ }),
443
+ ).catch(() => {});
444
+
445
+ // 累计2次验证码,切换账户
446
+ if (captchaCount >= 2) {
447
+ await handleAccountSwitch(`验证码累计 ${captchaCount} 次`);
448
+ captchaCount = 0;
449
+ }
424
450
  }
425
- }
426
451
 
427
- consecutiveNetworkErrors = 0;
452
+ consecutiveNetworkErrors = 0;
428
453
 
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();
454
+ // 更新关注/粉丝成功时间(仅登录状态下)
455
+ if (result.hasFollowData && result.keepFollow) {
456
+ const totalFollows =
457
+ (result.discoveredFollowing || []).length +
458
+ (result.discoveredFollowers || []).length;
459
+ if (totalFollows > 0) {
460
+ lastFollowSuccessTime = Date.now();
461
+ }
436
462
  }
437
- }
438
463
 
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,
464
+ const guessedLocation = result.locationCreated || null;
465
+
466
+ const payload = {
467
+ userInfo: result.userInfo || {},
468
+ discoveredFollowing: (result.discoveredFollowing || []).map((f) => ({
469
+ handle: Array.isArray(f) ? f[0] : f,
470
+ displayName: Array.isArray(f) ? f[1] : null,
471
+ guessedLocation,
472
+ })),
473
+ discoveredFollowers: (result.discoveredFollowers || []).map((f) => ({
474
+ handle: Array.isArray(f) ? f[0] : f,
475
+ displayName: Array.isArray(f) ? f[1] : null,
476
+ guessedLocation,
477
+ })),
478
+ processed: result.processed,
479
+ hasFollowData: result.hasFollowData,
480
+ keepFollow: result.keepFollow,
467
481
  locationCreated: result.locationCreated,
468
- ttSeller: result.userInfo?.ttSeller || false,
469
- });
470
- console.error(` 视频登记: ${registered} 条新增, ${skipped} 条已存在`);
471
- }
482
+ noVideo: result.noVideo,
483
+ collectedVideos: result.collectedVideos,
484
+ };
485
+ await apiPost(`${serverUrl}/api/job/${username}`, payload);
486
+
487
+ // 视频登记
488
+ if (result.videoList && result.videoList.length > 0) {
489
+ const { registered, skipped } = await apiPost(
490
+ `${serverUrl}/api/videos`,
491
+ {
492
+ sourceUser: username,
493
+ videoList: result.videoList,
494
+ locationCreated: result.locationCreated,
495
+ ttSeller: result.userInfo?.ttSeller || false,
496
+ },
497
+ );
498
+ console.error(` 视频登记: ${registered} 条新增, ${skipped} 条已存在`);
499
+ }
472
500
 
473
- console.error(" 已提交");
501
+ console.error(" 已提交");
474
502
 
475
- if (exploreMaxUsers > 0 && processedCount >= exploreMaxUsers) {
476
- console.error(`\n已达上限 ${exploreMaxUsers} 个用户,停止处理`);
477
- break;
503
+ if (exploreMaxUsers > 0 && processedCount >= exploreMaxUsers) {
504
+ console.error(`\n已达上限 ${exploreMaxUsers} 个用户,停止处理`);
505
+ break;
506
+ }
478
507
  }
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
508
 
487
- await browser.close().catch(() => {});
509
+ const stats = await apiGet(`${serverUrl}/api/stats`);
510
+ console.error(`\n完成: ${processedCount} 个用户处理, ${errorCount} 个出错`);
511
+ console.error(
512
+ ` 总用户: ${stats.totalUsers}, 已完成: ${stats.processedUsers}, 待处理: ${stats.pendingUsers}, 错误: ${stats.errorUsers}`,
513
+ );
514
+ } finally {
515
+ process.removeListener("SIGINT", onSigint);
516
+ process.removeListener("SIGTERM", onSigterm);
517
+ await browser?.close().catch(() => {});
518
+ }
488
519
  }