File size: 2,084 Bytes
e94c8f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/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()