85 lines
2.5 KiB
Python
85 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Extract the JSONPlaceholder /users table (10 records) into structured files.
|
|
|
|
Real tool call: urllib.request against a live public endpoint (no auth, no
|
|
paywall, no paid credits). Produces raw JSON, a CSV table, a Markdown table,
|
|
and a normalised JSON blob.
|
|
|
|
Output (in /root/.hermes/wizard-outputs/ledger/):
|
|
2026-10-23-jpv-users.raw.json - untouched HTTP response body
|
|
2026-10-23-jpv-users.csv - normalised table (8 columns)
|
|
2026-10-23-jpv-users.md - Markdown table
|
|
2026-10-23-jpv-users.json - normalised records (JSON)
|
|
"""
|
|
import csv
|
|
import json
|
|
import os
|
|
import urllib.request
|
|
|
|
SRC = "https://jsonplaceholder.typicode.com/users"
|
|
OUTDIR = "/root/.hermes/wizard-outputs/ledger"
|
|
RAW = os.path.join(OUTDIR, "2026-10-23-jpv-users.raw.json")
|
|
CSV = os.path.join(OUTDIR, "2026-10-23-jpv-users.csv")
|
|
MD = os.path.join(OUTDIR, "2026-10-23-jpv-users.md")
|
|
JSOUT = os.path.join(OUTDIR, "2026-10-23-jpv-users.json")
|
|
|
|
FIELDS = ["id", "name", "username", "email", "city", "phone", "website", "company"]
|
|
|
|
|
|
def fetch():
|
|
req = urllib.request.Request(
|
|
SRC, headers={"User-Agent": "ledger-extract/1.0"}
|
|
)
|
|
with urllib.request.urlopen(req, timeout=30) as r:
|
|
return r.read().decode("utf-8")
|
|
|
|
|
|
def main():
|
|
os.makedirs(OUTDIR, exist_ok=True)
|
|
raw = fetch()
|
|
with open(RAW, "w") as f:
|
|
f.write(raw)
|
|
|
|
recs = json.loads(raw)
|
|
rows = []
|
|
for rec in recs:
|
|
addr = rec.get("address") or {}
|
|
comp = rec.get("company") or {}
|
|
rows.append({
|
|
"id": rec["id"],
|
|
"name": rec["name"],
|
|
"username": rec["username"],
|
|
"email": rec["email"],
|
|
"city": addr.get("city"),
|
|
"phone": rec["phone"],
|
|
"website": rec["website"],
|
|
"company": comp.get("name"),
|
|
})
|
|
|
|
with open(CSV, "w", newline="") as f:
|
|
w = csv.DictWriter(f, fieldnames=FIELDS)
|
|
w.writeheader()
|
|
w.writerows(rows)
|
|
|
|
lines = [
|
|
"| id | name | username | email | city | phone | website | company |",
|
|
"|---|---|---|---|---|---|---|---|",
|
|
]
|
|
for r in rows:
|
|
lines.append("| " + " | ".join(str(r[c]) for c in FIELDS) + " |")
|
|
with open(MD, "w") as f:
|
|
f.write("\n".join(lines) + "\n")
|
|
|
|
with open(JSOUT, "w") as f:
|
|
json.dump(rows, f, indent=2)
|
|
|
|
print("fetched", len(rows), "users from", SRC)
|
|
print("raw:", RAW)
|
|
print("csv:", CSV)
|
|
print("md:", MD)
|
|
print("json:", JSOUT)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|