-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch-fetch.mjs
More file actions
48 lines (40 loc) · 1.22 KB
/
batch-fetch.mjs
File metadata and controls
48 lines (40 loc) · 1.22 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
#!/usr/bin/env node
/**
* Fetch multiple URLs with Plasmate and save results as JSON.
*/
import { execSync } from "node:child_process";
import { writeFileSync } from "node:fs";
const DEFAULT_URLS = [
"https://news.ycombinator.com",
"https://github.com/trending",
"https://www.reddit.com/r/programming",
];
function fetchUrl(url) {
try {
const output = execSync(`plasmate fetch "${url}"`, {
encoding: "utf-8",
stdio: ["pipe", "pipe", "pipe"],
});
return { url, som: JSON.parse(output) };
} catch (err) {
return { url, error: err.message };
}
}
const urls = process.argv.slice(2);
const targets = urls.length > 0 ? urls : DEFAULT_URLS;
console.log(`Fetching ${targets.length} URLs...`);
const results = targets.map((url) => {
console.log(` → ${url}`);
return fetchUrl(url);
});
const output = {
fetched_at: new Date().toISOString(),
count: results.length,
results,
};
const outputFile = "results.json";
writeFileSync(outputFile, JSON.stringify(output, null, 2));
const successes = results.filter((r) => r.som).length;
const errors = results.filter((r) => r.error).length;
console.log(`\nDone: ${successes} succeeded, ${errors} failed`);
console.log(`Results saved to ${outputFile}`);