tt-help-cli-ycl 1.3.24 → 1.3.26

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 (60) 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 +68 -68
  5. package/scripts/run-explore.bat +68 -68
  6. package/scripts/run-explore.ps1 +81 -81
  7. package/scripts/run-explore.sh +73 -73
  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/src/cli/attach.js +180 -180
  14. package/src/cli/auto.js +240 -186
  15. package/src/cli/config.js +152 -152
  16. package/src/cli/explore.js +439 -253
  17. package/src/cli/info.js +88 -88
  18. package/src/cli/open.js +102 -0
  19. package/src/cli/progress.js +111 -111
  20. package/src/cli/refresh.js +216 -216
  21. package/src/cli/scrape.js +47 -47
  22. package/src/cli/utils.js +18 -18
  23. package/src/cli/videos.js +41 -41
  24. package/src/cli/watch.js +31 -31
  25. package/src/lib/api-interceptor.js +191 -191
  26. package/src/lib/args.js +551 -517
  27. package/src/lib/browser/anti-detect.js +23 -23
  28. package/src/lib/browser/cdp.js +237 -194
  29. package/src/lib/browser/health-checker.js +93 -0
  30. package/src/lib/browser/launch.js +43 -43
  31. package/src/lib/browser/page.js +156 -156
  32. package/src/lib/constants.js +206 -199
  33. package/src/lib/delay.js +54 -54
  34. package/src/lib/explore-fetch.js +118 -118
  35. package/src/lib/fetcher.js +45 -45
  36. package/src/lib/filter.js +66 -66
  37. package/src/lib/io.js +54 -54
  38. package/src/lib/output.js +80 -80
  39. package/src/lib/page-error-detector.js +71 -71
  40. package/src/lib/parse-ssr.mjs +69 -69
  41. package/src/lib/parser.js +47 -47
  42. package/src/lib/retry.js +45 -45
  43. package/src/lib/scrape.js +89 -89
  44. package/src/lib/tiktok-scraper.mjs +194 -194
  45. package/src/lib/url.js +52 -52
  46. package/src/main.js +44 -42
  47. package/src/results/user-videos-bar.lar.lar.moeta.json +37 -0
  48. package/src/scraper/auto-core.js +203 -203
  49. package/src/scraper/core.js +211 -211
  50. package/src/scraper/explore-core.js +201 -189
  51. package/src/scraper/modules/captcha-handler.js +114 -114
  52. package/src/scraper/modules/comment-extractor.js +74 -74
  53. package/src/scraper/modules/follow-extractor.js +118 -118
  54. package/src/scraper/modules/guess-extractor.js +51 -51
  55. package/src/scraper/modules/page-helpers.js +48 -48
  56. package/src/scraper/refresh-core.js +179 -179
  57. package/src/videos/core.js +101 -101
  58. package/src/watch/data-store.js +828 -828
  59. package/src/watch/public/index.html +753 -753
  60. package/src/watch/server.js +705 -705
package/src/cli/auto.js CHANGED
@@ -1,186 +1,240 @@
1
- import { getOrCreatePage, isBrowserClosedError, relaunchBrowser } from '../lib/browser/page.js';
2
- import { userId as configuredUserId, saveUserId } from '../lib/constants.js';
3
- import { getMacOrUuid } from '../lib/mac-or-uuid.js';
4
- import { ensureBrowserReady as ensureBrowserReadyCDP } from '../lib/browser/cdp.js';
5
-
6
- const MAX_RETRY_WAIT = 5 * 60 * 1000;
7
-
8
- async function withRetry(label, fn) {
9
- let backoff = 1000;
10
- while (true) {
11
- try {
12
- return await fn();
13
- } catch (err) {
14
- console.error(`[连接] ${label} 失败: ${err.message},${backoff / 1000}秒后重试...`);
15
- await new Promise(r => setTimeout(r, backoff));
16
- if (backoff < MAX_RETRY_WAIT) backoff *= 2;
17
- }
18
- }
19
- }
20
-
21
- async function apiPost(url, body) {
22
- return withRetry(`POST ${url}`, async () => {
23
- const res = await fetch(url, {
24
- method: 'POST',
25
- headers: { 'Content-Type': 'application/json' },
26
- body: JSON.stringify(body),
27
- });
28
- return res.json();
29
- });
30
- }
31
-
32
- async function apiGet(url) {
33
- return withRetry(`GET ${url}`, async () => {
34
- const res = await fetch(url);
35
- return res.json();
36
- });
37
- }
38
-
39
- export async function handleAuto(options) {
40
- const { autoUsernames, autoCollectMax, autoScrapeDepth, autoMaxComments, autoMaxGuess,
41
- autoPreset, autoSwitchDelay, autoCommentDelay, serverUrl,
42
- autoEnableFollow, autoMaxFollowing, autoMaxFollowers } = options;
43
-
44
- let userId = configuredUserId;
45
- if (!userId) {
46
- userId = await getMacOrUuid();
47
- saveUserId(userId);
48
- console.error(`[初始化] 未检测到本地用户编号,已生成并使用: ${userId}`);
49
- }
50
-
51
- const runOptions = {
52
- collectMax: autoCollectMax,
53
- scrapeDepth: autoScrapeDepth,
54
- maxComments: autoMaxComments,
55
- maxGuess: autoMaxGuess,
56
- preset: autoPreset,
57
- switchMax: autoSwitchDelay,
58
- commentMax: autoCommentDelay,
59
- enableFollow: autoEnableFollow,
60
- maxFollowing: autoMaxFollowing,
61
- maxFollowers: autoMaxFollowers,
62
- userId,
63
- };
64
-
65
- await apiGet(`${serverUrl}/api/stats`);
66
-
67
- if (autoUsernames.length > 0) {
68
- const { added, skipped } = await apiPost(`${serverUrl}/api/users`, { usernames: autoUsernames });
69
- console.error(`种子用户: ${added} 个新增, ${skipped} 个已存在`);
70
- }
71
-
72
- console.error(`服务器: ${serverUrl}(断开会自动重连)`);
73
-
74
- const { ensureBrowserReady, processUser } = await import('../scraper/auto-core.js');
75
- let browser = await ensureBrowserReady();
76
-
77
- const page = await getOrCreatePage(browser);
78
-
79
- let processedCount = 0;
80
- let errorCount = 0;
81
- let consecutiveNetworkErrors = 0;
82
-
83
- while (true) {
84
- const job = await apiGet(`${serverUrl}/api/job?userId=${encodeURIComponent(userId)}`);
85
- if (!job.hasJob) break;
86
-
87
- const username = job.user.uniqueId;
88
- processedCount++;
89
-
90
- if (consecutiveNetworkErrors > 0) {
91
- const waitTime = consecutiveNetworkErrors <= 2
92
- ? 0
93
- : consecutiveNetworkErrors <= 5
94
- ? 30000
95
- : 300000;
96
- if (waitTime > 0) {
97
- console.error(` [网络] 连续 ${consecutiveNetworkErrors} 次网络异常,等待 ${waitTime / 1000}s 后重试...`);
98
- await new Promise(r => setTimeout(r, waitTime));
99
- }
100
- }
101
-
102
- console.error(`\n[${processedCount}] 处理 @${username}...`);
103
-
104
- const result = await processUser(page, username, { ...runOptions, browser }, console.error);
105
-
106
- if (result.restricted) {
107
- consecutiveNetworkErrors = 0;
108
- await apiPost(`${serverUrl}/api/job/${username}`, result);
109
- continue;
110
- }
111
-
112
- if (result.error) {
113
- // 浏览器关闭检测
114
- if (isBrowserClosedError(new Error(result.error))) {
115
- const newBrowser = await relaunchBrowser({}, 9222);
116
- browser = newBrowser;
117
- const newPage = await getOrCreatePage(browser);
118
- Object.assign(page, newPage);
119
- // 重试当前用户
120
- const retryResult = await processUser(page, username, { ...runOptions, browser }, console.error);
121
- Object.assign(result, retryResult);
122
- // 继续下方逻辑
123
- } else {
124
- consecutiveNetworkErrors++;
125
- errorCount++;
126
- await apiPost(`${serverUrl}/api/job/${username}`, result);
127
- const errorType = consecutiveNetworkErrors > 1 ? 'network' : 'other';
128
- await withRetry('report error', () =>
129
- apiPost(`${serverUrl}/api/error-report`, {
130
- userId,
131
- username,
132
- errorType,
133
- errorMessage: result.error,
134
- stage: 'process',
135
- errorStack: result.errorStack || '',
136
- })
137
- ).catch(() => {});
138
- continue;
139
- }
140
- }
141
-
142
- if (result.captchaDetected) {
143
- await withRetry('report captcha', () =>
144
- apiPost(`${serverUrl}/api/error-report`, {
145
- userId,
146
- username,
147
- errorType: 'captcha',
148
- errorMessage: result.captchaMessage || '页面出现验证码',
149
- stage: result.captchaStage || 'video-page',
150
- errorStack: '',
151
- })
152
- ).catch(() => {});
153
- }
154
-
155
- consecutiveNetworkErrors = 0;
156
-
157
- const guessedLocation = result.locationCreated || null;
158
-
159
- const payload = {
160
- userInfo: result.userInfo || {},
161
- discoveredVideoAuthors: (result.discoveredVideoAuthors || []).map(item =>
162
- typeof item === 'object' ? { ...item, guessedLocation } : item
163
- ),
164
- discoveredCommentAuthors: (result.discoveredCommentAuthors || []).map(author => ({ author, guessedLocation })),
165
- discoveredGuessAuthors: (result.discoveredGuessAuthors || []).map(author => ({ author, guessedLocation })),
166
- discoveredFollowing: (result.discoveredFollowing || []).map(f => ({
167
- handle: Array.isArray(f) ? f[0] : f,
168
- displayName: Array.isArray(f) ? f[1] : null,
169
- guessedLocation,
170
- })),
171
- discoveredFollowers: (result.discoveredFollowers || []).map(f => ({
172
- handle: Array.isArray(f) ? f[0] : f,
173
- displayName: Array.isArray(f) ? f[1] : null,
174
- guessedLocation,
175
- })),
176
- };
177
- await apiPost(`${serverUrl}/api/job/${username}`, payload);
178
- console.error(' 已提交');
179
- }
180
-
181
- const stats = await apiGet(`${serverUrl}/api/stats`);
182
- console.error(`\n完成: ${processedCount} 个用户处理, ${errorCount} 个出错`);
183
- console.error(` 总用户: ${stats.totalUsers}, 已完成: ${stats.processedUsers}, 待处理: ${stats.pendingUsers}, 错误: ${stats.errorUsers}`);
184
-
185
- await browser.close().catch(() => {});
186
- }
1
+ import {
2
+ getOrCreatePage,
3
+ isBrowserClosedError,
4
+ relaunchBrowser,
5
+ } from "../lib/browser/page.js";
6
+ import { userId as configuredUserId, saveUserId } from "../lib/constants.js";
7
+ import { getMacOrUuid } from "../lib/mac-or-uuid.js";
8
+ import { ensureBrowserReady as ensureBrowserReadyCDP } from "../lib/browser/cdp.js";
9
+
10
+ const MAX_RETRY_WAIT = 5 * 60 * 1000;
11
+
12
+ async function withRetry(label, fn) {
13
+ let backoff = 1000;
14
+ while (true) {
15
+ try {
16
+ return await fn();
17
+ } catch (err) {
18
+ console.error(
19
+ `[连接] ${label} 失败: ${err.message},${backoff / 1000}秒后重试...`,
20
+ );
21
+ await new Promise((r) => setTimeout(r, backoff));
22
+ if (backoff < MAX_RETRY_WAIT) backoff *= 2;
23
+ }
24
+ }
25
+ }
26
+
27
+ async function apiPost(url, body) {
28
+ return withRetry(`POST ${url}`, async () => {
29
+ const res = await fetch(url, {
30
+ method: "POST",
31
+ headers: { "Content-Type": "application/json" },
32
+ body: JSON.stringify(body),
33
+ });
34
+ return res.json();
35
+ });
36
+ }
37
+
38
+ async function apiGet(url) {
39
+ return withRetry(`GET ${url}`, async () => {
40
+ const res = await fetch(url);
41
+ return res.json();
42
+ });
43
+ }
44
+
45
+ export async function handleAuto(options) {
46
+ const {
47
+ autoUsernames,
48
+ autoCollectMax,
49
+ autoScrapeDepth,
50
+ autoMaxComments,
51
+ autoMaxGuess,
52
+ autoPreset,
53
+ autoSwitchDelay,
54
+ autoCommentDelay,
55
+ serverUrl,
56
+ autoEnableFollow,
57
+ autoMaxFollowing,
58
+ autoMaxFollowers,
59
+ } = options;
60
+
61
+ let userId = configuredUserId;
62
+ if (!userId) {
63
+ userId = await getMacOrUuid();
64
+ saveUserId(userId);
65
+ console.error(`[初始化] 未检测到本地用户编号,已生成并使用: ${userId}`);
66
+ }
67
+
68
+ const runOptions = {
69
+ collectMax: autoCollectMax,
70
+ scrapeDepth: autoScrapeDepth,
71
+ maxComments: autoMaxComments,
72
+ maxGuess: autoMaxGuess,
73
+ preset: autoPreset,
74
+ switchMax: autoSwitchDelay,
75
+ commentMax: autoCommentDelay,
76
+ enableFollow: autoEnableFollow,
77
+ maxFollowing: autoMaxFollowing,
78
+ maxFollowers: autoMaxFollowers,
79
+ userId,
80
+ };
81
+
82
+ await apiGet(`${serverUrl}/api/stats`);
83
+
84
+ if (autoUsernames.length > 0) {
85
+ const { added, skipped } = await apiPost(`${serverUrl}/api/users`, {
86
+ usernames: autoUsernames,
87
+ });
88
+ console.error(`种子用户: ${added} 个新增, ${skipped} 个已存在`);
89
+ }
90
+
91
+ console.error(`服务器: ${serverUrl}(断开会自动重连)`);
92
+
93
+ const { ensureBrowserReady, processUser } =
94
+ await import("../scraper/auto-core.js");
95
+ let browser = await ensureBrowserReady();
96
+
97
+ const page = await getOrCreatePage(browser);
98
+
99
+ let processedCount = 0;
100
+ let errorCount = 0;
101
+ let consecutiveNetworkErrors = 0;
102
+ let captchaCount = 0; // 验证码累计计数
103
+
104
+ while (true) {
105
+ const job = await apiGet(
106
+ `${serverUrl}/api/job?userId=${encodeURIComponent(userId)}`,
107
+ );
108
+ if (!job.hasJob) break;
109
+
110
+ const username = job.user.uniqueId;
111
+ processedCount++;
112
+
113
+ if (consecutiveNetworkErrors > 0) {
114
+ const waitTime =
115
+ consecutiveNetworkErrors <= 2
116
+ ? 0
117
+ : consecutiveNetworkErrors <= 5
118
+ ? 30000
119
+ : 300000;
120
+ if (waitTime > 0) {
121
+ console.error(
122
+ ` [网络] 连续 ${consecutiveNetworkErrors} 次网络异常,等待 ${waitTime / 1000}s 后重试...`,
123
+ );
124
+ await new Promise((r) => setTimeout(r, waitTime));
125
+ }
126
+ }
127
+
128
+ console.error(`\n[${processedCount}] 处理 @${username}...`);
129
+
130
+ const result = await processUser(
131
+ page,
132
+ username,
133
+ { ...runOptions, browser },
134
+ console.error,
135
+ );
136
+
137
+ if (result.restricted) {
138
+ consecutiveNetworkErrors = 0;
139
+ await apiPost(`${serverUrl}/api/job/${username}`, result);
140
+ continue;
141
+ }
142
+
143
+ if (result.error) {
144
+ // 浏览器关闭检测
145
+ if (isBrowserClosedError(new Error(result.error))) {
146
+ const newBrowser = await relaunchBrowser({}, 9222);
147
+ browser = newBrowser;
148
+ const newPage = await getOrCreatePage(browser);
149
+ Object.assign(page, newPage);
150
+ // 重试当前用户
151
+ const retryResult = await processUser(
152
+ page,
153
+ username,
154
+ { ...runOptions, browser },
155
+ console.error,
156
+ );
157
+ Object.assign(result, retryResult);
158
+ // 继续下方逻辑
159
+ } else {
160
+ consecutiveNetworkErrors++;
161
+ errorCount++;
162
+ await apiPost(`${serverUrl}/api/job/${username}`, result);
163
+ const errorType = consecutiveNetworkErrors > 1 ? "network" : "other";
164
+ await withRetry("report error", () =>
165
+ apiPost(`${serverUrl}/api/error-report`, {
166
+ userId,
167
+ username,
168
+ errorType,
169
+ errorMessage: result.error,
170
+ stage: "process",
171
+ errorStack: result.errorStack || "",
172
+ }),
173
+ ).catch(() => {});
174
+ continue;
175
+ }
176
+ }
177
+
178
+ if (result.captchaDetected) {
179
+ captchaCount++;
180
+ console.error(` [验证码] 累计 ${captchaCount} 次`);
181
+
182
+ await withRetry("report captcha", () =>
183
+ apiPost(`${serverUrl}/api/error-report`, {
184
+ userId,
185
+ username,
186
+ errorType: "captcha",
187
+ errorMessage: result.captchaMessage || "页面出现验证码",
188
+ stage: result.captchaStage || "video-page",
189
+ errorStack: "",
190
+ }),
191
+ ).catch(() => {});
192
+
193
+ // 累计2次验证码,标记异常(auto模式暂不支持账户切换)
194
+ if (captchaCount >= 2) {
195
+ console.error(
196
+ ` [警告] 验证码累计 ${captchaCount} 次,建议检查账户状态`,
197
+ );
198
+ captchaCount = 0;
199
+ }
200
+ }
201
+
202
+ consecutiveNetworkErrors = 0;
203
+
204
+ const guessedLocation = result.locationCreated || null;
205
+
206
+ const payload = {
207
+ userInfo: result.userInfo || {},
208
+ discoveredVideoAuthors: (result.discoveredVideoAuthors || []).map(
209
+ (item) =>
210
+ typeof item === "object" ? { ...item, guessedLocation } : item,
211
+ ),
212
+ discoveredCommentAuthors: (result.discoveredCommentAuthors || []).map(
213
+ (author) => ({ author, guessedLocation }),
214
+ ),
215
+ discoveredGuessAuthors: (result.discoveredGuessAuthors || []).map(
216
+ (author) => ({ author, guessedLocation }),
217
+ ),
218
+ discoveredFollowing: (result.discoveredFollowing || []).map((f) => ({
219
+ handle: Array.isArray(f) ? f[0] : f,
220
+ displayName: Array.isArray(f) ? f[1] : null,
221
+ guessedLocation,
222
+ })),
223
+ discoveredFollowers: (result.discoveredFollowers || []).map((f) => ({
224
+ handle: Array.isArray(f) ? f[0] : f,
225
+ displayName: Array.isArray(f) ? f[1] : null,
226
+ guessedLocation,
227
+ })),
228
+ };
229
+ await apiPost(`${serverUrl}/api/job/${username}`, payload);
230
+ console.error(" 已提交");
231
+ }
232
+
233
+ const stats = await apiGet(`${serverUrl}/api/stats`);
234
+ console.error(`\n完成: ${processedCount} 个用户处理, ${errorCount} 个出错`);
235
+ console.error(
236
+ ` 总用户: ${stats.totalUsers}, 已完成: ${stats.processedUsers}, 待处理: ${stats.pendingUsers}, 错误: ${stats.errorUsers}`,
237
+ );
238
+
239
+ await browser.close().catch(() => {});
240
+ }