# -*- coding: utf-8 -*-
import io, sys, re, json
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
html = open("index_main.html", encoding="utf-8").read()

# PROGRAMS 段
m = re.search(r"const PROGRAMS=\[(.*?)\];", html, re.S)
prog = m.group(1)
prog_ids = re.findall(r"\{id:\"([^\"]+)\"", prog)
prog_urls = set(re.findall(r"url:\"([^\"]+)\"", prog))

# VD 段
vm = re.search(r"const VERIFIED_DETAILS=new Set\(\[(.*?)\]\);", html, re.S)
vd = set(re.findall(r"\"([^\"]+)\"", vm.group(1)))

# CATD 段
cm = re.search(r"const CATD=\[(.*?)\];", html, re.S)
catd = cm.group(1)

cands = [
    ("cornell-c1f30e8", "Aerospace Engineering MEng", "https://www.duffield.cornell.edu/mae/meng/"),
    ("cornell-c60e314", "Civil & Environmental Engineering MEng", "https://www.duffield.cornell.edu/cee/meng/"),
    ("cornell-cffc740", "Earth Science and Engineering MEng", "https://www.duffield.cornell.edu/eas/meng/"),
    ("cornell-ca5b01c", "Engineering Physics MEng", "https://www.duffield.cornell.edu/aep/meng/"),
    ("cornell-cf3bfaf", "Materials Science & Engineering MEng", "https://www.duffield.cornell.edu/mse/meng/"),
    ("cornell-cdfe6e6", "Systems Engineering MEng", "https://www.duffield.cornell.edu/sys/meng/"),
]

for det, en, url in cands:
    detp = "details/" + det + ".html"
    in_prog = detp in prog_urls
    in_vd = detp in vd
    # CATD 行
    rows = [ln for ln in catd.splitlines() if detp in ln]
    print(f"=== {det} | {en}")
    print(f"  in PROGRAMS: {in_prog} | in VD: {in_vd} | CATD rows: {len(rows)}")
    for r in rows[:2]:
        print("  CATD:", r.strip()[:220])

# 已入库 cornell PROGRAMS
print("\n=== 已入库 cornell PROGRAMS ===")
for pid in prog_ids:
    if pid.startswith("cornell"):
        print(" ", pid)
