# -*- 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="mit-scm-res",s="mit",n="物流与供应链管理工程硕士",en="MEng in Logistics and Supply Chain Management (Residential Program)",dept="运输与物流中心 CTL",college="MIT SCALE 全球网络",disc="biz",deg="MEng",dur="待补（官网未列学制）",stem=1,test="req",ddl="Fall 8 月入学；R1 2026-11-01 / R2 2027-01-31 / R3 2027-04-01 23:59 ET；申请费 $90",lang="IELTS 7.0 / TOEFL iBT 100（iBT26 5）；自动豁免另见说明",tui="打包学费待补；资助按 MIT 研究生院口径",url="details/mit-supply-chain-management-residential.html"),
 dict(id="rice-macc",s="rice",n="会计硕士",en="Master of Accounting (MAcc)",dept="Jones 商学院",college="Jones 商学院",disc="biz",deg="MAcc",dur="待补",stem=0,test="",ddl="待补（catalog 未列）",lang="待补（catalog 未列）",tui="打包学费待补",url="details/rice-accounting.html"),
 dict(id="rice-meecon",s="rice",n="能源经济学硕士",en="Master of Energy Economics (MEEcon)",dept="经济系",college="经济系",disc="biz",deg="MEEcon",dur="待补",stem=0,test="",ddl="待补（catalog 未列）",lang="待补（catalog 未列）",tui="打包学费待补",url="details/rice-energy-economics.html"),
 dict(id="rice-mfin",s="rice",n="金融硕士",en="Master of Finance (MFin)",dept="Jones 商学院",college="Jones 商学院",disc="biz",deg="MFin",dur="待补",stem=0,test="",ddl="待补（catalog 未列）",lang="待补（catalog 未列）",tui="打包学费待补",url="details/rice-finance.html"),
 dict(id="wustl-cyber",s="wustl",n="网络安全管理硕士",en="Master of Cybersecurity Management (Sever)",dept="Sever 工程学院",college="Sever 工程学院",disc="cs",deg="MS",dur="30 units",stem=0,test="",ddl="待补（bulletin 未列）",lang="待补（bulletin 未列）",tui="打包学费待补",url="details/wustl-sever-cybersecurity-management-ms.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=idx[idx.find("const PROGRAMS="):idx.find("const CATD=")]
anchor='{id:"mit-mban",s:"mit"'
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))
