-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
393 lines (346 loc) · 13.7 KB
/
cli.ts
File metadata and controls
393 lines (346 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/env node
// ── ExportComments CLI ──
// A command-line tool for the ExportComments.com API, optimized for AI agent usage.
// All commands output structured JSON by default for easy parsing.
import { Command } from 'commander';
import { ExportCommentsClient } from './client.js';
import { PLATFORMS, detectPlatform } from './platforms.js';
import type { ExportOptions, JobResponse, CLIOutput, SocketEvent } from './types.js';
import { waitForJobRealtime } from './realtime.js';
// ── Helpers ──
function getToken(opts: { token?: string }): string {
const token = opts.token ?? process.env.EXPORTCOMMENTS_API_TOKEN;
if (!token) {
output({
ok: false,
error: 'Missing API token. Set EXPORTCOMMENTS_API_TOKEN env var or use --token <token>',
detail: 'Get your API token at https://app.exportcomments.com/user/api',
});
process.exit(1);
}
return token;
}
function getClient(opts: { token?: string; baseUrl?: string }): ExportCommentsClient {
return new ExportCommentsClient(getToken(opts), opts.baseUrl);
}
function output(data: CLIOutput | unknown): void {
console.log(JSON.stringify(data, null, 2));
}
function exitWithResult(result: CLIOutput): void {
output(result);
process.exit(result.ok ? 0 : 1);
}
function parseTimestamp(dateStr: string): number {
const ts = Date.parse(dateStr);
if (isNaN(ts)) {
output({ ok: false, error: `Invalid date: "${dateStr}". Use ISO 8601 format (e.g., 2024-01-15)` });
process.exit(1);
}
return Math.floor(ts / 1000);
}
// ── CLI Setup ──
const program = new Command();
program
.name('exportcomments')
.description(
`ExportComments.com CLI - Export comments and reviews from 40+ social media and review platforms.
This CLI is optimized for AI agent usage. All commands output structured JSON.
Authentication:
Set the EXPORTCOMMENTS_API_TOKEN environment variable, or pass --token <token> to each command.
Get your API token at: https://app.exportcomments.com/user/api
Supported platforms:
Instagram, YouTube, TikTok, Facebook, Twitter/X, LinkedIn, Reddit, Threads,
Trustpilot, Yelp, Amazon, Google Reviews, TripAdvisor, App Store, Play Store,
Twitch, Discord, Vimeo, IMDb, AliExpress, Shopee, Etsy, Walmart, Best Buy,
eBay, Flipkart, Product Hunt, Airbnb, Steam, VK, Lazada, Change.org, and more.
Examples:
$ exportcomments export https://www.instagram.com/p/ABC123/
$ exportcomments export https://www.youtube.com/watch?v=dQw4w9WgXcQ --replies --limit 500
$ exportcomments export https://www.amazon.com/dp/B08N5WRWNW --wait --download
$ exportcomments status <guid>
$ exportcomments list --page 1 --limit 10
$ exportcomments download <guid>
$ exportcomments download <guid> --json
$ exportcomments platforms
$ exportcomments platforms --detect https://www.youtube.com/watch?v=abc
$ exportcomments ping`
)
.version('1.0.0')
.option('--token <token>', 'API token (or set EXPORTCOMMENTS_API_TOKEN env var)')
.option('--base-url <url>', 'Override API base URL');
// ── export command ──
program
.command('export')
.alias('create')
.description('Create a new export job for a URL. Returns the job GUID for status tracking.')
.argument('<url>', 'The URL to export comments/reviews from')
.option('--replies', 'Include replies to comments')
.option('--limit <n>', 'Maximum number of items to export', parseInt)
.option('--min-date <date>', 'Minimum date filter (ISO 8601, e.g., 2024-01-15)')
.option('--max-date <date>', 'Maximum date filter (ISO 8601, e.g., 2024-06-30)')
.option('--vpn <country>', 'Use VPN with specified country (e.g., "Norway")')
.option('--cookies <json>', 'Cookies as JSON string (e.g., \'{"sessionid":"abc123"}\')')
.option('--tweets', 'Include tweets (Twitter/X)')
.option('--followers', 'Export followers (Twitter/X)')
.option('--following', 'Export following list (Twitter/X)')
.option('--likes', 'Export likes')
.option('--shares', 'Include shares data')
.option('--advanced', 'Enable advanced export features')
.option('--facebook-ads', 'Include Facebook ads data')
.option('--wait', 'Wait for export to complete (polls every 5s, timeout 10min)')
.option('--wait-interval <ms>', 'Polling interval in milliseconds (default: 5000)', parseInt)
.option('--wait-timeout <ms>', 'Maximum wait time in milliseconds (default: 600000)', parseInt)
.option('--realtime', 'Use WebSocket for real-time updates instead of polling (implies --wait)')
.option('--download', 'Download the file after export completes (implies --wait)')
.option('--download-json', 'Download the raw JSON data after export completes (implies --wait)')
.action(async (url: string, opts) => {
const globalOpts = program.opts();
const client = getClient(globalOpts);
// Build options
const options: ExportOptions = {};
if (opts.replies) options.replies = true;
if (opts.limit) options.limit = opts.limit;
if (opts.minDate) options.minTimestamp = parseTimestamp(opts.minDate);
if (opts.maxDate) options.maxTimestamp = parseTimestamp(opts.maxDate);
if (opts.vpn) options.vpn = opts.vpn;
if (opts.cookies) {
try {
options.cookies = JSON.parse(opts.cookies);
} catch {
exitWithResult({ ok: false, error: 'Invalid --cookies JSON. Use format: \'{"key":"value"}\'' });
return;
}
}
if (opts.tweets) options.tweets = true;
if (opts.followers) options.followers = true;
if (opts.following) options.following = true;
if (opts.likes) options.likes = true;
if (opts.shares) options.shares = true;
if (opts.advanced) options.advanced = true;
if (opts.facebookAds) options.facebookAds = true;
// Create the job
const result = await client.createJob({
url,
options: Object.keys(options).length > 0 ? options : undefined,
});
if (!result.ok) {
exitWithResult(result);
return;
}
const guid = result.data!.guid;
// If --wait, --realtime, or --download, wait until done
if (opts.wait || opts.realtime || opts.download || opts.downloadJson) {
let waitResult: CLIOutput<JobResponse>;
if (opts.realtime) {
waitResult = await waitForJobRealtime(getToken(program.opts()), guid, {
timeoutMs: opts.waitTimeout,
onEvent: (_event: string, data: SocketEvent) => {
process.stderr.write(
`\r[realtime] status=${data.status} exported=${data.current ?? 0}/${data.total ?? '?'} `
);
},
});
// Fall back to polling if WebSocket fails to connect
if (!waitResult.ok && (waitResult.error_code === 'WS_CONNECTION_TIMEOUT' || waitResult.error_code === 'WS_AUTH_FAILED' || waitResult.error_code === 'WS_ERROR')) {
process.stderr.write(`\n[realtime] WebSocket failed (${waitResult.error}), falling back to polling...\n`);
waitResult = await client.waitForJob(guid, {
intervalMs: opts.waitInterval,
timeoutMs: opts.waitTimeout,
onPoll: (job) => {
process.stderr.write(
`\r[polling] status=${job.status} exported=${job.total_exported ?? 0}/${job.total ?? '?'} `
);
},
});
}
} else {
waitResult = await client.waitForJob(guid, {
intervalMs: opts.waitInterval,
timeoutMs: opts.waitTimeout,
onPoll: (job) => {
process.stderr.write(
`\r[polling] status=${job.status} exported=${job.total_exported ?? 0}/${job.total ?? '?'} `
);
},
});
}
process.stderr.write('\n');
if (!waitResult.ok) {
exitWithResult(waitResult);
return;
}
const job = waitResult.data!;
// Download if requested
if (job.status === 'done' && (opts.download || opts.downloadJson)) {
if (opts.downloadJson) {
const jsonResult = await client.downloadJson(guid);
exitWithResult(jsonResult);
return;
} else {
const dlResult = await client.downloadJob(guid);
if (!dlResult.ok) {
exitWithResult(dlResult);
return;
}
exitWithResult({
ok: true,
data: {
...job,
downloaded_to: dlResult.data!.path,
downloaded_bytes: dlResult.data!.bytes,
},
});
return;
}
}
exitWithResult(waitResult);
return;
}
exitWithResult(result);
});
// ── status command ──
program
.command('status')
.alias('check')
.description('Check the status of an export job by its GUID.')
.argument('<guid>', 'The job GUID returned by the export command')
.option('--wait', 'Wait for export to complete')
.option('--wait-interval <ms>', 'Polling interval in milliseconds (default: 5000)', parseInt)
.option('--wait-timeout <ms>', 'Maximum wait time in milliseconds (default: 600000)', parseInt)
.option('--realtime', 'Use WebSocket for real-time updates instead of polling (implies --wait)')
.action(async (guid: string, opts) => {
const globalOpts = program.opts();
const client = getClient(globalOpts);
if (opts.wait || opts.realtime) {
let result: CLIOutput<JobResponse>;
if (opts.realtime) {
result = await waitForJobRealtime(getToken(globalOpts), guid, {
timeoutMs: opts.waitTimeout,
onEvent: (_event: string, data: SocketEvent) => {
process.stderr.write(
`\r[realtime] status=${data.status} exported=${data.current ?? 0}/${data.total ?? '?'} `
);
},
});
// Fall back to polling if WebSocket fails to connect
if (!result.ok && (result.error_code === 'WS_CONNECTION_TIMEOUT' || result.error_code === 'WS_AUTH_FAILED' || result.error_code === 'WS_ERROR')) {
process.stderr.write(`\n[realtime] WebSocket failed (${result.error}), falling back to polling...\n`);
result = await client.waitForJob(guid, {
intervalMs: opts.waitInterval,
timeoutMs: opts.waitTimeout,
onPoll: (job) => {
process.stderr.write(
`\r[polling] status=${job.status} exported=${job.total_exported ?? 0}/${job.total ?? '?'} `
);
},
});
}
} else {
result = await client.waitForJob(guid, {
intervalMs: opts.waitInterval,
timeoutMs: opts.waitTimeout,
onPoll: (job) => {
process.stderr.write(
`\r[polling] status=${job.status} exported=${job.total_exported ?? 0}/${job.total ?? '?'} `
);
},
});
}
process.stderr.write('\n');
exitWithResult(result);
} else {
const result = await client.getJob(guid);
exitWithResult(result);
}
});
// ── list command ──
program
.command('list')
.alias('ls')
.description('List all export jobs with pagination.')
.option('--page <n>', 'Page number (default: 1)', parseInt, 1)
.option('--limit <n>', 'Items per page (default: 20)', parseInt, 20)
.action(async (opts) => {
const globalOpts = program.opts();
const client = getClient(globalOpts);
const result = await client.listJobs(opts.page, opts.limit);
exitWithResult(result);
});
// ── download command ──
program
.command('download')
.alias('dl')
.description('Download the export file (Excel/CSV) or raw JSON data for a completed job.')
.argument('<guid>', 'The job GUID to download')
.option('--json', 'Download the raw JSON data instead of the formatted file')
.option('-o, --output <path>', 'Output file path (default: auto-detected from server)')
.action(async (guid: string, opts) => {
const globalOpts = program.opts();
const client = getClient(globalOpts);
if (opts.json) {
const result = await client.downloadJson(guid);
exitWithResult(result);
} else {
const result = await client.downloadJob(guid);
exitWithResult(result);
}
});
// ── platforms command ──
program
.command('platforms')
.description(
'List all supported platforms with URL patterns, options, and example URLs. ' +
'Use --detect <url> to identify which platform a URL belongs to.'
)
.option('--detect <url>', 'Detect which platform a URL belongs to')
.option('--id <platform_id>', 'Get info for a specific platform (e.g., "instagram")')
.action(async (opts) => {
if (opts.detect) {
const platform = detectPlatform(opts.detect);
if (platform) {
exitWithResult({ ok: true, data: platform });
} else {
exitWithResult({
ok: false,
error: `Could not detect platform for URL: ${opts.detect}`,
detail: 'The URL may not be from a supported platform. Use "exportcomments platforms" to see all supported platforms.',
});
}
return;
}
if (opts.id) {
const platform = PLATFORMS.find((p) => p.id === opts.id);
if (platform) {
exitWithResult({ ok: true, data: platform });
} else {
exitWithResult({
ok: false,
error: `Unknown platform: ${opts.id}`,
detail: `Available platforms: ${PLATFORMS.map((p) => p.id).join(', ')}`,
});
}
return;
}
exitWithResult({
ok: true,
data: {
total: PLATFORMS.length,
platforms: PLATFORMS,
},
});
});
// ── ping command ──
program
.command('ping')
.description('Check API connectivity and authentication.')
.action(async () => {
const globalOpts = program.opts();
const client = getClient(globalOpts);
const result = await client.ping();
exitWithResult(result);
});
// ── Run ──
program.parseAsync(process.argv).catch((err) => {
output({ ok: false, error: err.message ?? String(err) });
process.exit(1);
});