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