tt-help-cli-ycl 1.3.82 → 1.3.83

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tt-help-cli-ycl",
3
- "version": "1.3.82",
3
+ "version": "1.3.83",
4
4
  "description": "TikTok user & video data scraper - extract ttSeller, verified, locationCreated from HTML source",
5
5
  "type": "module",
6
6
  "bin": {
@@ -264,10 +264,11 @@ export async function handleExplore(options) {
264
264
  const FOLLOW_BLOCK_THRESHOLD = 10 * 60 * 1000; // 10分钟
265
265
 
266
266
  while (!shuttingDown) {
267
- const jobQuery = exploreJobLocations
268
- ? `${serverUrl}/api/job?userId=${encodeURIComponent(userId)}&locations=${encodeURIComponent(exploreJobLocations)}&loggedIn=${loggedIn}`
269
- : `${serverUrl}/api/job?userId=${encodeURIComponent(userId)}&loggedIn=${loggedIn}`;
270
- const job = await apiGet(jobQuery);
267
+ try {
268
+ const jobQuery = exploreJobLocations
269
+ ? `${serverUrl}/api/job?userId=${encodeURIComponent(userId)}&locations=${encodeURIComponent(exploreJobLocations)}&loggedIn=${loggedIn}`
270
+ : `${serverUrl}/api/job?userId=${encodeURIComponent(userId)}&loggedIn=${loggedIn}`;
271
+ const job = await apiGet(jobQuery);
271
272
  if (!job.hasJob) {
272
273
  console.error(`\n[Explore] 当前无任务,${exploreInterval} 秒后重试...`);
273
274
  await new Promise((r) => setTimeout(r, exploreInterval * 1000));
@@ -569,6 +570,28 @@ export async function handleExplore(options) {
569
570
  console.error(`\n已达上限 ${exploreMaxUsers} 个用户,停止处理`);
570
571
  break;
571
572
  }
573
+ } catch (e) {
574
+ // 浏览器关闭错误:自动重建 browser + page,然后重试当前轮次
575
+ if (isBrowserClosedError(e)) {
576
+ console.error(
577
+ `\n[浏览器] 检测到浏览器关闭 (${e.message}),正在重建...`,
578
+ );
579
+ const newBrowser = await relaunchBrowser(
580
+ cdpOptions,
581
+ cdpOptions.port || 9222,
582
+ );
583
+ browser = newBrowser;
584
+ const newPage = await setupNewPage(browser);
585
+ Object.assign(page, newPage);
586
+ // 重建后等待页面稳定
587
+ await new Promise((r) => setTimeout(r, 3000));
588
+ console.error(`[浏览器] 已重建,继续处理...`);
589
+ continue;
590
+ }
591
+ // 其他未预期错误:打印堆栈并跳过本轮
592
+ console.error(`\n[未捕获错误] ${e.message}`);
593
+ console.error(e.stack || "");
594
+ }
572
595
  }
573
596
 
574
597
  const stats = await apiGet(`${serverUrl}/api/stats`);
@@ -12,35 +12,61 @@ import { delay } from "./delay.js";
12
12
  * @param {function} options.onCaptcha - 验证码检测回调 (page) => Promise<{detected: boolean}>
13
13
  * @returns {Promise<{comments: Array, total: number, captchaDetected: boolean, error: string|null}>}
14
14
  */
15
- async function fetchUserCommentsAPI(page, { maxComments = 100, log = console.log, onCaptcha } = {}) {
15
+ async function fetchUserCommentsAPI(
16
+ page,
17
+ { maxComments = 100, log = console.log, onCaptcha } = {},
18
+ ) {
16
19
  // 先注册 API 拦截器,再点 tab(顺序不能反)
17
20
  let apiResolve = null;
18
21
  let apiRequestUrl = null;
19
- const apiPromise = new Promise(r => { apiResolve = r; });
22
+ const apiPromise = new Promise((r) => {
23
+ apiResolve = r;
24
+ });
20
25
 
21
26
  const handler = async (response) => {
22
27
  const url = response.url();
23
- if (response.status() === 200 && url.includes('/api/comment/list/') && !apiRequestUrl) {
28
+ if (
29
+ response.status() === 200 &&
30
+ url.includes("/api/comment/list/") &&
31
+ !apiRequestUrl
32
+ ) {
24
33
  apiRequestUrl = url;
25
34
  try {
26
- apiResolve(await response.json());
35
+ // 超时保护:response.json() 内部调用 CDP Network.getResponseBody,
36
+ // 当页面刷新/验证码导致响应资源丢失时会挂起,需独立超时控制
37
+ apiResolve(
38
+ await Promise.race([
39
+ response.json(),
40
+ new Promise((_, reject) =>
41
+ setTimeout(
42
+ () => reject(new Error("Response body fetch timeout (60s)")),
43
+ 60000,
44
+ ),
45
+ ),
46
+ ]),
47
+ );
27
48
  } catch (e) {
28
49
  apiResolve(null);
29
50
  }
30
51
  }
31
52
  };
32
53
 
33
- page.on('response', handler);
54
+ page.on("response", handler);
34
55
 
35
56
  try {
36
57
  // 点击评论 tab 触发 API
37
- log(' [API拦截] 点击评论 tab...');
58
+ log(" [API拦截] 点击评论 tab...");
38
59
  const tabs = page.locator('[class*="tabbar-item"]');
39
60
  const commentTab = tabs.filter({ hasText: /评论|Comment/ });
40
61
  const count = await commentTab.count();
41
62
 
42
63
  if (count === 0) {
43
- return { comments: [], total: 0, captchaDetected: false, error: '未找到评论 tab' };
64
+ return {
65
+ comments: [],
66
+ total: 0,
67
+ captchaDetected: false,
68
+ error: "未找到评论 tab",
69
+ };
44
70
  }
45
71
 
46
72
  await commentTab.first().click({ force: true });
@@ -55,7 +81,12 @@ async function fetchUserCommentsAPI(page, { maxComments = 100, log = console.log
55
81
 
56
82
  if (!data || !apiRequestUrl) {
57
83
  log(` [API拦截] 点击评论 tab 后 ${elapsed}ms 未拿到 API 响应`);
58
- return { comments: [], total: 0, captchaDetected: false, error: 'API 超时或未响应' };
84
+ return {
85
+ comments: [],
86
+ total: 0,
87
+ captchaDetected: false,
88
+ error: "API 超时或未响应",
89
+ };
59
90
  }
60
91
 
61
92
  // 验证码检测(API 拿完后检测)
@@ -65,7 +96,7 @@ async function fetchUserCommentsAPI(page, { maxComments = 100, log = console.log
65
96
  const captchaResult = await onCaptcha(page);
66
97
  captchaDetected = !!captchaResult.detected;
67
98
  if (captchaDetected) {
68
- log(' [API拦截] 检测到验证码');
99
+ log(" [API拦截] 检测到验证码");
69
100
  }
70
101
  } catch (e) {
71
102
  log(` [API拦截] 验证码检测异常: ${e.message}`);
@@ -73,10 +104,16 @@ async function fetchUserCommentsAPI(page, { maxComments = 100, log = console.log
73
104
  }
74
105
 
75
106
  const items = data.comments || [];
76
- log(` [API拦截] ${elapsed}ms 后拿到 ${items.length} 条评论 (total: ${data.total || '?'})`);
107
+ log(
108
+ ` [API拦截] ${elapsed}ms 后拿到 ${items.length} 条评论 (total: ${data.total || "?"})`,
109
+ );
77
110
 
78
111
  if (items.length >= maxComments) {
79
- return { comments: items.slice(0, maxComments), total: data.total || 0, captchaDetected };
112
+ return {
113
+ comments: items.slice(0, maxComments),
114
+ total: data.total || 0,
115
+ captchaDetected,
116
+ };
80
117
  }
81
118
 
82
119
  // 翻页
@@ -86,7 +123,10 @@ async function fetchUserCommentsAPI(page, { maxComments = 100, log = console.log
86
123
 
87
124
  while (hasMore && cursor && items.length < maxComments) {
88
125
  pageNum++;
89
- const pageUrl = apiRequestUrl.replace(/cursor=([^&]+)/, `cursor=${cursor}`);
126
+ const pageUrl = apiRequestUrl.replace(
127
+ /cursor=([^&]+)/,
128
+ `cursor=${cursor}`,
129
+ );
90
130
 
91
131
  const pageData = await page.evaluate(async (u) => {
92
132
  try {
@@ -103,7 +143,9 @@ async function fetchUserCommentsAPI(page, { maxComments = 100, log = console.log
103
143
  }
104
144
 
105
145
  const pageComments = pageData.comments || [];
106
- log(` [API拦截] 翻页 ${pageNum}: ${pageComments.length} 条 (累计: ${items.length + pageComments.length})`);
146
+ log(
147
+ ` [API拦截] 翻页 ${pageNum}: ${pageComments.length} 条 (累计: ${items.length + pageComments.length})`,
148
+ );
107
149
 
108
150
  items.push(...pageComments);
109
151
  cursor = pageData.cursor;
@@ -119,7 +161,7 @@ async function fetchUserCommentsAPI(page, { maxComments = 100, log = console.log
119
161
 
120
162
  return { comments: result, total: data.total || 0, captchaDetected };
121
163
  } finally {
122
- page.off('response', handler);
164
+ page.off("response", handler);
123
165
  }
124
166
  }
125
167
 
@@ -134,7 +134,17 @@ async function fetchUserVideosAPI(page, username, maxVideos, log) {
134
134
  { timeout: 30000 },
135
135
  );
136
136
 
137
- data = await response.json();
137
+ // 超时保护:response.json() 内部调用 CDP Network.getResponseBody,
138
+ // 当页面刷新/验证码导致响应资源丢失时会挂起,需独立超时控制
139
+ data = await Promise.race([
140
+ response.json(),
141
+ new Promise((_, reject) =>
142
+ setTimeout(
143
+ () => reject(new Error("Response body fetch timeout (60s)")),
144
+ 60000,
145
+ ),
146
+ ),
147
+ ]);
138
148
  } catch (e) {
139
149
  interceptionError = e.message;
140
150
  } finally {
@@ -1162,7 +1162,7 @@ function renderTargetTable() {
1162
1162
 
1163
1163
  return `<tr data-user="${u.uniqueId}">
1164
1164
  <td style="color:#9ca3af;font-size:12px;text-align:center" data-label="#">${i + 1}</td>
1165
- <td class="user-id" data-label="用户名">@${u.uniqueId}</td>
1165
+ <td class="user-id" data-label="用户名"><a href="https://www.tiktok.com/@${u.uniqueId}" target="_blank" style="color:#3b82f6;text-decoration:none">@${u.uniqueId}</a></td>
1166
1166
  <td data-label="昵称">${nick}</td>
1167
1167
  <td data-label="粉丝">${fans}</td>
1168
1168
  <td data-label="视频">${videos}</td>