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

new_entries = [
    dict(id="ut-mfi", s="ut", n="金融保险硕士（MFI）", en="Master of Financial Insurance", dept="Department of Statistical Sciences", college="Faculty of Arts and Science", disc="biz", deg="MFI", dur="授课型专业硕士", stem=0, test="notreq", ddl="待补（链接至系站）", lang="待补（IELTS/TOEFL 最低分）", tui="打包学费待补", url="details/ut-cdc0ba0.html"),
    dict(id="ut-csb-msc", s="ut", n="细胞与系统生物学理学硕士（MSc）", en="Master of Science in Cell and Systems Biology", dept="Department of Cell and Systems Biology", college="Faculty of Arts and Science", disc="sci", deg="MSc", dur="研究型硕士（含本科直博通道）", stem=0, test="notreq", ddl="待补（链接至系站）", lang="待补（英语水平成绩，最低分待补）", tui="打包学费待补", url="details/ut-c39f20e.html"),
    dict(id="ut-eeb-msc", s="ut", n="生态学与进化生物学理学硕士（MSc）", en="Master of Science in Ecology and Evolutionary Biology", dept="Department of Ecology and Evolutionary Biology", college="Faculty of Arts and Science", disc="sci", deg="MSc", dur="研究型硕士（须自行联系导师）", stem=0, test="notreq", ddl="Fall 2026：MSc 2026-01-09；PhD 国际生 2025-12-08", lang="待补（TOEFL/IELTS 最低分）", tui="打包学费待补", url="details/ut-cfc1e3f.html"),
    dict(id="ut-mbp-msc", s="ut", n="医学生物物理学理学硕士（MSc）", en="Master of Science in Medical Biophysics", dept="Graduate Department of Medical Biophysics", college="Faculty of Medicine", disc="med", deg="MSc", dur="研究型硕士（PhD 导师录取后分配）", stem=0, test="notreq", ddl="Fall 2026：第一轮 2025-11-07；第二轮 2026-02-06", lang="待补（ELP 最低分）", tui="打包学费待补", url="details/ut-c6a92f9.html"),
    dict(id="ut-mengcem", s="ut", n="城市工程与管理工程硕士（MEngCEM）", en="Master of Engineering in Cities Engineering and Management", dept="Faculty of Applied Science and Engineering", college="University of Toronto", disc="eng", deg="MEng", dur="授课型工程硕士", stem=0, test="notreq", ddl="待补（链接至系站）", lang="待补（IELTS/TOEFL 最低分）", tui="打包学费待补", url="details/ut-c5c2ac2.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:"ut-hpme-msc",s:"ut"'
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_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))
