tt-help-cli-ycl 1.0.2 → 1.0.4
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/lib/args.js +62 -59
- package/src/lib/constants.js +102 -76
- package/src/lib/explore.js +218 -124
- package/src/lib/fetcher.js +50 -36
- package/src/lib/filter.js +66 -0
- package/src/lib/io.js +13 -13
- package/src/lib/output.js +80 -80
- package/src/lib/parser.js +47 -47
- package/src/lib/scrape.js +39 -39
- package/src/main.mjs +280 -245
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
|
+
}
|
package/src/lib/parser.js
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
import { USER_SECTION_SIZE } from './constants.js';
|
|
2
|
-
|
|
3
|
-
export function extractUserSection(html) {
|
|
4
|
-
const idx = html.indexOf('"uniqueId"');
|
|
5
|
-
if (idx < 0) return null;
|
|
6
|
-
return html.substring(idx, idx + USER_SECTION_SIZE);
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function parseUserSection(section) {
|
|
10
|
-
const data = {};
|
|
11
|
-
|
|
12
|
-
for (const key of ['uniqueId', 'uid', 'secUid']) {
|
|
13
|
-
const m = section.match(new RegExp(`"${key}":"([^"]*)`));
|
|
14
|
-
if (m) data[key] = m[1];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
for (const key of ['nickname', 'signature']) {
|
|
18
|
-
const m = section.match(new RegExp(`"${key}":"((?:[^"\\\\]|\\\\.)*)"`, 'g'));
|
|
19
|
-
if (m) {
|
|
20
|
-
const raw = m[0].replace(`"${key}":"`, '').replace(/"$/, '');
|
|
21
|
-
data[key] = raw.replace(/\\n/g, '\n').replace(/\\\\/g, '\\');
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
for (const key of ['ttSeller', 'verified']) {
|
|
26
|
-
const m = section.match(new RegExp(`"${key}":\\s*(true|false)`));
|
|
27
|
-
data[key] = m ? m[1] === 'true' : undefined;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
for (const key of ['followerCount', 'followingCount', 'heartCount', 'videoCount', 'diggCount']) {
|
|
31
|
-
const m = section.match(new RegExp(`"${key}":(\\d+)`));
|
|
32
|
-
if (m) data[key] = parseInt(m[1], 10);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const mt = section.match(/"createTime":(\d+)/);
|
|
36
|
-
if (mt) data.createTime = parseInt(mt[1], 10);
|
|
37
|
-
|
|
38
|
-
const ma = section.match(/"avatarLarger":"([^"]*)/);
|
|
39
|
-
if (ma) data.avatarLarger = ma[1].replace(/\\u002F/g, '/');
|
|
40
|
-
|
|
41
|
-
return data;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function extractLocationCreated(html) {
|
|
45
|
-
const m = html.match(/"locationCreated":"([^"]*)/);
|
|
46
|
-
return m ? m[1] : null;
|
|
47
|
-
}
|
|
1
|
+
import { USER_SECTION_SIZE } from './constants.js';
|
|
2
|
+
|
|
3
|
+
export function extractUserSection(html) {
|
|
4
|
+
const idx = html.indexOf('"uniqueId"');
|
|
5
|
+
if (idx < 0) return null;
|
|
6
|
+
return html.substring(idx, idx + USER_SECTION_SIZE);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function parseUserSection(section) {
|
|
10
|
+
const data = {};
|
|
11
|
+
|
|
12
|
+
for (const key of ['uniqueId', 'uid', 'secUid']) {
|
|
13
|
+
const m = section.match(new RegExp(`"${key}":"([^"]*)`));
|
|
14
|
+
if (m) data[key] = m[1];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
for (const key of ['nickname', 'signature']) {
|
|
18
|
+
const m = section.match(new RegExp(`"${key}":"((?:[^"\\\\]|\\\\.)*)"`, 'g'));
|
|
19
|
+
if (m) {
|
|
20
|
+
const raw = m[0].replace(`"${key}":"`, '').replace(/"$/, '');
|
|
21
|
+
data[key] = raw.replace(/\\n/g, '\n').replace(/\\\\/g, '\\');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
for (const key of ['ttSeller', 'verified']) {
|
|
26
|
+
const m = section.match(new RegExp(`"${key}":\\s*(true|false)`));
|
|
27
|
+
data[key] = m ? m[1] === 'true' : undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
for (const key of ['followerCount', 'followingCount', 'heartCount', 'videoCount', 'diggCount']) {
|
|
31
|
+
const m = section.match(new RegExp(`"${key}":(\\d+)`));
|
|
32
|
+
if (m) data[key] = parseInt(m[1], 10);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const mt = section.match(/"createTime":(\d+)/);
|
|
36
|
+
if (mt) data.createTime = parseInt(mt[1], 10);
|
|
37
|
+
|
|
38
|
+
const ma = section.match(/"avatarLarger":"([^"]*)/);
|
|
39
|
+
if (ma) data.avatarLarger = ma[1].replace(/\\u002F/g, '/');
|
|
40
|
+
|
|
41
|
+
return data;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function extractLocationCreated(html) {
|
|
45
|
+
const m = html.match(/"locationCreated":"([^"]*)/);
|
|
46
|
+
return m ? m[1] : null;
|
|
47
|
+
}
|
package/src/lib/scrape.js
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
import { extractUserSection, parseUserSection, extractLocationCreated } from './parser.js';
|
|
2
|
-
import { fetchHtml, makeProfileUrl, isProfileUrl, isVideoUrl, extractProfileHandle } from './fetcher.js';
|
|
3
|
-
|
|
4
|
-
export async function extractUserData(profileUrl, proxyUrl) {
|
|
5
|
-
const profileHtml = await fetchHtml(profileUrl, proxyUrl);
|
|
6
|
-
const section = extractUserSection(profileHtml);
|
|
7
|
-
if (!section) throw new Error('无法解析用户信息');
|
|
8
|
-
const data = parseUserSection(section);
|
|
9
|
-
data.locationCreated = extractLocationCreated(profileHtml);
|
|
10
|
-
return data;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export async function extractVideoLocation(videoUrl, proxyUrl) {
|
|
14
|
-
const videoHtml = await fetchHtml(videoUrl, proxyUrl);
|
|
15
|
-
return extractLocationCreated(videoHtml);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export async function processUrl(url, proxyUrl) {
|
|
19
|
-
if (isProfileUrl(url)) {
|
|
20
|
-
const profileUrl = makeProfileUrl(url);
|
|
21
|
-
const profileData = await extractUserData(profileUrl, proxyUrl);
|
|
22
|
-
return [profileData];
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (isVideoUrl(url)) {
|
|
26
|
-
const profileHandle = extractProfileHandle(url);
|
|
27
|
-
if (!profileHandle) throw new Error(`无法从视频URL提取用户主页: ${url}`);
|
|
28
|
-
|
|
29
|
-
const profileUrl = makeProfileUrl(profileHandle);
|
|
30
|
-
const [profileData, locationCreated] = await Promise.all([
|
|
31
|
-
extractUserData(profileUrl, proxyUrl),
|
|
32
|
-
extractVideoLocation(url, proxyUrl),
|
|
33
|
-
]);
|
|
34
|
-
|
|
35
|
-
return [{ ...profileData, locationCreated }];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return [];
|
|
39
|
-
}
|
|
1
|
+
import { extractUserSection, parseUserSection, extractLocationCreated } from './parser.js';
|
|
2
|
+
import { fetchHtml, makeProfileUrl, isProfileUrl, isVideoUrl, extractProfileHandle } from './fetcher.js';
|
|
3
|
+
|
|
4
|
+
export async function extractUserData(profileUrl, proxyUrl) {
|
|
5
|
+
const profileHtml = await fetchHtml(profileUrl, proxyUrl);
|
|
6
|
+
const section = extractUserSection(profileHtml);
|
|
7
|
+
if (!section) throw new Error('无法解析用户信息');
|
|
8
|
+
const data = parseUserSection(section);
|
|
9
|
+
data.locationCreated = extractLocationCreated(profileHtml);
|
|
10
|
+
return data;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function extractVideoLocation(videoUrl, proxyUrl) {
|
|
14
|
+
const videoHtml = await fetchHtml(videoUrl, proxyUrl);
|
|
15
|
+
return extractLocationCreated(videoHtml);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function processUrl(url, proxyUrl) {
|
|
19
|
+
if (isProfileUrl(url)) {
|
|
20
|
+
const profileUrl = makeProfileUrl(url);
|
|
21
|
+
const profileData = await extractUserData(profileUrl, proxyUrl);
|
|
22
|
+
return [profileData];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (isVideoUrl(url)) {
|
|
26
|
+
const profileHandle = extractProfileHandle(url);
|
|
27
|
+
if (!profileHandle) throw new Error(`无法从视频URL提取用户主页: ${url}`);
|
|
28
|
+
|
|
29
|
+
const profileUrl = makeProfileUrl(profileHandle);
|
|
30
|
+
const [profileData, locationCreated] = await Promise.all([
|
|
31
|
+
extractUserData(profileUrl, proxyUrl),
|
|
32
|
+
extractVideoLocation(url, proxyUrl),
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
return [{ ...profileData, locationCreated }];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return [];
|
|
39
|
+
}
|