#!/usr/bin/env python3 """Is return #161's published table still reachable? Probe every sha it quotes; record the answer. Route 27's registered `next_step` is a comparison against "#161's published table ... all 1,307 entries". #161 is job 32's return, in the `hashes` map of which are nine artifacts, among them `out-L-ext.txt` -- the L table. Its `files` array is EMPTY (the same shape this run has been auditing: `files: []` next to a full `hashes` map), so the only way it can be reachable at all is by sha. This script asks the store for each of the nine and records the status code and, when the object is there, whether the bytes hash back to the sha the return quotes. Read-only. Output: `fetch161.json`, plus each retrieved object written under `r161/`. """ import hashlib import json import os import pathlib import sys import urllib.error import urllib.request TOK = os.environ["SOLVEATHOME_TOKEN"] BASE = "https://solveathome.org" HERE = pathlib.Path(__file__).parent OUT = HERE / "r161" OUT.mkdir(exist_ok=True) def get(url, accept="application/octet-stream"): r = urllib.request.Request(url, headers={"Authorization": "Bearer " + TOK, "Accept": accept, "User-Agent": "fetch161"}) try: with urllib.request.urlopen(r, timeout=90) as x: return x.status, x.read() except urllib.error.HTTPError as e: return e.code, e.read()[:300] except Exception as e: # noqa: BLE001 return 0, repr(e)[:200].encode() def sha(b): return hashlib.sha256(b).hexdigest() def main(): c, t = get("%s/projects/twin-primes/return/161" % BASE, accept="application/json") if c != 200: print(json.dumps({"ok": False, "why": "return 161 answered %s" % c}, indent=1)) return 1 d = json.loads(t) (OUT / "return161.json").write_bytes(t) hashes = d.get("hashes") or {} listed = [f.get("sha256") for f in (d.get("files") or [])] res = [] for name, s in sorted(hashes.items()): code, blob = get("%s/files/%s" % (BASE, s)) rec = {"name": name, "sha256": s, "http": code, "bytes": len(blob)} if code == 200: rec["hashes_back"] = (sha(blob) == s) if rec["hashes_back"]: (OUT / os.path.basename(name)).write_bytes(blob) res.append(rec) out = {"return_id": 161, "job_id": d.get("job_id"), "files_listed_on_return": len(listed), "hashes_quoted": len(hashes), "reachable": sum(1 for r in res if r.get("hashes_back")), "unreachable": [r["name"] for r in res if not r.get("hashes_back")], "probes": res} (HERE / "fetch161.json").write_text(json.dumps(out, indent=1, sort_keys=True), encoding="utf-8", newline="\n") print(json.dumps({k: out[k] for k in ("return_id", "job_id", "files_listed_on_return", "hashes_quoted", "reachable", "unreachable")}, indent=1)) return 0 if __name__ == "__main__": sys.exit(main())