"""Mirror the served docs tree that the QUESTIONS index generator reads. Mirrors research/*.md, research/history/staging/*.md and research/qc/* into work/job3754/corpus/ so `node research/gen-questions-index.js` can run against the bytes the server actually serves. Threaded, read-only, one request per file. """ import concurrent.futures as cf import io import os import sys import urllib.error import urllib.request sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace") SERVER = "https://solveathome.org" HERE = os.path.abspath(os.path.join("work", "job3754")) CORPUS = os.path.join(HERE, "corpus") def credential(): path = os.path.join(os.environ.get("LOCALAPPDATA", ""), "solveathome", "credentials", "twin-primes.token") with open(path, encoding="utf-8") as fh: return fh.read().strip() TOKEN = credential() def get(path): req = urllib.request.Request(SERVER + path, method="GET") req.add_header("Authorization", "Bearer " + TOKEN) req.add_header("Accept", "application/json") req.add_header("X-Model", "deepseek/deepseek-v4-flash") req.add_header("X-Effort", "max") req.add_header("User-Agent", "Buffy-FreebuffDesktop") try: with urllib.request.urlopen(req, timeout=120) as r: return r.status, r.read() except urllib.error.HTTPError as e: return e.code, e.read() def listing(d): code, data = get("/projects/twin-primes/docs/" + d) if code != 200: return [] names = [x for x in data.decode("utf-8", "replace").split("\n") if x.strip()] return [n for n in names if not n.endswith("/")] def fetch_one(rel): """rel is relative to docs/ ; store under corpus/ with the same layout.""" code, data = get("/projects/twin-primes/docs/" + rel) if code != 200: return rel, code, 0 dst = os.path.join(CORPUS, *rel.split("/")) os.makedirs(os.path.dirname(dst), exist_ok=True) with open(dst, "wb") as fh: fh.write(data) return rel, code, len(data) def main(): targets = [] for d in ("research", "research/history", "research/qc"): for n in listing(d): targets.append(d + "/" + n) staging = ["research/history/staging/" + n for n in listing("research/history/staging")] targets += staging # top-level research/*.md only (the generator's bodyMarkdown) plus any qc dir file md_body = [t for t in targets if t.startswith("research/") and t.count("/") == 1 and t.endswith(".md")] qc = [t for t in targets if t.startswith("research/qc/")] hist = [t for t in targets if t.startswith("research/history/") and t.count("/") == 2] todo = ["TODO.md"] wanted = sorted(set(md_body + qc + staging + hist + todo)) print("fetching %d paths (%d staging, %d body, %d qc, %d history, 1 TODO)" % (len(wanted), len(staging), len(md_body), len(qc), len(hist))) ok = bad = 0 total = 0 with cf.ThreadPoolExecutor(max_workers=8) as ex: for rel, code, size in ex.map(fetch_one, wanted): if code == 200: ok += 1 total += size else: bad += 1 print(" FAILED %s http=%s" % (rel, code)) print("fetched ok=%d failed=%d bytes=%d" % (ok, bad, total)) if __name__ == "__main__": main()