# -*- coding: utf-8 -*-
import json, re, io, sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

BASE = r"D:\work buddy\2026-11-05-14-15-52"
html = open(BASE + r"\index_main.html", encoding="utf-8").read()

prog_url = set(re.findall(r'url:"(details/[^"]+)"', html))
prog_en = set()
for chunk in html.split('{id:"')[1:]:
    ms = re.search(r's:"([^"]*)"', chunk)
    me = re.search(r'en:"([^"]*)"', chunk)
    if ms and me:
        prog_en.add((ms.group(1), re.sub(r'\s+', '', me.group(1)).lower()))

mvd = re.search(r'const VERIFIED_DETAILS=new Set\(\[(.*?)\]\)', html, re.S)
vd = set(re.findall(r'"(details/[^"]+)"', mvd.group(1))) if mvd else set()

todo = json.load(open(BASE + r"\todo_upgrade.json", encoding="utf-8"))

def norm(s):
    return re.sub(r'[^a-z0-9]', '', s.lower())

def is_phd(t):
    d = t.get("deg", "").lower()
    return "phd" in d or "ph.d" in d or "doctor" in d

# 已知可达/优先校；已知死路校
alive = {"uchicago", "uw", "harvard", "notre", "cornell", "duke", "wustl",
         "jhu", "stanford", "ut", "dartmouth", "col", "up", "umich",
         "vanderbilt", "mcg", "ubc", "brown", "rice"}

rows = []
for t in todo:
    det = t.get("det", "")
    if det in vd or det in prog_url:
        continue
    if (t.get("s", ""), norm(t.get("en", ""))) in prog_en:
        continue
    if is_phd(t):
        continue
    rows.append(t)

from collections import defaultdict
by = defaultdict(list)
for t in rows:
    by[t.get("s", "?")].append(t)

for s in ["uchicago", "uw", "harvard", "notre", "duke", "cornell", "jhu", "ut", "stanford"]:
    items = by.get(s, [])
    print(f"=== {s}  未匹配硕士 {len(items)} ===")
    for t in items[:20]:
        print(f"  [{t.get('disc','?'):3}] {t.get('deg',''):16} | {t.get('en','')[:46]:46} | {t.get('det','')}")
