# -*- coding: utf-8 -*-
import io, sys, json, re, ssl, urllib.request, os
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")

html = open("index_main.html", encoding="utf-8").read()

# VD 段
vd = re.search(r"const VERIFIED_DETAILS=new Set\(\[(.*?)\]\);", html, re.S).group(1)
vd_set = set(re.findall(r'"(details/[^"]+)"', vd))
print("VD 总数:", len(vd_set))

cands = ["cornell-cf3bfaf", "cornell-c60e314", "cornell-cdfe6e6",
         "cornell-ca5b01c", "cornell-cffc740", "cornell-ce9af91"]
for c in cands:
    det = "details/" + c + ".html"
    in_vd = det in vd_set
    in_prog = det in html and ('id:"cornell-'+c+'"' in html)
    # CATD 行
    row = re.search(r'\[[^\]]*"'+c+r'[^]]*\]', html)
    print(f"{c}: VD={in_vd} PROG={'id-in' if in_prog else 'no'} det_exists={os.path.exists(det)}")
    if row:
        print("   CATD:", row.group(0)[:220])

# 官网可达性探针
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
urls = {
    "MSE MEng": "https://www.duffield.cornell.edu/mse/meng/",
    "CEE MEng": "https://www.duffield.cornell.edu/cee/meng/",
    "Systems MEng": "https://www.duffield.cornell.edu/sys/meng/",
    "EngPhys MEng": "https://www.duffield.cornell.edu/aep/meng/",
    "EAS MEng": "https://www.duffield.cornell.edu/eas/meng/",
}
for name, u in urls.items():
    try:
        req = urllib.request.Request(u, headers={"User-Agent": "Mozilla/5.0"})
        b = urllib.request.urlopen(req, context=ctx, timeout=20).read()
        print(f"[{name}] {u} -> {len(b)} bytes")
        if name == "MSE MEng":
            open("_cornell_mse_raw.html", "wb").write(b)
    except Exception as e:
        print(f"[{name}] {u} -> ERR {type(e).__name__}: {e}")
