# -*- 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 = [
    {
        "id": "gatech-mid", "s": "gatech", "n": "工业设计硕士（MID）",
        "en": "Master of Industrial Design",
        "dept": "College of Design, Industrial Design", "college": "Georgia Tech",
        "disc": "art", "lvl": "m", "deg": "MID",
        "dur": "学制约 90 学分；Studio 为主；含 Thesis",
        "stem": 0, "rank": "", "test_cls": "",
        "ddl": "catalog 页未列具体轮次（待补）",
        "lang": "TOEFL iBT / IELTS Academic（最低分待补）",
        "tui": "catalog 页未列打包学费（待补）",
        "url": "details/gatech-mid.html",
    },
    {
        "id": "gatech-gmc-ms", "s": "gatech", "n": "全球媒体与文化硕士（MS GMC）",
        "en": "Master of Science in Global Media and Cultures",
        "dept": "School of Literature, Media, and Communication", "college": "Georgia Tech",
        "disc": "art", "lvl": "m", "deg": "MS",
        "dur": "学制约 30 学分；核心 9cr + 选修",
        "stem": 0, "rank": "", "test_cls": "",
        "ddl": "catalog 页未列具体轮次（待补）",
        "lang": "TOEFL iBT / IELTS Academic（最低分待补）",
        "tui": "catalog 页未列打包学费（待补）",
        "url": "details/gatech-gmc-ms.html",
    },
    {
        "id": "gatech-hts-ms", "s": "gatech", "n": "科技史与科技社会学硕士（MS HTS）",
        "en": "Master of Science in History and Sociology of Technology and Science",
        "dept": "School of History and Sociology", "college": "Georgia Tech",
        "disc": "soc", "lvl": "m", "deg": "MS",
        "dur": "学制约 30 学分；研究型；含定量/定性方法",
        "stem": 0, "rank": "", "test_cls": "",
        "ddl": "catalog 页未列具体轮次（待补）",
        "lang": "TOEFL iBT / IELTS Academic（最低分待补）",
        "tui": "catalog 页未列打包学费（待补）",
        "url": "details/gatech-hts-ms.html",
    },
]

# 去重检查（只看 PROGRAMS 段）
prog_seg = idx[idx.find("const PROGRAMS="):idx.find("const CATD=")]
for e in new_entries:
    if 'id:"%s"' % e["id"] in prog_seg:
        print("SKIP dup id:", e["id"]); sys.exit(1)
    if 'url:"%s"' % e["url"] in prog_seg:
        print("SKIP dup url:", e["id"]); sys.exit(1)

# 构造新条目
entries_str = ""
for e in new_entries:
    entries_str += '\n{id:"%s",s:"%s",n:"%s",en:"%s",dept:"%s",college:"%s",disc:"%s",lvl:"%s",deg:"%s",dur:"%s",stem:%d,rank:"%s",test:{cls:"%s"},ddl:"%s",lang:"%s",tui:"%s",url:"%s"},' % (
        e["id"], e["s"], e["n"], e["en"], e["dept"], e["college"],
        e["disc"], e["lvl"], e["deg"], e["dur"], e["stem"], e["rank"], e["test_cls"],
        e["ddl"], e["lang"], e["tui"], e["url"])

# 找末条锚点（已知末条 gatech-ms-architecture，其后是 \n];）
anchor = 'url:"details/gatech-ms-architecture.html"},\n];'
if anchor not in idx:
    print("ANCHOR NOT FOUND"); sys.exit(1)
new_anchor = anchor.replace("\n];", entries_str + "\n];")
idx2 = idx.replace(anchor, new_anchor, 1)

# VD 追加
vd_start = idx2.find("const VERIFIED_DETAILS=new Set([")
vd_end = idx2.find("]);", vd_start)
new_dets = ', "' + '", "'.join(e["url"] for e in new_entries) + '"'
idx2 = idx2[:vd_end] + new_dets + idx2[vd_end:]

# CATD 补录
catd_seg = idx2[idx2.find("const CATD="):idx2.find("const VERIFIED_DETAILS=")]
catd_add = ""
for e in new_entries:
    if e["url"] not in catd_seg:
        catd_add += '\n["%s","%s","%s","%s","%s","%s","%s","%s"],' % (
            e["s"], e["dept"], e["en"], e["n"], e["deg"],
            "https://catalog.gatech.edu/programs/", e["disc"], e["url"])
        print("CATD add:", e["id"])

if catd_add:
    catd_end = idx2.find("];", idx2.find("const CATD="))
    idx2 = idx2[:catd_end] + catd_add + idx2[catd_end:]

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