tt-help-cli-ycl 1.3.0 → 1.3.2
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 +44 -44
- package/src/cli/auto.js +94 -0
- package/src/cli/explore.js +117 -0
- package/src/cli/progress.js +111 -0
- package/src/cli/scrape.js +47 -0
- package/src/cli/utils.js +18 -0
- package/src/cli/videos.js +41 -0
- package/src/cli/watch.js +28 -0
- package/src/lib/args.js +386 -397
- package/src/lib/browser/anti-detect.js +23 -0
- package/src/lib/browser/cdp.js +142 -0
- package/src/lib/browser/launch.js +43 -0
- package/src/lib/browser/page.js +80 -0
- package/src/lib/constants.js +85 -168
- package/src/lib/delay.js +54 -0
- package/src/lib/explore-fetch.js +118 -0
- package/src/lib/fetcher.js +45 -60
- package/src/lib/filter.js +66 -66
- package/src/lib/io.js +54 -76
- package/src/lib/output.js +80 -80
- package/src/lib/parser.js +47 -47
- package/src/lib/retry.js +44 -0
- package/src/lib/scrape.js +40 -39
- package/src/lib/url.js +52 -0
- package/src/main.mjs +199 -962
- package/src/results/user-videos-bar.lar.lar.moeta.json +37 -0
- package/src/scraper/auto-core.mjs +183 -0
- package/src/scraper/{core.cjs → core.mjs} +188 -214
- package/src/{explore-core.cjs → scraper/explore-core.mjs} +44 -42
- package/src/scraper/modules/captcha-handler.mjs +114 -0
- package/src/scraper/modules/comment-extractor.mjs +69 -0
- package/src/scraper/modules/follow-extractor.mjs +121 -0
- package/src/scraper/modules/{guess-extractor.cjs → guess-extractor.mjs} +51 -53
- package/src/scraper/modules/page-error-detector.mjs +70 -0
- package/src/scraper/modules/page-helpers.mjs +46 -0
- package/src/scraper/modules/scroll-collector.mjs +189 -0
- package/src/{get-user-videos-core.cjs → videos/core.mjs} +126 -143
- package/src/watch/data-store.mjs +239 -0
- package/src/watch/public/index.html +446 -271
- package/src/watch/server.mjs +257 -153
- package/src/auto-core.cjs +0 -367
- package/src/data-store.cjs +0 -69
- package/src/get-user-videos.cjs +0 -59
- package/src/lib/auto-browser.mjs +0 -13
- package/src/lib/explore.js +0 -225
- package/src/lib/get-user-videos-browser.mjs +0 -6
- package/src/lib/scrape-browser.mjs +0 -6
- package/src/scraper/index.cjs +0 -97
- package/src/scraper/modules/comment-extractor.cjs +0 -49
- package/src/scraper/modules/follow-extractor.cjs +0 -112
- package/src/scraper/modules/page-helpers.cjs +0 -422
- package/src/scraper/modules/scroll-collector.cjs +0 -173
- package/src/scraper/modules/video-scanner.cjs +0 -43
- package/src/test-auto-follow.cjs +0 -109
- package/src/test-extractors.cjs +0 -75
- package/src/test-follow.cjs +0 -41
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { chromium } from 'playwright';
|
|
2
|
+
import { browser, saveBrowser, configPath } from './constants.js';
|
|
3
|
+
import { detectBrowser } from './browser/launch.js';
|
|
4
|
+
import { getAntiDetectScript } from './browser/anti-detect.js';
|
|
5
|
+
import { retryWithBackoff } from './retry.js';
|
|
6
|
+
import { scrollAndCollect } from '../scraper/modules/scroll-collector.mjs';
|
|
7
|
+
|
|
8
|
+
const EXPLORE_URL = 'https://www.tiktok.com/explore';
|
|
9
|
+
|
|
10
|
+
function sleep(ms) {
|
|
11
|
+
return new Promise(r => setTimeout(r, ms));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function fetchExplore(count = 100) {
|
|
15
|
+
let browserPath = browser;
|
|
16
|
+
let browserSource = '配置';
|
|
17
|
+
|
|
18
|
+
if (!browserPath) {
|
|
19
|
+
console.log(' [0/6] 未配置浏览器,正在自动探测...');
|
|
20
|
+
const detected = detectBrowser();
|
|
21
|
+
if (detected) {
|
|
22
|
+
browserPath = detected;
|
|
23
|
+
browserSource = '自动探测';
|
|
24
|
+
try {
|
|
25
|
+
saveBrowser(browserPath);
|
|
26
|
+
console.log(` [0/6] 已保存浏览器路径到配置: ${configPath}`);
|
|
27
|
+
} catch (err) {
|
|
28
|
+
console.log(` [0/6] 保存配置失败: ${err.message}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const launchOptions = {
|
|
34
|
+
headless: true,
|
|
35
|
+
args: [
|
|
36
|
+
'--no-sandbox',
|
|
37
|
+
'--disable-setuid-sandbox',
|
|
38
|
+
'--disable-blink-features=AutomationControlled',
|
|
39
|
+
'--disable-dev-shm-usage',
|
|
40
|
+
],
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
if (browserPath) {
|
|
44
|
+
console.log(` [0/6] 使用${browserSource}浏览器: ${browserPath}`);
|
|
45
|
+
launchOptions.executablePath = browserPath;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let instance;
|
|
49
|
+
try {
|
|
50
|
+
instance = await chromium.launch(launchOptions);
|
|
51
|
+
} catch (err) {
|
|
52
|
+
if (browserPath) {
|
|
53
|
+
console.log(` [0/6] 浏览器启动失败 (${err.message}),回退到 Playwright Chromium...`);
|
|
54
|
+
}
|
|
55
|
+
instance = await chromium.launch({
|
|
56
|
+
headless: true,
|
|
57
|
+
args: launchOptions.args,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
const context = await instance.newContext({
|
|
63
|
+
viewport: { width: 1280, height: 900 },
|
|
64
|
+
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
|
|
65
|
+
locale: 'en-US',
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
await context.addInitScript(getAntiDetectScript());
|
|
69
|
+
|
|
70
|
+
const page = await context.newPage();
|
|
71
|
+
await retryWithBackoff(() => page.goto(EXPLORE_URL, { waitUntil: 'load', timeout: 30000 }));
|
|
72
|
+
console.log(' [1/6] 页面已加载');
|
|
73
|
+
|
|
74
|
+
await sleep(5000);
|
|
75
|
+
|
|
76
|
+
const allUrls = await scrollAndCollect(page, {
|
|
77
|
+
container: null,
|
|
78
|
+
collectFn: () => ({
|
|
79
|
+
items: Array.from(document.querySelectorAll('a'))
|
|
80
|
+
.filter(a => /\/video\/\d{16,20}/.test(a.href))
|
|
81
|
+
.map(a => a.href),
|
|
82
|
+
}),
|
|
83
|
+
maxItems: count * 2,
|
|
84
|
+
delayRange: [1500, 2500],
|
|
85
|
+
staleThreshold: 5,
|
|
86
|
+
onRound: (round, items, allItems) => {
|
|
87
|
+
if ((round + 1) % 10 === 0) {
|
|
88
|
+
const uniqueCount = [...new Set(allItems)].length;
|
|
89
|
+
console.log(` [2/6] 滚动 ${round + 1},当前 ${uniqueCount} 个视频`);
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
await sleep(3000);
|
|
95
|
+
|
|
96
|
+
const unique = [...new Set(allUrls)];
|
|
97
|
+
console.log(` [4/6] 共检测到 ${unique.length} 个不重复视频`);
|
|
98
|
+
|
|
99
|
+
const results = [];
|
|
100
|
+
const seen = new Set();
|
|
101
|
+
for (const url of unique) {
|
|
102
|
+
if (results.length >= count) break;
|
|
103
|
+
const videoId = url.match(/video\/(\d{16,20})$/)?.[1];
|
|
104
|
+
if (videoId && !seen.has(videoId)) {
|
|
105
|
+
seen.add(videoId);
|
|
106
|
+
const user = url.match(/\/@([^/]+)/)?.[1];
|
|
107
|
+
if (user) {
|
|
108
|
+
results.push({ user, id: videoId, url });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
console.log(` [5/6] 去重后 ${results.length} 个`);
|
|
114
|
+
return results;
|
|
115
|
+
} finally {
|
|
116
|
+
await instance.close();
|
|
117
|
+
}
|
|
118
|
+
}
|
package/src/lib/fetcher.js
CHANGED
|
@@ -1,60 +1,45 @@
|
|
|
1
|
-
import { fetch, ProxyAgent } from 'undici';
|
|
2
|
-
import { DEFAULT_PROXY } from './constants.js';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
'
|
|
7
|
-
'Accept
|
|
8
|
-
'Accept-
|
|
9
|
-
'
|
|
10
|
-
'
|
|
11
|
-
'
|
|
12
|
-
'Sec-Fetch-
|
|
13
|
-
'Sec-Fetch-
|
|
14
|
-
'Sec-Fetch-
|
|
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
|
-
return `https://www.tiktok.com/${handle}`;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function isProfileUrl(url) {
|
|
50
|
-
return /\/@[\w-]+(?:$|[?#])/.test(url);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function isVideoUrl(url) {
|
|
54
|
-
return /\/video\/\d+/.test(url);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function extractProfileHandle(url) {
|
|
58
|
-
const m = url.match(/https:\/\/www\.tiktok\.com\/(@[\w-]+)/);
|
|
59
|
-
return m ? m[1] : null;
|
|
60
|
-
}
|
|
1
|
+
import { fetch, ProxyAgent } from 'undici';
|
|
2
|
+
import { DEFAULT_PROXY } from './constants.js';
|
|
3
|
+
import { isProfileUrl } from './url.js';
|
|
4
|
+
|
|
5
|
+
const HEADERS = {
|
|
6
|
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
|
|
7
|
+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
|
|
8
|
+
'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
|
|
9
|
+
'Accept-Encoding': 'gzip, deflate, br',
|
|
10
|
+
'Connection': 'keep-alive',
|
|
11
|
+
'Upgrade-Insecure-Requests': '1',
|
|
12
|
+
'Sec-Fetch-Dest': 'document',
|
|
13
|
+
'Sec-Fetch-Mode': 'navigate',
|
|
14
|
+
'Sec-Fetch-Site': 'none',
|
|
15
|
+
'Sec-Fetch-User': '?1',
|
|
16
|
+
'Cache-Control': 'max-age=0',
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export { isProfileUrl } from './url.js';
|
|
20
|
+
|
|
21
|
+
export async function fetchHtml(url, proxyUrl) {
|
|
22
|
+
const p = proxyUrl || DEFAULT_PROXY;
|
|
23
|
+
const agent = new ProxyAgent(p);
|
|
24
|
+
let lastError;
|
|
25
|
+
|
|
26
|
+
for (let attempt = 1; attempt <= 3; attempt++) {
|
|
27
|
+
try {
|
|
28
|
+
const res = await fetch(url, {
|
|
29
|
+
headers: HEADERS,
|
|
30
|
+
dispatcher: agent,
|
|
31
|
+
redirect: 'follow',
|
|
32
|
+
});
|
|
33
|
+
const html = await res.text();
|
|
34
|
+
return html;
|
|
35
|
+
} catch (err) {
|
|
36
|
+
lastError = err;
|
|
37
|
+
if (attempt < 3) {
|
|
38
|
+
const waitMs = Math.pow(2, attempt - 1) * 3000 + Math.random() * 2000;
|
|
39
|
+
await new Promise(r => setTimeout(r, waitMs));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
throw new Error(`请求 ${url} 失败(已重试 3 次),代理 ${p} 不可用`);
|
|
45
|
+
}
|
package/src/lib/filter.js
CHANGED
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
export function parseFilter(filterStr) {
|
|
2
|
-
if (!filterStr) return null;
|
|
3
|
-
|
|
4
|
-
const filter = {};
|
|
5
|
-
const pairs = filterStr.split('&');
|
|
6
|
-
|
|
7
|
-
for (const pair of pairs) {
|
|
8
|
-
const [key, value] = pair.split('=');
|
|
9
|
-
if (!key || value === undefined) continue;
|
|
10
|
-
|
|
11
|
-
const trimmedKey = key.trim();
|
|
12
|
-
const trimmedValue = value.trim();
|
|
13
|
-
|
|
14
|
-
// 处理布尔值
|
|
15
|
-
if (trimmedValue === 'true') {
|
|
16
|
-
filter[trimmedKey] = true;
|
|
17
|
-
} else if (trimmedValue === 'false') {
|
|
18
|
-
filter[trimmedKey] = false;
|
|
19
|
-
} else {
|
|
20
|
-
// 支持逗号分隔的多个值(如 locationCreated=DE,ES)
|
|
21
|
-
filter[trimmedKey] = trimmedValue.split(',').map(v => v.trim());
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return Object.keys(filter).length > 0 ? filter : null;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function applyFilter(results, filter) {
|
|
29
|
-
if (!filter || results.length === 0) return results;
|
|
30
|
-
|
|
31
|
-
return results.filter(item => {
|
|
32
|
-
for (const [key, expectedValue] of Object.entries(filter)) {
|
|
33
|
-
const actualValue = item[key];
|
|
34
|
-
|
|
35
|
-
// 如果字段不存在,过滤掉
|
|
36
|
-
if (actualValue === undefined || actualValue === null) {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// 数组值匹配(如 locationCreated=DE,ES)
|
|
41
|
-
if (Array.isArray(expectedValue)) {
|
|
42
|
-
if (!expectedValue.includes(String(actualValue))) {
|
|
43
|
-
return false;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
// 布尔值或精确匹配
|
|
47
|
-
else if (actualValue !== expectedValue) {
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return true;
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function formatFilterDescription(filter) {
|
|
56
|
-
if (!filter) return '';
|
|
57
|
-
|
|
58
|
-
const parts = Object.entries(filter).map(([key, value]) => {
|
|
59
|
-
if (Array.isArray(value)) {
|
|
60
|
-
return `${key}=${value.join(',')}`;
|
|
61
|
-
}
|
|
62
|
-
return `${key}=${value}`;
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
return parts.join(' & ');
|
|
66
|
-
}
|
|
1
|
+
export function parseFilter(filterStr) {
|
|
2
|
+
if (!filterStr) return null;
|
|
3
|
+
|
|
4
|
+
const filter = {};
|
|
5
|
+
const pairs = filterStr.split('&');
|
|
6
|
+
|
|
7
|
+
for (const pair of pairs) {
|
|
8
|
+
const [key, value] = pair.split('=');
|
|
9
|
+
if (!key || value === undefined) continue;
|
|
10
|
+
|
|
11
|
+
const trimmedKey = key.trim();
|
|
12
|
+
const trimmedValue = value.trim();
|
|
13
|
+
|
|
14
|
+
// 处理布尔值
|
|
15
|
+
if (trimmedValue === 'true') {
|
|
16
|
+
filter[trimmedKey] = true;
|
|
17
|
+
} else if (trimmedValue === 'false') {
|
|
18
|
+
filter[trimmedKey] = false;
|
|
19
|
+
} else {
|
|
20
|
+
// 支持逗号分隔的多个值(如 locationCreated=DE,ES)
|
|
21
|
+
filter[trimmedKey] = trimmedValue.split(',').map(v => v.trim());
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return Object.keys(filter).length > 0 ? filter : null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function applyFilter(results, filter) {
|
|
29
|
+
if (!filter || results.length === 0) return results;
|
|
30
|
+
|
|
31
|
+
return results.filter(item => {
|
|
32
|
+
for (const [key, expectedValue] of Object.entries(filter)) {
|
|
33
|
+
const actualValue = item[key];
|
|
34
|
+
|
|
35
|
+
// 如果字段不存在,过滤掉
|
|
36
|
+
if (actualValue === undefined || actualValue === null) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 数组值匹配(如 locationCreated=DE,ES)
|
|
41
|
+
if (Array.isArray(expectedValue)) {
|
|
42
|
+
if (!expectedValue.includes(String(actualValue))) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// 布尔值或精确匹配
|
|
47
|
+
else if (actualValue !== expectedValue) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function formatFilterDescription(filter) {
|
|
56
|
+
if (!filter) return '';
|
|
57
|
+
|
|
58
|
+
const parts = Object.entries(filter).map(([key, value]) => {
|
|
59
|
+
if (Array.isArray(value)) {
|
|
60
|
+
return `${key}=${value.join(',')}`;
|
|
61
|
+
}
|
|
62
|
+
return `${key}=${value}`;
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
return parts.join(' & ');
|
|
66
|
+
}
|
package/src/lib/io.js
CHANGED
|
@@ -1,76 +1,54 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
let lastBarCount = 0;
|
|
4
|
-
|
|
5
|
-
export function
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const
|
|
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
|
-
return ` [${prog}] ${bar.current}/${bar.total} ${icon} ${urlDisplay}`;
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
const output = lines.join('\n');
|
|
59
|
-
|
|
60
|
-
if (lastBarCount > 0) {
|
|
61
|
-
process.stdout.write(`\x1b[${lastBarCount}A`);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
process.stdout.write('\x1b[0J');
|
|
65
|
-
process.stdout.write(output + '\n');
|
|
66
|
-
|
|
67
|
-
lastBarCount = activeBars.length;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function clearProgressBars() {
|
|
71
|
-
if (lastBarCount > 0) {
|
|
72
|
-
process.stdout.write(`\x1b[${lastBarCount}A`);
|
|
73
|
-
process.stdout.write('\x1b[0J');
|
|
74
|
-
lastBarCount = 0;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
1
|
+
import { extractDisplayPath } from './url.js';
|
|
2
|
+
|
|
3
|
+
let lastBarCount = 0;
|
|
4
|
+
|
|
5
|
+
export function createProgressBar(current, total, maxWidth = 30) {
|
|
6
|
+
const filled = Math.round((current / total) * maxWidth);
|
|
7
|
+
return '█'.repeat(filled).padEnd(maxWidth);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function calculateConcurrency(total) {
|
|
11
|
+
return Math.min(5, Math.max(1, Math.floor(total / 10)), total);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function createMultiProgressBars(count) {
|
|
15
|
+
return Array.from({ length: count }, () => ({
|
|
16
|
+
current: 0,
|
|
17
|
+
total: 0,
|
|
18
|
+
status: 'pending',
|
|
19
|
+
url: '',
|
|
20
|
+
}));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function renderMultiProgressBars(bars, maxWidth = 30) {
|
|
24
|
+
const activeBars = bars.filter(bar => bar.total > 0);
|
|
25
|
+
|
|
26
|
+
if (activeBars.length === 0) return;
|
|
27
|
+
|
|
28
|
+
const lines = activeBars.map((bar) => {
|
|
29
|
+
const prog = createProgressBar(bar.current, bar.total, maxWidth);
|
|
30
|
+
const icon = bar.status === 'done' ? '✓' :
|
|
31
|
+
bar.status === 'error' ? '' : '⟳';
|
|
32
|
+
const urlDisplay = bar.url ? extractDisplayPath(bar.url) : '';
|
|
33
|
+
return ` [${prog}] ${bar.current}/${bar.total} ${icon} ${urlDisplay}`;
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const output = lines.join('\n');
|
|
37
|
+
|
|
38
|
+
if (lastBarCount > 0) {
|
|
39
|
+
process.stdout.write(`\x1b[${lastBarCount}A`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
process.stdout.write('\x1b[0J');
|
|
43
|
+
process.stdout.write(output + '\n');
|
|
44
|
+
|
|
45
|
+
lastBarCount = activeBars.length;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function clearProgressBars() {
|
|
49
|
+
if (lastBarCount > 0) {
|
|
50
|
+
process.stdout.write(`\x1b[${lastBarCount}A`);
|
|
51
|
+
process.stdout.write('\x1b[0J');
|
|
52
|
+
lastBarCount = 0;
|
|
53
|
+
}
|
|
54
|
+
}
|
package/src/lib/output.js
CHANGED
|
@@ -1,80 +1,80 @@
|
|
|
1
|
-
export function deduplicate(results) {
|
|
2
|
-
const seen = new Set();
|
|
3
|
-
return results.filter(r => {
|
|
4
|
-
if (r.id) {
|
|
5
|
-
const key = r.id;
|
|
6
|
-
if (seen.has(key)) return false;
|
|
7
|
-
seen.add(key);
|
|
8
|
-
return true;
|
|
9
|
-
}
|
|
10
|
-
const key = r.secUid || r.uniqueId;
|
|
11
|
-
if (seen.has(key)) return false;
|
|
12
|
-
seen.add(key);
|
|
13
|
-
return true;
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function formatTable(data) {
|
|
18
|
-
if (data.length === 0) return '';
|
|
19
|
-
|
|
20
|
-
if (data.length === 1) {
|
|
21
|
-
const lines = [];
|
|
22
|
-
for (const [key, val] of Object.entries(data[0])) {
|
|
23
|
-
if (typeof val === 'string' && val.length > 80) {
|
|
24
|
-
lines.push(` ${key}: ${val.substring(0, 80)}...`);
|
|
25
|
-
} else {
|
|
26
|
-
lines.push(` ${key}: ${val}`);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return lines.join('\n');
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const cols = [
|
|
33
|
-
{ key: 'uniqueId', label: '用户名', width: 20 },
|
|
34
|
-
{ key: 'locationCreated', label: '地区', width: 6 },
|
|
35
|
-
{ key: 'nickname', label: '昵称', width: 20 },
|
|
36
|
-
{ key: 'ttSeller', label: 'TT卖家', width: 8 },
|
|
37
|
-
{ key: 'verified', label: '已认证', width: 8 },
|
|
38
|
-
{ key: 'followerCount', label: '粉丝', width: 10 },
|
|
39
|
-
{ key: 'videoCount', label: '视频', width: 8 },
|
|
40
|
-
];
|
|
41
|
-
|
|
42
|
-
for (const row of data) {
|
|
43
|
-
for (const col of cols) {
|
|
44
|
-
const val = String(row[col.key] ?? '-');
|
|
45
|
-
col.width = Math.max(col.width, val.length, col.label.length);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const sep = (w) => '-'.repeat(w);
|
|
50
|
-
const pad = (s, w) => s.padEnd(w);
|
|
51
|
-
|
|
52
|
-
const header = cols.map(c => pad(c.label, c.width)).join(' │ ');
|
|
53
|
-
const divider = cols.map(c => sep(c.width)).join('-+-');
|
|
54
|
-
const rows = data.map(r =>
|
|
55
|
-
cols.map(c => pad(String(r[c.key] ?? '-'), c.width)).join(' │ ')
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
return [header, divider, ...rows].join('\n');
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function formatOutput(data, format) {
|
|
62
|
-
if (format === 'table') return formatTable(data);
|
|
63
|
-
|
|
64
|
-
if (format === 'raw') {
|
|
65
|
-
if (Array.isArray(data) && data.length > 0 && 'url' in data[0]) {
|
|
66
|
-
return data.map(d => d.url).join('\n');
|
|
67
|
-
}
|
|
68
|
-
if (Array.isArray(data) && data.length > 0 && 'uniqueId' in data[0]) {
|
|
69
|
-
return data.map(d => `https://www.tiktok.com/@${d.uniqueId}`).join('\n');
|
|
70
|
-
}
|
|
71
|
-
return JSON.stringify(data, null, 2);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// Default JSON output, but for explore results (url-only) output pure text
|
|
75
|
-
if (Array.isArray(data) && data.length > 0 && 'url' in data[0]) {
|
|
76
|
-
return data.map(d => d.url).join('\n');
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return JSON.stringify(data, null, 2);
|
|
80
|
-
}
|
|
1
|
+
export function deduplicate(results) {
|
|
2
|
+
const seen = new Set();
|
|
3
|
+
return results.filter(r => {
|
|
4
|
+
if (r.id) {
|
|
5
|
+
const key = r.id;
|
|
6
|
+
if (seen.has(key)) return false;
|
|
7
|
+
seen.add(key);
|
|
8
|
+
return true;
|
|
9
|
+
}
|
|
10
|
+
const key = r.secUid || r.uniqueId;
|
|
11
|
+
if (seen.has(key)) return false;
|
|
12
|
+
seen.add(key);
|
|
13
|
+
return true;
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function formatTable(data) {
|
|
18
|
+
if (data.length === 0) return '';
|
|
19
|
+
|
|
20
|
+
if (data.length === 1) {
|
|
21
|
+
const lines = [];
|
|
22
|
+
for (const [key, val] of Object.entries(data[0])) {
|
|
23
|
+
if (typeof val === 'string' && val.length > 80) {
|
|
24
|
+
lines.push(` ${key}: ${val.substring(0, 80)}...`);
|
|
25
|
+
} else {
|
|
26
|
+
lines.push(` ${key}: ${val}`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return lines.join('\n');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const cols = [
|
|
33
|
+
{ key: 'uniqueId', label: '用户名', width: 20 },
|
|
34
|
+
{ key: 'locationCreated', label: '地区', width: 6 },
|
|
35
|
+
{ key: 'nickname', label: '昵称', width: 20 },
|
|
36
|
+
{ key: 'ttSeller', label: 'TT卖家', width: 8 },
|
|
37
|
+
{ key: 'verified', label: '已认证', width: 8 },
|
|
38
|
+
{ key: 'followerCount', label: '粉丝', width: 10 },
|
|
39
|
+
{ key: 'videoCount', label: '视频', width: 8 },
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
for (const row of data) {
|
|
43
|
+
for (const col of cols) {
|
|
44
|
+
const val = String(row[col.key] ?? '-');
|
|
45
|
+
col.width = Math.max(col.width, val.length, col.label.length);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const sep = (w) => '-'.repeat(w);
|
|
50
|
+
const pad = (s, w) => s.padEnd(w);
|
|
51
|
+
|
|
52
|
+
const header = cols.map(c => pad(c.label, c.width)).join(' │ ');
|
|
53
|
+
const divider = cols.map(c => sep(c.width)).join('-+-');
|
|
54
|
+
const rows = data.map(r =>
|
|
55
|
+
cols.map(c => pad(String(r[c.key] ?? '-'), c.width)).join(' │ ')
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
return [header, divider, ...rows].join('\n');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function formatOutput(data, format) {
|
|
62
|
+
if (format === 'table') return formatTable(data);
|
|
63
|
+
|
|
64
|
+
if (format === 'raw') {
|
|
65
|
+
if (Array.isArray(data) && data.length > 0 && 'url' in data[0]) {
|
|
66
|
+
return data.map(d => d.url).join('\n');
|
|
67
|
+
}
|
|
68
|
+
if (Array.isArray(data) && data.length > 0 && 'uniqueId' in data[0]) {
|
|
69
|
+
return data.map(d => `https://www.tiktok.com/@${d.uniqueId}`).join('\n');
|
|
70
|
+
}
|
|
71
|
+
return JSON.stringify(data, null, 2);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Default JSON output, but for explore results (url-only) output pure text
|
|
75
|
+
if (Array.isArray(data) && data.length > 0 && 'url' in data[0]) {
|
|
76
|
+
return data.map(d => d.url).join('\n');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return JSON.stringify(data, null, 2);
|
|
80
|
+
}
|