import re, os, sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
base = os.path.dirname(os.path.abspath(__file__))
fp = os.path.join(base,'index_main.html')
html = open(fp, encoding='utf-8').read()
assert 'const PROGRAMS=[' in html

REPL = {
 "duke-mqm-ba": "2026-27 学费 $87,800（10 个月三期；含 $4,000 录取押金计入暑期学费）；健康费 $1,235 + MQM 协会费 $500；官方总 COA $124,321（含食宿书本生活预算）",
}
before=len(re.findall(r'\{id:"',html))
changed=[]
for tid,newtui in REPL.items():
    pat=re.compile(r'(\{id:"'+re.escape(tid)+r'"[\s\S]*?)tui:"([^"]*)"')
    mm=pat.search(html)
    if not mm: print(f"!! {tid} 未定位"); continue
    old=mm.group(2)
    if old!="待补": print(f"!! {tid} tui 非待补: {old}"); continue
    html=html[:mm.start()]+mm.group(1)+f'tui:"{newtui}"'+html[mm.end():]
    changed.append(tid)
after=len(re.findall(r'\{id:"',html))
assert before==after, f"id 数变化 {before}->{after}"
assert len(re.findall(r'\}\{',html))==0, "缺逗号"
open(fp,'w',encoding='utf-8').write(html)
print("已写盘：", changed, "bytes:", os.path.getsize(fp))
