tt-help-cli-ycl 1.3.20 → 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.
- 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/src/cli/attach.js +180 -180
- package/src/cli/auto.js +186 -186
- package/src/cli/config.js +152 -152
- package/src/cli/explore.js +249 -234
- 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 +129 -124
- package/src/lib/args.js +517 -517
- package/src/lib/browser/anti-detect.js +23 -23
- package/src/lib/browser/cdp.js +194 -194
- package/src/lib/browser/launch.js +43 -43
- package/src/lib/browser/page.js +156 -146
- package/src/lib/constants.js +199 -199
- 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/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 +42 -42
- package/src/scraper/auto-core.js +203 -203
- package/src/scraper/core.js +211 -211
- package/src/scraper/explore-core.js +170 -162
- 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 -121
- 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/utils/index.js +31 -0
- package/src/videos/core.js +97 -97
- package/src/watch/data-store.js +828 -680
- package/src/watch/public/index.html +753 -722
- package/src/watch/server.js +705 -562
- package/scripts/test-captcha-lib.mjs +0 -68
- package/scripts/test-captcha.mjs +0 -81
- package/scripts/test-incognito-lib.mjs +0 -36
- package/scripts/test-login-state.mjs +0 -128
- package/scripts/test-safe-click.mjs +0 -45
- package/src/results/user-videos-bar.lar.lar.moeta.json +0 -37
package/src/cli/watch.js
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import { existsSync } from 'fs';
|
|
2
|
-
import { createStore } from '../watch/data-store.js';
|
|
3
|
-
import { startWatchServer, openBrowser } from '../watch/server.js';
|
|
4
|
-
|
|
5
|
-
export async function handleWatch(options) {
|
|
6
|
-
const { outputFile, watchPort } = options;
|
|
7
|
-
|
|
8
|
-
if (!outputFile) {
|
|
9
|
-
console.error('用法: tt-help watch -o <数据文件> [-p 端口]');
|
|
10
|
-
console.error('示例: tt-help watch -o data.json');
|
|
11
|
-
console.error(' tt-help watch -o data.json -p 8080');
|
|
12
|
-
process.exit(1);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if (!existsSync(outputFile)) {
|
|
16
|
-
console.error(`文件不存�? ${outputFile}`);
|
|
17
|
-
process.exit(1);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const store = createStore(outputFile);
|
|
21
|
-
const { server, port } = await startWatchServer(outputFile, watchPort, store);
|
|
22
|
-
openBrowser(port);
|
|
23
|
-
|
|
24
|
-
process.once('SIGINT', () => {
|
|
25
|
-
store.stopBackup();
|
|
26
|
-
server.close();
|
|
27
|
-
process.exit(0);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
console.error('�?Ctrl+C 停止监控服务');
|
|
31
|
-
}
|
|
1
|
+
import { existsSync } from 'fs';
|
|
2
|
+
import { createStore } from '../watch/data-store.js';
|
|
3
|
+
import { startWatchServer, openBrowser } from '../watch/server.js';
|
|
4
|
+
|
|
5
|
+
export async function handleWatch(options) {
|
|
6
|
+
const { outputFile, watchPort } = options;
|
|
7
|
+
|
|
8
|
+
if (!outputFile) {
|
|
9
|
+
console.error('用法: tt-help watch -o <数据文件> [-p 端口]');
|
|
10
|
+
console.error('示例: tt-help watch -o data.json');
|
|
11
|
+
console.error(' tt-help watch -o data.json -p 8080');
|
|
12
|
+
process.exit(1);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (!existsSync(outputFile)) {
|
|
16
|
+
console.error(`文件不存�? ${outputFile}`);
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const store = createStore(outputFile);
|
|
21
|
+
const { server, port } = await startWatchServer(outputFile, watchPort, store);
|
|
22
|
+
openBrowser(port);
|
|
23
|
+
|
|
24
|
+
process.once('SIGINT', () => {
|
|
25
|
+
store.stopBackup();
|
|
26
|
+
server.close();
|
|
27
|
+
process.exit(0);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
console.error('�?Ctrl+C 停止监控服务');
|
|
31
|
+
}
|
|
@@ -1,124 +1,129 @@
|
|
|
1
|
-
import { delay } from './delay.js';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* @param {
|
|
10
|
-
* @param {
|
|
11
|
-
* @
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
apiResolve(
|
|
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
|
-
const
|
|
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
|
-
|
|
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
|
-
|
|
1
|
+
import { delay } from './delay.js';
|
|
2
|
+
import { retryWithBackoff } from './retry.js';
|
|
3
|
+
import { assertPageUrl } from './browser/page.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 通过拦截 TikTok 内部 API 获取用户视频列表
|
|
7
|
+
* 比 DOM 滚动解析快 5-10 倍
|
|
8
|
+
*
|
|
9
|
+
* @param {import('playwright').Page} page - Playwright page (CDP 连接)
|
|
10
|
+
* @param {string} username - TikTok 用户名
|
|
11
|
+
* @param {number} maxVideos - 最大视频数
|
|
12
|
+
* @param {Function} log - 日志函数
|
|
13
|
+
* @returns {Map<string, {id: string, href: string}>} 与 collectVideos 返回格式一致
|
|
14
|
+
*/
|
|
15
|
+
async function fetchUserVideosAPI(page, username, maxVideos, log) {
|
|
16
|
+
const url = `https://www.tiktok.com/@${username}`;
|
|
17
|
+
const items = [];
|
|
18
|
+
|
|
19
|
+
// 1. 注册 response 拦截器 + request URL 捕获
|
|
20
|
+
let apiResolve = null;
|
|
21
|
+
const apiPromise = new Promise(r => { apiResolve = r; });
|
|
22
|
+
|
|
23
|
+
let apiRequestUrl = null;
|
|
24
|
+
|
|
25
|
+
const responseHandler = async (response) => {
|
|
26
|
+
if (response.url().includes('/api/post/item_list/')) {
|
|
27
|
+
try {
|
|
28
|
+
apiResolve(await response.json());
|
|
29
|
+
} catch (e) {
|
|
30
|
+
apiResolve(null);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const requestHandler = (request) => {
|
|
36
|
+
if (request.url().includes('/api/post/item_list/') && !apiRequestUrl) {
|
|
37
|
+
apiRequestUrl = request.url();
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
page.on('response', responseHandler);
|
|
42
|
+
page.on('request', requestHandler);
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
// 2. 导航并等待 API 响应
|
|
46
|
+
log(' [API拦截] 导航到用户页,等待 /api/post/item_list/ ...');
|
|
47
|
+
const t0 = Date.now();
|
|
48
|
+
|
|
49
|
+
await retryWithBackoff(async () => {
|
|
50
|
+
await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 30000 });
|
|
51
|
+
await assertPageUrl(page, `@${username}`);
|
|
52
|
+
}, { maxRetries: 3, baseDelay: 3000, log });
|
|
53
|
+
|
|
54
|
+
const data = await Promise.race([
|
|
55
|
+
apiPromise,
|
|
56
|
+
new Promise(r => setTimeout(() => r(null), 8000)),
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
const elapsed = Date.now() - t0;
|
|
60
|
+
|
|
61
|
+
if (!data || !data.itemList) {
|
|
62
|
+
log(` [API拦截] ${elapsed}ms 后未拿到 API 数据`);
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 3. 提取首页视频
|
|
67
|
+
const firstPageItems = data.itemList || [];
|
|
68
|
+
for (const item of firstPageItems) {
|
|
69
|
+
if (items.length >= maxVideos) break;
|
|
70
|
+
const href = `https://www.tiktok.com/@${username}/video/${item.id}`;
|
|
71
|
+
items.push({ id: item.id, href });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
log(` [API拦截] ${elapsed}ms 获取首页 ${firstPageItems.length} 条视频`);
|
|
75
|
+
|
|
76
|
+
// 4. 翻页获取后续视频
|
|
77
|
+
let cursor = data.cursor;
|
|
78
|
+
let hasMore = data.hasMore;
|
|
79
|
+
|
|
80
|
+
while (hasMore && cursor && items.length < maxVideos) {
|
|
81
|
+
if (!apiRequestUrl) {
|
|
82
|
+
log(' [API拦截] 未捕获到 API 请求 URL,无法翻页');
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const newUrl = apiRequestUrl.replace(/cursor=\d+/, `cursor=${cursor}`);
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
const pageData = await page.evaluate(async (u) => {
|
|
90
|
+
const res = await fetch(u);
|
|
91
|
+
return await res.json();
|
|
92
|
+
}, newUrl);
|
|
93
|
+
|
|
94
|
+
if (pageData && pageData.itemList) {
|
|
95
|
+
for (const item of pageData.itemList) {
|
|
96
|
+
if (items.length >= maxVideos) break;
|
|
97
|
+
const href = `https://www.tiktok.com/@${username}/video/${item.id}`;
|
|
98
|
+
items.push({ id: item.id, href });
|
|
99
|
+
}
|
|
100
|
+
log(` [API拦截] 翻页 cursor=${cursor},获取 ${pageData.itemList.length} 条,累计 ${items.length}`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
cursor = pageData.cursor;
|
|
104
|
+
hasMore = pageData.hasMore;
|
|
105
|
+
} catch (e) {
|
|
106
|
+
log(` [API拦截] 翻页失败: ${e.message}`);
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
await delay(300, 600);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
log(` [API拦截] 总计获取 ${items.length} 条视频`);
|
|
114
|
+
|
|
115
|
+
// 转成 Map 返回,与 collectVideos 一致
|
|
116
|
+
const videoMap = new Map();
|
|
117
|
+
for (const v of items) {
|
|
118
|
+
if (!videoMap.has(v.id)) videoMap.set(v.id, v);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return videoMap;
|
|
122
|
+
} finally {
|
|
123
|
+
// 5. 必须清理拦截器,防止累积
|
|
124
|
+
page.off('response', responseHandler);
|
|
125
|
+
page.off('request', requestHandler);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export { fetchUserVideosAPI };
|