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

TARGET = ["ucla-aero-ms","ucla-applied-stats-ds-ms","ucla-ba-ms","ucla-ece-ms","ucla-ms-cs",
"ucla-ms-me","ucla-mse-ece","ucla-slavic-ma","ucla-stats-ms"]

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

for tid in TARGET:
    if tid not in blocks:
        print(f"### {tid}: 不在 PROGRAMS"); continue
    r = blocks[tid]
    print(f"### {tid} | {g(r,'n')} | {g(r,'deg')} | {g(r,'url')}")
    print(f"  en={g(r,'en')}")
    print(f"  ddl={g(r,'ddl')} | lang={g(r,'lang')} | tui={g(r,'tui')}")
    tm=re.search(r'test:\{cls:"([^"]*)"\}', r); print(f"  test={tm.group(1) if tm else ''}")
    dp = os.path.join(base, 'details', g(r,'url').split('/')[-1])
    if os.path.exists(dp):
        t = open(dp, encoding='utf-8').read()
        links = re.findall(r'https?://[^"\'<>\s]+', t)
        off = [u for u in links if 'file:' not in u and not u.startswith('details/') and 'fonts.g' not in u and 'fonts.gstatic' not in u and len(u)>12]
        print(f"  官网链接: {off[:3]}")
    else:
        print("  详情页磁盘缺失!")
    print()
