# -*- coding: utf-8 -*-
import io, sys, os, re, json
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="geo-econ-ms",s="geo",n="经济学理学硕士",en="Master of Science in Economics (MS Economics)",dept="文理研究生院 经济系",college="Georgetown University · College of Arts & Sciences",disc="soc",deg="MS",dur="2 年 · 30 course credits",stem=1,test="req",ddl="Fall 入学：Priority 2027-01-15；Final 2027-04-01",lang="TOEFL 100（新制 5）/ IELTS 7.0；英语学位可豁免；小分待补",tui="打包学费待补；奖学金口径以研究生院资助页为准",url="details/geo-economics-ms.html"),
 dict(id="up-biostat-ms",s="up",n="生物统计学硕士",en="Master of Science in Biostatistics",dept="Perelman 医学院 生物统计系",college="University of Pennsylvania · Perelman School of Medicine",disc="med",deg="MS",dur="2 年 · 12 course units · 学位论文",stem=0,test="",ddl="待补（catalog 未列截止）",lang="待补（catalog 未列语言最低分）",tui="打包学费待补",url="details/up-c568b0a.html"),
 dict(id="cmu-meim",s="cmu",n="娱乐产业管理硕士",en="Master of Entertainment Industry Management (MEIM)",dept="Heinz 信息系统与公共政策学院 × 艺术学院",college="Carnegie Mellon University · Heinz College",disc="biz",deg="MS",dur="2 年 · 4 学期 · 双城市（匹兹堡 + 洛杉矶实习）",stem=0,test="",ddl="Round 1 2026-12-01；Round 2 2027-01-10",lang="待补（项目页未列语言最低分）",tui="打包学费待补；官网口径 >95% 在读生获奖学金",url="details/cmu-c13f7d2.html"),
 dict(id="uw-econ-ma-coop",s="uw",n="经济学文学硕士（Co-op 带薪实习）",en="Master of Arts in Economics (Co-op)",dept="文学院 经济系",college="University of Waterloo · Faculty of Arts",disc="soc",deg="MA",dur="20 个月 · Co-op · Coursework 可转 Research Paper",stem=0,test="opt",ddl="Fall（9 月）入学；截止 2 月 1 日",lang="TOEFL 4.5（写 5 / 口 5）；IELTS 7.0（写 6.5 / 口 6.5）",tui="打包学费待补；按学籍身份在 Finance 学费页按学期计费",url="details/uw-c596728.html"),
 dict(id="up-meds-aeds",s="up",n="应用经济与数据科学硕士",en="Master of Applied Economics and Data Science (MEDS)",dept="文理学院 Liberal and Professional Studies",college="University of Pennsylvania · School of Arts & Sciences / LPS",disc="biz",deg="MEDS",dur="12 course units · 两年制 · Capstone",stem=0,test="",ddl="待补（catalog 未列截止）",lang="待补（catalog 未列语言最低分）",tui="打包学费待补",url="details/up-c6cb873.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:"uw-mdsai-coop",s:"uw"'
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 局部登记
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))
