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

VIT_LANG="TOEFL iBT ≥90；或 IELTS Academic ≥6.5；接受 PTE / Cambridge C1；USC 不接受多邻国；须由考试机构电子送分"
DOR_LANG="TOEFL 或 IELTS（按 USC Graduate Admission 英语能力政策）；项目级最低总分/小分官网未公布，留空不臆造"
KEK_LANG="国际生按 USC Graduate Admission 与 SOPHAS 要求提交英语成绩；项目级最低总分/小分官网未公布，留空不臆造"
VIT_DDL="2027 秋：2026-12-15（奖学金考虑轮）/ 2027-01-15（最终轮）；2027 春：2026-10-15"
def vit_tui(u): return f"2026-27 工程研究生 $2,742/学分；{u} 学分纯学费约 ${u*2742:,}；杂费、健康保险另计，以官方学费单为准"
entries=[
 dict(id="usc-eds-ms",n="环境数据科学硕士",en="MS in Environmental Data Science",dept="Data Science 方向 × Environmental Studies Program（Viterbi × Dornsife 联合）",college="Viterbi 工程学院",disc="cs",units=32,stem=1,test="notreq",ddl="仅秋季入学：2026-12-15（奖学金考虑轮）/ 2027-01-15（最终轮）；无春季、无夏季",lang=VIT_LANG,tui=vit_tui(32),url="details/usc-c97fb24.html"),
 dict(id="usc-mds-ms",n="数学数据科学硕士",en="MS in Mathematical Data Science",dept="数学系 · Dornsife 文理学院",college="Dornsife 文理学院",disc="cs",units=32,stem=0,test="notreq",ddl="2027 秋：2027-03-30（申请 9/1 开放）；2027 春：2026-10-15（申请 8/1 开放）",lang=DOR_LANG,tui="项目级学费费率/总额官网未单列，以 USC Catalogue 最新公布为准；留空不估算",url="details/usc-ce4fd5a.html"),
 dict(id="usc-em-ms",n="工程管理硕士",en="MS in Engineering Management",dept="Industrial and Systems Engineering 系 · Viterbi 工程学院（MSEMT）",college="Viterbi 工程学院",disc="eng",units=28,stem=1,test="notreq",ddl=VIT_DDL,lang=VIT_LANG,tui=vit_tui(28),url="details/usc-c32a6e0.html"),
 dict(id="usc-pde-ms",n="产品开发工程硕士",en="MS in Product Development Engineering",dept="Aerospace and Mechanical Engineering × Epstein ISE 联合 · Viterbi 工程学院",college="Viterbi 工程学院",disc="eng",units=28,stem=1,test="notreq",ddl=VIT_DDL,lang=VIT_LANG,tui=vit_tui(28),url="details/usc-cc9187d.html"),
 dict(id="usc-bio-ms",n="生物统计学硕士",en="MS in Biostatistics",dept="Department of Population and Public Health Sciences · Keck 医学院",college="Keck 医学院",disc="med",units=39,stem=0,test="notreq",ddl="Fall：Priority 12/1、Final 4/1；Spring：10/1、3/1；Summer：10/1、3/1（按 2027 入学季口径）",lang=KEK_LANG,tui="项目级学费费率/总额官网未单列，以 USC Catalogue / Keck 医学院最新公布为准；留空不估算",url="details/usc-c568b0a.html"),
]

def entry_str(e):
    stem="1" if e["stem"] else "0"
    return ('{id:"'+e["id"]+'",s:"usc",n:"'+e["n"]+'",en:"'+e["en"]+'",dept:"'+e["dept"]+'",college:"'+e["college"]+'",'
            'disc:"'+e["disc"]+'",lvl:"m",deg:"MS",dur:"1.5–2 年（'+str(e["units"])+' 学分）",stem:'+stem+',rank:"",'
            'test:{cls:"'+e["test"]+'"},ddl:"'+e["ddl"]+'",lang:"'+e["lang"]+'",tui:"'+e["tui"]+'",url:"'+e["url"]+'"},')

prog_seg_start=idx.find("const PROGRAMS=")
prog_seg_end=idx.find("const CATD=")
prog_seg=idx[prog_seg_start:prog_seg_end]
anchor='{id:"usc-dsl-ms",s:"usc"'
anchor_pos=idx.find(anchor)
assert anchor_pos>0,"末条锚点未找到"
line_end=idx.find("\n",anchor_pos)
to_add=[e for e in entries if ('{id:"'+e["id"]+'"') not in prog_seg]
if to_add:
    block="\n"+"\n".join(entry_str(e) for e in to_add)
    idx=idx[:line_end]+block+idx[line_end:]
    print("PROGRAMS 追加",len(to_add),"条：",[e["id"] for e in to_add])
else:
    print("PROGRAMS 无新增（均已存在）")

vd_m=re.search(r'(const VERIFIED_DETAILS=new Set\(\[)(.*?)(\]\);)',idx,re.S)
assert vd_m,"VD 段未找到"
vd_body=vd_m.group(2)
vd_list=re.findall(r'"([^"]+)"',vd_body)
new_det=[e["url"] for e in entries if e["url"] not in vd_list]
if new_det:
    add_str=",".join('"'+d+'"' for d in new_det)
    new_vd_body=vd_body.rstrip()
    if not new_vd_body.endswith(","):
        new_vd_body=new_vd_body+","
    new_vd_body=new_vd_body+add_str
    idx=idx[:vd_m.start(2)]+new_vd_body+idx[vd_m.end(2):]
    print("VD 登记",len(new_det),"条：",new_det)
else:
    print("VD 无新增")

open(IDX,"w",encoding="utf-8").write(idx)
print("写回完成，字符",len(idx))
