# -*- coding: utf-8 -*-
import io,sys,re,os
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding="utf-8")
p="index_main.html"
html=open(p,encoding="utf-8").read()
orig=html

DET="details/cornell-cc8aeab.html"
PID="cornell-dspp-ms"

# 1) PROGRAMS 幂等判断（基于原始 html）
if ('{id:"'+PID+'"') in html:
    print("PROGRAMS 已存在", PID, "跳过追加")
else:
    entry = (
        '{id:"cornell-dspp-ms",s:"cornell",n:"公共政策数据科学硕士（MS-DSP · 一年制在校）",'
        'en:"Master of Science in Data Science for Public Policy",'
        'dept:"布鲁克斯公共政策学院 Brooks School of Public Policy",'
        'college:"Cornell University · Ithaca 主校区",disc:"cs",lvl:"m",deg:"MS",'
        'dur:"12 个月 · 41 学分 · Summer 6 + Fall 16 + Winter 3 + Spring 16；6 月入学；Brooks Engaged Learning Capstone",'
        'stem:1,'
        'rank:"",'
        'test:{cls:"notreq"},'
        'ddl:"Summer 入学两轮：Round 1 10 月 15 日；Round 2 1 月 15 日（按 2027 夏入学对应 2026-10-15 / 2027-01-15，以申请门户当期为准）",'
        'lang:"首语言非英语须交 TOEFL 或 IELTS，或走 Cornell Graduate Standing/Special Exemption 豁免；项目页未公布最低分数门槛，以 Graduate School 当期标准为准",'
        'tui:"2026–27 12 个月 Tier 2 学费合计 $62,588（Summer $9,332 + Fall/Winter $26,628 + Spring $26,628）；Activity Fee $110；Health Fee $580；Health Insurance $3,685（可 waiver）；择优 merit scholarship 录取评审一并考虑、无需另申；2027–28 费率未公布",'
        'url:"details/cornell-cc8aeab.html"},\n'
    )
    anchor='url:"details/duke-cbd0bbe.html"},\n];'
    assert anchor in html, "PROGRAMS 锚点未找到"
    html=html.replace(anchor, 'url:"details/duke-cbd0bbe.html"},\n'+entry+'];', 1)
    print("PROGRAMS 追加", PID)

# 2) VD 幂等判断（限定 VERIFIED_DETAILS 段，基于原始 html）
m=re.search(r'const VERIFIED_DETAILS=new Set\(\[(.*?)\]\);', orig, re.S)
assert m, "VD 段未找到"
vd_set=m.group(1)
if ('"'+DET+'"') in vd_set:
    print("VD 已登记", DET, "跳过")
else:
    vd_anchor='"details/duke-cbd0bbe.html",'
    assert vd_anchor in html, "VD 锚点未找到"
    html=html.replace(vd_anchor, vd_anchor+'"'+DET+'",', 1)
    print("VD 登记", DET)

if html!=orig:
    open(p,"w",encoding="utf-8").write(html)
    print("写回完成，字节", len(orig.encode("utf-8")), "->", len(html.encode("utf-8")))
else:
    print("无变更")
