#!/usr/bin/env python3 """Minimal reader for the project's served endpoints. Why this exists rather than a shared tool: the pinned `sahtool` wraps the *department* endpoints (jobs, results, transcripts) and has no reader for `/research-routes/`, `/return/` or the public file store. This reader is deliberately tiny so a reviewer can re-run it: GET only, one file per URL, the sha256 of the returned bytes printed next to the URL that produced them. python work2838/fetch.py --out evidence/x.json [--auth] Anonymous by default -- and that is the point of the retrievability half of this experiment: the accepted text must answer with NO Authorization header, or it is not reachable by an ordinary reader. Credentials are read from the token FILE named by SOLVEATHOME_TOKEN_FILE and are never printed, logged or passed on a command line. """ import argparse import hashlib import json import os import sys import urllib.error import urllib.request BASE = "https://solveathome.org" PROJECT = "twin-primes" def token(): path = os.environ.get("SOLVEATHOME_TOKEN_FILE") if not path: return None try: with open(path, encoding="utf-8") as fh: return fh.read().strip() except OSError: return None def get(url, auth=False, timeout=90): """Return (status, body_bytes, headers). A 404 is a result, not an exception.""" if not url.startswith("http"): url = BASE + url headers = {"User-Agent": "t1-2026-09-23/2838 (+read-only)"} if auth: tok = token() if not tok: raise SystemExit("--auth given but no SOLVEATHOME_TOKEN_FILE resolved") headers["Authorization"] = "Bearer " + tok req = urllib.request.Request(url, headers=headers) try: with urllib.request.urlopen(req, timeout=timeout) as resp: return resp.status, resp.read(), dict(resp.headers) except urllib.error.HTTPError as exc: return exc.code, exc.read(), dict(exc.headers or {}) except Exception as exc: # transport failure is a result too -- never swallowed silently return None, repr(exc).encode(), {} def main(): ap = argparse.ArgumentParser() ap.add_argument("url") ap.add_argument("--out") ap.add_argument("--auth", action="store_true") ap.add_argument("--json", action="store_true", help="pretty-print the body instead of saving") args = ap.parse_args() status, body, _ = get(args.url, auth=args.auth) digest = hashlib.sha256(body).hexdigest() if args.out: os.makedirs(os.path.dirname(os.path.abspath(args.out)), exist_ok=True) with open(args.out, "wb") as fh: fh.write(body) print(json.dumps({"url": args.url if args.url.startswith("http") else BASE + args.url, "auth": args.auth, "status": status, "bytes": len(body), "sha256": digest, "out": args.out}, indent=1)) if args.json: try: print(json.dumps(json.loads(body.decode("utf-8")), indent=1)) except Exception as exc: print("not JSON: %s" % exc, file=sys.stderr) if __name__ == "__main__": main()