-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_fetch.py
More file actions
55 lines (47 loc) · 1.47 KB
/
batch_fetch.py
File metadata and controls
55 lines (47 loc) · 1.47 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
#!/usr/bin/env python3
"""Fetch multiple URLs with Plasmate and save results as JSON."""
import subprocess
import json
import sys
import os
from datetime import datetime
URLS = [
"https://news.ycombinator.com",
"https://github.com/trending",
"https://www.reddit.com/r/programming",
]
def fetch_url(url: str) -> dict:
"""Fetch a single URL and return the SOM."""
result = subprocess.run(
["plasmate", "fetch", url],
capture_output=True,
text=True,
)
if result.returncode != 0:
return {"url": url, "error": result.stderr.strip()}
try:
som = json.loads(result.stdout)
return {"url": url, "som": som}
except json.JSONDecodeError as e:
return {"url": url, "error": f"JSON decode error: {e}"}
def main():
urls = sys.argv[1:] if len(sys.argv) > 1 else URLS
print(f"Fetching {len(urls)} URLs...")
results = []
for url in urls:
print(f" → {url}")
results.append(fetch_url(url))
output = {
"fetched_at": datetime.utcnow().isoformat() + "Z",
"count": len(results),
"results": results,
}
output_file = "results.json"
with open(output_file, "w") as f:
json.dump(output, f, indent=2)
successes = sum(1 for r in results if "som" in r)
errors = sum(1 for r in results if "error" in r)
print(f"\nDone: {successes} succeeded, {errors} failed")
print(f"Results saved to {output_file}")
if __name__ == "__main__":
main()