tt-help-cli-ycl 1.3.55 → 1.3.57

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.
@@ -1,128 +0,0 @@
1
- /**
2
- * 测试工具:分析 TikTok view-source HTML 的三种情况
3
- * 1. 正常用户(有 SSR 数据)
4
- * 2. 空壳 HTML(11182 字节,无 SSR — 需要重试)
5
- * 3. 异常用户(有 SSR 但 userInfo 为空,statusCode=10202 — 重试无效)
6
- */
7
-
8
- import { TikTokScraper } from "../src/lib/tiktok-scraper.mjs";
9
- import fs from "fs";
10
-
11
- const testUsers = [
12
- { id: "nike", type: "正常用户" },
13
- { id: "galb508", type: "异常用户(可能被封/删除)" },
14
- { id: "notexist_user_xxxxxx12345", type: "不存在的用户" },
15
- ];
16
-
17
- async function analyzeUser(uniqueId, typeLabel) {
18
- console.log(`\n${"=".repeat(60)}`);
19
- console.log(`分析 @${uniqueId} (${typeLabel})`);
20
- console.log("=".repeat(60));
21
-
22
- const scraper = new TikTokScraper({ poolSize: 1 });
23
- await scraper.init();
24
- const slot = scraper._pickSlot();
25
-
26
- // 多次采样
27
- const samples = [];
28
- for (let i = 0; i < 3; i++) {
29
- const rawHtml = await scraper._fetchViewSource(
30
- `https://www.tiktok.com/@${uniqueId}`,
31
- slot,
32
- );
33
- const byteLen = Buffer.byteLength(rawHtml, "utf8");
34
- const hasSSR = rawHtml.includes("__UNIVERSAL_DATA_FOR_REHYDRATION__");
35
-
36
- let analysis = {
37
- round: i + 1,
38
- size: rawHtml.length,
39
- byteLen,
40
- hasSSR,
41
- };
42
-
43
- // 如果有 SSR 数据,进一步分析
44
- if (hasSSR) {
45
- try {
46
- const idx = rawHtml.indexOf("__UNIVERSAL_DATA_FOR_REHYDRATION__");
47
- const sIdx = rawHtml.indexOf(">", idx) + 1;
48
- const eIdx = rawHtml.indexOf("</script>", sIdx);
49
- const jsonStr = rawHtml.substring(sIdx, eIdx);
50
- const data = JSON.parse(jsonStr);
51
- const ud = data.__DEFAULT_SCOPE__?.["webapp.user-detail"];
52
-
53
- analysis.scopeKeys = data.__DEFAULT_SCOPE__
54
- ? Object.keys(data.__DEFAULT_SCOPE__)
55
- : [];
56
- analysis.hasUserInfo = !!(ud && ud.userInfo);
57
- analysis.statusCode = ud?.statusCode;
58
- analysis.statusMsg = ud?.statusMsg;
59
- analysis.needFix = ud?.needFix;
60
- analysis.udKeys = ud ? Object.keys(ud) : [];
61
- } catch (e) {
62
- analysis.parseError = e.message;
63
- }
64
- } else {
65
- // 空壳 HTML,检查特征
66
- analysis.hasEmptyTitle = rawHtml.includes(
67
- '<title data-rh="true"></title>',
68
- );
69
- analysis.hasEmotionStyle = rawHtml.includes('data-emotion="tiktok"');
70
- }
71
-
72
- samples.push(analysis);
73
- console.log(
74
- ` 第 ${i + 1} 次: ${rawHtml.length} 字符, ${byteLen} 字节, SSR: ${hasSSR ? "✓" : "✗"}`,
75
- );
76
- if (hasSSR && analysis.statusCode !== undefined) {
77
- console.log(
78
- ` statusCode: ${analysis.statusCode}, hasUserInfo: ${analysis.hasUserInfo}, udKeys: [${analysis.udKeys.join(", ")}]`,
79
- );
80
- }
81
- }
82
-
83
- // 总结
84
- const shellCount = samples.filter((s) => !s.hasSSR).length;
85
- const hasDataCount = samples.filter((s) => s.hasUserInfo).length;
86
- const statusCode10202 = samples.filter((s) => s.statusCode === 10202).length;
87
-
88
- console.log("\n 总结:");
89
- console.log(` 空壳 HTML 次数: ${shellCount}/3`);
90
- console.log(` 有 userInfo 次数: ${hasDataCount}/3`);
91
- console.log(` statusCode=10202 次数: ${statusCode10202}/3`);
92
-
93
- // 判断类型
94
- if (shellCount === 3) {
95
- console.log(" → 判定: 持续空壳(可能是并发限流,重试可能有效)");
96
- } else if (hasDataCount > 0) {
97
- console.log(" → 判定: 正常用户(有完整数据)");
98
- } else if (statusCode10202 > 0) {
99
- console.log(" → 判定: 异常用户(statusCode=10202,重试无效)");
100
- } else {
101
- console.log(" → 判定: 无法确定");
102
- }
103
-
104
- await scraper.close();
105
- return samples;
106
- }
107
-
108
- async function main() {
109
- console.log("TikTok view-source HTML 分析工具");
110
- console.log("测试三种情况的 HTML 特征差异\n");
111
-
112
- const results = {};
113
- for (const { id, type } of testUsers) {
114
- results[id] = await analyzeUser(id, type);
115
- }
116
-
117
- // 保存结果
118
- fs.writeFileSync(
119
- "./test-html-analysis-result.json",
120
- JSON.stringify(results, null, 2),
121
- );
122
- console.log("\n\n结果已保存到 test-html-analysis-result.json");
123
- }
124
-
125
- main().catch((err) => {
126
- console.error("测试失败:", err);
127
- process.exit(1);
128
- });
@@ -1,36 +0,0 @@
1
- import { chromium } from 'playwright';
2
- import { detectCaptcha, closeCaptcha, handleCaptcha, getIncognitoPage } from '../src/scraper/modules/captcha-handler.mjs';
3
-
4
- async function main() {
5
- const browser = await chromium.connectOverCDP('http://127.0.0.1:9222');
6
- const url = 'https://www.tiktok.com/@mariaelenasanchez607/video/7630110959650000150';
7
-
8
- // 测试1: 无痕模式打开 + 点击评论
9
- console.error('=== 测试: 无痕模式 ===');
10
- const { page, context } = await getIncognitoPage(browser, url);
11
- console.error('URL:', page.url());
12
-
13
- await page.evaluate(() => {
14
- const all = document.querySelectorAll('button');
15
- for (const el of all) {
16
- if (/^评论$/.test(el.textContent?.trim()) && el.offsetParent !== null && el.getBoundingClientRect().width > 0) {
17
- el.click();
18
- break;
19
- }
20
- }
21
- });
22
- await new Promise(r => setTimeout(r, 3000));
23
-
24
- const captcha = await detectCaptcha(page);
25
- console.error('验证码:', captcha);
26
-
27
- await page.screenshot({ path: '/tmp/incognito-lib-test.png' });
28
- console.error('截图: /tmp/incognito-lib-test.png');
29
-
30
- await context.close();
31
- }
32
-
33
- main().catch(err => {
34
- console.error('错误:', err);
35
- process.exit(1);
36
- });
@@ -1,128 +0,0 @@
1
- import { ensureBrowserReady } from '../src/lib/browser/cdp.js';
2
-
3
- async function main() {
4
- const browser = await ensureBrowserReady();
5
- const defaultContext = browser.contexts()[0];
6
- const page = defaultContext.pages()[0] || await defaultContext.newPage();
7
-
8
- // 确保在 tiktok 页面
9
- if (!page.url().includes('tiktok.com')) {
10
- console.error('当前不在 TikTok 页面:', page.url());
11
- await page.goto('https://www.tiktok.com', { waitUntil: 'domcontentloaded', timeout: 30000 });
12
- await new Promise(r => setTimeout(r, 3000));
13
- }
14
-
15
- console.error('当前 URL:', page.url());
16
-
17
- // 1. 检测现有 isLoggedIn 的逻辑
18
- const currentUserMenu = await page.evaluate(() => {
19
- const selectors = [
20
- '[class*="UserMenu"]',
21
- '[class*="user-menu"]',
22
- '[class*="CurrentUserInfo"]',
23
- ];
24
- const results = {};
25
- for (const sel of selectors) {
26
- const el = document.querySelector(sel);
27
- results[sel] = !!el;
28
- if (el) {
29
- results[sel + '_class'] = el.className;
30
- }
31
- }
32
- return results;
33
- });
34
-
35
- console.error('\n=== 当前选择器检测结果 ===');
36
- console.error(JSON.stringify(currentUserMenu, null, 2));
37
-
38
- const hasLoginButton = await page.evaluate(() => {
39
- const buttons = Array.from(document.querySelectorAll('button, [role="button"]'));
40
- const loginButtons = buttons.filter(el =>
41
- /^(登录|Log in|Sign in)$/i.test(el.textContent.trim())
42
- );
43
- return {
44
- total: buttons.length,
45
- loginCount: loginButtons.length,
46
- samples: loginButtons.slice(0, 5).map(el => ({
47
- text: el.textContent.trim(),
48
- class: el.className,
49
- })),
50
- };
51
- });
52
-
53
- console.error('\n=== 登录按钮检测 ===');
54
- console.error(JSON.stringify(hasLoginButton, null, 2));
55
-
56
- // 2. 用更宽泛的方式找用户相关元素
57
- const broadSearch = await page.evaluate(() => {
58
- const allClasses = [];
59
-
60
- // 搜索可能的用户头像/菜单元素
61
- const candidates = [];
62
-
63
- // 顶部导航区域
64
- const navArea = document.querySelector('[class*="nav"], [class*="Header"], [class*="header"]');
65
- if (navArea) {
66
- const avatars = navArea.querySelectorAll('[class*="avatar"], [class*="Avatar"], [class*="photo"], [class*="Photo"], [class*="image"], [class*="Image"]');
67
- avatars.forEach(el => {
68
- candidates.push({
69
- tag: el.tagName,
70
- class: el.className.substring(0, 200),
71
- text: el.textContent?.substring(0, 50),
72
- parent: el.parentElement?.className?.substring(0, 100),
73
- });
74
- });
75
- }
76
-
77
- // 搜索包含用户信息的链接
78
- const profileLinks = Array.from(document.querySelectorAll('a[href*="/@"]'));
79
- const profileSamples = profileLinks.slice(0, 10).map(el => ({
80
- href: el.href,
81
- class: el.className?.substring(0, 100),
82
- text: el.textContent?.substring(0, 50),
83
- }));
84
-
85
- // 搜索所有包含 User 关键字的类名
86
- const userElements = Array.from(document.querySelectorAll('*')).filter(el =>
87
- el.className && typeof el.className === 'string' &&
88
- /User|user|Profile|profile/.test(el.className) &&
89
- el.tagName !== 'STYLE' && el.tagName !== 'SCRIPT'
90
- ).slice(0, 30).map(el => ({
91
- tag: el.tagName,
92
- class: el.className.substring(0, 150),
93
- text: el.textContent?.substring(0, 30),
94
- }));
95
-
96
- return {
97
- navAreaFound: !!navArea,
98
- avatarCandidates: candidates,
99
- profileLinks: profileSamples,
100
- userElements,
101
- };
102
- });
103
-
104
- console.error('\n=== 宽泛搜索 ===');
105
- console.error(JSON.stringify(broadSearch, null, 2));
106
-
107
- // 3. 截图
108
- await page.screenshot({ path: '/tmp/login-debug.png' });
109
- console.error('\n截图已保存到 /tmp/login-debug.png');
110
-
111
- // 4. 汇总判断
112
- const isLoggedIn = currentUserMenu['[class*="UserMenu"]'] ||
113
- currentUserMenu['[class*="user-menu"]'] ||
114
- currentUserMenu['[class*="CurrentUserInfo"]'] ||
115
- broadSearch.userElements.length > 0;
116
-
117
- console.error('\n=== 结论 ===');
118
- console.error('isLoggedIn 函数返回:', !!(currentUserMenu['[class*="UserMenu"]'] || currentUserMenu['[class*="user-menu"]'] || currentUserMenu['[class*="CurrentUserInfo"]']) && !hasLoginButton.loginCount);
119
- console.error('宽泛搜索是否找到用户元素:', broadSearch.userElements.length > 0);
120
- console.error('找到用户相关元素数量:', broadSearch.userElements.length);
121
- console.error('头像候选数量:', broadSearch.avatarCandidates.length);
122
- console.error('Profile 链接数量:', broadSearch.profileLinks.length);
123
- }
124
-
125
- main().catch(err => {
126
- console.error('错误:', err);
127
- process.exit(1);
128
- });
@@ -1,45 +0,0 @@
1
- import { chromium } from 'playwright';
2
- import { safeClickComment, detectCaptcha } from '../src/scraper/modules/captcha-handler.mjs';
3
- import { ensureBrowserReady } from '../src/lib/browser/cdp.js';
4
-
5
- const URL = 'https://www.tiktok.com/@mariaelenasanchez607/video/7630110959650000150';
6
-
7
- async function main() {
8
- const browser = await ensureBrowserReady();
9
- const defaultContext = browser.contexts()[0];
10
- const page = defaultContext.pages()[0] || await defaultContext.newPage();
11
-
12
- for (let i = 1; i <= 3; i++) {
13
- console.error(`\n===== 第 ${i} 轮 =====`);
14
- await page.goto(URL, { waitUntil: 'domcontentloaded', timeout: 30000 });
15
- await new Promise(r => setTimeout(r, 5000));
16
-
17
- const result = await safeClickComment(page);
18
- console.error('结果:', JSON.stringify(result));
19
-
20
- const stillThere = await detectCaptcha(page);
21
- console.error('验证码残留:', !!stillThere);
22
-
23
- await page.screenshot({ path: `/tmp/safe-click-run-${i}.png` });
24
-
25
- // 关闭评论面板
26
- await page.evaluate(() => {
27
- const rightPanel = document.querySelector('[class*="RightPanelContainer"]');
28
- if (rightPanel) {
29
- const tabContainer = rightPanel.querySelector('[class*="TabContainer"]');
30
- if (tabContainer) {
31
- const closeOverlay = tabContainer.querySelector('div:last-child');
32
- if (closeOverlay) closeOverlay.click();
33
- }
34
- }
35
- });
36
- await new Promise(r => setTimeout(r, 2000));
37
- }
38
-
39
- console.error('\n完成');
40
- }
41
-
42
- main().catch(err => {
43
- console.error('错误:', err);
44
- process.exit(1);
45
- });
@@ -1,246 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { mkdtempSync, rmSync } from "node:fs";
3
- import os from "node:os";
4
- import path from "node:path";
5
-
6
- import { createStore, closeStoreDb } from "../src/watch/data-store.js";
7
- import { startWatchServer } from "../src/watch/server.js";
8
-
9
- function createTempDbPath(prefix) {
10
- const dir = mkdtempSync(path.join(os.tmpdir(), `${prefix}-`));
11
- return {
12
- dir,
13
- dbPath: path.join(dir, "result.db"),
14
- };
15
- }
16
-
17
- function cleanupTempDir(dir) {
18
- rmSync(dir, { recursive: true, force: true });
19
- }
20
-
21
- function seedClaimStore(store) {
22
- store.addUser({
23
- uniqueId: "follow-tier2",
24
- status: "pending",
25
- followerCount: 100,
26
- videoCount: 1,
27
- guessedLocation: "DE",
28
- sources: ["following"],
29
- });
30
- store.addUser({
31
- uniqueId: "seller-tier2",
32
- status: "pending",
33
- followerCount: 50,
34
- videoCount: 1,
35
- guessedLocation: "DE",
36
- ttSeller: true,
37
- verified: false,
38
- sources: ["comment"],
39
- });
40
- store.addUser({
41
- uniqueId: "seed-tier1",
42
- status: "pending",
43
- followerCount: 10,
44
- videoCount: 1,
45
- guessedLocation: "PL",
46
- sources: ["seed"],
47
- });
48
- }
49
-
50
- function seedWatchStore(store) {
51
- store.addUser({
52
- uniqueId: "seed-tier1",
53
- nickname: "Seed User",
54
- status: "pending",
55
- followerCount: 10,
56
- videoCount: 1,
57
- guessedLocation: "PL",
58
- ttSeller: false,
59
- verified: false,
60
- sources: ["seed"],
61
- });
62
- store.addUser({
63
- uniqueId: "seller-target",
64
- nickname: "Seller Target",
65
- status: "pending",
66
- followerCount: 500,
67
- videoCount: 1,
68
- guessedLocation: "DE",
69
- locationCreated: "DE",
70
- ttSeller: true,
71
- verified: false,
72
- sources: ["comment"],
73
- });
74
- store.addUser({
75
- uniqueId: "follow-tier2",
76
- nickname: "Follow User",
77
- status: "pending",
78
- followerCount: 300,
79
- videoCount: 1,
80
- guessedLocation: "FR",
81
- ttSeller: false,
82
- verified: false,
83
- sources: ["following"],
84
- });
85
- store.addUser({
86
- uniqueId: "pending-update",
87
- nickname: "Needs Update",
88
- status: "pending",
89
- guessedLocation: "US",
90
- ttSeller: null,
91
- verified: false,
92
- userUpdateCount: 0,
93
- sources: ["comment"],
94
- });
95
- store.addUser({
96
- uniqueId: "done-es",
97
- nickname: "Done ES",
98
- status: "done",
99
- followerCount: 800,
100
- videoCount: 3,
101
- guessedLocation: "ES",
102
- locationCreated: "ES",
103
- ttSeller: true,
104
- verified: false,
105
- processedAt: Date.now(),
106
- processed: true,
107
- sources: ["processed"],
108
- });
109
- store.addUser({
110
- uniqueId: "restricted-it",
111
- nickname: "Restricted IT",
112
- status: "restricted",
113
- locationCreated: "IT",
114
- ttSeller: false,
115
- verified: false,
116
- sources: ["comment"],
117
- });
118
- store.addUser({
119
- uniqueId: "error-user",
120
- nickname: "Error User",
121
- status: "error",
122
- locationCreated: "US",
123
- ttSeller: false,
124
- verified: false,
125
- sources: ["comment"],
126
- });
127
- }
128
-
129
- async function testClaimPriorityAndRenewal() {
130
- const { dir, dbPath } = createTempDbPath("tt-watch-claim");
131
- try {
132
- const store = createStore(dbPath);
133
- seedClaimStore(store);
134
-
135
- const first = store.claimNextJob("worker-a", 5 * 60 * 1000, null, true);
136
- assert.ok(first, "expected a claim for logged-in worker");
137
- assert.equal(first.uniqueId, "seed-tier1");
138
-
139
- const renewed = store.claimNextJob("worker-a", 5 * 60 * 1000, null, true);
140
- assert.ok(renewed, "expected renewal claim for same worker");
141
- assert.equal(renewed.uniqueId, "seed-tier1");
142
- assert.ok(
143
- renewed.claimedAt >= first.claimedAt,
144
- "expected renewal claimedAt to advance or stay equal",
145
- );
146
-
147
- const loggedOut = store.claimNextJob(
148
- "worker-b",
149
- 5 * 60 * 1000,
150
- null,
151
- false,
152
- );
153
- assert.ok(loggedOut, "expected a claim for logged-out worker");
154
- assert.equal(loggedOut.uniqueId, "follow-tier2");
155
- } finally {
156
- closeStoreDb();
157
- cleanupTempDir(dir);
158
- }
159
- }
160
-
161
- async function testWatchHttpEndpoints() {
162
- const { dir, dbPath } = createTempDbPath("tt-watch-http");
163
- let server;
164
- try {
165
- const store = createStore(dbPath);
166
- seedWatchStore(store);
167
-
168
- const started = await startWatchServer(dbPath, 0, store);
169
- server = started.server;
170
- const actualPort = server.address().port;
171
- const baseUrl = `http://127.0.0.1:${actualPort}`;
172
-
173
- const [statsRes, usersRes, targetRes, lightUsersRes] = await Promise.all([
174
- fetch(`${baseUrl}/api/stats`),
175
- fetch(`${baseUrl}/api/users?limit=3`),
176
- fetch(`${baseUrl}/api/target-users`),
177
- fetch(`${baseUrl}/api/users?limit=2&view=light`),
178
- ]);
179
-
180
- assert.equal(statsRes.status, 200);
181
- assert.equal(usersRes.status, 200);
182
- assert.equal(targetRes.status, 200);
183
- assert.equal(lightUsersRes.status, 200);
184
-
185
- const [stats, users, targets, lightUsers] = await Promise.all([
186
- statsRes.json(),
187
- usersRes.json(),
188
- targetRes.json(),
189
- lightUsersRes.json(),
190
- ]);
191
-
192
- assert.equal(stats.totalUsers, 7);
193
- assert.equal(stats.pendingUsers, 4);
194
- assert.equal(stats.processedUsers, 1);
195
- assert.equal(stats.restrictedUsers, 1);
196
- assert.equal(stats.errorUsers, 1);
197
- assert.equal(stats.targetUsers, 2);
198
- assert.equal(stats.userUpdateTasks, 1);
199
- assert.ok(Array.isArray(stats.countryStats));
200
- assert.ok(stats.countryStats.some((item) => item.country === "ES"));
201
-
202
- assert.equal(users.total, 7);
203
- assert.equal(users.users.length, 3);
204
- assert.equal(users.users[0].uniqueId, "seller-target");
205
-
206
- assert.equal(targets.total, 2);
207
- assert.deepEqual(
208
- targets.users.map((item) => item.uniqueId),
209
- ["done-es", "seller-target"],
210
- );
211
-
212
- assert.equal(lightUsers.total, 7);
213
- assert.equal(lightUsers.users.length, 2);
214
- assert.deepEqual(Object.keys(lightUsers.users[0]).sort(), [
215
- "followerCount",
216
- "guessedLocation",
217
- "locationCreated",
218
- "nickname",
219
- "pinned",
220
- "processedAt",
221
- "sources",
222
- "status",
223
- "ttSeller",
224
- "uniqueId",
225
- "verified",
226
- ]);
227
- } finally {
228
- if (server) {
229
- await new Promise((resolve) => server.close(resolve));
230
- }
231
- closeStoreDb();
232
- cleanupTempDir(dir);
233
- }
234
- }
235
-
236
- async function main() {
237
- await testClaimPriorityAndRenewal();
238
- await testWatchHttpEndpoints();
239
- console.log("watch db smoke test passed");
240
- }
241
-
242
- main().catch((error) => {
243
- console.error("watch db smoke test failed");
244
- console.error(error);
245
- process.exitCode = 1;
246
- });