# -*- 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="jhu-bid-mse",s="jhu",n="生物工程创新与设计工程硕士",en="MSE in Bioengineering Innovation and Design",dept="Department of Biomedical Engineering",college="Whiting School of Engineering",disc="eng",deg="MSE",dur="46–48cr，12 个月（summer→fall/spring）",stem=0,test="",ddl="待补（catalogue 未列）",lang="待补（catalogue 未列）",tui="打包学费待补；硕士资助名额有限",url="details/jhu-c064173.html"),
 dict(id="jhu-bmb-scm",s="jhu",n="生物化学与分子生物学科学硕士",en="ScM in Biochemistry and Molecular Biology",dept="Department of Biochemistry and Molecular Biology",college="Bloomberg School of Public Health",disc="sci",deg="ScM",dur="Year 1 课程 + spring-summer 研究论文（约 11–12 个月）",stem=0,test="",ddl="待补（catalogue 未列）",lang="待补（catalogue 未列）",tui="打包学费待补；硕士资助名额有限",url="details/jhu-ca597d7.html"),
 dict(id="jhu-biochem-ms",s="jhu",n="生物物理化学与生物技术设计硕士",en="MS in Biophysical Chemistry and Design for Biotechnology",dept="Biophysics",college="Krieger School of Arts and Sciences",disc="sci",deg="MS",dur="10–24 个月（full-time 在校通道）",stem=0,test="",ddl="待补（catalogue 未列）",lang="待补（catalogue 未列）",tui="打包学费待补；硕士资助名额有限",url="details/jhu-c03ef7d.html"),
 dict(id="jhu-econ-ma",s="jhu",n="经济学文学硕士",en="MA in Economics",dept="Department of Economics",college="Krieger School of Arts and Sciences",disc="soc",deg="MA",dur="46cr（25cr core + 21cr electives），两年",stem=1,test="",ddl="待补（catalogue 未列）",lang="待补（catalogue 未列）",tui="打包学费待补；硕士资助名额有限",url="details/jhu-c529fb8.html"),
 dict(id="jhu-gile-ms",s="jhu",n="工程全球创新与领导力硕士",en="MS in Global Innovation and Leadership Through Engineering",dept="Center for Leadership Education",college="Whiting School of Engineering",disc="eng",deg="MS",dur="技术 track 6 门 18cr + 管理 track 10 门",stem=0,test="",ddl="待补（catalogue 未列）",lang="待补（catalogue 未列）",tui="打包学费待补；硕士资助名额有限",url="details/jhu-ca25d43.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:"jhu-ahsi-ms",s:"jhu"'
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))
