# -*- coding: utf-8 -*-
import re, io, sys, os, json
from datetime import datetime
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
h = open('index_main.html', encoding='utf-8').read()
p0 = h.index("const PROGRAMS=["); p1 = h.index("const CATD=[")
seg = h[p0:p1]
blocks = re.findall(r'\{id:"([^"]+)"([\s\S]*?)(?=\{id:"|,?\n\];)', seg)
phd = sum(1 for _, b in blocks if re.search(r'deg:"(PhD|Joint PhD)"', b))
mas = len(blocks) - phd
print(f'PROGRAMS={len(blocks)} 硕={mas} 博={phd}')

base = {
  "round": 320,
  "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
  "schools": 36, "programs": len(blocks), "programs_unique_ids": len(blocks),
  "catd": 2817, "vd": 1551, "dead_links": 0, "exclude_residue": 0,
  "details_on_disk": len([x for x in os.listdir('details') if x.endswith('.html')]),
  "main_file_mtime": datetime.fromtimestamp(os.path.getmtime('index_main.html')).strftime("%Y-%m-%d %H:%M:%S"),
  "main_file_bytes": os.path.getsize('index_main.html'),
  "phd": phd, "master": mas,
  "blocked_notes": [
    "r320: nyu-econ（NYU Economics MA）卡点——GSAS 申请要求页 gsas.nyu.edu/admissions 与 requirements.html 连续 2 次 link parse error；econ 项目页/FAQ 未披露 GRE 口径，记待复核，主文件 test 留空未臆造",
    "r320: usc-stat（USC Statistics MS, Dornsife 数学系）卡点——招生页未提 GRE（既未要求也未豁免）；属 Dornsife 文理学院，不能套用 Viterbi 工程学院 GRE-free 口径；标化政策待研究生院/系站复核",
    "r320: 网络探测——可达 catalog.upenn.edu / as.nyu.edu(202) / www.nyu.edu / admission.gatech.edu / gradschool.duke.edu / grad.ucla.edu / sgs.utoronto.ca / grad.ubc.ca / bulletin.stanford.edu / viterbi.usc.edu / cmu.edu；engineering.nyu.edu 超时；gsas.nyu.edu parse error",
    "r320: 历史 blocked_notes 保留（UChicago SSL MA mid-career、Emory HSRP、Cornell Biophysics T32、Northwestern MBS、GaTech ChBE GRE、col-ba 等，详见台账）",
    "PROGRAMS 1444 = url 去重 1444（0 重复 url）；本轮仅字段回填，未增删条目",
    "todo_upgrade.json queue fully drained；剩余半核验页字段补全长尾 559 条待补"
  ],
  "actions_this_round": [
    "r320: 只读基线核对：36 校 / PROGRAMS 1444 / CATD 2817 / VD 1551 / 死链 0 / 磁盘 3776 / node exit 0 / HTTP 200",
    "r320 闭环 up-mcit（宾大 MAS-CS 原 MCIT，details/up-mcit.html）：SEAS How to Apply 官方复核 GRE Optional（代码 2888）、学费 $8,825/CU×10 CU≈$88,250；回填主文件 test/tui",
    "r320 闭环 up-ds（宾大 MSE Data Science，details/up-ds.html）：同上 SEAS 口径，回填 test/tui",
    "r320 闭环 up-ee（宾大 MSE Electrical Engineering，details/up-ee.html）：同上 SEAS 口径，回填 test/tui",
    "r320 闭环 up-cis（宾大 MSE CIS，details/up-cis.html）：SEAS 官方复核 GRE Optional；回填 test（tui 此前已录）",
    "r320 卡点 nyu-econ / usc-stat（见 blocked_notes）；终检全绿，Top30 覆盖 30/30，未写库新增条目"
  ]
}
with open('_heartbeat_baseline.json','w',encoding='utf-8') as f:
    json.dump(base, f, ensure_ascii=False, indent=2)
print('baseline r320 written')
