#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Freeze the live evidence for the SEARCH-CONVENTIONS.md correction. The claim is a claim about CHANNELS, so the evidence is the raw bytes each channel returned, saved with their sha256, plus the parse of what the corpus cares about (count, newest date). Uses only urllib: the same transport class the failed keyword channel used, so a reviewer can reproduce it with no tooling. """ import hashlib import json import os import re import sys import urllib.parse import urllib.request sys.stdout.reconfigure(encoding="utf-8", errors="replace") RUN = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) OUT = os.path.join(RUN, "evidence", "search-channel") UA = {"User-Agent": "solveathome-twin-primes-research/1.0 (channel evidence freeze)"} QUERIES = [ ("control-trivial", "all:%22twin+primes%22"), ("targeted-nt-recent", "cat:math.NT+AND+abs:%22twin+prime%22&sortBy=submittedDate&sortOrder=descending"), ("corpus-own-citation", "abs:%22M%C3%B6bius%22+AND+abs:%22Elliott-Halberstam%22"), ] def fetch(url): req = urllib.request.Request(url, headers=UA) with urllib.request.urlopen(req, timeout=45) as r: return r.status, r.read() def summarize(raw): t = raw.decode("utf-8", "replace") total = re.search(r"]*>(\d+)<", t) updated = re.search(r"([^<]+)", t) titles = re.findall(r".*?(.*?)", t, re.S) dates = re.findall(r".*?([^<]+)", t, re.S) ids = re.findall(r".*?(http[^<]*abs/[^<]+)", t, re.S) abstracts = re.findall(r".*?(.*?)", t, re.S) return { "totalResults": int(total.group(1)) if total else None, "feed_updated": updated.group(1) if updated else None, "entries_returned": len(titles), "newest_published": max(dates) if dates else None, "first": ([{"id": ids[i], "title": " ".join(titles[i].split()), "published": dates[i]} for i in range(min(2, len(titles)))]), "abstract_chars_first": len(abstracts[0]) if abstracts else 0, } def main(): os.makedirs(OUT, exist_ok=True) report = {"at": None, "transport": "urllib.request (direct HTTPS GET)", "queries": []} for name, q in QUERIES: url = "https://export.arxiv.org/api/query?search_query=" + q + "&start=0&max_results=5" rec = {"name": name, "query": q, "url": url} try: status, raw = fetch(url) p = os.path.join(OUT, "arxiv-%s.xml" % name) with open(p, "wb") as fh: fh.write(raw) rec.update({"http_status": status, "bytes": len(raw), "sha256": hashlib.sha256(raw).hexdigest(), "saved": os.path.relpath(p, RUN).replace("\\", "/"), "parsed": summarize(raw)}) except Exception as e: # a channel that refuses is the finding, so record it rec.update({"error": "%s: %s" % (type(e).__name__, e)}) report["queries"].append(rec) print(json.dumps(rec, ensure_ascii=False, indent=1)) with open(os.path.join(RUN, "artifacts", "search-channel.json"), "w", encoding="utf-8", newline="\n") as fh: fh.write(json.dumps(report, ensure_ascii=False, indent=1) + "\n") ok = sum(1 for q in report["queries"] if q.get("http_status") == 200) print(json.dumps({"channels_answering": ok, "of": len(QUERIES)})) return 0 if __name__ == "__main__": sys.exit(main())