# -*- 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-ds-mse",s="jhu",n="数据科学工程硕士",en="MSE in Data Science",dept="Department of Applied Mathematics and Statistics",college="Whiting School of Engineering",disc="cs",deg="MSE",dur="核心 + 5 附加课 + 4 选修 + 6cr Capstone",stem=0,test="",ddl="待补（catalogue 未列）",lang="待补（catalogue 未列）",tui="打包学费待补；硕士资助名额有限",url="details/jhu-cd967c0.html"),
 dict(id="jhu-finfe-ms",s="jhu",n="金融硕士（金融计量经济学方向）",en="MS in Finance, Financial Econometrics Concentration",dept="Carey Business School",college="Carey Business School",disc="biz",deg="MS",dur="36cr，三学期（12/15 个月）",stem=1,test="",ddl="待补（catalogue 未列）",lang="待补（catalogue 未列）",tui="打包学费待补；硕士资助名额有限",url="details/jhu-cb6c788.html"),
 dict(id="jhu-hcm-ms",s="jhu",n="医疗管理硕士",en="MS in Health Care Management",dept="Carey Business School",college="Carey Business School",disc="biz",deg="MS",dur="30cr，两学期 fall/spring",stem=0,test="",ddl="待补（catalogue 未列）",lang="待补（catalogue 未列）",tui="打包学费待补；硕士资助名额有限",url="details/jhu-c2babcf.html"),
 dict(id="jhu-hfm-mhs",s="jhu",n="健康金融与管理公共卫生硕士",en="MHS in Health Finance and Management",dept="Department of Health Policy and Management",college="Bloomberg School of Public Health",disc="med",deg="MHS",dur="64cr",stem=0,test="",ddl="待补（catalogue 未列）",lang="待补（catalogue 未列）",tui="打包学费待补；硕士资助名额有限",url="details/jhu-cab39af.html"),
 dict(id="jhu-ahsi-ms",s="jhu",n="应用健康科学信息学硕士",en="MS in Applied Health Sciences Informatics",dept="School of Medicine",college="School of Medicine",disc="med",deg="MS",dur="onsite 12 个月全日制 + Capstone",stem=0,test="",ddl="待补（catalogue 未列）",lang="待补（catalogue 未列）",tui="打包学费待补；硕士资助名额有限",url="details/jhu-cfe2f27.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-heor-mhs",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))
