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

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

new_entries = [
    ("unc-stor-ms", "unc", "统计/分析/数据科学硕士（MS STANDS）", "Master of Science in Statistics, Analytics, and Data Science (STANDS)",
     "Department of Statistics and Operations Research", "University of North Carolina at Chapel Hill", "cs", "m", "MS",
     "3–4 学期；全日制；数据科学/分析职业导向", 1, "", "",
     "catalog 页未列具体轮次（待补）",
     "TOEFL iBT / IELTS Academic（最低分待补）",
     "catalog 页未列打包学费（待补）",
     "details/unc-stor-ms.html"),

    ("unc-biostat-ms", "unc", "生物统计硕士（MS Biostatistics）", "Master of Science in Biostatistics",
     "Department of Biostatistics, Gillings School of Global Public Health", "University of North Carolina at Chapel Hill", "sci", "m", "MS",
     "研究型；健康科学应用", 1, "", "",
     "catalog 页未列具体轮次（待补）",
     "TOEFL iBT / IELTS Academic（最低分待补）",
     "catalog 页未列打包学费（待补）",
     "details/unc-biostat-ms.html"),

    ("unc-bcb-ms", "unc", "生物信息与计算生物学硕士（MS BCB）", "Master of Science in Bioinformatics and Computational Biology",
     "Curriculum in Bioinformatics and Computational Biology (BCB)", "University of North Carolina at Chapel Hill", "sci", "m", "MS",
     "跨学科；含实验室轮转", 1, "", "",
     "catalog 页未列具体轮次（待补）",
     "TOEFL iBT / IELTS Academic（最低分待补）",
     "catalog 页未列打包学费（待补）",
     "details/unc-bcb-ms.html"),

    ("unc-biology-ms", "unc", "生物学硕士（MS Biology）", "Master of Science in Biology",
     "Department of Biology", "University of North Carolina at Chapel Hill", "sci", "m", "MS",
     "研究型含独立研究与论文", 1, "", "",
     "catalog 页未列具体轮次（待补）",
     "TOEFL iBT / IELTS Academic（最低分待补）",
     "catalog 页未列打包学费（待补）",
     "details/unc-biology-ms.html"),

    ("unc-ese-ms", "unc", "环境科学与工程硕士（MS ESE）", "Master of Science in Environmental Sciences and Engineering",
     "Department of Environmental Sciences and Engineering, Gillings School of Global Public Health", "University of North Carolina at Chapel Hill", "eng", "m", "MS",
     "Gillings 公卫学院；物理/健康/工程/政策交叉", 1, "", "",
     "catalog 页未列具体轮次（待补）",
     "TOEFL iBT / IELTS Academic（最低分待补）",
     "catalog 页未列打包学费（待补）",
     "details/unc-ese-ms.html"),
]

def fmt(e):
    eid,s,n,en,dept,college,disc,lvl,deg,dur,stem,rank,tcls,ddl,lang,tui,det = e
    return ('{id:"'+eid+'",s:"'+s+'",n:"'+n+'",en:"'+en+'",dept:"'+dept+'",college:"'+college+'",'
            'disc:"'+disc+'",lvl:"'+lvl+'",deg:"'+deg+'",dur:"'+dur+'",stem:'+str(stem)+',rank:"'+rank+'",'
            'test:{cls:"'+tcls+'"},ddl:"'+ddl+'",lang:"'+lang+'",tui:"'+tui+'",url:"'+det+'"},')

anchor = 'url:"details/unc-mcrp.html"},\n];'
if anchor not in idx:
    print("ANCHOR NOT FOUND"); sys.exit(1)
entries_str = "\n" + "\n".join(fmt(e) for e in new_entries)
new_anchor = anchor.replace("\n];", entries_str + "\n];")
idx = idx.replace(anchor, new_anchor, 1)

# CATD anchor — 上轮末条 unc-mcrp（行末 ],\n];）
catd_anchor = '["unc","Department of City and Regional Planning","Master of City and Regional Planning","城市与区域规划硕士（MCRP）","MCRP","https://catalog.unc.edu/graduate/schools-departments/city-regional-planning/","soc","details/unc-mcrp.html"],\n];'
if catd_anchor not in idx:
    print("CATD ANCHOR NOT FOUND"); sys.exit(1)

url_map = {
    "details/unc-stor-ms.html": "https://catalog.unc.edu/graduate/schools-departments/statistics-operations-research/",
    "details/unc-biostat-ms.html": "https://catalog.unc.edu/graduate/schools-departments/biostatistics/",
    "details/unc-bcb-ms.html": "https://catalog.unc.edu/graduate/schools-departments/bioinformatics-computational-biology/",
    "details/unc-biology-ms.html": "https://catalog.unc.edu/graduate/schools-departments/biology/",
    "details/unc-ese-ms.html": "https://catalog.unc.edu/graduate/schools-departments/environmental-sciences-engineering/",
}
catd_rows = []
for e in new_entries:
    eid,s,n,en,dept,college,disc,lvl,deg,dur,stem,rank,tcls,ddl,lang,tui,det = e
    catd_rows.append('["'+s+'","'+dept+'","'+en+'","'+n+'","'+deg+'","'+url_map[det]+'","'+disc+'","'+det+'"],')
    print("CATD add:", eid)
catd_new = "\n" + "\n".join(catd_rows)
catd_anchor_new = catd_anchor.replace("],\n];", catd_new + "\n];")
idx = idx.replace(catd_anchor, catd_anchor_new, 1)

# VD append
vd_start = idx.find("const VERIFIED_DETAILS=new Set(")
if vd_start < 0:
    print("VD START NOT FOUND"); sys.exit(1)
vd_end = idx.find("]);", vd_start)
if vd_end < 0:
    print("VD END NOT FOUND"); sys.exit(1)
vd_new_items = ", ".join('"'+e[16]+'"' for e in new_entries)
idx = idx[:vd_end] + ", " + vd_new_items + idx[vd_end:]

open("index_main.html","w",encoding="utf-8").write(idx)
print("WRITTEN. new entries:", len(new_entries))
