# -*- coding: utf-8 -*-
import io, sys, os, re, json
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="uw-mmsc-coop",s="uw",n="管理科学硕士（MMSc · Co-op 带薪实习）",en="Master of Management Science (MMSc) in Management Science (Co-op)",dept="管理科学与工程系",college="University of Waterloo · Faculty of Engineering",disc="biz",deg="MMSc",dur="20 个月 · Co-op · Coursework 无论文",stem=0,test="",ddl="Fall（9–12 月）入学；截止 2 月 1 日",lang="TOEFL 4.5（写 5 / 口 5）；IELTS 7.0（写 6.5 / 口 6.5）",tui="打包学费待补；按学籍身份在 Finance 学费页按学期计费",url="details/uw-cfa123b.html"),
 dict(id="yale-ide-ma",s="yale",n="国际与发展经济学文学硕士",en="Master of Arts in International & Development Economics (IDE)",dept="经济系",college="Yale University · Graduate School of Arts and Sciences",disc="soc",deg="MA",dur="1 年",stem=0,test="req",ddl="2027-01-02 23:59（美东）",lang="TOEFL iBT / IELTS Academic required；英语授课学位且居住满 3 年可豁免",tui="硕士一般不资助；打包学费待补",url="details/yale-international-development-economics.html"),
 dict(id="harvard-cse-me",s="harvard",n="计算科学与工程工程硕士（ME）",en="Master of Engineering (ME) in Computational Science and Engineering",dept="工程与应用科学学院（SEAS）",college="Harvard University · John A. Paulson School of Engineering and Applied Sciences",disc="eng",deg="ME",dur="未列（SEAS 硕士项目）",stem=0,test="opt",ddl="2026-12-01 17:00（美东）",lang="待补（GSAS 通用英语要求，本页未列分数）",tui="打包学费待补",url="details/harvard-computational-science-and-engineering.html"),
 dict(id="uw-meng-ece-coop",s="uw",n="电子与计算机工程工程硕士（MEng · Co-op 带薪实习）",en="Master of Engineering (MEng) in Electrical and Computer Engineering (Co-op)",dept="电子与计算机工程系",college="University of Waterloo · Faculty of Engineering",disc="eng",deg="MEng",dur="20–24 个月 · Co-op · Coursework + 可选研究项目",stem=0,test="",ddl="Fall 2/1；Winter 6/1；Spring 10/1",lang="TOEFL 4.5（写 4.5 / 口 4 / 读 4 / 听 4）；IELTS 6.5（写 6.0 / 口 6.0）",tui="打包学费待补；按学籍身份在 Finance 学费页按学期计费",url="details/uw-c1fa362.html"),
 dict(id="up-bioe-mse",s="up",n="生物工程工程硕士（MSE）",en="Master of Science in Engineering in Bioengineering",dept="生物工程系",college="University of Pennsylvania · School of Engineering and Applied Science",disc="eng",deg="MSE",dur="2 年 · 10 course units · 论文/无论文双轨",stem=0,test="",ddl="待补（catalog 未列截止）",lang="待补（catalog 未列语言最低分）",tui="打包学费待补",url="details/up-c6b3731.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:"up-meds-aeds",s:"up"'
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))
