tt-help-cli-ycl 1.3.12 → 1.3.14

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 (55) hide show
  1. package/README.md +17 -17
  2. package/cli.js +9 -9
  3. package/package.json +47 -45
  4. package/scripts/run-explore.bat +68 -68
  5. package/scripts/run-explore.ps1 +81 -81
  6. package/scripts/run-explore.sh +73 -73
  7. package/scripts/test-captcha-lib.mjs +68 -0
  8. package/scripts/test-captcha.mjs +81 -0
  9. package/scripts/test-incognito-lib.mjs +36 -0
  10. package/scripts/test-login-state.mjs +128 -0
  11. package/scripts/test-safe-click.mjs +45 -0
  12. package/src/cli/attach.js +160 -0
  13. package/src/cli/auto.js +186 -157
  14. package/src/cli/config.js +39 -3
  15. package/src/cli/explore.js +234 -193
  16. package/src/cli/info.js +88 -0
  17. package/src/cli/progress.js +111 -111
  18. package/src/cli/refresh.js +216 -0
  19. package/src/cli/scrape.js +47 -47
  20. package/src/cli/utils.js +18 -18
  21. package/src/cli/videos.js +41 -41
  22. package/src/cli/watch.js +31 -31
  23. package/src/lib/args.js +517 -402
  24. package/src/lib/browser/anti-detect.js +23 -23
  25. package/src/lib/browser/cdp.js +52 -10
  26. package/src/lib/browser/launch.js +43 -43
  27. package/src/lib/browser/page.js +146 -87
  28. package/src/lib/constants.js +199 -115
  29. package/src/lib/delay.js +54 -54
  30. package/src/lib/explore-fetch.js +118 -118
  31. package/src/lib/fetcher.js +45 -45
  32. package/src/lib/filter.js +66 -66
  33. package/src/lib/io.js +54 -54
  34. package/src/lib/output.js +80 -80
  35. package/src/lib/parse-ssr.mjs +69 -0
  36. package/src/lib/parser.js +47 -47
  37. package/src/lib/retry.js +45 -45
  38. package/src/lib/scrape.js +89 -40
  39. package/src/lib/tiktok-scraper.mjs +176 -0
  40. package/src/lib/url.js +52 -52
  41. package/src/main.js +12 -16
  42. package/src/results/user-videos-bar.lar.lar.moeta.json +37 -0
  43. package/src/scraper/auto-core.js +203 -194
  44. package/src/scraper/core.js +211 -190
  45. package/src/scraper/explore-core.js +162 -171
  46. package/src/scraper/modules/captcha-handler.js +114 -114
  47. package/src/scraper/modules/comment-extractor.js +74 -69
  48. package/src/scraper/modules/follow-extractor.js +121 -121
  49. package/src/scraper/modules/guess-extractor.js +51 -51
  50. package/src/scraper/modules/page-helpers.js +48 -48
  51. package/src/scraper/refresh-core.js +179 -0
  52. package/src/videos/core.js +126 -126
  53. package/src/watch/data-store.js +536 -302
  54. package/src/watch/public/index.html +721 -701
  55. package/src/watch/server.js +527 -359
@@ -1,302 +1,536 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
-
4
- function inferStatus(u) {
5
- if (u.restricted) return 'restricted';
6
- if (u.error) return 'error';
7
- if (u.processed) return 'done';
8
- return 'pending';
9
- }
10
-
11
- export function createStore(filePath) {
12
- let data = [];
13
- let clientErrors = new Map();
14
-
15
- let backupTimer = null;
16
-
17
- if (filePath) {
18
- const resolved = path.resolve(filePath);
19
- const backupDir = path.join(path.dirname(resolved), '.backup');
20
- const maxBackups = 3;
21
-
22
- if (fs.existsSync(resolved)) {
23
- try {
24
- const content = fs.readFileSync(resolved, 'utf-8');
25
- data = JSON.parse(content);
26
- if (!Array.isArray(data)) {
27
- data = [];
28
- }
29
- } catch (e) {
30
- console.error(`[data-store] 读取文件失败: ${e.message}`);
31
- data = [];
32
- }
33
- }
34
-
35
- function runBackup() {
36
- if (!fs.existsSync(resolved)) return;
37
- if (!fs.existsSync(backupDir)) fs.mkdirSync(backupDir, { recursive: true });
38
- const now = new Date();
39
- const timestamp = now.toISOString().replace(/[:.]/g, '-').slice(0, 13);
40
- const backupFile = path.join(backupDir, `data-${timestamp}.json`);
41
- try {
42
- fs.copyFileSync(resolved, backupFile);
43
- const files = fs.readdirSync(backupDir)
44
- .filter(f => f.startsWith('data-') && f.endsWith('.json'))
45
- .sort()
46
- .map(f => path.join(backupDir, f));
47
- while (files.length > maxBackups) {
48
- fs.unlinkSync(files.shift());
49
- }
50
- } catch (e) {
51
- console.error(`[data-store] 备份失败: ${e.message}`);
52
- }
53
- }
54
-
55
- backupTimer = setInterval(runBackup, 60 * 60 * 1000);
56
- }
57
-
58
- for (const u of data) {
59
- if (!u.status) u.status = inferStatus(u);
60
- }
61
-
62
- function save() {
63
- if (!filePath) return;
64
- const resolved = path.resolve(filePath);
65
- const json = JSON.stringify(data, null, 2);
66
- fs.writeFileSync(resolved, json, 'utf-8');
67
- }
68
-
69
- function stopBackup() {
70
- if (backupTimer) {
71
- clearInterval(backupTimer);
72
- backupTimer = null;
73
- }
74
- }
75
-
76
- function getUser(uid) {
77
- return data.find(u => u.uniqueId === uid);
78
- }
79
-
80
- function hasUser(uid) {
81
- return getUser(uid) !== undefined;
82
- }
83
-
84
- function addUser(user, append) {
85
- const existing = getUser(user.uniqueId);
86
- if (existing) {
87
- for (const key of Object.keys(user)) {
88
- if (key === 'uniqueId' || key === 'sources') continue;
89
- if (user[key] !== undefined && user[key] !== null && user[key] !== '') {
90
- existing[key] = user[key];
91
- }
92
- }
93
- } else {
94
- if (!user.status) user.status = inferStatus(user);
95
- if (user.processed) user.processedAt = user.processedAt || Date.now();
96
- if (append) data.push(user);
97
- else data.unshift(user);
98
- }
99
- }
100
-
101
- function getPendingUsers() {
102
- return data.filter(u => u.status === 'pending');
103
- }
104
-
105
- function getProcessedUsers() {
106
- return data.filter(u => u.status === 'done');
107
- }
108
-
109
- function getAllUsers() {
110
- return data;
111
- }
112
-
113
- function claimNextJob(userId, expireMs = 5 * 60 * 1000) {
114
- const now = Date.now();
115
-
116
- // 0. 该客户端有未过期的任务,续期返回
117
- const ongoing = data.find(u =>
118
- u.status === 'processing' && u.claimedBy === userId && u.claimedAt && (now - u.claimedAt) < expireMs
119
- );
120
- if (ongoing) {
121
- ongoing.claimedAt = now;
122
- return { uniqueId: ongoing.uniqueId, nickname: ongoing.nickname, claimedAt: ongoing.claimedAt, claimedBy: userId };
123
- }
124
-
125
- let next = data.find(u => u.status === 'pending' && u.pinned);
126
-
127
- if (!next) {
128
- const expired = data.find(u =>
129
- u.status === 'processing' && u.claimedAt && (now - u.claimedAt) > expireMs
130
- );
131
- if (expired) {
132
- expired.status = 'pending';
133
- delete expired.claimedAt;
134
- next = expired;
135
- }
136
- }
137
-
138
- if (!next) {
139
- next = data.find(u => u.status === 'pending' && u.sources && u.sources.includes('seed'));
140
- }
141
-
142
- if (!next) {
143
- next = data.find(u => u.status === 'pending' && u.sources && (u.sources.includes('following') || u.sources.includes('follower')));
144
- }
145
-
146
- if (!next) {
147
- next = data.find(u => u.status === 'pending');
148
- }
149
-
150
- if (next) {
151
- next.status = 'processing';
152
- next.claimedAt = now;
153
- next.claimedBy = userId;
154
- return { uniqueId: next.uniqueId, nickname: next.nickname, claimedAt: next.claimedAt, claimedBy: userId };
155
- }
156
- return null;
157
- }
158
-
159
- function commitJob(uniqueId, result) {
160
- const user = getUser(uniqueId);
161
- if (!user) return { saved: false, error: 'user not found' };
162
-
163
- if (result.restricted) {
164
- user.status = 'restricted';
165
- if (result.userInfo) {
166
- const info = result.userInfo;
167
- for (const key of Object.keys(info)) {
168
- if (key === 'uniqueId' || key === 'sources') continue;
169
- if (info[key] !== undefined && info[key] !== null && info[key] !== '') {
170
- user[key] = info[key];
171
- }
172
- }
173
- }
174
- user.processed = true;
175
- user.processedAt = Date.now();
176
- user.noVideo = true;
177
- user.sources = [...new Set([...(user.sources || []), 'restricted'])];
178
- } else if (result.error) {
179
- user.status = 'error';
180
- user.error = result.error;
181
- user.sources = [...new Set([...(user.sources || []), 'error'])];
182
- } else {
183
- user.status = 'done';
184
- user.processed = true;
185
- user.processedAt = Date.now();
186
- user.followerCount = result.userInfo?.followerCount ?? user.followerCount;
187
- user.videoCount = result.userInfo?.videoCount ?? user.videoCount;
188
- user.nickname = result.userInfo?.nickname || user.nickname;
189
- user.locationCreated = result.userInfo?.locationCreated || user.locationCreated;
190
- user.ttSeller = result.userInfo?.ttSeller ?? user.ttSeller;
191
- user.verified = result.userInfo?.verified ?? user.verified;
192
- user.region = result.userInfo?.region || user.region;
193
- user.signature = result.userInfo?.signature ?? user.signature;
194
- user.followingCount = result.userInfo?.followingCount ?? user.followingCount;
195
- user.heartCount = result.userInfo?.heartCount ?? user.heartCount;
196
- if (result.userInfo?.secUid) user.secUid = result.userInfo.secUid;
197
- const extraFields = ['restricted', 'error', 'userInfo', 'discoveredVideoAuthors',
198
- 'discoveredCommentAuthors', 'discoveredGuessAuthors', 'discoveredFollowing',
199
- 'discoveredFollowers', 'uniqueId', 'sources'];
200
- for (const key of Object.keys(result)) {
201
- if (extraFields.includes(key)) continue;
202
- if (result[key] !== undefined && result[key] !== null && result[key] !== '') {
203
- user[key] = result[key];
204
- }
205
- }
206
- user.sources = [...new Set([...(user.sources || []), 'processed'])];
207
-
208
- const discovered = [
209
- ...(result.discoveredVideoAuthors || []).map(v => ({
210
- uniqueId: v.uniqueId, nickname: v.nickname, locationCreated: v.locationCreated,
211
- sources: ['video']
212
- })),
213
- ...(result.discoveredCommentAuthors || []).map(c => {
214
- const id = typeof c === 'string' ? c.replace(/^@/, '') : c.uniqueId;
215
- return typeof c === 'string'
216
- ? { uniqueId: id, sources: ['comment'] }
217
- : { uniqueId: id, ...c, sources: [...new Set([...(c.sources || []), 'comment'])] };
218
- }),
219
- ...(result.discoveredGuessAuthors || []).map(g => {
220
- const id = typeof g === 'string' ? g.replace(/^@/, '') : g.uniqueId;
221
- return typeof g === 'string'
222
- ? { uniqueId: id, sources: ['guess'] }
223
- : { uniqueId: id, ...g, sources: [...new Set([...(g.sources || []), 'guess'])] };
224
- }),
225
- ...(result.discoveredFollowing || []).map(([handle, name]) => ({
226
- uniqueId: handle.replace(/^@/, ''), nickname: name, sources: ['following']
227
- })),
228
- ...(result.discoveredFollowers || []).map(([handle, name]) => ({
229
- uniqueId: handle.replace(/^@/, ''), nickname: name, sources: ['follower']
230
- })),
231
- ];
232
-
233
- for (const du of discovered) {
234
- if (!du.uniqueId) continue;
235
- addUser(du, true);
236
- }
237
- }
238
-
239
- delete user.claimedAt;
240
- save();
241
- return { saved: true };
242
- }
243
-
244
- function resetJob(uniqueId) {
245
- const user = getUser(uniqueId);
246
- if (!user) return { saved: false, error: 'user not found' };
247
- user.status = 'pending';
248
- delete user.claimedAt;
249
- delete user.processedAt;
250
- delete user.processed;
251
- delete user.error;
252
- delete user.restricted;
253
- delete user.noVideo;
254
- save();
255
- return { saved: true };
256
- }
257
-
258
- function togglePin(uniqueId) {
259
- const user = getUser(uniqueId);
260
- if (!user) return { saved: false, error: 'user not found' };
261
- user.pinned = !user.pinned;
262
- save();
263
- return { saved: true, pinned: user.pinned };
264
- }
265
-
266
- function reportClientError(userId, errorType, errorMessage, username) {
267
- const existing = clientErrors.get(userId);
268
- if (existing) {
269
- existing.errorType = errorType;
270
- existing.errorMessage = errorMessage;
271
- existing.username = username;
272
- existing.timestamp = Date.now();
273
- existing.reportCount = (existing.reportCount || 1) + 1;
274
- } else {
275
- clientErrors.set(userId, {
276
- userId,
277
- errorType,
278
- errorMessage,
279
- username,
280
- timestamp: Date.now(),
281
- reportCount: 1,
282
- });
283
- }
284
- }
285
-
286
- function deleteClientError(userId) {
287
- clientErrors.delete(userId);
288
- }
289
-
290
- function getClientErrors() {
291
- return Array.from(clientErrors.values());
292
- }
293
-
294
- return {
295
- save, getUser, hasUser, addUser,
296
- getPendingUsers, getProcessedUsers, getAllUsers,
297
- claimNextJob, commitJob, resetJob, togglePin,
298
- reportClientError, deleteClientError, getClientErrors,
299
- stopBackup,
300
- data,
301
- };
302
- }
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+
4
+ function inferStatus(u) {
5
+ if (u.restricted) return 'restricted';
6
+ if (u.error) return 'error';
7
+ if (u.processed) return 'done';
8
+ return 'pending';
9
+ }
10
+
11
+ export function createStore(filePath) {
12
+ let data = [];
13
+ let clientErrors = new Map();
14
+
15
+ // 视频存储(独立 JSON 文件)
16
+ let videos = [];
17
+ let videoFilePath = null;
18
+ if (filePath) {
19
+ const resolved = path.resolve(filePath);
20
+ videoFilePath = resolved.replace(/\.json$/, '-videos.json');
21
+ if (fs.existsSync(videoFilePath)) {
22
+ try {
23
+ const content = fs.readFileSync(videoFilePath, 'utf-8');
24
+ const parsed = JSON.parse(content);
25
+ if (Array.isArray(parsed)) {
26
+ videos = parsed;
27
+ }
28
+ } catch (e) {
29
+ console.error(`[data-store] 读取视频文件失败: ${e.message}`);
30
+ }
31
+ }
32
+ }
33
+
34
+ let backupTimer = null;
35
+
36
+ if (filePath) {
37
+ const resolved = path.resolve(filePath);
38
+ const backupDir = path.join(path.dirname(resolved), '.backup');
39
+ const maxBackups = 3;
40
+
41
+ if (fs.existsSync(resolved)) {
42
+ try {
43
+ const content = fs.readFileSync(resolved, 'utf-8');
44
+ data = JSON.parse(content);
45
+ if (!Array.isArray(data)) {
46
+ data = [];
47
+ }
48
+ } catch (e) {
49
+ console.error(`[data-store] 读取文件失败: ${e.message}`);
50
+ data = [];
51
+ }
52
+ }
53
+
54
+ function runBackup() {
55
+ if (!fs.existsSync(resolved)) return;
56
+ if (!fs.existsSync(backupDir)) fs.mkdirSync(backupDir, { recursive: true });
57
+ const now = new Date();
58
+ const timestamp = now.toISOString().replace(/[:.]/g, '-').slice(0, 13);
59
+ const backupFile = path.join(backupDir, `data-${timestamp}.json`);
60
+ try {
61
+ fs.copyFileSync(resolved, backupFile);
62
+ const files = fs.readdirSync(backupDir)
63
+ .filter(f => f.startsWith('data-') && f.endsWith('.json'))
64
+ .sort()
65
+ .map(f => path.join(backupDir, f));
66
+ while (files.length > maxBackups) {
67
+ fs.unlinkSync(files.shift());
68
+ }
69
+ } catch (e) {
70
+ console.error(`[data-store] 备份失败: ${e.message}`);
71
+ }
72
+ }
73
+
74
+ backupTimer = setInterval(runBackup, 60 * 60 * 1000);
75
+ }
76
+
77
+ for (const u of data) {
78
+ if (!u.status) u.status = inferStatus(u);
79
+ }
80
+
81
+ function save() {
82
+ if (!filePath) return;
83
+ const resolved = path.resolve(filePath);
84
+ const json = JSON.stringify(data, null, 2);
85
+ fs.writeFileSync(resolved, json, 'utf-8');
86
+ }
87
+
88
+ function saveVideos() {
89
+ if (!videoFilePath) return;
90
+ const json = JSON.stringify(videos, null, 2);
91
+ fs.writeFileSync(videoFilePath, json, 'utf-8');
92
+ }
93
+
94
+ function stopBackup() {
95
+ if (backupTimer) {
96
+ clearInterval(backupTimer);
97
+ backupTimer = null;
98
+ }
99
+ }
100
+
101
+ function getUser(uid) {
102
+ return data.find(u => u.uniqueId === uid);
103
+ }
104
+
105
+ function hasUser(uid) {
106
+ return getUser(uid) !== undefined;
107
+ }
108
+
109
+ function userExists(uid) {
110
+ return hasUser(uid);
111
+ }
112
+
113
+ function addUser(user, append) {
114
+ const existing = getUser(user.uniqueId);
115
+ if (existing) {
116
+ for (const key of Object.keys(user)) {
117
+ if (key === 'uniqueId' || key === 'sources') continue;
118
+ if (user[key] !== undefined && user[key] !== null && user[key] !== '') {
119
+ existing[key] = user[key];
120
+ }
121
+ }
122
+ } else {
123
+ if (!user.status) user.status = inferStatus(user);
124
+ if (user.processed) user.processedAt = user.processedAt || Date.now();
125
+ if (append) data.push(user);
126
+ else data.unshift(user);
127
+ }
128
+ }
129
+
130
+ function getPendingUsers() {
131
+ return data.filter(u => u.status === 'pending');
132
+ }
133
+
134
+ function getProcessedUsers() {
135
+ return data.filter(u => u.status === 'done');
136
+ }
137
+
138
+ function getAllUsers() {
139
+ return data;
140
+ }
141
+
142
+ function claimNextJob(userId, expireMs = 5 * 60 * 1000) {
143
+ const now = Date.now();
144
+
145
+ // 0. 该客户端有未过期的任务,续期返回
146
+ const ongoing = data.find(u =>
147
+ u.status === 'processing' && u.claimedBy === userId && u.claimedAt && (now - u.claimedAt) < expireMs
148
+ );
149
+ if (ongoing) {
150
+ ongoing.claimedAt = now;
151
+ return { uniqueId: ongoing.uniqueId, nickname: ongoing.nickname, claimedAt: ongoing.claimedAt, claimedBy: userId };
152
+ }
153
+
154
+ // 按猜测国家梯队排序:第一梯队优先,无国家排最后
155
+ const tier1 = new Set(['PL', 'NL', 'BE']);
156
+ const tier2 = new Set(['DE', 'FR', 'IT', 'IE', 'ES']);
157
+ function locationTier(u) {
158
+ const loc = (u.guessedLocation || '').toUpperCase();
159
+ if (tier1.has(loc)) return 0;
160
+ if (tier2.has(loc)) return 1;
161
+ return 2;
162
+ }
163
+
164
+ function pickFirst(sorted) {
165
+ return sorted.length > 0 ? sorted[0] : null;
166
+ }
167
+
168
+ let next = data.find(u => u.status === 'pending' && u.pinned);
169
+
170
+ if (!next) {
171
+ const expired = data.find(u =>
172
+ u.status === 'processing' && u.claimedAt && (now - u.claimedAt) > expireMs
173
+ );
174
+ if (expired) {
175
+ expired.status = 'pending';
176
+ delete expired.claimedAt;
177
+ next = expired;
178
+ }
179
+ }
180
+
181
+ if (!next) {
182
+ const seed = data.filter(u => u.status === 'pending' && u.sources && u.sources.includes('seed'));
183
+ seed.sort((a, b) => locationTier(a) - locationTier(b));
184
+ next = pickFirst(seed);
185
+ }
186
+
187
+ if (!next) {
188
+ const ttSeller = data.filter(u => u.status === 'pending' && u.ttSeller === true && u.verified === false);
189
+ ttSeller.sort((a, b) => locationTier(a) - locationTier(b));
190
+ next = pickFirst(ttSeller);
191
+ }
192
+
193
+ if (!next) {
194
+ const follow = data.filter(u => u.status === 'pending' && u.sources && (u.sources.includes('following') || u.sources.includes('follower')));
195
+ follow.sort((a, b) => locationTier(a) - locationTier(b));
196
+ next = pickFirst(follow);
197
+ }
198
+
199
+ if (!next) {
200
+ const all = data.filter(u => u.status === 'pending');
201
+ all.sort((a, b) => locationTier(a) - locationTier(b));
202
+ next = pickFirst(all);
203
+ }
204
+
205
+ if (next) {
206
+ next.status = 'processing';
207
+ next.claimedAt = now;
208
+ next.claimedBy = userId;
209
+ return { uniqueId: next.uniqueId, nickname: next.nickname, claimedAt: next.claimedAt, claimedBy: userId };
210
+ }
211
+ return null;
212
+ }
213
+
214
+ function processDiscoveredUsers(result) {
215
+ const guessedLocation = result.guessedLocation || null;
216
+ const discovered = [
217
+ ...(result.discoveredVideoAuthors || []).map(v => ({
218
+ uniqueId: typeof v === 'string' ? v.replace(/^@/, '') : v.uniqueId?.replace(/^@/, '') || '',
219
+ nickname: typeof v === 'string' ? null : v.nickname || null,
220
+ locationCreated: typeof v === 'string' ? null : v.locationCreated || null,
221
+ guessedLocation: typeof v === 'string' ? guessedLocation : (v.guessedLocation || guessedLocation),
222
+ sources: ['video'],
223
+ })),
224
+ ...(result.discoveredCommentAuthors || []).map(c => {
225
+ if (typeof c === 'string') return { uniqueId: c.replace(/^@/, ''), sources: ['comment'], guessedLocation };
226
+ return { uniqueId: (c.author || c.uniqueId || '').replace(/^@/, ''), nickname: c.nickname || null, sources: ['comment'], guessedLocation: c.guessedLocation || guessedLocation };
227
+ }),
228
+ ...(result.discoveredGuessAuthors || []).map(g => {
229
+ if (typeof g === 'string') return { uniqueId: g.replace(/^@/, ''), sources: ['guess'], guessedLocation };
230
+ return { uniqueId: (g.author || g.uniqueId || '').replace(/^@/, ''), nickname: g.nickname || null, sources: ['guess'], guessedLocation: g.guessedLocation || guessedLocation };
231
+ }),
232
+ ...(result.discoveredFollowing || []).map(f => {
233
+ const handle = Array.isArray(f) ? f[0] : (f.handle || '');
234
+ const name = Array.isArray(f) ? f[1] : (f.displayName || null);
235
+ return { uniqueId: handle.replace(/^@/, ''), nickname: name, sources: ['following'], guessedLocation: (typeof f === 'object' && f.guessedLocation) || guessedLocation };
236
+ }),
237
+ ...(result.discoveredFollowers || []).map(f => {
238
+ const handle = Array.isArray(f) ? f[0] : (f.handle || '');
239
+ const name = Array.isArray(f) ? f[1] : (f.displayName || null);
240
+ return { uniqueId: handle.replace(/^@/, ''), nickname: name, sources: ['follower'], guessedLocation: (typeof f === 'object' && f.guessedLocation) || guessedLocation };
241
+ }),
242
+ ].filter(u => u.uniqueId);
243
+
244
+ const newUsers = [];
245
+ for (const d of discovered) {
246
+ const existing = getUser(d.uniqueId);
247
+ if (!existing) {
248
+ addUser(d, true);
249
+ newUsers.push(d.uniqueId);
250
+ }
251
+ }
252
+ return newUsers;
253
+ }
254
+
255
+ function updateUserFromResult(user, result) {
256
+ if (result.restricted) {
257
+ user.status = 'restricted';
258
+ if (result.userInfo) {
259
+ const info = result.userInfo;
260
+ for (const key of Object.keys(info)) {
261
+ if (key === 'uniqueId' || key === 'sources') continue;
262
+ if (info[key] !== undefined && info[key] !== null && info[key] !== '') {
263
+ user[key] = info[key];
264
+ }
265
+ }
266
+ }
267
+ user.processed = true;
268
+ user.processedAt = Date.now();
269
+ user.sources = [...new Set([...(user.sources || []), 'restricted'])];
270
+ } else if (result.error) {
271
+ user.status = 'error';
272
+ user.error = result.error;
273
+ user.sources = [...new Set([...(user.sources || []), 'error'])];
274
+ } else {
275
+ user.status = 'done';
276
+ user.processed = true;
277
+ user.processedAt = Date.now();
278
+ user.noVideo = result.noVideo || false;
279
+ user.keepFollow = result.keepFollow || false;
280
+ user.hasFollowData = result.hasFollowData || false;
281
+
282
+ if (result.userInfo) {
283
+ const info = result.userInfo;
284
+ for (const key of Object.keys(info)) {
285
+ if (key === 'uniqueId' || key === 'sources') continue;
286
+ if (info[key] !== undefined && info[key] !== null && info[key] !== '') {
287
+ user[key] = info[key];
288
+ }
289
+ }
290
+ }
291
+
292
+ user.followerCount = result.userInfo?.followerCount ?? user.followerCount;
293
+ user.videoCount = result.userInfo?.videoCount ?? user.videoCount;
294
+ user.nickname = result.userInfo?.nickname || user.nickname;
295
+ user.locationCreated = result.userInfo?.locationCreated || user.locationCreated;
296
+ user.ttSeller = result.userInfo?.ttSeller ?? user.ttSeller;
297
+ user.verified = result.userInfo?.verified ?? user.verified;
298
+ user.region = result.userInfo?.region || user.region;
299
+ user.signature = result.userInfo?.signature ?? user.signature;
300
+ user.followingCount = result.userInfo?.followingCount ?? user.followingCount;
301
+ user.heartCount = result.userInfo?.heartCount ?? user.heartCount;
302
+ if (result.userInfo?.secUid) user.secUid = result.userInfo.secUid;
303
+ const extraFields = ['restricted', 'error', 'userInfo', 'discoveredVideoAuthors',
304
+ 'discoveredCommentAuthors', 'discoveredGuessAuthors', 'discoveredFollowing',
305
+ 'discoveredFollowers', 'uniqueId', 'sources'];
306
+ for (const key of Object.keys(result)) {
307
+ if (extraFields.includes(key)) continue;
308
+ if (result[key] !== undefined && result[key] !== null && result[key] !== '') {
309
+ user[key] = result[key];
310
+ }
311
+ }
312
+ user.sources = [...new Set([...(user.sources || []), 'processed'])];
313
+ }
314
+ }
315
+
316
+ function commitJob(uniqueId, result) {
317
+ const user = getUser(uniqueId);
318
+ if (!user) return { saved: false, error: 'user not found' };
319
+
320
+ updateUserFromResult(user, result);
321
+ delete user.claimedAt;
322
+ const newUsers = processDiscoveredUsers(result);
323
+
324
+ save();
325
+ return { saved: true, status: user.status, newUsers };
326
+ }
327
+
328
+ function commitNewExplore(uniqueId, result) {
329
+ const existing = getUser(uniqueId);
330
+ if (existing) {
331
+ updateUserFromResult(existing, result);
332
+ const newUsers = processDiscoveredUsers(result);
333
+ save();
334
+ return { saved: true, created: false, status: existing.status, newUsers };
335
+ }
336
+
337
+ const userObj = {
338
+ uniqueId,
339
+ ...(result.userInfo || {}),
340
+ sources: ['refresh-explore'],
341
+ };
342
+ updateUserFromResult(userObj, result);
343
+ addUser(userObj, true);
344
+ const newUsers = processDiscoveredUsers(result);
345
+
346
+ save();
347
+ return { saved: true, created: true, status: userObj.status, newUsers };
348
+ }
349
+
350
+ function resetJob(uniqueId) {
351
+ const user = getUser(uniqueId);
352
+ if (!user) return { saved: false, error: 'user not found' };
353
+ user.status = 'pending';
354
+ delete user.claimedAt;
355
+ delete user.processedAt;
356
+ delete user.processed;
357
+ delete user.error;
358
+ delete user.restricted;
359
+ delete user.noVideo;
360
+ save();
361
+ return { saved: true };
362
+ }
363
+
364
+ function togglePin(uniqueId) {
365
+ const user = getUser(uniqueId);
366
+ if (!user) return { saved: false, error: 'user not found' };
367
+ user.pinned = !user.pinned;
368
+ save();
369
+ return { saved: true, pinned: user.pinned };
370
+ }
371
+
372
+ function getNextRedoJob(userId) {
373
+ const now = Date.now();
374
+ const defaultTime = new Date('2016-01-01T00:00:00Z').getTime();
375
+
376
+ // 筛选目标国家用户,按 refreshTime 升序取最远的(没有则默认 2016-01-01)
377
+ const targetLocations = ['ES', 'PL', 'NL', 'BE', 'DE', 'FR', 'IT', 'IE'];
378
+ const targetUsers = data.filter(u => u.ttSeller && u.verified === false && targetLocations.includes(u.locationCreated));
379
+ if (targetUsers.length === 0) return null;
380
+
381
+ targetUsers.sort((a, b) => {
382
+ const ta = a.refreshTime || defaultTime;
383
+ const tb = b.refreshTime || defaultTime;
384
+ return ta - tb;
385
+ });
386
+
387
+ const next = targetUsers[0];
388
+ next.refreshTime = now;
389
+ return { uniqueId: next.uniqueId, nickname: next.nickname, refreshTime: next.refreshTime };
390
+ }
391
+
392
+ function commitRedoJob(uniqueId, result) {
393
+ const user = getUser(uniqueId);
394
+ if (!user) return { saved: false, error: 'user not found' };
395
+
396
+ user.refreshTime = Date.now();
397
+
398
+ if (result.userInfo) {
399
+ const info = result.userInfo;
400
+ for (const key of Object.keys(info)) {
401
+ if (key === 'uniqueId' || key === 'sources') continue;
402
+ if (info[key] !== undefined && info[key] !== null && info[key] !== '') {
403
+ user[key] = info[key];
404
+ }
405
+ }
406
+ }
407
+
408
+ return { saved: true };
409
+ }
410
+
411
+ function reportClientError(userId, errorType, errorMessage, username, stage, errorStack) {
412
+ const existing = clientErrors.get(userId);
413
+ if (existing) {
414
+ existing.timestamp = Date.now();
415
+ if (errorType === 'captcha') {
416
+ existing.captchaCount = (existing.captchaCount || 0) + 1;
417
+ if (!existing.captchaStage) existing.captchaStage = stage || '';
418
+ if (!existing.captchaMessage) existing.captchaMessage = errorMessage || '';
419
+ if (!existing.captchaStack) existing.captchaStack = errorStack || '';
420
+ } else {
421
+ existing.errorType = errorType;
422
+ existing.errorMessage = errorMessage || '';
423
+ existing.errorStack = errorStack || '';
424
+ existing.stage = stage || '';
425
+ existing.reportCount = (existing.reportCount || 1) + 1;
426
+ }
427
+ if (username) existing.username = username;
428
+ } else {
429
+ clientErrors.set(userId, {
430
+ userId,
431
+ errorType,
432
+ errorMessage: errorMessage || '',
433
+ errorStack: errorStack || '',
434
+ username,
435
+ stage: stage || '',
436
+ timestamp: Date.now(),
437
+ reportCount: 1,
438
+ captchaCount: errorType === 'captcha' ? 1 : 0,
439
+ captchaStage: errorType === 'captcha' ? (stage || '') : '',
440
+ captchaMessage: errorType === 'captcha' ? (errorMessage || '') : '',
441
+ captchaStack: errorType === 'captcha' ? (errorStack || '') : '',
442
+ });
443
+ }
444
+ }
445
+
446
+ function deleteClientError(userId) {
447
+ clientErrors.delete(userId);
448
+ }
449
+
450
+ function getClientErrors() {
451
+ return Array.from(clientErrors.values());
452
+ }
453
+
454
+ function getPendingUserUpdateTasks(limit) {
455
+ const l = Math.max(1, parseInt(limit) || 5);
456
+ const pending = data.filter(u => {
457
+ const updateCount = u.userUpdateCount;
458
+ const ttSellerEmpty = u.ttSeller === null || u.ttSeller === undefined || u.ttSeller === '';
459
+ if (!ttSellerEmpty) return false;
460
+ return updateCount === null || updateCount === undefined || updateCount <= 0;
461
+ }).slice(0, l);
462
+ // 接受任务时 userUpdateCount + 1
463
+ pending.forEach(u => {
464
+ u.userUpdateCount = (u.userUpdateCount || 0) + 1;
465
+ u.updatedAt = Date.now();
466
+ });
467
+ save();
468
+ return pending;
469
+ }
470
+
471
+ function updateUserInfo(uniqueId, info) {
472
+ const user = getUser(uniqueId);
473
+ if (!user) return { error: 'user not found' };
474
+ for (const key of Object.keys(info)) {
475
+ if (key === 'uniqueId' || key === 'sources') continue;
476
+ if (info[key] !== undefined && info[key] !== null && info[key] !== '') {
477
+ user[key] = info[key];
478
+ }
479
+ }
480
+ user.userUpdateCount = (user.userUpdateCount || 0) + 1;
481
+ user.updatedAt = Date.now();
482
+ save();
483
+ return { ok: true, userUpdateCount: user.userUpdateCount };
484
+ }
485
+
486
+ // 视频登记
487
+ function registerVideos(sourceUser, videoList, locationCreated, ttSeller) {
488
+ if (!videoList || !Array.isArray(videoList) || videoList.length === 0) {
489
+ return { registered: 0, skipped: 0 };
490
+ }
491
+
492
+ const existingIds = new Set(videos.map(v => v.id));
493
+ let registered = 0;
494
+ let skipped = 0;
495
+
496
+ for (const item of videoList) {
497
+ if (existingIds.has(item.id)) {
498
+ skipped++;
499
+ continue;
500
+ }
501
+ videos.push({
502
+ id: item.id,
503
+ href: item.href,
504
+ authorUniqueId: sourceUser,
505
+ locationCreated: locationCreated || null,
506
+ ttSeller: ttSeller || false,
507
+ registeredAt: Date.now(),
508
+ });
509
+ existingIds.add(item.id);
510
+ registered++;
511
+ }
512
+
513
+ saveVideos();
514
+ return { registered, skipped };
515
+ }
516
+
517
+ function getVideos() {
518
+ return videos;
519
+ }
520
+
521
+ function getVideoCount() {
522
+ return videos.length;
523
+ }
524
+
525
+ return {
526
+ save, getUser, hasUser, userExists, addUser,
527
+ getPendingUsers, getProcessedUsers, getAllUsers,
528
+ claimNextJob, commitJob, commitNewExplore, resetJob, togglePin,
529
+ getNextRedoJob, commitRedoJob,
530
+ getPendingUserUpdateTasks, updateUserInfo,
531
+ reportClientError, deleteClientError, getClientErrors,
532
+ registerVideos, getVideos, getVideoCount,
533
+ stopBackup,
534
+ data,
535
+ };
536
+ }