import re, os, sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
base = os.path.dirname(os.path.abspath(__file__))
r = open(os.path.join(base,"index_main.html"), encoding="utf-8").read()
seg = re.search(r'const PROGRAMS\s*=\s*\[(.*?)\];', r, re.S).group(1)
blocks = re.findall(r'\{id:"([^"]+)"([\s\S]*?)(?=\{id:"|$)', seg)
want = ["cmu-msba-ms","cmu-msin","cmu-msis","cmu-msm-ms","duke-aipi",
        "jhu-finance-ms","jhu-mim","jhu-mkt","jhu-baai",
        "uw-mdsai-coop","uw-meng-ece-coop","uw-compmath-mmath-coop",
        "wustl-sds-stats-ma","wustl-sds-applied-ma","mcg-mma","geo-bioinfo-ms","illinois-mspe-ms"]
byid={bid:body for bid,body in blocks}
for bid in want:
    body=byid.get(bid)
    if not body:
        print(f"[{bid}] 不在库"); continue
    n=re.search(r'n:"([^"]*)"',body); en=re.search(r'en:"([^"]*)"',body)
    deg=re.search(r'deg:"([^"]*)"',body); url=re.search(r'url:"([^"]*)"',body)
    test=re.search(r'cls:"([^"]*)"',body); ddl=re.search(r'ddl:"([^"]*)"',body)
    tui=re.search(r'tui:"([^"]*)"',body)
    print(f"[{bid}] {n.group(1) if n else '?'} | {deg.group(1) if deg else '?'}")
    print(f"   en={en.group(1) if en else '?'} | det={url.group(1) if url else '?'}")
    print(f"   test={test.group(1) if test else '-'} | ddl={ddl.group(1) if ddl else '-'}")
    # 详情页官网链接
    dp=os.path.join(base, (url.group(1) if url else "").replace("/",os.sep))
    links=[]
    if os.path.exists(dp):
        h=open(dp,encoding="utf-8",errors="ignore").read()
        links=[u for u in re.findall(r'https?://[^"\'<>\s]+',h) if not any(x in u for x in ["fonts.googleapis","gstatic","file://"])]
        links=list(dict.fromkeys(links))
    print(f"   官网链接: {links[:3]}")
    print()
