-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch-fetch.sh
More file actions
executable file
·50 lines (41 loc) · 1.46 KB
/
batch-fetch.sh
File metadata and controls
executable file
·50 lines (41 loc) · 1.46 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
#!/usr/bin/env bash
# batch-fetch.sh — Fetch multiple URLs via Plasmate and save results as JSON.
#
# Usage: ./batch-fetch.sh [url1] [url2] ...
# Output: results.json
# Requires: plasmate, jq
set -euo pipefail
DEFAULT_URLS=(
"https://news.ycombinator.com"
"https://github.com/trending"
"https://lobste.rs"
)
command -v plasmate >/dev/null 2>&1 || { echo "Error: plasmate not found. Install: curl -fsSL https://plasmate.app/install.sh | sh" >&2; exit 1; }
command -v jq >/dev/null 2>&1 || { echo "Error: jq not found. Install: https://jqlang.github.io/jq/download/" >&2; exit 1; }
URLS=("${@:-${DEFAULT_URLS[@]}}")
OUTPUT="results.json"
echo "Fetching ${#URLS[@]} URLs..." >&2
results="[]"
ok=0
fail=0
for url in "${URLS[@]}"; do
echo " → $url" >&2
som=$(plasmate fetch "$url" 2>/dev/null) && rc=0 || rc=$?
if [ "$rc" -eq 0 ]; then
entry=$(jq -n --arg url "$url" --argjson som "$som" '{url: $url, som: $som}')
ok=$((ok + 1))
else
entry=$(jq -n --arg url "$url" --arg error "fetch failed (exit $rc)" '{url: $url, error: $error}')
fail=$((fail + 1))
fi
results=$(echo "$results" | jq --argjson e "$entry" '. + [$e]')
done
fetched_at=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
jq -n \
--arg fetched_at "$fetched_at" \
--argjson count "${#URLS[@]}" \
--argjson results "$results" \
'{fetched_at: $fetched_at, count: $count, results: $results}' > "$OUTPUT"
echo "" >&2
echo "Done: $ok succeeded, $fail failed" >&2
echo "Results saved to $OUTPUT" >&2