# -*- 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="illinois-msim-ms",s="illinois",n="信息管理硕士",en="Master of Science in Information Management (MSIM)",dept="School of Information Sciences (iSchool)",college="School of Information Sciences",disc="biz",deg="MS",dur="40 学分；在校 18-24 个月",stem=1,test="notreq",ddl="Early Action 2026-11-17；Regular Decision 2027-01-29",lang="TOEFL 79/103；IELTS 6.5/7.5；Duolingo 115/135；机构码 1836；学位豁免",tui="self-supporting 无州支持；费率待补",url="details/illinois-cd99a06.html"),
 dict(id="umich-mhi-ms",s="umich",n="健康信息学硕士",en="Master of Health Informatics (MHI)",dept="School of Information (UMSI)",college="School of Information",disc="med",deg="MHI",dur="学制/学分待补",stem=0,test="notreq",ddl="Priority 2027-01-15；Final 2027-05-01",lang="TOEFL/IELTS 2 年内；分数线待补；学位豁免条件见页",tui="学费/资助待补",url="details/umich-c08bd16.html"),
 dict(id="dartmouth-meng-ms",s="dartmouth",n="工程硕士（在校版）",en="Master of Engineering (MEng, On-Campus)",dept="Thayer School of Engineering",college="Thayer School of Engineering",disc="eng",deg="MEng",dur="全日制最短 9 个月；no thesis",stem=0,test="",ddl="Fall R1 2026-12-15；R2 2027-02-15；Winter/Spring rolling",lang="TOEFL iBT 100+；IELTS 7.0；Duolingo 135；学位豁免",tui="学费/费用待补",url="details/dartmouth-engineering-meng.html"),
 dict(id="dartmouth-mem-ms",s="dartmouth",n="工程管理硕士",en="Master of Engineering Management (MEM)",dept="Thayer School of Engineering",college="Thayer School of Engineering",disc="eng",deg="MEM",dur="含夏季实习 ENGG 390；月数待补",stem=0,test="",ddl="R1 2027-01-15；R2 2027-03-01；R3 2027-03-15",lang="TOEFL iBT 100+；IELTS 7.0；Duolingo 135；学位豁免",tui="学费/资助待补；professional 奖学金有限",url="details/dartmouth-engineering-management.html"),
 dict(id="illinois-mspe-ms",s="illinois",n="政策经济学硕士",en="Master of Science in Policy Economics (MSPE)",dept="Department of Economics",college="College of Liberal Arts & Sciences",disc="soc",deg="MS",dur="1-2 年；40 学分",stem=1,test="",ddl="系站不可达，截止待补",lang="系站不可达，语言门槛待补",tui="self-supporting 无学费减免；费率待补",url="details/illinois-c0c492e.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:"dartmouth-hds-ms",s:"dartmouth"'
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))
