tt-help-cli-ycl 1.3.19 → 1.3.21

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 (58) 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/src/cli/attach.js +180 -180
  9. package/src/cli/auto.js +186 -186
  10. package/src/cli/config.js +152 -152
  11. package/src/cli/explore.js +249 -234
  12. package/src/cli/info.js +88 -88
  13. package/src/cli/progress.js +111 -111
  14. package/src/cli/refresh.js +216 -216
  15. package/src/cli/scrape.js +47 -47
  16. package/src/cli/utils.js +18 -18
  17. package/src/cli/videos.js +41 -41
  18. package/src/cli/watch.js +31 -31
  19. package/src/lib/api-interceptor.js +129 -124
  20. package/src/lib/args.js +517 -517
  21. package/src/lib/browser/anti-detect.js +23 -23
  22. package/src/lib/browser/cdp.js +194 -194
  23. package/src/lib/browser/launch.js +43 -43
  24. package/src/lib/browser/page.js +156 -146
  25. package/src/lib/constants.js +199 -199
  26. package/src/lib/delay.js +54 -54
  27. package/src/lib/explore-fetch.js +118 -118
  28. package/src/lib/fetcher.js +45 -45
  29. package/src/lib/filter.js +66 -66
  30. package/src/lib/io.js +54 -54
  31. package/src/lib/output.js +80 -80
  32. package/src/lib/parse-ssr.mjs +69 -69
  33. package/src/lib/parser.js +47 -47
  34. package/src/lib/retry.js +45 -45
  35. package/src/lib/scrape.js +89 -89
  36. package/src/lib/tiktok-scraper.mjs +194 -194
  37. package/src/lib/url.js +52 -52
  38. package/src/main.js +42 -42
  39. package/src/scraper/auto-core.js +203 -203
  40. package/src/scraper/core.js +211 -211
  41. package/src/scraper/explore-core.js +170 -162
  42. package/src/scraper/modules/captcha-handler.js +114 -114
  43. package/src/scraper/modules/comment-extractor.js +74 -74
  44. package/src/scraper/modules/follow-extractor.js +118 -121
  45. package/src/scraper/modules/guess-extractor.js +51 -51
  46. package/src/scraper/modules/page-helpers.js +48 -48
  47. package/src/scraper/refresh-core.js +179 -179
  48. package/src/utils/index.js +31 -0
  49. package/src/videos/core.js +97 -97
  50. package/src/watch/data-store.js +828 -705
  51. package/src/watch/public/index.html +753 -722
  52. package/src/watch/server.js +705 -562
  53. package/scripts/test-captcha-lib.mjs +0 -68
  54. package/scripts/test-captcha.mjs +0 -81
  55. package/scripts/test-incognito-lib.mjs +0 -36
  56. package/scripts/test-login-state.mjs +0 -128
  57. package/scripts/test-safe-click.mjs +0 -45
  58. package/src/results/user-videos-bar.lar.lar.moeta.json +0 -37
@@ -1,179 +1,179 @@
1
- import {
2
- delay,
3
- retryWithBackoff,
4
- detectPageError,
5
- assertPageUrl,
6
- } from './modules/page-helpers.js';
7
- import { detectCaptcha } from './modules/captcha-handler.js';
8
- import {
9
- getUserInfo,
10
- collectVideos,
11
- } from '../videos/core.js';
12
- import { extractFollowAndFollowers } from './modules/follow-extractor.js';
13
- import { processExplore } from './explore-core.js';
14
-
15
- export async function processRefresh(page, username, serverUrl, options, log) {
16
- const {
17
- maxFollowing = 100,
18
- maxFollowers = 100,
19
- maxVideos = 100,
20
- } = options;
21
-
22
- const result = {
23
- userInfo: null,
24
- discoveredVideoAuthors: [],
25
- discoveredFollowing: [],
26
- discoveredFollowers: [],
27
- newUsersAdded: 0,
28
- collectedVideos: 0,
29
- error: null,
30
- };
31
-
32
- try {
33
- log(` 访问 @${username} 主页...`);
34
- const homeUrl = `https://www.tiktok.com/@${username}`;
35
- await retryWithBackoff(async () => {
36
- await page.goto(homeUrl, { waitUntil: 'domcontentloaded', timeout: 30000 });
37
- assertPageUrl(page, `@${username}`);
38
- }, { log });
39
- await page.waitForSelector('[class*="DivVideoList"]', { timeout: 10000 }).catch(() => {});
40
- await delay(1000, 2000);
41
-
42
- log(' 获取用户信息...');
43
- const info = await getUserInfo(page);
44
- if (info) {
45
- result.userInfo = info;
46
- log(` 用户: ${info.nickname || username} | 粉丝: ${info.followerCount || '-'} | 视频: ${info.videoCount || '-'}`);
47
- }
48
-
49
- const captcha = await detectCaptcha(page);
50
- if (captcha && captcha.visible) {
51
- log(`[验证码] @${username} 页面出现验证码`);
52
- result.captchaDetected = true;
53
- result.captchaStage = result.captchaStage || 'video-page';
54
- result.captchaMessage = result.captchaMessage || '视频页出现验证码';
55
- }
56
-
57
- // 采集视频
58
- log(` 采集视频 (最多 ${maxVideos} 个)...`);
59
- const videoList = await collectVideos(page, username, maxVideos, log);
60
- const videoArray = videoList ? [...videoList.values()] : [];
61
- result.collectedVideos = videoArray.length;
62
- result.discoveredVideoAuthors = videoArray.map(v => v.author);
63
-
64
- if (videoArray.length <= 0) {
65
- result.noVideo = true;
66
- const pageError = await detectPageError(page);
67
- if (pageError) {
68
- result.restricted = true;
69
- log(` @${username} 页面受限(${pageError}),标记跳过`);
70
- }
71
- return result;
72
- }
73
-
74
- // 采集关注和粉丝
75
- log(` 采集关注 (最多 ${maxFollowing}) + 粉丝 (最多 ${maxFollowers})...`);
76
- try {
77
- const followResult = await extractFollowAndFollowers(page, {
78
- maxFollowing,
79
- maxFollowers,
80
- });
81
- result.discoveredFollowing = followResult.following || [];
82
- result.discoveredFollowers = followResult.followers || [];
83
- log(` 关注: ${result.discoveredFollowing.length}, 粉丝: ${result.discoveredFollowers.length}`);
84
- } catch (e) {
85
- log(` [关注/粉丝采集失败] ${e.message}`);
86
- result.discoveredFollowing = [];
87
- result.discoveredFollowers = [];
88
- }
89
-
90
- // 处理新发现的用户(关注 + 粉丝),循环执行完整 explore
91
- // follow-extractor 返回 [handle, displayName] 数组
92
- const allDiscovered = [
93
- ...result.discoveredFollowing.map(h => ({ handle: Array.isArray(h) ? h[0] : h, source: 'refresh-following' })),
94
- ...result.discoveredFollowers.map(h => ({ handle: Array.isArray(h) ? h[0] : h, source: 'refresh-follower' })),
95
- ];
96
-
97
- for (const { handle, source } of allDiscovered) {
98
- const uniqueId = handle.replace('@', '');
99
-
100
- // 检查用户是否已存在
101
- const existsResp = await fetch(`${serverUrl}/api/user-exists/${encodeURIComponent(uniqueId)}`);
102
- const existsData = await existsResp.json();
103
-
104
- if (existsData.exists) {
105
- continue;
106
- }
107
-
108
- log(` [新用户] @${uniqueId} 不存在,开始探索 (来源: ${source})...`);
109
- await delay(1000, 2000);
110
-
111
- // 对新用户做完整 explore(与 explore 命令逻辑一致)
112
- const exploreResult = await processExplore(page, uniqueId, {
113
- maxComments: 10,
114
- maxGuess: 0,
115
- enableFollow: true,
116
- maxFollowing: 5,
117
- maxFollowers: 5,
118
- location: 'PL,NL,BE,DE,FR,IT,ES,IE',
119
- }, log);
120
-
121
- // 提交 explore 结果到服务端(和 explore 命令的 commitJob 一致)
122
- if (exploreResult.userInfo) {
123
- const guessedLocation = exploreResult.locationCreated || null;
124
-
125
- const payload = {
126
- userInfo: exploreResult.userInfo || {},
127
- discoveredVideoAuthors: (exploreResult.discoveredVideoAuthors || []).map(item =>
128
- typeof item === 'object' ? { ...item, guessedLocation } : item
129
- ),
130
- discoveredCommentAuthors: (exploreResult.discoveredCommentAuthors || []).map(author => ({ author, guessedLocation })),
131
- discoveredGuessAuthors: (exploreResult.discoveredGuessAuthors || []).map(author => ({ author, guessedLocation })),
132
- discoveredFollowing: (exploreResult.discoveredFollowing || []).map(f => ({
133
- handle: Array.isArray(f) ? f[0] : f,
134
- displayName: Array.isArray(f) ? f[1] : null,
135
- guessedLocation,
136
- })),
137
- discoveredFollowers: (exploreResult.discoveredFollowers || []).map(f => ({
138
- handle: Array.isArray(f) ? f[0] : f,
139
- displayName: Array.isArray(f) ? f[1] : null,
140
- guessedLocation,
141
- })),
142
- processed: exploreResult.processed,
143
- hasFollowData: exploreResult.hasFollowData,
144
- keepFollow: exploreResult.keepFollow,
145
- locationCreated: exploreResult.locationCreated,
146
- noVideo: exploreResult.noVideo,
147
- restricted: exploreResult.restricted,
148
- error: exploreResult.error,
149
- };
150
-
151
- const addResp = await fetch(`${serverUrl}/api/explore-new/${uniqueId}`, {
152
- method: 'POST',
153
- headers: { 'Content-Type': 'application/json' },
154
- body: JSON.stringify(payload),
155
- });
156
- const addResult = await addResp.json();
157
-
158
- if (!addResult.saved) {
159
- log(` [跳过] @${uniqueId} 提交失败`);
160
- continue;
161
- }
162
-
163
- result.newUsersAdded++;
164
- if (exploreResult.captchaDetected) {
165
- result.captchaDetected = true;
166
- }
167
- log(` [已提交] @${uniqueId} ${addResult.created ? '(新用户)' : '(已存在)'} | 发现: ${addResult.newUsers?.length || 0} 个`);
168
- }
169
-
170
- await delay(2000, 4000);
171
- }
172
- } catch (e) {
173
- log(` [错误] ${e.message}`);
174
- result.error = e.message;
175
- result.errorStack = e.stack || '';
176
- }
177
-
178
- return result;
179
- }
1
+ import {
2
+ delay,
3
+ retryWithBackoff,
4
+ detectPageError,
5
+ assertPageUrl,
6
+ } from './modules/page-helpers.js';
7
+ import { detectCaptcha } from './modules/captcha-handler.js';
8
+ import {
9
+ getUserInfo,
10
+ collectVideos,
11
+ } from '../videos/core.js';
12
+ import { extractFollowAndFollowers } from './modules/follow-extractor.js';
13
+ import { processExplore } from './explore-core.js';
14
+
15
+ export async function processRefresh(page, username, serverUrl, options, log) {
16
+ const {
17
+ maxFollowing = 100,
18
+ maxFollowers = 100,
19
+ maxVideos = 100,
20
+ } = options;
21
+
22
+ const result = {
23
+ userInfo: null,
24
+ discoveredVideoAuthors: [],
25
+ discoveredFollowing: [],
26
+ discoveredFollowers: [],
27
+ newUsersAdded: 0,
28
+ collectedVideos: 0,
29
+ error: null,
30
+ };
31
+
32
+ try {
33
+ log(` 访问 @${username} 主页...`);
34
+ const homeUrl = `https://www.tiktok.com/@${username}`;
35
+ await retryWithBackoff(async () => {
36
+ await page.goto(homeUrl, { waitUntil: 'domcontentloaded', timeout: 30000 });
37
+ assertPageUrl(page, `@${username}`);
38
+ }, { log });
39
+ await page.waitForSelector('[class*="DivVideoList"]', { timeout: 10000 }).catch(() => {});
40
+ await delay(1000, 2000);
41
+
42
+ log(' 获取用户信息...');
43
+ const info = await getUserInfo(page);
44
+ if (info) {
45
+ result.userInfo = info;
46
+ log(` 用户: ${info.nickname || username} | 粉丝: ${info.followerCount || '-'} | 视频: ${info.videoCount || '-'}`);
47
+ }
48
+
49
+ const captcha = await detectCaptcha(page);
50
+ if (captcha && captcha.visible) {
51
+ log(`[验证码] @${username} 页面出现验证码`);
52
+ result.captchaDetected = true;
53
+ result.captchaStage = result.captchaStage || 'video-page';
54
+ result.captchaMessage = result.captchaMessage || '视频页出现验证码';
55
+ }
56
+
57
+ // 采集视频
58
+ log(` 采集视频 (最多 ${maxVideos} 个)...`);
59
+ const videoList = await collectVideos(page, username, maxVideos, log);
60
+ const videoArray = videoList ? [...videoList.values()] : [];
61
+ result.collectedVideos = videoArray.length;
62
+ result.discoveredVideoAuthors = videoArray.map(v => v.author);
63
+
64
+ if (videoArray.length <= 0) {
65
+ result.noVideo = true;
66
+ const pageError = await detectPageError(page);
67
+ if (pageError) {
68
+ result.restricted = true;
69
+ log(` @${username} 页面受限(${pageError}),标记跳过`);
70
+ }
71
+ return result;
72
+ }
73
+
74
+ // 采集关注和粉丝
75
+ log(` 采集关注 (最多 ${maxFollowing}) + 粉丝 (最多 ${maxFollowers})...`);
76
+ try {
77
+ const followResult = await extractFollowAndFollowers(page, {
78
+ maxFollowing,
79
+ maxFollowers,
80
+ });
81
+ result.discoveredFollowing = followResult.following || [];
82
+ result.discoveredFollowers = followResult.followers || [];
83
+ log(` 关注: ${result.discoveredFollowing.length}, 粉丝: ${result.discoveredFollowers.length}`);
84
+ } catch (e) {
85
+ log(` [关注/粉丝采集失败] ${e.message}`);
86
+ result.discoveredFollowing = [];
87
+ result.discoveredFollowers = [];
88
+ }
89
+
90
+ // 处理新发现的用户(关注 + 粉丝),循环执行完整 explore
91
+ // follow-extractor 返回 [handle, displayName] 数组
92
+ const allDiscovered = [
93
+ ...result.discoveredFollowing.map(h => ({ handle: Array.isArray(h) ? h[0] : h, source: 'refresh-following' })),
94
+ ...result.discoveredFollowers.map(h => ({ handle: Array.isArray(h) ? h[0] : h, source: 'refresh-follower' })),
95
+ ];
96
+
97
+ for (const { handle, source } of allDiscovered) {
98
+ const uniqueId = handle.replace('@', '');
99
+
100
+ // 检查用户是否已存在
101
+ const existsResp = await fetch(`${serverUrl}/api/user-exists/${encodeURIComponent(uniqueId)}`);
102
+ const existsData = await existsResp.json();
103
+
104
+ if (existsData.exists) {
105
+ continue;
106
+ }
107
+
108
+ log(` [新用户] @${uniqueId} 不存在,开始探索 (来源: ${source})...`);
109
+ await delay(1000, 2000);
110
+
111
+ // 对新用户做完整 explore(与 explore 命令逻辑一致)
112
+ const exploreResult = await processExplore(page, uniqueId, {
113
+ maxComments: 10,
114
+ maxGuess: 0,
115
+ enableFollow: true,
116
+ maxFollowing: 5,
117
+ maxFollowers: 5,
118
+ location: 'PL,NL,BE,DE,FR,IT,ES,IE',
119
+ }, log);
120
+
121
+ // 提交 explore 结果到服务端(和 explore 命令的 commitJob 一致)
122
+ if (exploreResult.userInfo) {
123
+ const guessedLocation = exploreResult.locationCreated || null;
124
+
125
+ const payload = {
126
+ userInfo: exploreResult.userInfo || {},
127
+ discoveredVideoAuthors: (exploreResult.discoveredVideoAuthors || []).map(item =>
128
+ typeof item === 'object' ? { ...item, guessedLocation } : item
129
+ ),
130
+ discoveredCommentAuthors: (exploreResult.discoveredCommentAuthors || []).map(author => ({ author, guessedLocation })),
131
+ discoveredGuessAuthors: (exploreResult.discoveredGuessAuthors || []).map(author => ({ author, guessedLocation })),
132
+ discoveredFollowing: (exploreResult.discoveredFollowing || []).map(f => ({
133
+ handle: Array.isArray(f) ? f[0] : f,
134
+ displayName: Array.isArray(f) ? f[1] : null,
135
+ guessedLocation,
136
+ })),
137
+ discoveredFollowers: (exploreResult.discoveredFollowers || []).map(f => ({
138
+ handle: Array.isArray(f) ? f[0] : f,
139
+ displayName: Array.isArray(f) ? f[1] : null,
140
+ guessedLocation,
141
+ })),
142
+ processed: exploreResult.processed,
143
+ hasFollowData: exploreResult.hasFollowData,
144
+ keepFollow: exploreResult.keepFollow,
145
+ locationCreated: exploreResult.locationCreated,
146
+ noVideo: exploreResult.noVideo,
147
+ restricted: exploreResult.restricted,
148
+ error: exploreResult.error,
149
+ };
150
+
151
+ const addResp = await fetch(`${serverUrl}/api/explore-new/${uniqueId}`, {
152
+ method: 'POST',
153
+ headers: { 'Content-Type': 'application/json' },
154
+ body: JSON.stringify(payload),
155
+ });
156
+ const addResult = await addResp.json();
157
+
158
+ if (!addResult.saved) {
159
+ log(` [跳过] @${uniqueId} 提交失败`);
160
+ continue;
161
+ }
162
+
163
+ result.newUsersAdded++;
164
+ if (exploreResult.captchaDetected) {
165
+ result.captchaDetected = true;
166
+ }
167
+ log(` [已提交] @${uniqueId} ${addResult.created ? '(新用户)' : '(已存在)'} | 发现: ${addResult.newUsers?.length || 0} 个`);
168
+ }
169
+
170
+ await delay(2000, 4000);
171
+ }
172
+ } catch (e) {
173
+ log(` [错误] ${e.message}`);
174
+ result.error = e.message;
175
+ result.errorStack = e.stack || '';
176
+ }
177
+
178
+ return result;
179
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * 显示当前进程的内存和 CPU 使用情况
3
+ *
4
+ * @param {Function} log - 日志函数
5
+ * @returns {string} 格式化的状态字符串
6
+ */
7
+ export function showResourceUsage(log = console.error) {
8
+ const mem = process.memoryUsage();
9
+ const memMB = (mem.rss / 1024 / 1024).toFixed(1);
10
+ const heapUsedMB = (mem.heapUsed / 1024 / 1024).toFixed(1);
11
+ const heapTotalMB = (mem.heapTotal / 1024 / 1024).toFixed(1);
12
+
13
+ // CPU 使用率需要两次采样
14
+ const cpuStart = process.cpuUsage();
15
+ const now = new Date().toISOString().substring(11, 23);
16
+
17
+ const msg = `[${now}] 内存 RSS: ${memMB}MB | Heap: ${heapUsedMB}/${heapTotalMB}MB`;
18
+ log(msg);
19
+ return msg;
20
+ }
21
+
22
+ /**
23
+ * 启动周期性资源监控,每隔 intervalMs 打印一次
24
+ *
25
+ * @param {number} intervalMs - 间隔毫秒,默认 30000
26
+ * @param {Function} log - 日志函数
27
+ * @returns {NodeJS.Timeout} 定时器句柄, clearInterval 可停止
28
+ */
29
+ export function startResourceMonitor(intervalMs, log) {
30
+ return setInterval(() => showResourceUsage(log), intervalMs);
31
+ }
@@ -1,97 +1,97 @@
1
- import { delay, ensureBrowserReady, ensureTikTokPage, retryWithBackoff } from '../scraper/modules/page-helpers.js';
2
- import { fetchUserVideosAPI } from '../lib/api-interceptor.js';
3
-
4
- async function getUserInfo(page) {
5
- return await page.evaluate(() => {
6
- const html = document.documentElement.outerHTML;
7
- const result = {};
8
-
9
- const m = window.location.href.match(/\/@([^/]+)/);
10
- if (m) result.uniqueId = m[1];
11
-
12
- const patterns = {
13
- secUid: /"secUid":"([^"]+)"/,
14
- nickname: /"nickname":"((?:[^"\\]|\\.)*)"/,
15
- ttSeller: /"ttSeller":\s*(true|false)/,
16
- verified: /"verified":\s*(true|false)/,
17
- followerCount: /"followerCount":(\d+)/,
18
- videoCount: /"videoCount":(\d+)/,
19
- followingCount: /"followingCount":(\d+)/,
20
- heartCount: /"heartCount":(\d+)/,
21
- signature: /"signature":"((?:[^"\\]|\\.)*)"/,
22
- locationCreated: /"locationCreated":"([^"]*)/,
23
- region: /"region":"([^"]*)/,
24
- };
25
-
26
- const boolKeys = ['ttSeller', 'verified'];
27
- const numKeys = ['followerCount', 'videoCount', 'followingCount', 'heartCount'];
28
-
29
- for (const [key, pat] of Object.entries(patterns)) {
30
- const match = html.match(pat);
31
- if (match) {
32
- if (boolKeys.includes(key)) result[key] = match[1] === 'true';
33
- else if (numKeys.includes(key)) result[key] = parseInt(match[1], 10);
34
- else if (key === 'signature') result[key] = match[1].replace(/\\n/g, '\n').replace(/\\\\/g, '\\');
35
- else result[key] = match[1];
36
- }
37
- }
38
-
39
- return result;
40
- });
41
- }
42
-
43
- async function collectVideos(page, username, maxVideos, log) {
44
- const apiResult = await fetchUserVideosAPI(page, username, maxVideos, log);
45
- if (apiResult && apiResult.size > 0) {
46
- log(`收集完成: ${apiResult.size} 个视频`);
47
- return apiResult;
48
- }
49
- return new Map();
50
- }
51
-
52
- async function runGetUserVideos(options) {
53
- const { username, maxVideos = 5, log = console.error } = options;
54
- const url = `https://www.tiktok.com/@${username}`;
55
-
56
- log(`用户: @${username}`);
57
- log(`URL: ${url}`);
58
- log(`最大视频数: ${maxVideos}\n`);
59
-
60
- log('连接浏览器...');
61
- const browser = await ensureBrowserReady();
62
-
63
- let page;
64
- try {
65
- page = await ensureTikTokPage(browser, url);
66
- } catch (e) {
67
- await browser.close().catch(() => {});
68
- throw e;
69
- }
70
-
71
- await retryWithBackoff(() => page.goto(url, { waitUntil: 'load', timeout: 30000 }), { log });
72
- await delay(3000, 5000);
73
- await page.waitForSelector('[class*="DivVideoList"]', { timeout: 10000 }).catch(() => {});
74
-
75
- log('获取用户信息...');
76
- const userInfo = await getUserInfo(page);
77
- log('用户信息: ' + JSON.stringify(userInfo, null, 2));
78
-
79
- log('\n开始滚动收集视频...');
80
- const videos = await collectVideos(page, username, maxVideos, log);
81
- const allVideos = Array.from(videos.values());
82
-
83
- log(`\n总计: ${allVideos.length} 个视频`);
84
-
85
- const output = {
86
- user: userInfo,
87
- totalVideos: Math.min(allVideos.length, maxVideos),
88
- videos: allVideos.slice(0, maxVideos).map(v => ({
89
- id: v.id,
90
- url: v.href.startsWith('http') ? v.href : `https://www.tiktok.com${v.href}`,
91
- })),
92
- };
93
-
94
- return { output, browser };
95
- }
96
-
97
- export { getUserInfo, collectVideos, runGetUserVideos };
1
+ import { delay, ensureBrowserReady, ensureTikTokPage, retryWithBackoff } from '../scraper/modules/page-helpers.js';
2
+ import { fetchUserVideosAPI } from '../lib/api-interceptor.js';
3
+
4
+ async function getUserInfo(page) {
5
+ return await page.evaluate(() => {
6
+ const html = document.documentElement.outerHTML;
7
+ const result = {};
8
+
9
+ const m = window.location.href.match(/\/@([^/]+)/);
10
+ if (m) result.uniqueId = m[1];
11
+
12
+ const patterns = {
13
+ secUid: /"secUid":"([^"]+)"/,
14
+ nickname: /"nickname":"((?:[^"\\]|\\.)*)"/,
15
+ ttSeller: /"ttSeller":\s*(true|false)/,
16
+ verified: /"verified":\s*(true|false)/,
17
+ followerCount: /"followerCount":(\d+)/,
18
+ videoCount: /"videoCount":(\d+)/,
19
+ followingCount: /"followingCount":(\d+)/,
20
+ heartCount: /"heartCount":(\d+)/,
21
+ signature: /"signature":"((?:[^"\\]|\\.)*)"/,
22
+ locationCreated: /"locationCreated":"([^"]*)/,
23
+ region: /"region":"([^"]*)/,
24
+ };
25
+
26
+ const boolKeys = ['ttSeller', 'verified'];
27
+ const numKeys = ['followerCount', 'videoCount', 'followingCount', 'heartCount'];
28
+
29
+ for (const [key, pat] of Object.entries(patterns)) {
30
+ const match = html.match(pat);
31
+ if (match) {
32
+ if (boolKeys.includes(key)) result[key] = match[1] === 'true';
33
+ else if (numKeys.includes(key)) result[key] = parseInt(match[1], 10);
34
+ else if (key === 'signature') result[key] = match[1].replace(/\\n/g, '\n').replace(/\\\\/g, '\\');
35
+ else result[key] = match[1];
36
+ }
37
+ }
38
+
39
+ return result;
40
+ });
41
+ }
42
+
43
+ async function collectVideos(page, username, maxVideos, log) {
44
+ const apiResult = await fetchUserVideosAPI(page, username, maxVideos, log);
45
+ if (apiResult && apiResult.size > 0) {
46
+ log(`收集完成: ${apiResult.size} 个视频`);
47
+ return apiResult;
48
+ }
49
+ return new Map();
50
+ }
51
+
52
+ async function runGetUserVideos(options) {
53
+ const { username, maxVideos = 5, log = console.error } = options;
54
+ const url = `https://www.tiktok.com/@${username}`;
55
+
56
+ log(`用户: @${username}`);
57
+ log(`URL: ${url}`);
58
+ log(`最大视频数: ${maxVideos}\n`);
59
+
60
+ log('连接浏览器...');
61
+ const browser = await ensureBrowserReady();
62
+
63
+ let page;
64
+ try {
65
+ page = await ensureTikTokPage(browser, url);
66
+ } catch (e) {
67
+ await browser.close().catch(() => {});
68
+ throw e;
69
+ }
70
+
71
+ await retryWithBackoff(() => page.goto(url, { waitUntil: 'load', timeout: 30000 }), { log });
72
+ await delay(3000, 5000);
73
+ await page.waitForSelector('[class*="DivVideoList"]', { timeout: 10000 }).catch(() => {});
74
+
75
+ log('获取用户信息...');
76
+ const userInfo = await getUserInfo(page);
77
+ log('用户信息: ' + JSON.stringify(userInfo, null, 2));
78
+
79
+ log('\n开始滚动收集视频...');
80
+ const videos = await collectVideos(page, username, maxVideos, log);
81
+ const allVideos = Array.from(videos.values());
82
+
83
+ log(`\n总计: ${allVideos.length} 个视频`);
84
+
85
+ const output = {
86
+ user: userInfo,
87
+ totalVideos: Math.min(allVideos.length, maxVideos),
88
+ videos: allVideos.slice(0, maxVideos).map(v => ({
89
+ id: v.id,
90
+ url: v.href.startsWith('http') ? v.href : `https://www.tiktok.com${v.href}`,
91
+ })),
92
+ };
93
+
94
+ return { output, browser };
95
+ }
96
+
97
+ export { getUserInfo, collectVideos, runGetUserVideos };