# -*- coding: utf-8 -*-
import io, sys, os, re
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
os.chdir(r"D:\work buddy\2026-11-05-14-15-52")
IDX="index_main.html"
idx=open(IDX,encoding="utf-8").read()

new_entries=[
 dict(id="ut-mf",s="ut",n="金融学硕士（MF）",en="Master of Finance",dept="Rotman School of Management",college="University of Toronto",disc="biz",deg="MF",dur="15个月全日制 · 9月入学",stem=0,test="rec",ddl="待补（Rotman 当期招生公告）",lang="待补（IELTS/TOEFL 最低分）",tui="打包学费待补",url="details/ut-c0e5352.html"),
 dict(id="ut-mfrm",s="ut",n="金融风险管理硕士（MFRM）",en="Master of Financial Risk Management",dept="Rotman School of Management",college="University of Toronto",disc="biz",deg="MFRM",dur="10个月全日制 · 9月入学",stem=0,test="rec",ddl="待补",lang="待补（IELTS/TOEFL 最低分）",tui="打包学费待补",url="details/ut-c5760ab.html"),
 dict(id="ut-mma",s="ut",n="管理分析硕士（MMA）",en="Master of Management Analytics",dept="Rotman School of Management",college="University of Toronto",disc="biz",deg="MMA",dur="11个月课程+4个月practicum · 8月入学",stem=0,test="rec",ddl="待补",lang="待补（IELTS/TOEFL 最低分）",tui="打包学费待补",url="details/ut-c90d689.html"),
 dict(id="ut-econ-ma",s="ut",n="经济学文学硕士（MA）",en="Master of Arts in Economics",dept="Department of Economics",college="University of Toronto",disc="soc",deg="MA",dur="研究型硕士",stem=0,test="",ddl="待补",lang="待补（IELTS/TOEFL 最低分）",tui="打包学费待补",url="details/ut-c529fb8.html"),
 dict(id="ut-bme-masc",s="ut",n="生物医学工程应用科学硕士（MASc）",en="Master of Applied Science in Biomedical Engineering",dept="Department of Biomedical Engineering",college="University of Toronto",disc="eng",deg="MASc",dur="研究型硕士",stem=0,test="",ddl="待补",lang="待补（IELTS/TOEFL 最低分）",tui="打包学费待补",url="details/ut-c4ca221.html"),
]

prog_seg=idx[idx.find("const PROGRAMS="):idx.find("const CATD=")]
def entry_str(e):
    stem="1" if e["stem"] else "0"
    return ('{id:"'+e["id"]+'",s:"'+e["s"]+'",n:"'+e["n"]+'",en:"'+e["en"]+'",dept:"'+e["dept"]+'",college:"'+e["college"]+'",'
     'disc:"'+e["disc"]+'",lvl:"m",deg:"'+e["deg"]+'",dur:"'+e["dur"]+'",stem:'+stem+',rank:"",'
     'test:{cls:"'+e["test"]+'"},ddl:"'+e["ddl"]+'",lang:"'+e["lang"]+'",tui:"'+e["tui"]+'",url:"'+e["url"]+'"},')

anchor='{id:"cornell-apphys-ms",s:"cornell"'
anchor_pos=idx.find(anchor)
assert anchor_pos>0,"末条锚点未找到"
line_end=idx.find("\n",anchor_pos)
to_add=[e for e in new_entries if ('{id:"'+e["id"]+'"') not in prog_seg]
if to_add:
    block="\n"+"\n".join(entry_str(e) for e in to_add)
    idx=idx[:line_end]+block+idx[line_end:]
    print("PROGRAMS 追加",len(to_add),"条：",[e["id"] for e in to_add])
else:
    print("PROGRAMS 无新增")

vd_m=re.search(r'(const VERIFIED_DETAILS=new Set\(\[)(.*?)(\]\);)',idx,re.S)
assert vd_m,"VD 段未找到"
vd_body=vd_m.group(2)
vd_list=re.findall(r'"([^"]+)"',vd_body)
all_det=[e["url"] for e in new_entries]
new_det=[d for d in all_det if d not in vd_list]
if new_det:
    add_str=",".join('"'+d+'"' for d in new_det)
    new_vd_body=vd_body.rstrip()
    if not new_vd_body.endswith(","):
        new_vd_body=new_vd_body+","
    new_vd_body=new_vd_body+add_str
    idx=idx[:vd_m.start(2)]+new_vd_body+idx[vd_m.end(2):]
    print("VD 登记",len(new_det),"条：",new_det)
else:
    print("VD 无新增")

open(IDX,"w",encoding="utf-8").write(idx)
print("写回完成，字符",len(idx))
