#!/usr/bin/env python3 """Re-apply HS parsing to draft CSVs using cached hs_raw (no network).""" from __future__ import annotations import csv from pathlib import Path from fetch_bbref_draft_hs import load_cache, parse_hs_raw_line ROOT = Path(__file__).resolve().parents[1] RAW = ROOT / "sources" / "bbref_raw" def main() -> None: cache = load_cache() updated = 0 for path in sorted(RAW.glob("draft_*.csv")): with path.open(encoding="utf-8") as f: rows = list(csv.DictReader(f)) if not rows: continue fields = list(rows[0].keys()) for row in rows: slug = row.get("bbref_player_slug") or "" hs_raw = "" if slug and slug in cache: hs_raw = cache[slug].get("hs_raw") or "" if not hs_raw: hs_raw = row.get("hs_raw") or "" if not hs_raw: continue parsed = parse_hs_raw_line(hs_raw) old = row.get("high_school_finish") or "" new = parsed.get("high_school_finish") or "" if new and new != old: updated += 1 if new: row["high_school_finish"] = new row["hs_city"] = parsed.get("hs_city", "") row["hs_state"] = parsed.get("hs_state", "") row["hs_raw"] = hs_raw if slug and slug in cache and new: cache[slug]["high_school_finish"] = new cache[slug]["hs_city"] = parsed.get("hs_city", "") cache[slug]["hs_state"] = parsed.get("hs_state", "") cache[slug]["hs_all"] = parsed.get("hs_all", "") with path.open("w", newline="", encoding="utf-8") as f: w = csv.DictWriter(f, fieldnames=fields, extrasaction="ignore") w.writeheader() w.writerows(rows) print(f"{path.name}: reparsed") from fetch_bbref_draft_hs import save_cache save_cache(cache) print(f"updated finishing-school strings: {updated}") if __name__ == "__main__": main()