# -*- 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="up-datsai-mse",s="up",n="数据科学与人工智能工程硕士",en="Master of Science in Engineering in Data Science & Artificial Intelligence (DATS & AI)",dept="工程与应用科学学院 SEAS",college="工程与应用科学学院 SEAS",disc="cs",deg="MSE",dur="1.5–2 年 · 10 course units",stem=0,test="",ddl="待补（catalog 未列）",lang="待补（catalog 未列）",tui="打包学费待补；硕士资助按 SEAS/学院口径",url="details/up-c0e9103.html"),
 dict(id="yale-stat-ma",s="yale",n="统计学文学硕士",en="Master of Arts in Statistics",dept="统计与数据科学系",college="文理研究生院 GSAS",disc="cs",deg="MA",dur="待补（官网未列学分/学制）",stem=0,test="opt",ddl="12 月 1 日 23:59 ET",lang="TOEFL iBT / IELTS Academic（小分门槛待补）",tui="硕士一般不获普遍资助；打包学费待补",url="details/yale-statistics.html"),
 dict(id="cmu-mscs",s="cmu",n="计算机科学硕士",en="Master of Science in Computer Science (MSCS)",dept="计算机科学系 CSD",college="计算机学院 SCS",disc="cs",deg="MSCS",dur="多数 3 学期；转专业可能加一学期",stem=0,test="",ddl="待补（系站未列）",lang="待补（系站未列）",tui="打包学费待补",url="details/cmu-c2ee442.html"),
 dict(id="mit-mfin",s="mit",n="金融硕士",en="MIT Sloan Master of Finance (MFin)",dept="斯隆管理学院 Sloan",college="斯隆管理学院 Sloan",disc="biz",deg="MFin",dur="待补（官网未列学制）",stem=0,test="opt",ddl="2027 年 1 月 5 日；Summer 7 月入学；申请费 $150",lang="不要求英语考试、不接受 TOEFL/IELTS，面试评估",tui="打包学费待补；资助为储蓄/贷款/奖助学金/雇主等组合",url="details/mit-finance.html"),
 dict(id="mit-mban",s="mit",n="商业分析硕士",en="MIT Sloan Master of Business Analytics (MBAn)",dept="斯隆管理学院 Sloan · 关联运筹学中心 ORC",college="斯隆管理学院 Sloan",disc="biz",deg="MBAn",dur="待补（官网未列学制）",stem=0,test="opt",ddl="2027 年 1 月 5 日；Fall 9 月入学；申请费 $150",lang="不要求英语考试、不接受 TOEFL/IELTS，面试评估",tui="打包学费待补；资助为储蓄/贷款/奖助学金/雇主等组合",url="details/mit-business-analytics.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:"nyu-quantecon-ms",s:"nyu"'
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))
