# -*- coding: utf-8 -*-
import re, io, sys, json, collections
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
html = open('index_main.html', encoding='utf-8').read()
m = re.search(r'const PROGRAMS=\[([\s\S]*?)\];\s*\n', html)
seg = m.group(1)
recs = re.findall(r'\{id:"[^"]+"[\s\S]*?(?=\{id:"|,?\n\];)', seg)
reachable = {"col","up","nyu","gatech","duke","ucla","ut","ubc","stanford"}
prio = {"CS":0,"EE":1,"金融":2,"经济":3,"BA":4,"DS":5,"统计":6,"机械":7,"生物":8,"管理":9}
def miss(r):
    out=[]
    for f in ["ddl","tui","lang"]:
        v=re.search(rf'{f}:"([^"]*)"', r)
        if not v or v.group(1)=="" or "待补" in v.group(1): out.append(f)
    t=re.search(r'test:\{cls:"([^"]*)"', r)
    if not t or t.group(1)=="" or "待补" in t.group(1): out.append("test")
    return out
cand=[]
for r in recs:
    sid=re.search(r'\{id:"([^"]+)".*?s:"([^"]+)"', r)
    if not sid: continue
    pid, sch = sid.group(1), sid.group(2)
    if sch not in reachable: continue
    mm=miss(r)
    if mm:
        en=re.search(r'en:"([^"]*)"', r); n=re.search(r'n:"([^"]*)"', r); deg=re.search(r'deg:"([^"]*)"', r)
        url=re.search(r'url:"([^"]*)"', r)
        cand.append((sch, pid, n.group(1) if n else "", en.group(1) if en else "", deg.group(1) if deg else "", ",".join(mm), url.group(1) if url else ""))
print("可达校半核验候选:", len(cand))
bysch=collections.Counter(c[0] for c in cand)
print("按校:", dict(bysch))
for c in cand[:40]:
    print(" | ".join(c))
