# -*- 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="notre-msae",s="notre",n="航空工程理学硕士（MSAE）",en="Master of Science in Aerospace Engineering",dept="航空与机械工程研究生项目（AME）",college="University of Notre Dame",disc="eng",deg="MSAE",dur="研究型硕士 · 在校（1 学期最低住宿）",stem=0,test="opt",ddl="Fall 12/31；Spring 10/15",lang="TOEFL/IELTS/Duolingo（非英语母语；最低分待补）",tui="打包学费待补",url="details/notre-aerospace-engineering-msae---masters-traditional.html"),
 dict(id="notre-cee-meng",s="notre",n="土木与环境工程工程硕士（MEng）",en="Master of Engineering in Civil and Environmental Engineering",dept="土木与环境工程及地球科学系（CEEES）",college="University of Notre Dame",disc="eng",deg="MEng",dur="9 个月在校职业硕士",stem=0,test="notreq",ddl="Fall 3/18；Spring 10/1",lang="TOEFL/IELTS/Duolingo（非英语母语；最低分待补）",tui="打包学费待补",url="details/notre-civil-and-environmental-engineering-meng---masters-professional.html"),
 dict(id="notre-msce",s="notre",n="土木工程理学硕士（MSCE）",en="Master of Science in Civil Engineering",dept="土木与环境工程及地球科学系（CEEES）",college="University of Notre Dame",disc="eng",deg="MSCE",dur="研究型硕士 · 在校（1 学期最低住宿）",stem=0,test="opt",ddl="Fall 1/1；Spring 10/1",lang="TOEFL/IELTS/Duolingo（非英语母语；最低分待补）",tui="打包学费待补",url="details/notre-civil-engineering-msce---masters-traditional.html"),
 dict(id="notre-msenve",s="notre",n="环境工程理学硕士（MSEnvE）",en="Master of Science in Environmental Engineering",dept="土木与环境工程及地球科学系（CEEES）",college="University of Notre Dame",disc="eng",deg="MSEnvE",dur="研究型硕士 · 在校（1 学期最低住宿）",stem=0,test="opt",ddl="Fall 1/1；Spring 10/1",lang="TOEFL/IELTS/Duolingo（非英语母语；最低分待补）",tui="打包学费待补",url="details/notre-civil-engineering-msenve---masters-traditional.html"),
 dict(id="jhu-ehe-ms",s="jhu",n="环境健康工程理学硕士（MS）",en="Master of Science in Environmental Health and Engineering",dept="环境健康工程系",college="Johns Hopkins University",disc="eng",deg="MS",dur="30 学分 · 两学期 · 在校全日制",stem=0,test="",ddl="待补（e-catalogue 未列截止）",lang="待补（e-catalogue 未列语言最低分）",tui="打包学费待补",url="details/jhu-cd46a5d.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:"yale-pmae-ms",s:"yale"'
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))
