# -*- coding: utf-8 -*-
import io, sys, os, re, ast
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
os.chdir(r"D:\work buddy\2026-11-05-14-15-52")

p = "index_main.html"
idx = open(p, encoding="utf-8").read()

# 防重复：若 id 已存在则跳过
new_ids = ["ut-maccfin","ut-march","ut-imm-msc","ut-pharm-msc2","ut-mirhr"]
exist = [i for i in new_ids if ('id:"'+i+'"') in idx]
if exist:
    print("ALREADY EXISTS, abort:", exist); sys.exit(0)

# 1. PROGRAMS 追加：在末条 ut-mi 后、]; 前插入
anchor = 'url:"details/ut-c142af9.html"},\n];'
assert anchor in idx, "PROGRAMS anchor not found"

entries = [
 '{id:"ut-maccfin",s:"ut",n:"会计与金融硕士（MAccFin）",en:"Master of Accounting and Finance",dept:"UTSC 管理与会计专业",college:"University of Toronto",disc:"biz",lvl:"m",deg:"MAccFin",dur:"授课型专业硕士",stem:0,rank:"",test:{cls:"notreq"},ddl:"待补（链接至系站）",lang:"待补（IELTS/TOEFL 最低分）",tui:"打包学费待补",url:"details/ut-c6f566f.html"},',
 '{id:"ut-march",s:"ut",n:"建筑学硕士（MArch）",en:"Master of Architecture",dept:"John H. Daniels Faculty of Architecture, Landscape, and Design",college:"University of Toronto",disc:"art",lvl:"m",deg:"MArch",dur:"授课型专业建筑硕士（NAAB 认证）",stem:0,rank:"",test:{cls:"notreq"},ddl:"待补（链接至系站）",lang:"待补（IELTS/TOEFL 最低分）",tui:"打包学费待补",url:"details/ut-c0ea97e.html"},',
 '{id:"ut-imm-msc",s:"ut",n:"免疫学硕士（MSc）",en:"Master of Science in Immunology",dept:"Department of Immunology",college:"University of Toronto",disc:"med",lvl:"m",deg:"MSc",dur:"研究型硕士（thesis）",stem:0,rank:"",test:{cls:"notreq"},ddl:"待补（链接至系站）",lang:"待补（IELTS/TOEFL 最低分）",tui:"打包学费待补",url:"details/ut-cf0519a.html"},',
 '{id:"ut-pharm-msc2",s:"ut",n:"药理学硕士（MSc）",en:"Master of Science in Pharmacology",dept:"Department of Pharmacology and Toxicology",college:"University of Toronto",disc:"med",lvl:"m",deg:"MSc",dur:"研究型硕士（须自行联系导师）",stem:0,rank:"",test:{cls:"notreq"},ddl:"待补（链接至系站）",lang:"待补（IELTS/TOEFL 最低分）",tui:"打包学费待补",url:"details/ut-pharm-msc2.html"},',
 '{id:"ut-mirhr",s:"ut",n:"劳资关系与人力资源硕士（MIRHR）",en:"Master of Industrial Relations and Human Resources",dept:"Centre for Industrial Relations and Human Resources",college:"University of Toronto",disc:"biz",lvl:"m",deg:"MIRHR",dur:"授课型专业硕士",stem:0,rank:"",test:{cls:"notreq"},ddl:"待补（链接至系站）",lang:"待补（IELTS/TOEFL 最低分）",tui:"打包学费待补",url:"details/ut-c3a91ad.html"},',
]
block = "\n".join(entries) + "\n"
idx = idx.replace(anchor, 'url:"details/ut-c142af9.html"},\n' + block + "];", 1)

# 2. CATD 补 1 条（pharmacology）：在 CATD 段末 ]; 前插入
# 找 CATD 段结束
catd_start = idx.find("const CATD=")
vd_start = idx.find("const VERIFIED_DETAILS=")
catd_seg = idx[catd_start:vd_start]
catd_end = catd_seg.rfind("];")
assert catd_end > 0, "CATD end not found"
catd_entry = '["ut", "医学院", "Pharmacology", "药理学", "MSc", "https://www.sgs.utoronto.ca/programs/pharmacology/", "med", "details/ut-pharm-msc2.html"],\n'
idx = idx[:catd_start+catd_end] + catd_entry + idx[catd_start+catd_end:]

# 3. VD 登记：在 VERIFIED_DETAILS 段内局部 replace，追加 5 个 det
vd_start2 = idx.find("const VERIFIED_DETAILS=")
vd_end = idx.find("]);", vd_start2)
assert vd_end > 0, "VD end not found"
vd_seg = idx[vd_start2:vd_end]
new_dets = ['"details/ut-c6f566f.html"','"details/ut-c0ea97e.html"','"details/ut-cf0519a.html"','"details/ut-pharm-msc2.html"','"details/ut-c3a91ad.html"']
# 去重追加
existing = set(re.findall(r'"(details/[^"]+\.html)"', vd_seg))
add = [d for d in new_dets if d not in existing]
if add:
    # 在 VD 数组末尾（最后一个 "..." 后）追加
    vd_new = vd_seg.rstrip().rstrip("]")
    if vd_new.endswith(","):
        vd_new = vd_new + " " + ", ".join(add) + "]"
    else:
        vd_new = vd_new + ", " + ", ".join(add) + "]"
    idx = idx[:vd_start2] + vd_new + idx[vd_end:]

open(p,"w",encoding="utf-8").write(idx)
print("APPENDED 5 PROGRAMS + 1 CATD +", len(add), "VD")
