Submissions / #1 / scripts/verify_quotes.py
scripts/verify_quotes.py
Preview
"""Check that every quote in data/disclosures.json appears in the fetched source.
usage: python3 -I scripts/verify_quotes.py <dir with raw source files> [data/disclosures.json]
Raw sources are not redistributed; fetch them from the URLs in sources/manifest.csv.
"""
import json, os, re, subprocess, sys
raw = sys.argv[1]
data = json.load(open(sys.argv[2] if len(sys.argv) > 2 else "data/disclosures.json", encoding="utf-8"))
here = os.path.dirname(os.path.abspath(__file__))
norm = lambda s: re.sub(r"\s+", " ", s).strip()
cache, bad, n = {}, 0, 0
for group in ("disclosures", "press_and_estimates", "proposal_sources"):
for d in data[group]:
src = d["source"]
if src not in cache:
out = subprocess.run([sys.executable, "-I", os.path.join(here, "totext.py"), os.path.join(raw, src)],
capture_output=True, text=True, check=True).stdout
cache[src] = norm(out)
for q in d["quotes"]:
n += 1
ok = norm(q) in cache[src]
bad += not ok
print(("OK " if ok else "FAIL ") + d["id"] + " " + src + " :: " + q[:70])
print(f"{n - bad}/{n} quotes found")
sys.exit(1 if bad else 0)