# -*- coding: utf-8 -*-
import re, io, sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
h = open('index_main.html', encoding='utf-8').read()
p0 = h.index("const PROGRAMS=["); p1 = h.index("const CATD=[")
seg = h[p0:p1]

TARGETS = {
    "nyu-econ": "tui",
    "usc-stat": "tui",
}
for pid, field in TARGETS.items():
    m = re.search(r'(\{id:"'+pid+r'"[\s\S]*?)(?=\{id:"|,?\n\];)', seg)
    if not m:
        print("[MISS]", pid); continue
    block = m.group(1)
    new_block, n = re.subn(field+r':"[^"]*"', field+r':""', block, count=1)
    seg = seg[:m.start()] + new_block + seg[m.end():]
    print(f"[OK] {pid} {field} cleared (sub={n})")

h2 = h[:p0] + seg + h[p1:]
# 缺逗号检查
assert not re.search(r'\}\{', h2), "缺逗号 }{"
open('index_main.html','w',encoding='utf-8').write(h2)

# 复查
for pid in TARGETS:
    mm = re.search(r'\{id:"'+pid+r'"[\s\S]*?tui:"([^"]*)"', h2)
    print("  check", pid, "tui =>", repr(mm.group(1)) if mm else "NF")
