import re, os, sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
base = os.path.dirname(os.path.abspath(__file__))
html = open(os.path.join(base,'index_main.html'), encoding='utf-8').read()

blocks = dict(re.findall(r'\{id:"([^"]+)"([\s\S]*?)(?=\{id:"|$)', html))
def g(rest,key):
    mm=re.search(key+r':"([^"]*)"',rest); return mm.group(1) if mm else ''

EXPECT = {
 "ubc-meng-ece": "国际生首年 C$27,419.34",
 "emory-econ-ms": "$24,900/学期",
 "emory-ms-ds": "$24,900/学期",
}
for tid, kw in EXPECT.items():
    r=blocks.get(tid)
    if not r: print(f"!! {tid} 不在 PROGRAMS"); continue
    t=g(r,'tui')
    ok = kw in t
    print(f"{'OK' if ok else '!!'} {tid} | tui={t[:60]}")
    dp=os.path.join(base,'details',g(r,'url').split('/')[-1])
    print(f"    磁盘详情页存在: {os.path.exists(dp)} | 在 VD: {g(r,'url') in html}")

# 确认 emory-econ-ma 未被误改
r=blocks.get('emory-econ-ma')
if r: print(f"[对照] emory-econ-ma tui={g(r,'tui')}（应仍待补）")

# 统计剩余纯 tui 缺口
def missing(v): return v=='' or v=='待补' or v.startswith('待补')
only=[]
for tid,r in blocks.items():
    tui=g(r,'tui'); ddl=g(r,'ddl'); lang=g(r,'lang')
    tm=re.search(r'test:\{cls:"([^"]*)"\}',r); cls=tm.group(1) if tm else ''
    if missing(tui) and not missing(ddl) and not missing(lang) and not missing(cls):
        only.append((g(r,'s'),tid))
print(f"\n剩余仅缺学费条目: {len(only)} 条")
