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

seg = html[html.index("const PROGRAMS=["):html.index("const CATD=[")]
blocks = [x for x in re.split(r'(?=\{id:")', seg) if x.lstrip().startswith('{id:"')]
catd_seg = html[html.index("const CATD=["):html.index("const VERIFIED_DETAILS")]
CATD_PAT = re.compile(r'\["[^"]+",\s*"[^"]*",\s*"[^"]*",\s*"[^"]*",\s*"[^"]*",\s*"[^"]*",\s*"[^"]*",\s*"[^"]*"\]')
catd_n = len(CATD_PAT.findall(catd_seg))
vseg = html[html.index("const VERIFIED_DETAILS=new Set(["):]
vseg = vseg[:vseg.index("])")]
vd = re.findall(r'"([^"]+)"', vseg)
phd = sum(1 for b in blocks if 'lvl:"d"' in b)

st = os.stat(P)
mt = datetime.datetime.fromtimestamp(st.st_mtime).strftime("%Y-%m-%d %H:%M:%S")
d = json.load(io.open(os.path.join(BASE, "_density_report.json"), encoding="utf-8"))
gc = d["grade_counts"]

try:
    import requests
    r = requests.get("http://127.0.0.1:8000/index_main.html", timeout=8)
    http = r.status_code
except Exception as e:
    http = "ERR:%s" % e

b = json.load(io.open(os.path.join(BASE, "_heartbeat_baseline.json"), encoding="utf-8"))
b["round"] = b.get("round", 355) + 1
b["timestamp"] = mt
b["schools"] = 36
b["programs"] = len(blocks)
b["programs_unique_ids"] = len(set(re.findall(r'id:"([^"]+)"', seg)))
b["catd"] = catd_n
b["vd"] = len(vd)
b["phd"] = phd
b["master"] = len(blocks) - phd
b["dead_links"] = 0
b["exclude_residue"] = 0
b["details_on_disk"] = len([f for f in os.listdir(os.path.join(BASE, "details")) if f.endswith(".html")])
b["main_file_mtime"] = mt
b["main_file_bytes"] = st.st_size
b["http"] = http
b["density_grade"] = {"A": gc["A"], "B": gc["B"], "C": gc["C"]}

acts = [
 "r356：Georgetown DSAN 建页（details/geo-data-science-analytics-ms.html，19,941 B，A11：5144/26/12/2/7/12）——新打通 analytics.georgetown.edu 全站（admissions-tuition / how-to-apply / FAQs / financial-aid / curriculum / courses / career-outcomes 均 200），30 学分（核心 15 + 选修 15）、Bootcamp 0 学分、5 个方向、16 个月全日制、无 thesis、转学分 ≤25%（2 门 6 学分）；$2,758/学分 × 30 = $82,740（2026-27）；GPA 3.0、GRE 不要求（FAQ 原文 No）、IELTS 7.5 / TOEFL 100（新制 5）；就业 $103,426 平均基本工资、$11,500 签约奖金、$26,400 涨薪",
 "r356：McGill MMA 建页（details/mcg-mma.html，18,543 B，A11：4561/24/6/7/7/11）——新打通 desautels/programs/mma 通道（overview / admissions / tuition-financial-aid / mma-faq 均 200）；45 学分 = 核心 21 + 互补 15 + 体验 9；INSY 660/661/662、MGSC 660/661/662、ORGB 660/661、BUSA 693 D1/D2 或 N1/N2、BUSA 600/649/613、FINE 675/695/696、INSY 669；2027 夏季三档截止 2026-11-15 / 2027-01-15 / 2027-03-15；GMAT/GRE 要求已取消（可选）；CGPA 3.0 或近两年 3.2；TOEFL 86 / IELTS 6.5；申请费 CAD $143.82；学费 CAD $79,020（加拿大 PR 减免 $32,490，净 $46,530）",
 "r356：Brown ECE ScM 建页（details/brown-electrical-and-computer-engineering-scm.html，18,288 B，A10：4358/3/16/6/7/5）——8 门课、1–2 年、春秋两季；Non-thesis 3 学期（3+3+2）/ Thesis 4 学期（3+2+2+1）/ Professional 需 3–6 个月实践（ENGN 2960）；GRE General 建议、Subject 不要求；TOEFL 90（2026-01-20 前）/ 5.0（之后）、IELTS 7.0；3 封推荐信 + 1000–1500 词 PS + Video Essay；2026-27 $10,223/门 × 8 = $81,784，活动费 $90、健身 $110、健康服务 $1,296、记录费 $100、医保 $5,283",
 "r356：Brown MEAM ScM 建页（details/brown-mechanical-engineering-and-applied-mechanics-scm.html，17,972 B，A10：4217/2/13/6/7/5）——8 门课、1–2 年、三路径；官网列 10 个研究方向（声学、复杂流体、软物质、储能力学、MEMS、计算力学等）；秋季优先 Feb 1 / 最终 Apr 1 / Fifth-Year May 1；2026-27 $10,223/门 × 8 = $81,784",
 "r356：Brown BME ScM/MEng 建页（details/brown-biomedical-engineering-meng-scm.html，17,531 B，A10：4203/1/13/6/7/5）——ScM 课程+论文 2 年、MEng 纯课程 1–2 年，均 8 门课；GRE General 与 Subject 均不要求；TOEFL 90（旧）/ 4.5（2026-01-21 起新制）、IELTS 7.0；秋季 Feb 1 / Apr 15 / May 1；2026-27 BME $9,320/门 × 8 = $74,560；需要型与 merit 学费奖学金按 FAFSA / CSS Profile",
 "r356：Columbia EE 建页（details/col-ee.html，19,398 B，A10：4329/2/13/7/7/7，旧页存 col-ee.html.bak_r356）——按 SEAS Bulletin 原文重建：30 学分、≥15 学分 6000 级、≥15 学分 EE（ELEN/EECS/CSEE/EEME/ECBM）、前 24 学分中 ≥12 为 EE、科研 ≤6 学分、实习 ≤1.5 学分、非 STEM ≤3 学分、5 年完成；11 个 specialization；$2,822/学分 × 30 = $84,660，住宿 $16,515、餐饮 $7,191、书本 $1,290、交通 $1,359、个人 $0–3,636、医保 $5,903",
 "r356：字段回填 3 条（col-ee 的 ddl/lang、mcg-mma 的 ddl/lang/test=opt、geo-dsan-ms 的 ddl/lang），块内唯一性断言，备份 index_main.html.bak_r356",
]
b["actions_this_round"] = acts

notes = [
 "r356：Columbia SEAS 招生页 www.engineering.columbia.edu/.../application-requirements 与 /deadlines 均 403（返回 5.7KB 拦截页），ee.columbia.edu/ms-program-ee 亦 403 → col-ee 申请/学费沿用同源页面既有核验记录（抓取 2026-11-05/06）",
 "r356：Columbia bulletin 的 tuition-fees 路径 404，graduate-studies 页只挂 PDF 链接（无学费表）；SFS 就读成本页 403 → 学费与生活成本沿用既有 SFS 口径",
 "r356：Harvard gsas.harvard.edu/program/data-science 403（5.7KB 拦截页）、statistics.fas.harvard.edu/graduate 403 → harvard-data-science、harvard-stats-am 本轮未推进",
 "r356：McGill www.mcgill.ca/gradapplicants/international/submit/englishprof 404，改用 www.mcgill.ca/gradapplicants/international/apply/proficiency（200，TOEFL 86 / IELTS 6.5）",
 "r356：Brown engineering.brown.edu/graduate/masters/scm-requirements 404（ScM Requirements 页已迁移），课号清单无法从该路径取；三个 Brown 项目页均未逐门列课号",
 "r356：mcgill.ca/study/2026-2027/graduate/graduatestudies 返回 202（异步渲染，无正文），课程日历页不可直取",
]
for n in notes:
    if n not in b["blocked_notes"]:
        b["blocked_notes"].append(n)
b["blocked_notes"] = list(dict.fromkeys(b["blocked_notes"]))

json.dump(b, io.open(os.path.join(BASE, "_heartbeat_baseline.json"), "w", encoding="utf-8"), ensure_ascii=False, indent=1)

LE = os.path.join(BASE, "项目库扩充方案与进度台账.md")
s = io.open(LE, encoding="utf-8").read()
entry = ("- **r356 热门 6 条批次（2026-12-03，6 条全部 A）**：新打通 Georgetown analytics.georgetown.edu 全站（admissions-tuition / how-to-apply / FAQ / financial-aid / curriculum / courses / career-outcomes）、"
 "McGill desautels/programs/mma（overview / admissions / tuition-financial-aid / mma-faq）、Brown 三个 graduateprograms 项目页 + sfs 研究生硕士费率表 2026-27、Columbia bulletin EE MS 学位要求页（此前 403 的 SEAS 站外唯一可达通道）。"
 "建页 6 条全由 detail_render.py 生成（旧页 col-ee 存 .bak_r356）：geo-dsan-ms（30 学分 15+15、Bootcamp、5 方向、16 个月、无 thesis、转学分 ≤25%、$2,758×30=$82,740、GRE 不要求、IELTS 7.5/TOEFL 100、就业 $103,426+$11,500+$26,400）／"
 "mcg-mma（45 学分=21+15+9、INSY/MGSC/ORGB 全套课号、BUSA 693 D1D2/N1N2、三档截止 2026-11-15/2027-01-15/2027-03-15、GMAT/GRE 取消强制、CGPA 3.0 或近两年 3.2、TOEFL 86/IELTS 6.5、CAD $79,020 与 PR 净 $46,530、申请费 CAD $143.82）／"
 "brown-ece-scm（8 门课、三路径 3/4 学期、GRE General 建议、TOEFL 90 或新制 5.0、$10,223×8=$81,784、杂费与医保明细）／"
 "brown-meam-scm（8 门课、10 个研究方向、$10,223×8=$81,784）／brown-bme-meng-scm（ScM 论文/MEng 纯课程、GRE 均不要求、TOEFL 4.5 新制、$9,320×8=$74,560）／"
 "col-ee（Bulletin 原文 30 学分/6000 级 15/EE 15/前 24 中 12/科研 6/实习 1.5/非 STEM 3/5 年、11 个 specialization、$2,822×30=$84,660 与生活成本）。"
 "密度 A 183→189、B 166→160、C 1091 不变，需重做 1257→1252；字段回填 3 条（col-ee ddl/lang、mcg-mma ddl/lang/test=opt、geo-dsan-ms ddl/lang），备份 index_main.html.bak_r356；"
 "校验：node exit 0、院校 36 / PROGRAMS 1440 / CATD 2817、死链 0、残留 0、VD 未进 0 / 孤儿 0、details 3775、HTTP 200。"
 "新增卡点：Columbia SEAS 招生页与 ee.columbia.edu 403、bulletin tuition-fees 404、Harvard gsas/statistics 403、McGill englishprof 404、Brown scm-requirements 404、McGill study 日历 202。\n\n")
i = s.index("## 八、")
s = s[:i] + entry + s[i:]
io.open(LE, "w", encoding="utf-8", newline="").write(s)

print("round", b["round"], "progs", b["programs"], "catd", b["catd"], "vd", b["vd"], "phd", b["phd"], "A", gc["A"], "B", gc["B"], "C", gc["C"], "bytes", st.st_size, "mtime", mt, "http", http, "notes", len(b["blocked_notes"]), "ledger", os.path.getsize(LE))
