import re
html = open('index_main.html', encoding='utf-8').read()
m = re.search(r'const PROGRAMS=\[(.*?)\n\];', html, re.S)
prog_text = m.group(1)

target_urls = ['details/ut-ca0f935.html', 'details/ut-c027862.html', 'details/ut-c4f263b.html', 'details/nyu-a3sr-ms.html']

for u in target_urls:
    print(f"\n=== {u} ===")
    # 找所有 url 出现位置
    for match in re.finditer(re.escape(f'url:"{u}"'), prog_text):
        pos = match.start()
        # 往前找最近的 {id:"
        id_start = prog_text.rfind('{id:"', 0, pos)
        if id_start == -1:
            continue
        # 从 id_start 提取 id
        id_m = re.match(r'\{id:"([^"]+)"', prog_text[id_start:])
        pid = id_m.group(1) if id_m else '?'
        # 从 id_start 到下一个 {id:" 或结尾提取块
        next_id = prog_text.find('{id:"', id_start + 1)
        if next_id == -1:
            block = prog_text[id_start:]
        else:
            block = prog_text[id_start:next_id]
        n = re.search(r'n:"([^"]*)"', block)
        en = re.search(r'en:"([^"]*)"', block)
        deg = re.search(r'deg:"([^"]*)"', block)
        s = re.search(r's:"([^"]*)"', block)
        ddl = re.search(r'ddl:"([^"]*)"', block)
        tui = re.search(r'tui:"([^"]*)"', block)
        print(f"  id={pid}  s={s.group(1) if s else '?'}")
        print(f"    n={n.group(1) if n else '?'}")
        print(f"    en={en.group(1) if en else '?'}")
        print(f"    deg={deg.group(1) if deg else '?'}  ddl={ddl.group(1)[:50] if ddl else '?'}  tui={tui.group(1)[:50] if tui else '?'}")
