tt-help-cli-ycl 1.3.25 → 1.3.27
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 +464 -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 +182 -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 +54 -6
- 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 +105 -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 +167 -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 +125 -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/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,191 +1,182 @@
|
|
|
1
|
-
import { delay } from
|
|
2
|
-
import { retryWithBackoff } from
|
|
3
|
-
import { assertPageUrl } from
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const
|
|
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
|
-
|
|
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
|
-
return await processAPIResponse(data, username, maxVideos, items, page, apiRequestUrl, log);
|
|
184
|
-
} finally {
|
|
185
|
-
// 5. 必须清理拦截器,防止累积
|
|
186
|
-
page.off('response', responseHandler);
|
|
187
|
-
page.off('request', requestHandler);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
export { fetchUserVideosAPI };
|
|
1
|
+
import { delay } from "./delay.js";
|
|
2
|
+
import { retryWithBackoff } from "./retry.js";
|
|
3
|
+
import { assertPageUrl } from "./browser/page.js";
|
|
4
|
+
import {
|
|
5
|
+
detectPageError,
|
|
6
|
+
detectPageErrorWithWait,
|
|
7
|
+
} from "./page-error-detector.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 处理 API 响应数据:提取首页视频 + 翻页
|
|
11
|
+
*/
|
|
12
|
+
async function processAPIResponse(
|
|
13
|
+
data,
|
|
14
|
+
username,
|
|
15
|
+
maxVideos,
|
|
16
|
+
items,
|
|
17
|
+
page,
|
|
18
|
+
apiRequestUrl,
|
|
19
|
+
log,
|
|
20
|
+
) {
|
|
21
|
+
// 提取首页视频
|
|
22
|
+
const firstPageItems = data.itemList || [];
|
|
23
|
+
for (const item of firstPageItems) {
|
|
24
|
+
if (items.length >= maxVideos) break;
|
|
25
|
+
const href = `https://www.tiktok.com/@${username}/video/${item.id}`;
|
|
26
|
+
items.push({ id: item.id, href });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
log(` [API拦截] 获取首页 ${firstPageItems.length} 条视频`);
|
|
30
|
+
|
|
31
|
+
// 翻页获取后续视频
|
|
32
|
+
let cursor = data.cursor;
|
|
33
|
+
let hasMore = data.hasMore;
|
|
34
|
+
|
|
35
|
+
while (hasMore && cursor && items.length < maxVideos) {
|
|
36
|
+
const reqUrl = apiRequestUrl || data.apiRequestUrl;
|
|
37
|
+
if (!reqUrl) {
|
|
38
|
+
log(" [API拦截] 未捕获到 API 请求 URL,无法翻页");
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const newUrl = reqUrl.replace(/cursor=\d+/, `cursor=${cursor}`);
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
const pageData = await page.evaluate(async (u) => {
|
|
46
|
+
const res = await fetch(u);
|
|
47
|
+
return await res.json();
|
|
48
|
+
}, newUrl);
|
|
49
|
+
|
|
50
|
+
if (pageData && pageData.itemList) {
|
|
51
|
+
for (const item of pageData.itemList) {
|
|
52
|
+
if (items.length >= maxVideos) break;
|
|
53
|
+
const href = `https://www.tiktok.com/@${username}/video/${item.id}`;
|
|
54
|
+
items.push({ id: item.id, href });
|
|
55
|
+
}
|
|
56
|
+
log(
|
|
57
|
+
` [API拦截] 翻页 cursor=${cursor},获取 ${pageData.itemList.length} 条,累计 ${items.length}`,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
cursor = pageData.cursor;
|
|
62
|
+
hasMore = pageData.hasMore;
|
|
63
|
+
} catch (e) {
|
|
64
|
+
log(` [API拦截] 翻页失败: ${e.message}`);
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
await delay(300, 600);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
log(` [API拦截] 总计获取 ${items.length} 条视频`);
|
|
72
|
+
|
|
73
|
+
// 转成 Map 返回,与 collectVideos 一致
|
|
74
|
+
const videoMap = new Map();
|
|
75
|
+
for (const v of items) {
|
|
76
|
+
if (!videoMap.has(v.id)) videoMap.set(v.id, v);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return videoMap;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 通过拦截 TikTok 内部 API 获取用户视频列表
|
|
84
|
+
* 比 DOM 滚动解析快 5-10 倍
|
|
85
|
+
*
|
|
86
|
+
* @param {import('playwright').Page} page - Playwright page (CDP 连接)
|
|
87
|
+
* @param {string} username - TikTok 用户名
|
|
88
|
+
* @param {number} maxVideos - 最大视频数
|
|
89
|
+
* @param {Function} log - 日志函数
|
|
90
|
+
* @returns {Map<string, {id: string, href: string}>} 与 collectVideos 返回格式一致
|
|
91
|
+
* @throws {Error} API 超时或网络异常时抛出
|
|
92
|
+
*/
|
|
93
|
+
async function fetchUserVideosAPI(page, username, maxVideos, log) {
|
|
94
|
+
const url = `https://www.tiktok.com/@${username}`;
|
|
95
|
+
const items = [];
|
|
96
|
+
|
|
97
|
+
// 1. 注册 response 拦截器 + request URL 捕获
|
|
98
|
+
let apiResolve = null;
|
|
99
|
+
const apiPromise = new Promise((r) => {
|
|
100
|
+
apiResolve = r;
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
let apiRequestUrl = null;
|
|
104
|
+
|
|
105
|
+
const responseHandler = async (response) => {
|
|
106
|
+
if (response.url().includes("/api/post/item_list/")) {
|
|
107
|
+
try {
|
|
108
|
+
apiResolve(await response.json());
|
|
109
|
+
} catch (e) {
|
|
110
|
+
apiResolve(null);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const requestHandler = (request) => {
|
|
116
|
+
if (request.url().includes("/api/post/item_list/") && !apiRequestUrl) {
|
|
117
|
+
apiRequestUrl = request.url();
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
page.on("response", responseHandler);
|
|
122
|
+
page.on("request", requestHandler);
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
// 2. 导航并等待 API 响应
|
|
126
|
+
log(" [API拦截] 导航到用户页,等待 /api/post/item_list/ ...");
|
|
127
|
+
const t0 = Date.now();
|
|
128
|
+
|
|
129
|
+
await retryWithBackoff(
|
|
130
|
+
async () => {
|
|
131
|
+
await page.goto(url, { waitUntil: "domcontentloaded", timeout: 30000 });
|
|
132
|
+
await assertPageUrl(page, `@${username}`);
|
|
133
|
+
},
|
|
134
|
+
{ maxRetries: 3, baseDelay: 3000, log },
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
const data = await Promise.race([
|
|
138
|
+
apiPromise,
|
|
139
|
+
new Promise((r) => setTimeout(() => r(null), 8000)),
|
|
140
|
+
]);
|
|
141
|
+
|
|
142
|
+
const elapsed = Date.now() - t0;
|
|
143
|
+
|
|
144
|
+
if (!data || !data.itemList) {
|
|
145
|
+
// 使用 waitForFunction 等待错误信息出现(最多 8 秒)
|
|
146
|
+
const pageError = await detectPageErrorWithWait(page, 8000);
|
|
147
|
+
|
|
148
|
+
if (pageError === "service_error") {
|
|
149
|
+
throw new Error(`被封: ${username}`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// API 超时了,页面肯定有问题
|
|
153
|
+
// 如果识别不出具体错误类型,抛异常(而不是静默返回空 Map)
|
|
154
|
+
if (!pageError) {
|
|
155
|
+
throw new Error(`@${username} 页面异常(无法识别错误类型)`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 其他已知错误(login_required, captcha, rate_limited 等)返回空 Map
|
|
159
|
+
log(
|
|
160
|
+
` [API拦截] ${elapsed}ms 后未拿到 API 数据,页面异常(${pageError})`,
|
|
161
|
+
);
|
|
162
|
+
return new Map();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// 处理数据(提取首页 + 翻页)
|
|
166
|
+
return await processAPIResponse(
|
|
167
|
+
data,
|
|
168
|
+
username,
|
|
169
|
+
maxVideos,
|
|
170
|
+
items,
|
|
171
|
+
page,
|
|
172
|
+
apiRequestUrl,
|
|
173
|
+
log,
|
|
174
|
+
);
|
|
175
|
+
} finally {
|
|
176
|
+
// 5. 必须清理拦截器,防止累积
|
|
177
|
+
page.off("response", responseHandler);
|
|
178
|
+
page.off("request", requestHandler);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export { fetchUserVideosAPI };
|