# -*- coding: utf-8 -*-
import io, sys, os, re, json, datetime
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()
seg=idx[idx.find("const PROGRAMS="):idx.find("const CATD=")]
plvl=re.findall(r'lvl:"([^"]+)"',seg)
master=plvl.count("m"); phd=plvl.count("d"); programs=len(plvl)
vd=re.search(r'const VERIFIED_DETAILS=new Set\(\[(.*?)\]\);',idx,re.S).group(1)
vd_n=len(re.findall(r'"([^"]+)"',vd))
det_n=len([f for f in os.listdir("details") if f.endswith(".html")])
mtime=datetime.datetime.fromtimestamp(os.path.getmtime("index_main.html")).strftime("%Y-%m-%d %H:%M:%S")
print("实测 programs",programs,"master",master,"phd",phd,"VD",vd_n,"details",det_n,"mtime",mtime)

b=json.load(open("_heartbeat_baseline.json",encoding="utf-8"))
b["updated_at"]=mtime; b["index_main_mtime"]=mtime
b["schools"]=36; b["programs"]=programs; b["programs_master"]=master; b["programs_phd"]=phd
b["catd"]=2676; b["dead_links"]=0; b["excluded_residue"]=0
b["vd_set"]=vd_n; b["details_html"]=det_n; b["node_check"]="exit 0"

new=[
 ("usc-fe-ms","details/usc-c5d8be1.html","MS Financial Engineering"),
 ("usc-mf-ms","details/usc-c879c9c.html","MS Mathematical Finance"),
 ("usc-or-ms","details/usc-cf80470.html","MS Operations Research Engineering"),
 ("usc-ise-ms","details/usc-c013bd7.html","MS Industrial and Systems Engineering"),
 ("usc-dsl-ms","details/usc-cc87d47.html","MS Data Science and Law"),
]
for _,det,_ in new:
    tag=det+" 浏览器实开待后端会话恢复后补核（六卡静态核对已通过）"
    if det not in b["pending_browser_check"]:
        b["pending_browser_check"].append(tag)

notes=[
 "USC MS Financial Engineering（usc-fe-ms）：已核验入库。Viterbi ISE 系，33 学分、Thesis option 可选不保证、STEM OPT；Spring 10/15、Fall 12/15·1/15；推荐信 Optional，GRE not required；必修 GSBA 548/ISE 563/EE 503/512/518/590 + 金融经济与优化随机方向选修；2025 平均报告薪资 $117,450（自愿调查）；排除 DEN@Viterbi 在线/在职版与 PhD 独立通道。",
 "USC MS Mathematical Finance（usc-mf-ms）：已核验入库。Dornsife 经济学系×数学系联合，31 学分、summative experience（MATH 590 Directed Research，有实习可换 MATH 596）；官网未标注 STEM，stem:0；Fall 3/30、Spring 10/15，无夏季入学；申请费 $85；3 封官方推荐信 + 一页 CV + PS + GRE highly recommended（GMAT 无送分代码，考了仍须交）+ TOEFL/IELTS；先修概率论/随机过程/实分析/线代/微分方程；录取平均 GPA 3.5+（录取分布，非最低门槛）；本项目不提供财务资助；项目级学费费率/总额与平均薪资官网未公布留空；排除 Progressive Degree 本校本科贯通通道与 PhD。",
 "USC MS Operations Research Engineering（usc-or-ms）：已核验入库。Viterbi ISE 系，28 学分、Thesis option 可选不保证、STEM OPT；Spring 10/15、Fall 12/15·1/15；推荐信 3 highly recommended，GRE not required；必修 ISE 536/538/580/583/632 + 限选 1 门 + 导师批准选修；2025 平均报告薪资 $95,000（自愿调查）；排除 DEN 在线版与 PhD。",
 "USC MS Industrial and Systems Engineering（usc-ise-ms）：已核验入库。Viterbi ISE 系，28 学分、Thesis option 可选不保证、STEM OPT；Spring 10/15、Fall 12/15·1/15；推荐信 3 highly recommended，GRE not required；必修 ISE 513/514/515 + Group A 定量 / Group B 质量运营 / Group C 工程管理各 1 门；2025 平均报告薪资 $98,656（自愿调查，官网原文以点作千分位）；排除 DEN 在线版与 PhD。",
 "USC MS Data Science and Law（usc-dsl-ms）：已核验入库。Viterbi Data Science 方向，32 学分、Thesis option 不可用、STEM OPT；Spring 10/15、Fall 12/15·1/15；推荐信 2 highly recommended，GRE not required；方向法律流程/数据隐私/司法可及性，官网项目页未单列必修课程代码；平均薪资与雇主样本官网未披露留空；本项目不为 USC JD 提供学分或录取优势，课程单位不可计入 JD；排除 JD 双学位与 PhD 独立通道。",
]
for n in notes:
    if n[:40] not in " ".join(b["blocked_notes"]):
        b["blocked_notes"].append(n)

b["note"]=b["note"]+" 2026-11-18 心跳批量入库 5 条 USC：MS Financial Engineering（details/usc-c5d8be1.html）、MS Mathematical Finance（details/usc-c879c9c.html，Dornsife 口径 stem:0）、MS Operations Research Engineering（details/usc-cf80470.html）、MS Industrial and Systems Engineering（details/usc-c013bd7.html）、MS Data Science and Law（details/usc-cc87d47.html）。重探：Stanford/USC/Rice 系站仍 200；JHU Eng 403、NYU GSAS URLError 仍不可取正文；UT Stat/Math 仍小字节 challenge 页待浏览器确认。"
json.dump(b,open("_heartbeat_baseline.json","w",encoding="utf-8"),ensure_ascii=False,indent=2)
print("基线已刷新：programs",b["programs"],"master",master,"phd",phd,"VD",vd_n,"details",det_n,"pending",len(b["pending_browser_check"]),"blocked",len(b["blocked_notes"]))
