# -*- 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()

# 1) 升级既有 usc-msba 占位条目（核验化字段，url 保持占位 det）
usca_entry=('{id:"usc-msba",s:"usc",n:"商业分析硕士",en:"M.S. in Business Analytics (MSBA)",dept:"Marshall 商学院",college:"Marshall 商学院",'
 'disc:"biz",lvl:"m",deg:"MS",dur:"2 年全日制在校",stem:1,rank:"",'
 'test:{cls:""},ddl:"待补（项目官网本页未列申请截止轮次）",lang:"待补（项目官网本页未列国际生英语最低分）",tui:"打包学费待补（不按单位费率估算，以 Marshall 学费页当期公告为准）",url:"details/usc-msba.html"},')
usca_pat=re.compile(r'\{id:"usc-msba"[\s\S]*?url:"details/usc-msba\.html"\},')
if usca_pat.search(idx):
    idx=usca_pat.sub(usca_entry,idx)
    print("USC MSBA 占位条目已核验化升级")
else:
    print("!! usc-msba 占位未匹配，需人工核对")

# 2) 新增 4 条
new_entries=[
 dict(id="cornell-aem-ms",s="cornell",n="应用经济与管理理学硕士",en="Master of Science in Applied Economics & Management (M.S. AEM)",dept="Dyson 应用经济与管理学院",college="Dyson 应用经济与管理学院",disc="biz",deg="MS",dur="2 年 · 63 credit hours",stem=0,test="req",ddl="待补（项目官网本页未列截止轮次）",lang="国际生须提交 TOEFL 或 IELTS；最低分待补",tui="打包学费待补",url="details/cornell-c4ed07c.html"),
 dict(id="cornell-mps-mgmt",s="cornell",n="管理专业研究硕士",en="Master of Professional Studies in Management (MPS–M)",dept="SC Johnson 商学院 Johnson Graduate School",college="SC Johnson 商学院",disc="biz",deg="MPS",dur="10 个月 · 30 credit hours",stem=1,test="req",ddl="待补（项目官网本页未列截止）",lang="TOEFL / IELTS Academic / DET 三选一或满足豁免；最低分待补",tui="打包学费待补",url="details/cornell-c3b1b9a.html"),
 dict(id="cornell-mmh",s="cornell",n="酒店管理管理硕士",en="Master of Management in Hospitality (MMH)",dept="Nolan 酒店学院（SHA）",college="Nolan 酒店学院",disc="biz",deg="MMH",dur="3 学期 · 8 月入学",stem=0,test="",ddl="国际生建议前三轮申请；具体轮次日期待补",lang="待补（本页未列语言最低分）",tui="官网 Est. Total Tuition $110,919（总学费估算；杂费/资助另见学费页）",url="details/cornell-c7c652f.html"),
 dict(id="uw-mdsai-coop",s="uw",n="数据科学与人工智能硕士（Co-op 带薪实习）",en="Master of Data Science and Artificial Intelligence (MDSAI, Co-op)",dept="数学学院 数据科学系",college="University of Waterloo · Faculty of Mathematics",disc="cs",deg="MDSAI",dur="16 个月 · Coursework 无论文 · Co-op",stem=0,test="",ddl="Fall（9 月）入学；申请截止 1 月 15 日",lang="TOEFL iBT 写作 5 / 口语 5；IELTS Academic 7.5（写作 7.0 / 口语 7.0），如适用",tui="打包学费待补；按学籍身份在 Finance 学费页按学期计费",url="details/uw-c2f6425.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:"wustl-cyber",s:"wustl"'
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 无新增")

# 3) 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=["details/usc-msba.html"]+[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))
