# -*- 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=")]

# 每条以 {id:" 开头，到下一个 {id:" 或 ]; 为止
records = {}
for m in re.finditer(r'\{id:"([^"]+)"([\s\S]*?)(?=\{id:"|,?\n\];)', seg):
    pid = m.group(1)
    body = m.group(2)
    def f(name):
        mm = re.search(name + r':"([^"]*)"', body)
        return mm.group(1) if mm else ""
    records[pid] = {"s": f("s"), "n": f("n"), "en": f("en"), "deg": f("deg"), "url": f("url")}

snap = {
    "snapshot_at": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
    "count": len(records),
    "programs": records
}
json.dump(snap, open("_daily_snapshot.json", "w", encoding="utf-8"), ensure_ascii=False, indent=2)
print(f"快照已建立: {len(records)} 条, 时间 {snap['snapshot_at']}")
from collections import Counter
c = Counter(r["s"] for r in records.values())
print("按校分布:")
for k,v in sorted(c.items(), key=lambda x:-x[1]):
    print(f"  {k}: {v}")
