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
package/src/cli/auto.js CHANGED
@@ -1,265 +1,265 @@
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
- let browser = null;
61
- let shuttingDown = false;
62
-
63
- const shutdown = async (signal) => {
64
- if (shuttingDown) return;
65
- shuttingDown = true;
66
- console.error(`\n[Auto] 收到 ${signal},正在关闭浏览器...`);
67
- await browser?.close().catch(() => {});
68
- console.error("[Auto] 已退出");
69
- process.exit(0);
70
- };
71
-
72
- const onSigint = () => {
73
- void shutdown("SIGINT");
74
- };
75
- const onSigterm = () => {
76
- void shutdown("SIGTERM");
77
- };
78
-
79
- process.once("SIGINT", onSigint);
80
- process.once("SIGTERM", onSigterm);
81
-
82
- try {
83
- let userId = configuredUserId;
84
- if (!userId) {
85
- userId = await getMacOrUuid();
86
- saveUserId(userId);
87
- console.error(`[初始化] 未检测到本地用户编号,已生成并使用: ${userId}`);
88
- }
89
-
90
- const runOptions = {
91
- collectMax: autoCollectMax,
92
- scrapeDepth: autoScrapeDepth,
93
- maxComments: autoMaxComments,
94
- maxGuess: autoMaxGuess,
95
- preset: autoPreset,
96
- switchMax: autoSwitchDelay,
97
- commentMax: autoCommentDelay,
98
- enableFollow: autoEnableFollow,
99
- maxFollowing: autoMaxFollowing,
100
- maxFollowers: autoMaxFollowers,
101
- userId,
102
- };
103
-
104
- await apiGet(`${serverUrl}/api/stats`);
105
-
106
- if (autoUsernames.length > 0) {
107
- const { added, skipped } = await apiPost(`${serverUrl}/api/users`, {
108
- usernames: autoUsernames,
109
- });
110
- console.error(`种子用户: ${added} 个新增, ${skipped} 个已存在`);
111
- }
112
-
113
- console.error(`服务器: ${serverUrl}(断开会自动重连)`);
114
-
115
- const { ensureBrowserReady, processUser } =
116
- await import("../scraper/auto-core.js");
117
- browser = await ensureBrowserReady();
118
-
119
- const page = await getOrCreatePage(browser);
120
-
121
- let processedCount = 0;
122
- let errorCount = 0;
123
- let consecutiveNetworkErrors = 0;
124
- let captchaCount = 0; // 验证码累计计数
125
-
126
- while (!shuttingDown) {
127
- const job = await apiGet(
128
- `${serverUrl}/api/job?userId=${encodeURIComponent(userId)}`,
129
- );
130
- if (!job.hasJob) break;
131
-
132
- const username = job.user.uniqueId;
133
- processedCount++;
134
-
135
- if (consecutiveNetworkErrors > 0) {
136
- const waitTime =
137
- consecutiveNetworkErrors <= 2
138
- ? 0
139
- : consecutiveNetworkErrors <= 5
140
- ? 30000
141
- : 300000;
142
- if (waitTime > 0) {
143
- console.error(
144
- ` [网络] 连续 ${consecutiveNetworkErrors} 次网络异常,等待 ${waitTime / 1000}s 后重试...`,
145
- );
146
- await new Promise((r) => setTimeout(r, waitTime));
147
- }
148
- }
149
-
150
- console.error(`\n[${processedCount}] 处理 @${username}...`);
151
-
152
- const result = await processUser(
153
- page,
154
- username,
155
- { ...runOptions, browser },
156
- console.error,
157
- );
158
-
159
- if (result.restricted) {
160
- consecutiveNetworkErrors = 0;
161
- await apiPost(`${serverUrl}/api/job/${username}`, result);
162
- continue;
163
- }
164
-
165
- if (result.error) {
166
- // 浏览器关闭检测
167
- if (isBrowserClosedError(new Error(result.error))) {
168
- const newBrowser = await relaunchBrowser({}, 9222);
169
- browser = newBrowser;
170
- const newPage = await getOrCreatePage(browser);
171
- Object.assign(page, newPage);
172
- // 重试当前用户
173
- const retryResult = await processUser(
174
- page,
175
- username,
176
- { ...runOptions, browser },
177
- console.error,
178
- );
179
- Object.assign(result, retryResult);
180
- // 继续下方逻辑
181
- } else {
182
- consecutiveNetworkErrors++;
183
- errorCount++;
184
- await apiPost(`${serverUrl}/api/job/${username}`, result);
185
- const errorType = consecutiveNetworkErrors > 1 ? "network" : "other";
186
- await withRetry("report error", () =>
187
- apiPost(`${serverUrl}/api/error-report`, {
188
- userId,
189
- username,
190
- errorType,
191
- errorMessage: result.error,
192
- stage: "process",
193
- errorStack: result.errorStack || "",
194
- }),
195
- ).catch(() => {});
196
- continue;
197
- }
198
- }
199
-
200
- if (result.captchaDetected) {
201
- captchaCount++;
202
- console.error(` [验证码] 累计 ${captchaCount} 次`);
203
-
204
- await withRetry("report captcha", () =>
205
- apiPost(`${serverUrl}/api/error-report`, {
206
- userId,
207
- username,
208
- errorType: "captcha",
209
- errorMessage: result.captchaMessage || "页面出现验证码",
210
- stage: result.captchaStage || "video-page",
211
- errorStack: "",
212
- }),
213
- ).catch(() => {});
214
-
215
- // 累计2次验证码,标记异常(auto模式暂不支持账户切换)
216
- if (captchaCount >= 2) {
217
- console.error(
218
- ` [警告] 验证码累计 ${captchaCount} 次,建议检查账户状态`,
219
- );
220
- captchaCount = 0;
221
- }
222
- }
223
-
224
- consecutiveNetworkErrors = 0;
225
-
226
- const guessedLocation = result.locationCreated || null;
227
-
228
- const payload = {
229
- userInfo: result.userInfo || {},
230
- discoveredVideoAuthors: (result.discoveredVideoAuthors || []).map(
231
- (item) =>
232
- typeof item === "object" ? { ...item, guessedLocation } : item,
233
- ),
234
- discoveredCommentAuthors: (result.discoveredCommentAuthors || []).map(
235
- (author) => ({ author, guessedLocation }),
236
- ),
237
- discoveredGuessAuthors: (result.discoveredGuessAuthors || []).map(
238
- (author) => ({ author, guessedLocation }),
239
- ),
240
- discoveredFollowing: (result.discoveredFollowing || []).map((f) => ({
241
- handle: Array.isArray(f) ? f[0] : f,
242
- displayName: Array.isArray(f) ? f[1] : null,
243
- guessedLocation,
244
- })),
245
- discoveredFollowers: (result.discoveredFollowers || []).map((f) => ({
246
- handle: Array.isArray(f) ? f[0] : f,
247
- displayName: Array.isArray(f) ? f[1] : null,
248
- guessedLocation,
249
- })),
250
- };
251
- await apiPost(`${serverUrl}/api/job/${username}`, payload);
252
- console.error(" 已提交");
253
- }
254
-
255
- const stats = await apiGet(`${serverUrl}/api/stats`);
256
- console.error(`\n完成: ${processedCount} 个用户处理, ${errorCount} 个出错`);
257
- console.error(
258
- ` 总用户: ${stats.totalUsers}, 已完成: ${stats.processedUsers}, 待处理: ${stats.pendingUsers}, 错误: ${stats.errorUsers}`,
259
- );
260
- } finally {
261
- process.removeListener("SIGINT", onSigint);
262
- process.removeListener("SIGTERM", onSigterm);
263
- await browser?.close().catch(() => {});
264
- }
265
- }
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
+ let browser = null;
61
+ let shuttingDown = false;
62
+
63
+ const shutdown = async (signal) => {
64
+ if (shuttingDown) return;
65
+ shuttingDown = true;
66
+ console.error(`\n[Auto] 收到 ${signal},正在关闭浏览器...`);
67
+ await browser?.close().catch(() => {});
68
+ console.error("[Auto] 已退出");
69
+ process.exit(0);
70
+ };
71
+
72
+ const onSigint = () => {
73
+ void shutdown("SIGINT");
74
+ };
75
+ const onSigterm = () => {
76
+ void shutdown("SIGTERM");
77
+ };
78
+
79
+ process.once("SIGINT", onSigint);
80
+ process.once("SIGTERM", onSigterm);
81
+
82
+ try {
83
+ let userId = configuredUserId;
84
+ if (!userId) {
85
+ userId = await getMacOrUuid();
86
+ saveUserId(userId);
87
+ console.error(`[初始化] 未检测到本地用户编号,已生成并使用: ${userId}`);
88
+ }
89
+
90
+ const runOptions = {
91
+ collectMax: autoCollectMax,
92
+ scrapeDepth: autoScrapeDepth,
93
+ maxComments: autoMaxComments,
94
+ maxGuess: autoMaxGuess,
95
+ preset: autoPreset,
96
+ switchMax: autoSwitchDelay,
97
+ commentMax: autoCommentDelay,
98
+ enableFollow: autoEnableFollow,
99
+ maxFollowing: autoMaxFollowing,
100
+ maxFollowers: autoMaxFollowers,
101
+ userId,
102
+ };
103
+
104
+ await apiGet(`${serverUrl}/api/stats`);
105
+
106
+ if (autoUsernames.length > 0) {
107
+ const { added, skipped } = await apiPost(`${serverUrl}/api/users`, {
108
+ usernames: autoUsernames,
109
+ });
110
+ console.error(`种子用户: ${added} 个新增, ${skipped} 个已存在`);
111
+ }
112
+
113
+ console.error(`服务器: ${serverUrl}(断开会自动重连)`);
114
+
115
+ const { ensureBrowserReady, processUser } =
116
+ await import("../scraper/auto-core.js");
117
+ browser = await ensureBrowserReady();
118
+
119
+ const page = await getOrCreatePage(browser);
120
+
121
+ let processedCount = 0;
122
+ let errorCount = 0;
123
+ let consecutiveNetworkErrors = 0;
124
+ let captchaCount = 0; // 验证码累计计数
125
+
126
+ while (!shuttingDown) {
127
+ const job = await apiGet(
128
+ `${serverUrl}/api/job?userId=${encodeURIComponent(userId)}`,
129
+ );
130
+ if (!job.hasJob) break;
131
+
132
+ const username = job.user.uniqueId;
133
+ processedCount++;
134
+
135
+ if (consecutiveNetworkErrors > 0) {
136
+ const waitTime =
137
+ consecutiveNetworkErrors <= 2
138
+ ? 0
139
+ : consecutiveNetworkErrors <= 5
140
+ ? 30000
141
+ : 300000;
142
+ if (waitTime > 0) {
143
+ console.error(
144
+ ` [网络] 连续 ${consecutiveNetworkErrors} 次网络异常,等待 ${waitTime / 1000}s 后重试...`,
145
+ );
146
+ await new Promise((r) => setTimeout(r, waitTime));
147
+ }
148
+ }
149
+
150
+ console.error(`\n[${processedCount}] 处理 @${username}...`);
151
+
152
+ const result = await processUser(
153
+ page,
154
+ username,
155
+ { ...runOptions, browser },
156
+ console.error,
157
+ );
158
+
159
+ if (result.restricted) {
160
+ consecutiveNetworkErrors = 0;
161
+ await apiPost(`${serverUrl}/api/job/${username}`, result);
162
+ continue;
163
+ }
164
+
165
+ if (result.error) {
166
+ // 浏览器关闭检测
167
+ if (isBrowserClosedError(new Error(result.error))) {
168
+ const newBrowser = await relaunchBrowser({}, 9222);
169
+ browser = newBrowser;
170
+ const newPage = await getOrCreatePage(browser);
171
+ Object.assign(page, newPage);
172
+ // 重试当前用户
173
+ const retryResult = await processUser(
174
+ page,
175
+ username,
176
+ { ...runOptions, browser },
177
+ console.error,
178
+ );
179
+ Object.assign(result, retryResult);
180
+ // 继续下方逻辑
181
+ } else {
182
+ consecutiveNetworkErrors++;
183
+ errorCount++;
184
+ await apiPost(`${serverUrl}/api/job/${username}`, result);
185
+ const errorType = consecutiveNetworkErrors > 1 ? "network" : "other";
186
+ await withRetry("report error", () =>
187
+ apiPost(`${serverUrl}/api/error-report`, {
188
+ userId,
189
+ username,
190
+ errorType,
191
+ errorMessage: result.error,
192
+ stage: "process",
193
+ errorStack: result.errorStack || "",
194
+ }),
195
+ ).catch(() => {});
196
+ continue;
197
+ }
198
+ }
199
+
200
+ if (result.captchaDetected) {
201
+ captchaCount++;
202
+ console.error(` [验证码] 累计 ${captchaCount} 次`);
203
+
204
+ await withRetry("report captcha", () =>
205
+ apiPost(`${serverUrl}/api/error-report`, {
206
+ userId,
207
+ username,
208
+ errorType: "captcha",
209
+ errorMessage: result.captchaMessage || "页面出现验证码",
210
+ stage: result.captchaStage || "video-page",
211
+ errorStack: "",
212
+ }),
213
+ ).catch(() => {});
214
+
215
+ // 累计2次验证码,标记异常(auto模式暂不支持账户切换)
216
+ if (captchaCount >= 2) {
217
+ console.error(
218
+ ` [警告] 验证码累计 ${captchaCount} 次,建议检查账户状态`,
219
+ );
220
+ captchaCount = 0;
221
+ }
222
+ }
223
+
224
+ consecutiveNetworkErrors = 0;
225
+
226
+ const guessedLocation = result.locationCreated || null;
227
+
228
+ const payload = {
229
+ userInfo: result.userInfo || {},
230
+ discoveredVideoAuthors: (result.discoveredVideoAuthors || []).map(
231
+ (item) =>
232
+ typeof item === "object" ? { ...item, guessedLocation } : item,
233
+ ),
234
+ discoveredCommentAuthors: (result.discoveredCommentAuthors || []).map(
235
+ (author) => ({ author, guessedLocation }),
236
+ ),
237
+ discoveredGuessAuthors: (result.discoveredGuessAuthors || []).map(
238
+ (author) => ({ author, guessedLocation }),
239
+ ),
240
+ discoveredFollowing: (result.discoveredFollowing || []).map((f) => ({
241
+ handle: Array.isArray(f) ? f[0] : f,
242
+ displayName: Array.isArray(f) ? f[1] : null,
243
+ guessedLocation,
244
+ })),
245
+ discoveredFollowers: (result.discoveredFollowers || []).map((f) => ({
246
+ handle: Array.isArray(f) ? f[0] : f,
247
+ displayName: Array.isArray(f) ? f[1] : null,
248
+ guessedLocation,
249
+ })),
250
+ };
251
+ await apiPost(`${serverUrl}/api/job/${username}`, payload);
252
+ console.error(" 已提交");
253
+ }
254
+
255
+ const stats = await apiGet(`${serverUrl}/api/stats`);
256
+ console.error(`\n完成: ${processedCount} 个用户处理, ${errorCount} 个出错`);
257
+ console.error(
258
+ ` 总用户: ${stats.totalUsers}, 已完成: ${stats.processedUsers}, 待处理: ${stats.pendingUsers}, 错误: ${stats.errorUsers}`,
259
+ );
260
+ } finally {
261
+ process.removeListener("SIGINT", onSigint);
262
+ process.removeListener("SIGTERM", onSigterm);
263
+ await browser?.close().catch(() => {});
264
+ }
265
+ }