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

RICE_LANG="TOEFL 90 / IELTS 7 / Duolingo 115（最低，EPMP 工程硕士通用）；英语授课学位可豁免"
RICE_BG="工程或相关学科学士；GPA 3.0+；全部成绩单、3 推荐信；GRE 代码 6609；holistic 评审"
RICE_TUI="项目级费率未单列，留空不估算；merit 奖学金按项目口径自动评审"
entries=[
 dict(id="cmu-mscb-ms",s="cmu",n="计算生物学硕士",en="M.S. in Computational Biology (MSCB)",dept="计算生物学系(SCS)×生物科学系(MCS)联合",college="Mellon College of Science / School of Computer Science",disc="sci",deg="MS",dur="四学期；Foundation/Background/Breadth & Depth 课程",stem=0,test="opt",ddl="项目页未列截止，走 SCS 申请门户，以门户当年公告为准，待补",lang="Duolingo 115（105/95/115/90）；TOEFL 新版 4.5（R4.5/L5.0/S4.0/W4.5，旧版 86）；IELTS 6.5（6.5/6.5/6.0/6.0）；非母语不豁免",tui="项目级费率本页未列示，待补，留空不估算",url="details/cmu-c274a8e.html"),
 dict(id="rice-mcaam-ms",s="rice",n="计算与应用数学硕士",en="Master of Computational and Applied Mathematics (MCAAM)",dept="Computational Applied Mathematics and Operations Research (CMOR)",college="George R. Brown School of Engineering and Computing",disc="cs",deg="MCAAM",dur="non-thesis 职业型；学分总数待 GA/系站补核",stem=0,test="opt",ddl="Fall 2027 Priority 2027-01-15 / Late 2027-02-28；无春季入学",lang=RICE_LANG,tui=RICE_TUI,url="details/rice-computational-applied-mathematics-operations-research.html"),
 dict(id="rice-mcse-ms",s="rice",n="计算科学与工程硕士",en="Master of Computational Science and Engineering (MCSE)",dept="CMOR × Statistics 合办",college="School of Engineering",disc="cs",deg="MCSE",dur="non-thesis 跨学科；home 系 CMOR/STAT 二选一；学分总数待补",stem=0,test="opt",ddl="Fall 2027 Priority 2027-01-15 / Late 2027-02-28；无春季入学",lang=RICE_LANG,tui=RICE_TUI,url="details/rice-computational-science-engineering.html"),
 dict(id="rice-mie-ms",s="rice",n="工业工程硕士",en="Master of Industrial Engineering (MIE)",dept="CMOR 系监管",college="George R. Brown School of Engineering and Computing",disc="eng",deg="MIE",dur="31 学分 500 级及以上，含 1 学分 final project；non-thesis",stem=0,test="opt",ddl="Fall 2027 2027-02-01；无春季入学",lang=RICE_LANG,tui=RICE_TUI,url="details/rice-industrial-engineering.html"),
 dict(id="rice-mme-ms",s="rice",n="机械工程硕士",en="Master of Mechanical Engineering (MME)",dept="Mechanical Engineering",college="George R. Brown School of Engineering and Computing",disc="eng",deg="MME",dur="non-thesis 职业型；学分总数待 GA/系站补核",stem=0,test="opt",ddl="Fall 2027 2026-12-15 或 2027-01-15；Spring 2026-10-15",lang=RICE_LANG,tui=RICE_TUI,url="details/rice-mechanical-engineering.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_start=idx.find("const PROGRAMS=")
prog_seg_end=idx.find("const CATD=")
prog_seg=idx[prog_seg_start:prog_seg_end]
anchor='{id:"cmu-msm-ms",s:"cmu"'
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))
