# -*- coding: utf-8 -*-
import io, sys, re, os
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
BASE = r"D:\work buddy\2026-11-05-14-15-52"
fp = os.path.join(BASE, "index_main.html")
html = open(fp, encoding="utf-8").read()

PID = "harvard-data-science"
DET = "details/harvard-data-science.html"

# 1) PROGRAMS 是否已含该 id
if ('{id:"' + PID + '"') in html:
    print("PROGRAMS 已存在", PID, "，跳过追加")
else:
    entry = (
        '{id:"' + PID + '",s:"harvard",n:"数据科学硕士（SM）",'
        'en:"Master of Science (SM) in Data Science",'
        'dept:"工程与应用科学学院（SEAS）· 数据科学",'
        'college:"Harvard John A. Paulson School of Engineering and Applied Sciences (SEAS)",'
        'disc:"cs",lvl:"m",deg:"SM",'
        'dur:"全日制在校；两年结构（第一学年 full year，第二学年仅一学期）；秋季入学；总学分要求项目页未公布",'
        'stem:0,rank:"2027 USNews 第 3",test:{cls:"opt"},'
        'ddl:"2026-12-01 17:00（美东）；仅秋季入学",'
        'lang:"TOEFL iBT 新制总分 5 / 口语 4.5（旧制 2026-01-21 前总分 95 / 口语 23）；IELTS Academic 总分 7.0 / 口语 6.5；不接受 Duolingo 及其他备用考试；本科英语教学院校学位可豁免（仅美国院校学位可请愿豁免，研究生学位不作证明）",'
        'tui:"2026–2027 学费：第一年 $67,504；第二年（仅一学期）$33,752；非全日制按课程费率每学期 1 门 $8,438 / 2 门 $16,876 / 3 门 $25,314 / 4 门 $33,752；不含生活与杂费；项目级硕士资助口径未单列",'
        'url:"' + DET + '"},\n'
    )
    anchor = 'url:"details/notre-msme.html"},\n];'
    if anchor not in html:
        print("ERROR: PROGRAMS 锚点未找到"); sys.exit(1)
    html = html.replace(anchor, 'url:"details/notre-msme.html"},\n' + entry + '];', 1)
    print("PROGRAMS 追加完成", PID)

# 2) VD 登记：基于原始写回前字符串判断（此处 html 若已改，重新在 VD 段判断）
m_vd = re.search(r'const VERIFIED_DETAILS=new Set\(\[(.*?)\]\)', html, re.S)
vd_block = m_vd.group(1)
if DET in vd_block:
    print("VD 已登记", DET, "，跳过")
else:
    vd_anchor = '"details/notre-msme.html",'
    if vd_anchor not in html:
        print("ERROR: VD 锚点未找到"); sys.exit(1)
    html = html.replace(vd_anchor, vd_anchor + '"' + DET + '",', 1)
    print("VD 登记完成", DET)

open(fp, "w", encoding="utf-8").write(html)
print("写回完成，字节数:", len(html.encode("utf-8")))
