"""Fetch USGS ComCat events of magnitude 6.5 and above, as CSV, from the FDSN event service. Standard library only; appends URL, size and SHA-256 to fetch-log.jsonl. Usage: python3 -I fetch_usgs.py """ import datetime, hashlib, json, pathlib, sys, urllib.request outdir = pathlib.Path(sys.argv[1]); start, end = sys.argv[2], sys.argv[3] outdir.mkdir(parents=True, exist_ok=True) url = ("https://earthquake.usgs.gov/fdsnws/event/1/query?format=csv" f"&starttime={start}&endtime={end}&minmagnitude=6.5&orderby=time-asc") req = urllib.request.Request(url, headers={"User-Agent": "factchecked.science verification (Python urllib)"}) with urllib.request.urlopen(req, timeout=300) as r: data = r.read() name = f"usgs-m6.5-{start}-{end}.csv" (outdir / name).write_bytes(data) rec = {"file": name, "url": url, "bytes": len(data), "sha256": hashlib.sha256(data).hexdigest(), "fetched_utc": datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")} with (outdir / "fetch-log.jsonl").open("a", encoding="utf-8") as f: f.write(json.dumps(rec) + "\n") print(json.dumps(rec, indent=1))