#!/usr/bin/env python3
"""Job #4528 (route 100 step check): fetch the served record the comparison needs.
Public, credential-free endpoints only:
GET /projects/twin-primes/return/
GET /projects/twin-primes/research-routes/
It writes every retrieved record verbatim into ./served/ and a small manifest
of what exists (so the step check is reproducible from the raw bytes).
"""
import json
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "..", "..", ".solveathome", "private", "lib"))
from served import served_evidence as se # noqa: E402
DEST = os.path.join(HERE, "served")
# route 100's own returns, the step's required sources, and the two step-check
# returns already on the route; then a whole-corpus sweep of everything that could
# have been recorded after the step-setter #1833.
IDS = [1264, 1267, 1804, 1833, 2018, 2019]
RANGE = range(1834, 2041)
def get(url, path):
try:
raw = se.fetch_bytes(url, timeout=45)
except Exception as exc: # missing ids 404; record the absence, do not invent
return {"path": path, "error": "%s" % type(exc).__name__}
with open(os.path.join(DEST, os.path.basename(path)), "wb") as fh:
fh.write(raw)
try:
body = json.loads(raw)
except Exception:
return {"path": path, "bytes": len(raw), "error": "not-json"}
return {"path": path, "bytes": len(raw), "id": body.get("id"),
"route": body.get("research_route_id"), "created_at": body.get("created_at"),
"type": body.get("type"), "status": body.get("status")}
def main():
os.makedirs(DEST, exist_ok=True)
manifest = []
for rid in sorted(set(list(IDS) + list(RANGE))):
manifest.append(get(se.return_url(se.DEFAULT_BASE, rid), "return-%d.json" % rid))
for r in (100, 127):
manifest.append(get(se.route_url(se.DEFAULT_BASE, r), "route-%d.json" % r))
with open(os.path.join(HERE, "fetch_manifest.json"), "w", encoding="utf-8",
newline="\n") as fh:
json.dump(manifest, fh, indent=1)
present = [m for m in manifest if "error" not in m and "id" in m]
print("fetched %d return records, %d route records"
% (len([m for m in present if m["path"].startswith("return")]),
len([m for m in present if m["path"].startswith("route")])))
if __name__ == "__main__":
main()