tt-help-cli-ycl 1.3.45 → 1.3.46
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 +33 -33
- package/cli.js +9 -9
- package/package.json +52 -52
- package/scripts/run-explore copy.bat +101 -101
- package/scripts/run-explore.bat +134 -134
- package/scripts/run-explore.ps1 +159 -159
- package/scripts/run-explore.sh +121 -121
- package/src/cli/attach.js +331 -313
- package/src/cli/auto.js +265 -265
- package/src/cli/comments.js +620 -620
- package/src/cli/config.js +170 -170
- package/src/cli/db-import.js +51 -51
- package/src/cli/explore.js +555 -555
- package/src/cli/info.js +10 -16
- package/src/cli/open.js +111 -111
- package/src/cli/progress.js +111 -111
- package/src/cli/refresh.js +288 -288
- package/src/cli/scrape.js +47 -47
- package/src/cli/utils.js +18 -18
- package/src/cli/videos.js +41 -41
- package/src/cli/videostats.js +196 -196
- package/src/cli/watch.js +30 -30
- package/src/cli/webserver.js +19 -0
- package/src/lib/api-interceptor.js +161 -161
- package/src/lib/args.js +809 -778
- package/src/lib/browser/anti-detect.js +23 -23
- package/src/lib/browser/cdp.js +261 -261
- package/src/lib/browser/health-checker.js +114 -114
- package/src/lib/browser/launch.js +43 -43
- package/src/lib/browser/page.js +184 -184
- package/src/lib/constants.js +297 -287
- 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 +109 -109
- 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 +90 -89
- package/src/lib/target-locations.js +61 -61
- package/src/lib/tiktok-scraper.mjs +160 -106
- package/src/lib/url.js +52 -52
- package/src/main.js +73 -70
- package/src/npm-main.js +70 -69
- package/src/scraper/auto-core.js +203 -203
- package/src/scraper/core.js +255 -255
- package/src/scraper/explore-core.js +208 -208
- package/src/scraper/modules/captcha-handler.js +114 -114
- package/src/scraper/modules/follow-extractor.js +250 -250
- package/src/scraper/modules/guess-extractor.js +51 -51
- package/src/scraper/modules/page-helpers.js +48 -48
- package/src/scraper/refresh-core.js +213 -213
- package/src/videos/core.js +143 -143
- package/src/watch/data-store.js +2980 -2978
- package/src/watch/public/index.html +2355 -2345
- package/src/watch/server.js +727 -727
- package/src/webserver/server.mjs +174 -0
- 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/scripts/test-watch-db-smoke.mjs +0 -246
- package/src/results/user-videos-bar.lar.lar.moeta.json +0 -37
package/src/npm-main.js
CHANGED
|
@@ -1,69 +1,70 @@
|
|
|
1
|
-
import { parseArgs } from "./lib/args.js";
|
|
2
|
-
import { PUBLIC_HELP_TEXT } from "./lib/constants.js";
|
|
3
|
-
import { handleInfo } from "./cli/info.js";
|
|
4
|
-
import { handleExplore } from "./cli/explore.js";
|
|
5
|
-
import { handleAttach } from "./cli/attach.js";
|
|
6
|
-
import { handleConfig, showConfig, showUsage, version } from "./cli/config.js";
|
|
7
|
-
import { handleOpen } from "./cli/open.js";
|
|
8
|
-
import { handleComments } from "./cli/comments.js";
|
|
9
|
-
|
|
10
|
-
function exitUnsupportedCommand(command) {
|
|
11
|
-
console.error(
|
|
12
|
-
`[${command}] 当前 npm 发布包不包含该命令;如需使用,请在仓库源码环境中运行 node src/main.js ${command} ...`,
|
|
13
|
-
);
|
|
14
|
-
process.exit(1);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
async function main() {
|
|
18
|
-
const parsed = parseArgs();
|
|
19
|
-
|
|
20
|
-
switch (parsed.subcommand) {
|
|
21
|
-
case "explore":
|
|
22
|
-
return handleExplore(parsed);
|
|
23
|
-
case "info":
|
|
24
|
-
return handleInfo(parsed);
|
|
25
|
-
case "attach":
|
|
26
|
-
return handleAttach(parsed);
|
|
27
|
-
case "watch":
|
|
28
|
-
case "
|
|
29
|
-
case "
|
|
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
|
-
if (
|
|
55
|
-
if (
|
|
56
|
-
if (
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
1
|
+
import { parseArgs } from "./lib/args.js";
|
|
2
|
+
import { PUBLIC_HELP_TEXT } from "./lib/constants.js";
|
|
3
|
+
import { handleInfo } from "./cli/info.js";
|
|
4
|
+
import { handleExplore } from "./cli/explore.js";
|
|
5
|
+
import { handleAttach } from "./cli/attach.js";
|
|
6
|
+
import { handleConfig, showConfig, showUsage, version } from "./cli/config.js";
|
|
7
|
+
import { handleOpen } from "./cli/open.js";
|
|
8
|
+
import { handleComments } from "./cli/comments.js";
|
|
9
|
+
|
|
10
|
+
function exitUnsupportedCommand(command) {
|
|
11
|
+
console.error(
|
|
12
|
+
`[${command}] 当前 npm 发布包不包含该命令;如需使用,请在仓库源码环境中运行 node src/main.js ${command} ...`,
|
|
13
|
+
);
|
|
14
|
+
process.exit(1);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function main() {
|
|
18
|
+
const parsed = parseArgs();
|
|
19
|
+
|
|
20
|
+
switch (parsed.subcommand) {
|
|
21
|
+
case "explore":
|
|
22
|
+
return handleExplore(parsed);
|
|
23
|
+
case "info":
|
|
24
|
+
return handleInfo(parsed);
|
|
25
|
+
case "attach":
|
|
26
|
+
return handleAttach(parsed);
|
|
27
|
+
case "watch":
|
|
28
|
+
case "webserver":
|
|
29
|
+
case "videostats":
|
|
30
|
+
case "db-import":
|
|
31
|
+
return exitUnsupportedCommand(parsed.subcommand);
|
|
32
|
+
case "open":
|
|
33
|
+
return handleOpen(parsed);
|
|
34
|
+
case "comments":
|
|
35
|
+
return handleComments(parsed);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const {
|
|
39
|
+
urls,
|
|
40
|
+
outputFile,
|
|
41
|
+
exploreCount,
|
|
42
|
+
showConfig: showCfg,
|
|
43
|
+
showHelp,
|
|
44
|
+
showVersion,
|
|
45
|
+
configAction,
|
|
46
|
+
configKey,
|
|
47
|
+
configValue,
|
|
48
|
+
} = parsed;
|
|
49
|
+
|
|
50
|
+
if (showVersion) {
|
|
51
|
+
console.log(version);
|
|
52
|
+
process.exit(0);
|
|
53
|
+
}
|
|
54
|
+
if (showHelp) return showUsage(PUBLIC_HELP_TEXT);
|
|
55
|
+
if (configAction) return handleConfig(configAction, configKey, configValue);
|
|
56
|
+
if (showCfg) return showConfig(urls, outputFile);
|
|
57
|
+
if (urls.length === 0 && exploreCount === 0)
|
|
58
|
+
return showUsage(PUBLIC_HELP_TEXT);
|
|
59
|
+
|
|
60
|
+
if (exploreCount > 0) {
|
|
61
|
+
return handleExplore({ ...parsed, subcommand: "explore" });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return handleInfo(parsed);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
main().catch((err) => {
|
|
68
|
+
console.error(`错误: ${err.message}`);
|
|
69
|
+
process.exit(1);
|
|
70
|
+
});
|
package/src/scraper/auto-core.js
CHANGED
|
@@ -1,203 +1,203 @@
|
|
|
1
|
-
import {
|
|
2
|
-
delay,
|
|
3
|
-
ensureBrowserReady,
|
|
4
|
-
ensureTikTokPage,
|
|
5
|
-
setDelayConfig,
|
|
6
|
-
getDelayConfig,
|
|
7
|
-
closeCommentPanel,
|
|
8
|
-
retryWithBackoff,
|
|
9
|
-
detectPageError,
|
|
10
|
-
isLoggedIn,
|
|
11
|
-
assertPageUrl,
|
|
12
|
-
} from './modules/page-helpers.js';
|
|
13
|
-
import { detectCaptcha } from './modules/captcha-handler.js';
|
|
14
|
-
export { ensureBrowserReady };
|
|
15
|
-
import {
|
|
16
|
-
getUserInfo,
|
|
17
|
-
collectVideos,
|
|
18
|
-
} from '../videos/core.js';
|
|
19
|
-
import { runScrape } from './core.js';
|
|
20
|
-
import { extractFollowAndFollowers } from './modules/follow-extractor.js';
|
|
21
|
-
|
|
22
|
-
function mergeUserInfo(existing, incoming, source) {
|
|
23
|
-
const merged = { ...existing };
|
|
24
|
-
for (const [key, value] of Object.entries(incoming)) {
|
|
25
|
-
if (key === '_sources') continue;
|
|
26
|
-
if (value === undefined || value === null || value === '') continue;
|
|
27
|
-
if (typeof value === 'number' && typeof merged[key] === 'number') {
|
|
28
|
-
merged[key] = Math.max(merged[key], value);
|
|
29
|
-
} else if (merged[key] === undefined || merged[key] === null || merged[key] === '') {
|
|
30
|
-
merged[key] = value;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
if (source) {
|
|
34
|
-
if (!merged._sources) merged._sources = [];
|
|
35
|
-
if (!merged._sources.includes(source)) merged._sources.push(source);
|
|
36
|
-
}
|
|
37
|
-
return merged;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async function processUser(page, username, options, log) {
|
|
41
|
-
const {
|
|
42
|
-
collectMax = 1,
|
|
43
|
-
scrapeDepth = 50,
|
|
44
|
-
maxComments = 200,
|
|
45
|
-
maxGuess = 10,
|
|
46
|
-
preset = 'fast',
|
|
47
|
-
switchMax = null,
|
|
48
|
-
commentMax = null,
|
|
49
|
-
enableFollow = false,
|
|
50
|
-
maxFollowing = 200,
|
|
51
|
-
maxFollowers = 200,
|
|
52
|
-
browser = null,
|
|
53
|
-
} = options;
|
|
54
|
-
|
|
55
|
-
const result = {
|
|
56
|
-
userInfo: null,
|
|
57
|
-
collectedVideos: [],
|
|
58
|
-
discoveredVideoAuthors: [],
|
|
59
|
-
discoveredCommentAuthors: [],
|
|
60
|
-
discoveredGuessAuthors: [],
|
|
61
|
-
discoveredFollowing: [],
|
|
62
|
-
discoveredFollowers: [],
|
|
63
|
-
error: null,
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
try {
|
|
67
|
-
log(`\n[processUser] 访问 @${username}...`);
|
|
68
|
-
await retryWithBackoff(async () => {
|
|
69
|
-
await page.goto(`https://www.tiktok.com/@${username}`, {
|
|
70
|
-
waitUntil: 'load', timeout: 30000,
|
|
71
|
-
});
|
|
72
|
-
assertPageUrl(page, `@${username}`);
|
|
73
|
-
}, { log });
|
|
74
|
-
await page.waitForSelector('[class*="DivVideoList"]', { timeout: 10000 }).catch(() => {});
|
|
75
|
-
await delay(1000, 2000);
|
|
76
|
-
|
|
77
|
-
const info = await getUserInfo(page);
|
|
78
|
-
result.userInfo = info;
|
|
79
|
-
if (!info.uniqueId) info.uniqueId = username;
|
|
80
|
-
log(` 昵称: ${info.nickname || '-'} | 粉丝: ${info.followerCount || 0}`);
|
|
81
|
-
|
|
82
|
-
if (options.enableFollow) {
|
|
83
|
-
const loggedIn = await isLoggedIn(page);
|
|
84
|
-
if (!loggedIn) {
|
|
85
|
-
log(' [跳过] 提取关注/粉丝:未登录,请先登录 TikTok');
|
|
86
|
-
result.discoveredFollowing = [];
|
|
87
|
-
result.discoveredFollowers = [];
|
|
88
|
-
} else {
|
|
89
|
-
try {
|
|
90
|
-
log(' 提取关注/粉丝列表...');
|
|
91
|
-
const { following, followers } = await extractFollowAndFollowers(page, {
|
|
92
|
-
maxFollowing: options.maxFollowing || 200,
|
|
93
|
-
maxFollowers: options.maxFollowers || 200,
|
|
94
|
-
log,
|
|
95
|
-
});
|
|
96
|
-
result.discoveredFollowing = following;
|
|
97
|
-
result.discoveredFollowers = followers;
|
|
98
|
-
log(` 关注: ${following.length} | 粉丝: ${followers.length}`);
|
|
99
|
-
} catch (e) {
|
|
100
|
-
log(` 关注/粉丝提取失败: ${e.message}`);
|
|
101
|
-
result.discoveredFollowing = [];
|
|
102
|
-
result.discoveredFollowers = [];
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const captcha = await detectCaptcha(page);
|
|
108
|
-
if (captcha && captcha.visible) {
|
|
109
|
-
log(`[验证码] @${username} 页面出现验证码`);
|
|
110
|
-
result.captchaDetected = true;
|
|
111
|
-
result.captchaStage = result.captchaStage || 'video-page';
|
|
112
|
-
result.captchaMessage = result.captchaMessage || '视频页出现验证码';
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const videos = await collectVideos(page, username, collectMax, log);
|
|
116
|
-
const videoList = Array.from(videos.values()).slice(0, collectMax);
|
|
117
|
-
result.collectedVideos = videoList.map(v => ({
|
|
118
|
-
videoId: v.id,
|
|
119
|
-
videoUrl: v.href,
|
|
120
|
-
}));
|
|
121
|
-
|
|
122
|
-
if (videoList.length > 0) {
|
|
123
|
-
const allVideoAuthors = new Map();
|
|
124
|
-
const allCommentAuthors = new Set();
|
|
125
|
-
const allGuessAuthors = new Set();
|
|
126
|
-
|
|
127
|
-
for (let i = 0; i < videoList.length; i++) {
|
|
128
|
-
const video = videoList[i];
|
|
129
|
-
const videoUrl = video.href.startsWith('http')
|
|
130
|
-
? video.href
|
|
131
|
-
: `https://www.tiktok.com${video.href}`;
|
|
132
|
-
log(` [${i + 1}/${videoList.length}] 开始 scrape: ${videoUrl} (深度 ${scrapeDepth})`);
|
|
133
|
-
|
|
134
|
-
const scrapeResult = await runScrape({
|
|
135
|
-
videoUrl,
|
|
136
|
-
maxVideos: scrapeDepth,
|
|
137
|
-
maxComments,
|
|
138
|
-
maxGuess,
|
|
139
|
-
preset,
|
|
140
|
-
switchMax,
|
|
141
|
-
commentMax,
|
|
142
|
-
browser,
|
|
143
|
-
page,
|
|
144
|
-
log,
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
if (scrapeResult.captchaDetected) {
|
|
148
|
-
result.captchaDetected = true;
|
|
149
|
-
result.captchaStage = scrapeResult.captchaStage || 'scrape';
|
|
150
|
-
result.captchaMessage = scrapeResult.captchaMessage || 'scrape阶段出现验证码';
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
const scrapeOutput = scrapeResult.output;
|
|
154
|
-
|
|
155
|
-
if (scrapeOutput && scrapeOutput.videoDetails) {
|
|
156
|
-
for (const vd of scrapeOutput.videoDetails) {
|
|
157
|
-
if (!allVideoAuthors.has(vd.uniqueId)) {
|
|
158
|
-
allVideoAuthors.set(vd.uniqueId, {
|
|
159
|
-
uniqueId: vd.uniqueId,
|
|
160
|
-
nickname: vd.nickname,
|
|
161
|
-
locationCreated: vd.locationCreated,
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (scrapeOutput && scrapeOutput.commentUsers) {
|
|
168
|
-
for (const cu of scrapeOutput.commentUsers) {
|
|
169
|
-
allCommentAuthors.add(cu);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
if (scrapeOutput && scrapeOutput.guessAuthors) {
|
|
174
|
-
for (const ga of scrapeOutput.guessAuthors) {
|
|
175
|
-
allGuessAuthors.add(ga);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
result.discoveredVideoAuthors = [...allVideoAuthors.values()];
|
|
181
|
-
result.discoveredCommentAuthors = [...allCommentAuthors];
|
|
182
|
-
result.discoveredGuessAuthors = [...allGuessAuthors];
|
|
183
|
-
|
|
184
|
-
log(` 发现: ${result.discoveredVideoAuthors.length} 个视频作者, ${result.discoveredCommentAuthors.length} 个评论作者, ${result.discoveredGuessAuthors.length} 个猜你喜欢作者`);
|
|
185
|
-
} else {
|
|
186
|
-
const pageError = await detectPageError(page);
|
|
187
|
-
result.restricted = !!pageError;
|
|
188
|
-
if (pageError) {
|
|
189
|
-
log(` @${username} 页面受限(${pageError}),标记跳过`);
|
|
190
|
-
} else {
|
|
191
|
-
log(` @${username} 没有视频,跳过 scrape`);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
} catch (e) {
|
|
195
|
-
result.error = e.message;
|
|
196
|
-
result.errorStack = e.stack || '';
|
|
197
|
-
log(` [错误] ${e.message}`);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
return result;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
export { processUser, mergeUserInfo };
|
|
1
|
+
import {
|
|
2
|
+
delay,
|
|
3
|
+
ensureBrowserReady,
|
|
4
|
+
ensureTikTokPage,
|
|
5
|
+
setDelayConfig,
|
|
6
|
+
getDelayConfig,
|
|
7
|
+
closeCommentPanel,
|
|
8
|
+
retryWithBackoff,
|
|
9
|
+
detectPageError,
|
|
10
|
+
isLoggedIn,
|
|
11
|
+
assertPageUrl,
|
|
12
|
+
} from './modules/page-helpers.js';
|
|
13
|
+
import { detectCaptcha } from './modules/captcha-handler.js';
|
|
14
|
+
export { ensureBrowserReady };
|
|
15
|
+
import {
|
|
16
|
+
getUserInfo,
|
|
17
|
+
collectVideos,
|
|
18
|
+
} from '../videos/core.js';
|
|
19
|
+
import { runScrape } from './core.js';
|
|
20
|
+
import { extractFollowAndFollowers } from './modules/follow-extractor.js';
|
|
21
|
+
|
|
22
|
+
function mergeUserInfo(existing, incoming, source) {
|
|
23
|
+
const merged = { ...existing };
|
|
24
|
+
for (const [key, value] of Object.entries(incoming)) {
|
|
25
|
+
if (key === '_sources') continue;
|
|
26
|
+
if (value === undefined || value === null || value === '') continue;
|
|
27
|
+
if (typeof value === 'number' && typeof merged[key] === 'number') {
|
|
28
|
+
merged[key] = Math.max(merged[key], value);
|
|
29
|
+
} else if (merged[key] === undefined || merged[key] === null || merged[key] === '') {
|
|
30
|
+
merged[key] = value;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (source) {
|
|
34
|
+
if (!merged._sources) merged._sources = [];
|
|
35
|
+
if (!merged._sources.includes(source)) merged._sources.push(source);
|
|
36
|
+
}
|
|
37
|
+
return merged;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function processUser(page, username, options, log) {
|
|
41
|
+
const {
|
|
42
|
+
collectMax = 1,
|
|
43
|
+
scrapeDepth = 50,
|
|
44
|
+
maxComments = 200,
|
|
45
|
+
maxGuess = 10,
|
|
46
|
+
preset = 'fast',
|
|
47
|
+
switchMax = null,
|
|
48
|
+
commentMax = null,
|
|
49
|
+
enableFollow = false,
|
|
50
|
+
maxFollowing = 200,
|
|
51
|
+
maxFollowers = 200,
|
|
52
|
+
browser = null,
|
|
53
|
+
} = options;
|
|
54
|
+
|
|
55
|
+
const result = {
|
|
56
|
+
userInfo: null,
|
|
57
|
+
collectedVideos: [],
|
|
58
|
+
discoveredVideoAuthors: [],
|
|
59
|
+
discoveredCommentAuthors: [],
|
|
60
|
+
discoveredGuessAuthors: [],
|
|
61
|
+
discoveredFollowing: [],
|
|
62
|
+
discoveredFollowers: [],
|
|
63
|
+
error: null,
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
log(`\n[processUser] 访问 @${username}...`);
|
|
68
|
+
await retryWithBackoff(async () => {
|
|
69
|
+
await page.goto(`https://www.tiktok.com/@${username}`, {
|
|
70
|
+
waitUntil: 'load', timeout: 30000,
|
|
71
|
+
});
|
|
72
|
+
assertPageUrl(page, `@${username}`);
|
|
73
|
+
}, { log });
|
|
74
|
+
await page.waitForSelector('[class*="DivVideoList"]', { timeout: 10000 }).catch(() => {});
|
|
75
|
+
await delay(1000, 2000);
|
|
76
|
+
|
|
77
|
+
const info = await getUserInfo(page);
|
|
78
|
+
result.userInfo = info;
|
|
79
|
+
if (!info.uniqueId) info.uniqueId = username;
|
|
80
|
+
log(` 昵称: ${info.nickname || '-'} | 粉丝: ${info.followerCount || 0}`);
|
|
81
|
+
|
|
82
|
+
if (options.enableFollow) {
|
|
83
|
+
const loggedIn = await isLoggedIn(page);
|
|
84
|
+
if (!loggedIn) {
|
|
85
|
+
log(' [跳过] 提取关注/粉丝:未登录,请先登录 TikTok');
|
|
86
|
+
result.discoveredFollowing = [];
|
|
87
|
+
result.discoveredFollowers = [];
|
|
88
|
+
} else {
|
|
89
|
+
try {
|
|
90
|
+
log(' 提取关注/粉丝列表...');
|
|
91
|
+
const { following, followers } = await extractFollowAndFollowers(page, {
|
|
92
|
+
maxFollowing: options.maxFollowing || 200,
|
|
93
|
+
maxFollowers: options.maxFollowers || 200,
|
|
94
|
+
log,
|
|
95
|
+
});
|
|
96
|
+
result.discoveredFollowing = following;
|
|
97
|
+
result.discoveredFollowers = followers;
|
|
98
|
+
log(` 关注: ${following.length} | 粉丝: ${followers.length}`);
|
|
99
|
+
} catch (e) {
|
|
100
|
+
log(` 关注/粉丝提取失败: ${e.message}`);
|
|
101
|
+
result.discoveredFollowing = [];
|
|
102
|
+
result.discoveredFollowers = [];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const captcha = await detectCaptcha(page);
|
|
108
|
+
if (captcha && captcha.visible) {
|
|
109
|
+
log(`[验证码] @${username} 页面出现验证码`);
|
|
110
|
+
result.captchaDetected = true;
|
|
111
|
+
result.captchaStage = result.captchaStage || 'video-page';
|
|
112
|
+
result.captchaMessage = result.captchaMessage || '视频页出现验证码';
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const videos = await collectVideos(page, username, collectMax, log);
|
|
116
|
+
const videoList = Array.from(videos.values()).slice(0, collectMax);
|
|
117
|
+
result.collectedVideos = videoList.map(v => ({
|
|
118
|
+
videoId: v.id,
|
|
119
|
+
videoUrl: v.href,
|
|
120
|
+
}));
|
|
121
|
+
|
|
122
|
+
if (videoList.length > 0) {
|
|
123
|
+
const allVideoAuthors = new Map();
|
|
124
|
+
const allCommentAuthors = new Set();
|
|
125
|
+
const allGuessAuthors = new Set();
|
|
126
|
+
|
|
127
|
+
for (let i = 0; i < videoList.length; i++) {
|
|
128
|
+
const video = videoList[i];
|
|
129
|
+
const videoUrl = video.href.startsWith('http')
|
|
130
|
+
? video.href
|
|
131
|
+
: `https://www.tiktok.com${video.href}`;
|
|
132
|
+
log(` [${i + 1}/${videoList.length}] 开始 scrape: ${videoUrl} (深度 ${scrapeDepth})`);
|
|
133
|
+
|
|
134
|
+
const scrapeResult = await runScrape({
|
|
135
|
+
videoUrl,
|
|
136
|
+
maxVideos: scrapeDepth,
|
|
137
|
+
maxComments,
|
|
138
|
+
maxGuess,
|
|
139
|
+
preset,
|
|
140
|
+
switchMax,
|
|
141
|
+
commentMax,
|
|
142
|
+
browser,
|
|
143
|
+
page,
|
|
144
|
+
log,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
if (scrapeResult.captchaDetected) {
|
|
148
|
+
result.captchaDetected = true;
|
|
149
|
+
result.captchaStage = scrapeResult.captchaStage || 'scrape';
|
|
150
|
+
result.captchaMessage = scrapeResult.captchaMessage || 'scrape阶段出现验证码';
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const scrapeOutput = scrapeResult.output;
|
|
154
|
+
|
|
155
|
+
if (scrapeOutput && scrapeOutput.videoDetails) {
|
|
156
|
+
for (const vd of scrapeOutput.videoDetails) {
|
|
157
|
+
if (!allVideoAuthors.has(vd.uniqueId)) {
|
|
158
|
+
allVideoAuthors.set(vd.uniqueId, {
|
|
159
|
+
uniqueId: vd.uniqueId,
|
|
160
|
+
nickname: vd.nickname,
|
|
161
|
+
locationCreated: vd.locationCreated,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (scrapeOutput && scrapeOutput.commentUsers) {
|
|
168
|
+
for (const cu of scrapeOutput.commentUsers) {
|
|
169
|
+
allCommentAuthors.add(cu);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (scrapeOutput && scrapeOutput.guessAuthors) {
|
|
174
|
+
for (const ga of scrapeOutput.guessAuthors) {
|
|
175
|
+
allGuessAuthors.add(ga);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
result.discoveredVideoAuthors = [...allVideoAuthors.values()];
|
|
181
|
+
result.discoveredCommentAuthors = [...allCommentAuthors];
|
|
182
|
+
result.discoveredGuessAuthors = [...allGuessAuthors];
|
|
183
|
+
|
|
184
|
+
log(` 发现: ${result.discoveredVideoAuthors.length} 个视频作者, ${result.discoveredCommentAuthors.length} 个评论作者, ${result.discoveredGuessAuthors.length} 个猜你喜欢作者`);
|
|
185
|
+
} else {
|
|
186
|
+
const pageError = await detectPageError(page);
|
|
187
|
+
result.restricted = !!pageError;
|
|
188
|
+
if (pageError) {
|
|
189
|
+
log(` @${username} 页面受限(${pageError}),标记跳过`);
|
|
190
|
+
} else {
|
|
191
|
+
log(` @${username} 没有视频,跳过 scrape`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
} catch (e) {
|
|
195
|
+
result.error = e.message;
|
|
196
|
+
result.errorStack = e.stack || '';
|
|
197
|
+
log(` [错误] ${e.message}`);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return result;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export { processUser, mergeUserInfo };
|