"""A review summary for a Reddit scraper pull request (issue #23). python -m collection review-reddit --base BASE.json [--out summary.md] [--pr N] Compares the pull request's data/public/reddit_comments.json with `BASE.json` (the same file on main) and reads the runs as built from it (data/public/runs_clean.csv, so run `python -m collection build` first). The summary lists what a reviewer needs to decide on, before merging: - runs added, changed (answers differ) and gone; - runs that break a game rule: left out of the route model and the statistics until corrected; - other flags on the new or changed runs (kept, for information); - runs that may also be in the community form (analysis/overlap/overlap.py: same answers within a day); - runs whose result page is already a "reddit thread" row (counted once, automatically). Corrections go in collection/manual/reddit_comments.yaml (reject / fix / add; see collection/README.md). With `--pr N` the summary is posted on the pull request, replacing the previous one. """ import importlib.util import json import sys from .common import ROOT, path, pipeline, read_yaml MARKER = "" PROVENANCE = "read from a Reddit comment" def _overlap(): spec = importlib.util.spec_from_file_location("overlap", ROOT / "analysis/overlap/overlap.py") mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) return mod def link(run_id, thread): cid = str(run_id).split("-")[0] return f"[{run_id}](https://www.reddit.com/r/fourthwing/comments/{thread}/_/{cid}/)" def diff(base, head): """(added, changed, gone) comment-run ids between two reddit_comments.json documents.""" b = {str(r["id"]): r for r in base.get("runs", [])} h = {str(r["id"]): r for r in head.get("runs", [])} added = sorted(set(h) - set(b)) gone = sorted(set(b) - set(h)) changed = sorted(k for k in set(h) & set(b) if h[k].get("answers") != b[k].get("answers")) return added, changed, gone def summary(base, head, runs, known_results=(), overlap=None): """The Markdown summary. `runs`: typed records of runs_clean.csv (with run_id).""" sys.path.insert(0, str(ROOT / "core")) from atlas import runs as runs_io added, changed, gone = diff(base, head) focus = set(added) | set(changed) thread = {str(r["id"]): r.get("thread") for r in head.get("runs", [])} result = {str(r["id"]): r.get("result") for r in head.get("runs", [])} rows = {r["run_id"][len("comment-"):]: r for r in runs if r["source"] == "reddit comment"} flags = {k: [f for f in (r.get("data_flags") or "").split("; ") if f and not f.startswith(PROVENANCE)] for k, r in rows.items()} impossible = sorted(k for k in rows if runs_io.impossible(rows[k])) L = [MARKER, "## Reddit comment runs: review summary", ""] L.append(f"**{len(head.get('runs', []))} runs** in this file: **{len(added)} new**, {len(changed)} changed, " f"{len(gone)} gone since main. {head.get('unread', 0)} comments mention a bond or a burn but could not " "be read as a run.") L.append("") L += ["### Runs that break a game rule", "", "Left out of the route model and the statistics until corrected. Check the comment: `fix` a misread " "answer, or `reject` a run that is not one.", ""] bad = [k for k in impossible if k in focus] + [k for k in impossible if k not in focus] if bad: L += ["| Run | New? | Rule |", "|---|---|---|"] L += [f"| {link(k, thread.get(k) or '')} | {'yes' if k in focus else ''} | " f"{'; '.join(f for f in flags[k] if runs_io.impossible({'data_flags': f}))} |" for k in bad] else: L.append("None.") L.append("") other = [k for k in sorted(focus) if k in flags and flags[k] and k not in impossible] L += ["### Other flags on new or changed runs (kept)", ""] L += [f"- {link(k, thread.get(k) or '')}: {'; '.join(flags[k])}" for k in other] or ["None."] L.append("") if overlap is not None: twice = {m["comment"][len("comment-"):]: m["form"] for m in overlap.get("matches", [])} hits = [k for k in sorted(focus) if k in twice] L += ["### May also be in the community form", "", f"Same answers as a form row submitted within {overlap['rule']['window_days']} day(s). Over all Reddit " f"runs: {overlap['matched']} of {overlap['comparable']} comparable runs match, about " f"{overlap['expected_by_chance']} by chance, so about {overlap['likely_double']} look reported twice. " "Nothing is removed: identical routes are expected.", ""] L += [f"- {link(k, thread.get(k) or '')}: {', '.join(twice[k][:5])}" for k in hits] or ["None among the new runs."] L.append("") counted = [k for k in sorted(focus) if result.get(k) and str(result[k]).upper() in known_results] L += ["### Result pages already counted", "", "Their result page is also a \"reddit thread\" row; the run is counted once, with its route.", ""] L += [f"- {link(k, thread.get(k) or '')}: {result[k]}" for k in counted] or ["None."] L.append("") L += ["### How to correct", "", "In `collection/manual/reddit_comments.yaml` (applied at build time, on this branch or later):", "- `reject: [comment id]`: not a run (a joke, a friend's run, two runs told as one);", "- `fix: {comment id: {question: answer}}`: answers read wrong (`null` removes one);", "- `add: [{id, thread, posted, answers}]`: a run the reader missed.", "", "A corrected run loses the \"not checked by hand\" note. Then merge."] return "\n".join(L) + "\n" def main(base_file, out=None, pr=None): sys.path.insert(0, str(ROOT / "core")) from atlas import runs as runs_io spec = pipeline()["sources"]["reddit_comments"] base = json.loads(open(base_file, encoding="utf-8").read()) if base_file else {"runs": []} head = json.loads(path(spec["load"]).read_text(encoding="utf-8")) runs = runs_io.read(drop=()) thread_spec = pipeline()["sources"].get("reddit_thread") or {} manual = (thread_spec.get("transform") or {}).get("manual") known = {str(r["id"]).upper() for r in (read_yaml(manual)["results"] if manual else [])} text = summary(base, head, runs, known, _overlap().measure(runs)) if out: open(out, "w", encoding="utf-8").write(text) else: print(text) if pr: from . import forge forge.upsert_comment(int(pr), MARKER, text) print(f"summary posted on #{pr}") return 0