# -*- coding: utf-8 -*-
import io, sys, os, re, json
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()

LANG_BLANK="国际生按 USC Graduate Admission 英语能力政策提交 TOEFL/IELTS；项目级最低总分/小分官网未公布，留空不臆造"
TUI_BLANK="项目级学费费率/总额官网未单列，以 USC Catalogue 与对应学院最新公布为准；留空不估算"
entries=[
 dict(id="usc-gscm-ms",n="全球供应链管理硕士",en="MS in Global Supply Chain Management",dept="Marshall 商学院 × Viterbi 工程学院 联合",college="Marshall 商学院",disc="biz",dur="16 个月（四学期在校）",stem=1,test="",ddl="项目页未公布申请截止日期，以 USC Marshall 招生页最新公布为准",lang=LANG_BLANK,tui=TUI_BLANK,url="details/usc-ca8f13a.html"),
 dict(id="usc-macc-ms",n="会计硕士",en="Master of Accounting",dept="Leventhal 会计学院",college="Marshall 商学院",disc="biz",dur="30–45 学分（视入学起点）",stem=1,test="",ddl="项目页未公布申请截止日期，以 Leventhal Admissions 最新公布为准",lang=LANG_BLANK,tui=TUI_BLANK,url="details/usc-cec582e.html"),
 dict(id="usc-comds-ms",n="传播数据科学硕士",en="MS in Communication Data Science",dept="Annenberg 传播与新闻学院 × Viterbi 工程学院 联合",college="Annenberg 传播与新闻学院",disc="soc",dur="学分/学制官网未公布",stem=1,test="",ddl="2026-12-15（Annenberg 招生表口径）",lang=LANG_BLANK,tui=TUI_BLANK,url="details/usc-c4d3c5a.html"),
 dict(id="usc-mecds-ms",n="经济学与数据科学硕士",en="MS in Economics and Data Science",dept="Dornsife 经济学系 × Viterbi 数据科学 联合",college="Dornsife 文理学院",disc="soc",dur="32 学分（8 门 4 学分课程）",stem=1,test="req",ddl="Fall 2027：Early Decision 2027-03-01；Final 2027-04-15",lang=LANG_BLANK,tui=TUI_BLANK,url="details/usc-ca6df7b.html"),
 dict(id="usc-am-ms",n="应用数学硕士",en="MS in Applied Mathematics",dept="数学系",college="Dornsife 文理学院",disc="sci",dur="学分官网未公布",stem=0,test="notreq",ddl="Fall 2/1（9/1 开放）；Spring 10/15（8/1 开放）",lang="TOEFL 100 / IELTS 7.0 / PTE 68（数学系 PhD 与 Masters 通用最低线）",tui=TUI_BLANK,url="details/usc-ca09c65.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:"'+e["dur"]+'",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-bio-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))
