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()

m = re.search(r'const PROGRAMS\s*=\s*\[(.*?)\];', html, re.S)
body = m.group(1)
blocks = dict(re.findall(r'\{id:"([^"]+)"([\s\S]*?)(?=\{id:"|$)', body))
vdm = re.search(r'const VERIFIED_DETAILS=new Set\(\[(.*?)\]\)', html, re.S)
vd = set(re.findall(r'"([^"]+)"', vdm.group(1))) if vdm else set()

renames = [
    ("col-mesaas-ma", "Middle Eastern, South Asian, and African Studies (MA)", "中东、南亚与非洲研究文学硕士（MA, MESAAS）"),
    ("nyu-cinema-ma", "Cinema Studies (MA)", "电影研究文学硕士（MA）"),
    ("nyu-hebrew-ma", "Hebrew and Judaic Studies (MA)", "希伯来与犹太研究文学硕士（MA）"),
    ("col-religion-ma", "Religion (MA, free-standing)", "宗教学文学硕士（MA，独立学位）"),
]

def g(rest, key):
    mm = re.search(key+r':"([^"]*)"', rest)
    return mm.group(1) if mm else ''

for tid, old_n, new_n in renames:
    rest = blocks[tid]
    n=g(rest,'n'); en=g(rest,'en'); url=g(rest,'url')
    det = os.path.join(base, url.replace('/', os.sep))
    exists = os.path.exists(det)
    ok_n = (n == new_n)
    ok_old = old_n not in html
    invd = url in vd
    print(f"{tid} | n_ok={ok_n} | old残留={not ok_old} | en保留={'(' in en or en != ''} | VD={invd} | disk={exists}")
    print(f"   n={n}\n   en={en}\n   url={url}")

# 剩余全英文 n 扫描（纯 ASCII 字母/标点，无中文字符）
left = []
for tid, rest in blocks.items():
    n = g(rest,'n')
    if n and not re.search(r'[\u4e00-\u9fff]', n):
        left.append((tid, n))
print("\n剩余全英文 n:", len(left))
for tid, n in left:
    print(f"  {tid} | {n}")

print("\nPROGRAMS 条数:", len(blocks), "url 去重:", len(set(g(r,'url') for r in blocks.values())))
print("bytes:", os.path.getsize(os.path.join(base,'index_main.html')))
