# -*- 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="uw-meng-mme-coop",s="uw",n="机械与机电工程工程硕士（Co-op）（MEng）",en="Master of Engineering in Mechanical and Mechatronics Engineering (Co-op)",dept="工程学院",college="University of Waterloo",disc="eng",deg="MEng",dur="20–24 个月全日制 · Co-op 授课型 · 带薪实习",stem=0,test="",ddl="2/1（9 月）· 6/1（次年 1 月）· 10/1（次年 5 月）",lang="TOEFL 4.5（写4.5/口4/读4/听4）/ IELTS 6.5（写6.0/口6.0）",tui="打包学费待补",url="details/uw-c435976.html"),
 dict(id="uw-meng-cheme-coop",s="uw",n="化学工程工程硕士（Co-op）（MEng）",en="Master of Engineering in Chemical Engineering (Co-op)",dept="工程学院",college="University of Waterloo",disc="eng",deg="MEng",dur="20–24 个月全日制 · Co-op 授课型",stem=0,test="",ddl="2/1（9 月）· 6/1（次年 1 月）· 10/1（次年 5 月）",lang="TOEFL 4.5（写4.5/口4/读4/听4）/ IELTS 6.5（写6.0/口6.0）",tui="打包学费待补",url="details/uw-cb1d654.html"),
 dict(id="uw-compmath-mmath-coop",s="uw",n="计算数学数学硕士（Co-op）（MMath）",en="Master of Mathematics in Computational Mathematics (Co-op)",dept="数学学院",college="University of Waterloo",disc="sci",deg="MMath",dur="20 个月全日制 · Co-op 研究型 · Research Paper",stem=0,test="",ddl="1/15（仅 Fall 入学）；coursework 通道当前停招",lang="TOEFL 4.5（写5/口5）/ IELTS 7.0（写6.5/口6.5）",tui="打包学费待补",url="details/uw-cb1705c.html"),
 dict(id="dartmouth-engsci-ms",s="dartmouth",n="工程科学理学硕士（MS）",en="Master of Science in Engineering Sciences",dept="Thayer School of Engineering",college="Dartmouth College",disc="eng",deg="MS",dur="六门研究生课 + 论文 · 研究型",stem=0,test="",ddl="待补（项目页未列截止）",lang="待补（项目页未列语言最低分）",tui="打包学费待补",url="details/dartmouth-engineering-sciences-ms.html"),
 dict(id="yale-pmae-ms",s="yale",n="个性化医疗与应用工程理学硕士（MS）",en="Master of Science in Personalized Medicine & Applied Engineering",dept="研究生院（GSAS）",college="Yale University",disc="eng",deg="MS",dur="交叉硕士 · 学分/学制待补",stem=0,test="opt",ddl="12 月 15 日 23:59 ET",lang="TOEFL iBT / IELTS Academic（英语授课学位可豁免）",tui="打包学费待补",url="details/yale-personalized-medicine-applied-engineering.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-amcs-ma",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))
