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/main.mjs
CHANGED
|
@@ -1,245 +1,280 @@
|
|
|
1
|
-
import { parseArgs } from './lib/args.js';
|
|
2
|
-
import { HELP_TEXT, CONFIG_TEXT, proxy, configFile, configPath, DEFAULT_PROXY } from './lib/constants.js';
|
|
3
|
-
import { fetchExplore } from './lib/explore.js';
|
|
4
|
-
import { processUrl } from './lib/scrape.js';
|
|
5
|
-
import { deduplicate, formatOutput } from './lib/output.js';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
if (
|
|
12
|
-
lines.push(
|
|
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
|
-
console.
|
|
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
|
-
|
|
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
|
-
if (
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const
|
|
146
|
-
const
|
|
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
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
1
|
+
import { parseArgs } from './lib/args.js';
|
|
2
|
+
import { HELP_TEXT, CONFIG_TEXT, proxy, configFile, configPath, DEFAULT_PROXY, saveBrowser } from './lib/constants.js';
|
|
3
|
+
import { fetchExplore } from './lib/explore.js';
|
|
4
|
+
import { processUrl } from './lib/scrape.js';
|
|
5
|
+
import { deduplicate, formatOutput } from './lib/output.js';
|
|
6
|
+
import { parseFilter, applyFilter, formatFilterDescription } from './lib/filter.js';
|
|
7
|
+
import { writeFileSync, readFileSync, existsSync } from 'fs';
|
|
8
|
+
|
|
9
|
+
function showConfig(urls, outputFile) {
|
|
10
|
+
const lines = [...CONFIG_TEXT];
|
|
11
|
+
if (outputFile) lines.push(` 输出文件: ${outputFile}`);
|
|
12
|
+
if (urls.length > 0) lines.push(` 待处理URL: ${urls.length}`);
|
|
13
|
+
lines.push('', '参数:', ' -c, --config 显示当前配置', ' -h, --help 显示帮助');
|
|
14
|
+
console.log(lines.join('\n'));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function showUsage() {
|
|
18
|
+
console.log(HELP_TEXT.join('\n'));
|
|
19
|
+
process.exit(0);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function handleConfig(action, value) {
|
|
23
|
+
if (action === 'show' || action === null) {
|
|
24
|
+
showConfig([], null);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (action === 'set' || action === 'set-proxy') {
|
|
28
|
+
if (!value) {
|
|
29
|
+
console.error('用法: tt-help config set <代理地址>');
|
|
30
|
+
process.exit(1);
|
|
31
|
+
}
|
|
32
|
+
const cfg = existsSync(configPath) ? JSON.parse(readFileSync(configPath, 'utf-8')) : {};
|
|
33
|
+
cfg.proxy = value;
|
|
34
|
+
writeFileSync(configPath, JSON.stringify(cfg, null, 2), 'utf-8');
|
|
35
|
+
console.log(`代理已设置为: ${value}`);
|
|
36
|
+
console.log(`配置文件: ${configPath}`);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (action === 'set-browser') {
|
|
40
|
+
if (!value) {
|
|
41
|
+
console.error('用法: tt-help config set-browser <浏览器路径 或 auto>');
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
if (value === 'auto') {
|
|
45
|
+
if (existsSync(configPath)) {
|
|
46
|
+
const cfg = JSON.parse(readFileSync(configPath, 'utf-8'));
|
|
47
|
+
delete cfg.browser;
|
|
48
|
+
writeFileSync(configPath, JSON.stringify(cfg, null, 2), 'utf-8');
|
|
49
|
+
}
|
|
50
|
+
console.log('已切换为自动探测浏览器模式');
|
|
51
|
+
} else {
|
|
52
|
+
saveBrowser(value);
|
|
53
|
+
console.log(`浏览器已设置为: ${value}`);
|
|
54
|
+
}
|
|
55
|
+
console.log(`配置文件: ${configPath}`);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (action === 'reset') {
|
|
59
|
+
if (existsSync(configPath)) {
|
|
60
|
+
const cfg = JSON.parse(readFileSync(configPath, 'utf-8'));
|
|
61
|
+
cfg.proxy = DEFAULT_PROXY;
|
|
62
|
+
writeFileSync(configPath, JSON.stringify(cfg, null, 2), 'utf-8');
|
|
63
|
+
console.log(`已恢复默认代理: ${DEFAULT_PROXY}`);
|
|
64
|
+
console.log(`配置文件: ${configPath}`);
|
|
65
|
+
} else {
|
|
66
|
+
console.log('当前使用默认代理,无需重置');
|
|
67
|
+
}
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
console.error(`未知配置命令: ${action}`);
|
|
71
|
+
console.error('用法: tt-help config [show|set|set-browser|reset]');
|
|
72
|
+
process.exit(1);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function cleanError(msg) {
|
|
76
|
+
return msg
|
|
77
|
+
.replace(/\x1b\[[0-9;]*m/g, '')
|
|
78
|
+
.replace(/\s*- navigating to.*/s, '')
|
|
79
|
+
.replace(/\s*Call log:/s, '')
|
|
80
|
+
.trim();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function runExplore(exploreCount, urls, proxyUrl, outputFile, outputFormat, isPipe, filter) {
|
|
84
|
+
console.log(`\n代理: ${proxyUrl}`);
|
|
85
|
+
console.log(`Explore 数量: ${exploreCount}`);
|
|
86
|
+
if (urls.length > 0) {
|
|
87
|
+
console.log(`额外 URL: ${urls.length}\n`);
|
|
88
|
+
} else {
|
|
89
|
+
console.log('');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const allResults = [];
|
|
93
|
+
|
|
94
|
+
if (exploreCount > 0) {
|
|
95
|
+
try {
|
|
96
|
+
const exploreResults = await fetchExplore(exploreCount);
|
|
97
|
+
console.log(` 获取到 ${exploreResults.length} 个视频\n`);
|
|
98
|
+
if (isPipe) {
|
|
99
|
+
const videoUrls = exploreResults.map(r => r.url).filter(Boolean);
|
|
100
|
+
if (videoUrls.length > 0) {
|
|
101
|
+
await runScrape(videoUrls, proxyUrl, outputFile, outputFormat, filter);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
allResults.push(...exploreResults);
|
|
106
|
+
} catch (err) {
|
|
107
|
+
console.error(` Explore 获取失败: ${cleanError(err.message)}\n`);
|
|
108
|
+
console.error(` 请确保代理 ${proxyUrl} 正常运行\n`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (urls.length > 0) {
|
|
113
|
+
const errors = [];
|
|
114
|
+
|
|
115
|
+
for (let i = 0; i < urls.length; i++) {
|
|
116
|
+
const bar = '█'.repeat(i + 1).padEnd(urls.length);
|
|
117
|
+
process.stdout.write(`\r [${bar}] ${i + 1}/${urls.length}`);
|
|
118
|
+
try {
|
|
119
|
+
const results = await processUrl(urls[i], proxyUrl);
|
|
120
|
+
allResults.push(...results);
|
|
121
|
+
} catch (err) {
|
|
122
|
+
errors.push({ url: urls[i], message: err.message });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
console.log();
|
|
126
|
+
|
|
127
|
+
if (errors.length > 0) {
|
|
128
|
+
const msg = errors[0].message;
|
|
129
|
+
if (msg.includes('不可用') || msg.includes('连接被拒绝') || msg.includes('连接中断') ||
|
|
130
|
+
msg.includes('超时') || msg.includes('无法解析')) {
|
|
131
|
+
console.error(` ${errors.length} 个请求失败,请检查代理是否可用: ${proxyUrl}\n`);
|
|
132
|
+
} else {
|
|
133
|
+
console.error(` ${errors.length} 个失败:`);
|
|
134
|
+
const show = errors.slice(0, 5);
|
|
135
|
+
for (const e of show) {
|
|
136
|
+
console.error(` ✗ ${e.url}: ${e.message}`);
|
|
137
|
+
}
|
|
138
|
+
if (errors.length > 5) {
|
|
139
|
+
console.error(` ... 还有 ${errors.length - 5} 个`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const uniqueResults = deduplicate(allResults);
|
|
146
|
+
const filteredResults = applyFilter(uniqueResults, filter);
|
|
147
|
+
|
|
148
|
+
if (filteredResults.length === 0) {
|
|
149
|
+
console.log('\n未获取到数据');
|
|
150
|
+
if (outputFile) {
|
|
151
|
+
writeFileSync(outputFile, '[]', 'utf-8');
|
|
152
|
+
}
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const output = formatOutput(filteredResults, outputFormat);
|
|
157
|
+
|
|
158
|
+
if (outputFile) {
|
|
159
|
+
writeFileSync(outputFile, output, 'utf-8');
|
|
160
|
+
console.log(`\n结果已写入: ${outputFile}`);
|
|
161
|
+
} else {
|
|
162
|
+
console.log(output);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (filter) {
|
|
166
|
+
console.log(`\n共 ${uniqueResults.length} 个数据,过滤后 ${filteredResults.length} 个(过滤条件: ${formatFilterDescription(filter)})`);
|
|
167
|
+
} else {
|
|
168
|
+
console.log(`\n共 ${filteredResults.length} 个数据`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async function runScrape(urls, proxyUrl, outputFile, outputFormat, filter) {
|
|
173
|
+
const allResults = [];
|
|
174
|
+
const errors = [];
|
|
175
|
+
|
|
176
|
+
for (let i = 0; i < urls.length; i++) {
|
|
177
|
+
const bar = '█'.repeat(i + 1).padEnd(urls.length);
|
|
178
|
+
process.stdout.write(`\r [${bar}] ${i + 1}/${urls.length}`);
|
|
179
|
+
try {
|
|
180
|
+
const results = await processUrl(urls[i], proxyUrl);
|
|
181
|
+
allResults.push(...results);
|
|
182
|
+
} catch (err) {
|
|
183
|
+
errors.push({ url: urls[i], message: err.message });
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
console.log();
|
|
187
|
+
|
|
188
|
+
const uniqueResults = deduplicate(allResults);
|
|
189
|
+
const filteredResults = applyFilter(uniqueResults, filter);
|
|
190
|
+
|
|
191
|
+
if (errors.length > 0) {
|
|
192
|
+
if (filteredResults.length === 0) {
|
|
193
|
+
const msg = errors[0].message;
|
|
194
|
+
if (msg.includes('不可用') || msg.includes('连接被拒绝') || msg.includes('连接中断') ||
|
|
195
|
+
msg.includes('超时') || msg.includes('无法解析')) {
|
|
196
|
+
console.error(` 所有请求失败,请检查代理是否可用: ${proxyUrl}\n`);
|
|
197
|
+
} else {
|
|
198
|
+
const show = errors.slice(0, 5);
|
|
199
|
+
for (const e of show) {
|
|
200
|
+
console.error(` ✗ ${e.url}: ${e.message}\n`);
|
|
201
|
+
}
|
|
202
|
+
if (errors.length > 5) {
|
|
203
|
+
console.error(` ... 还有 ${errors.length - 5} 个失败\n`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
console.log('未获取到数据');
|
|
207
|
+
if (outputFile) {
|
|
208
|
+
writeFileSync(outputFile, '[]', 'utf-8');
|
|
209
|
+
}
|
|
210
|
+
return;
|
|
211
|
+
} else {
|
|
212
|
+
const msg = errors[0].message;
|
|
213
|
+
if (msg.includes('不可用') || msg.includes('连接被拒绝') || msg.includes('连接中断') ||
|
|
214
|
+
msg.includes('超时') || msg.includes('无法解析')) {
|
|
215
|
+
console.error(` ${errors.length} 个请求失败,请检查代理是否可用: ${proxyUrl}\n`);
|
|
216
|
+
} else {
|
|
217
|
+
console.error(` ${errors.length} 个失败:`);
|
|
218
|
+
const show = errors.slice(0, 5);
|
|
219
|
+
for (const e of show) {
|
|
220
|
+
console.error(` ✗ ${e.url}: ${e.message}`);
|
|
221
|
+
}
|
|
222
|
+
if (errors.length > 5) {
|
|
223
|
+
console.error(` ... 还有 ${errors.length - 5} 个`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const output = formatOutput(filteredResults, outputFormat);
|
|
230
|
+
|
|
231
|
+
if (outputFile) {
|
|
232
|
+
writeFileSync(outputFile, output, 'utf-8');
|
|
233
|
+
console.log(`\n结果已写入: ${outputFile}`);
|
|
234
|
+
} else {
|
|
235
|
+
console.log(output);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (filter) {
|
|
239
|
+
console.log(`\n共 ${uniqueResults.length} 个数据,过滤后 ${filteredResults.length} 个(过滤条件: ${formatFilterDescription(filter)})`);
|
|
240
|
+
} else {
|
|
241
|
+
console.log(`\n共 ${filteredResults.length} 个用户的数据`);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async function main() {
|
|
246
|
+
const { urls, outputFile, outputFormat, exploreCount, showConfig: showCfg, showHelp, customProxy, configAction, configValue, pipeMode, filterStr } = parseArgs();
|
|
247
|
+
const proxyUrl = customProxy || proxy;
|
|
248
|
+
const filter = parseFilter(filterStr);
|
|
249
|
+
|
|
250
|
+
if (showHelp) {
|
|
251
|
+
showUsage();
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (configAction) {
|
|
256
|
+
handleConfig(configAction, configValue);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (showCfg) {
|
|
261
|
+
showConfig(urls, outputFile);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (urls.length === 0 && exploreCount === 0) {
|
|
266
|
+
showUsage();
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (exploreCount > 0) {
|
|
271
|
+
await runExplore(exploreCount, urls, proxyUrl, outputFile, outputFormat, pipeMode, filter);
|
|
272
|
+
} else {
|
|
273
|
+
await runScrape(urls, proxyUrl, outputFile, outputFormat, filter);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
main().catch(err => {
|
|
278
|
+
console.error(`错误: ${err.message}`);
|
|
279
|
+
process.exit(1);
|
|
280
|
+
});
|