import re, os, json

base = os.path.dirname(os.path.abspath(__file__))
html = open(os.path.join(base, "index_main.html"), encoding="utf-8").read()

ids = [
    "ut-mmpa", "uva-msa", "usc-aeec-ms", "illinois-mspe",
    "usc-seda-ms", "berkeley-macss", "ut-mmi", "ucsd-bioe-ms-med",
]

# PROGRAMS 段
m = re.search(r"const PROGRAMS\s*=\s*\[(.*?)\];", html, re.S)
seg = m.group(1)
blocks = re.findall(r"\{id:\"([^\"]+)\"([\s\S]*?)(?=\{id:\"|$)", seg)
bmap = {bid: body for bid, body in blocks}

# VD
vd = set(re.findall(r'"(details/[^"]+\.html)"', re.search(r"const VERIFIED_DETAILS[^;]+;", html).group(0)))

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

for i in ids:
    body = bmap.get(i)
    if body is None:
        print(i, "!! 未在 PROGRAMS")
        continue
    def f(key):
        mm = re.search(key + r':"([^"]*)"', body)
        return mm.group(1) if mm else None
    lvl = f("lvl"); deg = f("deg"); n = f("n"); en = f("en"); url = f("url")
    disk = os.path.exists(os.path.join(base, url)) if url else False
    invd = url in vd if url else False
    catd_hit = f'"{url}"' in catd
    print(f"{i} | lvl={lvl} deg={deg} | n={n} | en={en}")
    print(f"    url={url} | disk={disk} | vd={invd} | catd={catd_hit}")
