# -*- 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()

entries=[
 dict(id="vandy-msece",s="vanderbilt",n="电气与计算机工程硕士",en="MS in Electrical and Computer Engineering",dept="Department of Electrical and Computer Engineering",college="School of Engineering",disc="eng",deg="MS",dur="30 学分；1–2 年",stem=0,test="notreq",ddl="Fall 2027 MS 截止 2027-03-15；Spring 2027 截止 2026-10-15",lang="TOEFL 89 / IELTS 7.0 / DET 130；符合豁免可申请豁免",tui="MS 自费；专属打包学费未单列，待补",url="details/vanderbilt-engineering-departments-electrical-computer-engineering.html"),
 dict(id="vandy-msme",s="vanderbilt",n="机械工程硕士",en="MS in Mechanical Engineering",dept="Department of Mechanical Engineering",college="School of Engineering",disc="eng",deg="MS",dur="30 didactic 学分（或 24+6 research）",stem=0,test="notreq",ddl="MS priority 2027-01-08、final 2027-03-15",lang="TOEFL 89 / IELTS 7.0 / DET 130；符合豁免可申请豁免",tui="MS 自费；专属打包学费未单列，待补",url="details/vanderbilt-engineering-departments-mechanical-engineering.html"),
 dict(id="vandy-mscbe",s="vanderbilt",n="化学与生物分子工程硕士",en="MS in Chemical and Biomolecular Engineering",dept="Department of Chemical and Biomolecular Engineering",college="School of Engineering",disc="eng",deg="MS",dur="24 学期课时研究生课 + thesis",stem=0,test="notreq",ddl="MS priority 2027-01-08、final 2027-03-15；MS 无申请费",lang="TOEFL 89 / IELTS 7.0 / DET 130；符合豁免可申请豁免",tui="MS 自费；专属打包学费未单列，待补",url="details/vanderbilt-engineering-departments-chemical-biomolecular-engineering.html"),
 dict(id="vandy-mengcps",s="vanderbilt",n="信息物理系统工程硕士（MEng）",en="MEng in Cyber-Physical Systems",dept="School of Engineering M.Eng. Program",college="School of Engineering",disc="eng",deg="MEng",dur="30 学分",stem=0,test="notreq",ddl="Fall 6/15、Spring 11/15；滚动评审",lang="TOEFL 如适用；项目级最低分待补（总站 89/7.0/130）",tui="MEng；专属打包学费未单列，待补",url="details/vanderbilt-engineering-academics-master-of-engineering-degree-cps.html"),
 dict(id="vandy-mengesi",s="vanderbilt",n="外科与介入工程硕士（MEng）",en="MEng in Surgery and Intervention",dept="School of Engineering M.Eng. Program",college="School of Engineering",disc="eng",deg="MEng",dur="30 学分；1–2 年（三 track）",stem=0,test="notreq",ddl="rolling 自 8/1；Spring 11/15、Fall 5/15",lang="TOEFL/IELTS/DET（国际生）；项目级最低分待补",tui="MEng；专属打包学费未单列，待补",url="details/vanderbilt-engineering-academics-master-of-engineering-degree-esi.html"),
]

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"]+'"},')

prog_seg_start=idx.find("const PROGRAMS=")
prog_seg_end=idx.find("const CATD=")
prog_seg=idx[prog_seg_start:prog_seg_end]
anchor='{id:"vandy-msbme",s:"vanderbilt"'
anchor_pos=idx.find(anchor)
assert anchor_pos>0,"末条锚点未找到"
line_end=idx.find("\n",anchor_pos)
to_add=[e for e in 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)
new_det=[e["url"] for e in entries if e["url"] 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))
