#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Improved extraction: split PROGRAMS block by {id:" markers."""
import json, re, os

ROOT = r"D:\work buddy\2026-11-05-14-15-52"
os.chdir(ROOT)

h = open("index_main.html", encoding="utf-8").read()
m = re.search(r"const PROGRAMS\s*=\s*\[(.*?)\];", h, re.DOTALL)
block = m.group(1)

# Split by {id:"  -- each chunk starts with id:"..."  and ends before next {id:"
chunks = re.split(r'(?=\{id:")', block)
cur = {}
for chunk in chunks:
    chunk = chunk.strip().rstrip(",")
    if not chunk.startswith('{id:"'): continue
    pid_m = re.match(r'\{id:"([^"]+)"', chunk)
    if not pid_m: continue
    pid = pid_m.group(1)
    def grab(key):
        mm = re.search(key + r':"([^"]*)"', chunk)
        return mm.group(1) if mm else ""
    s = grab("s"); n = grab("n"); en = grab("en"); deg = grab("deg"); url = grab("url")
    cur[pid] = {"s": s, "n": n, "en": en, "deg": deg, "url": url}

print(f"Extracted: {len(cur)}")

# Save new snapshot
snap_out = {
    "snapshot_at": "2026-11-25 09:30:00",
    "count": len(cur),
    "programs": cur
}
json.dump(snap_out, open("_daily_snapshot.json","w",encoding="utf-8"), ensure_ascii=False, indent=2)
print(f"[OK] _daily_snapshot.json written, count={len(cur)}")

# Also update diff tmp with this full current
d = json.load(open("_daily_diff_tmp.json",encoding="utf-8"))
old = json.load(open("_daily_snapshot.json",encoding="utf-8"))  # placeholder, we need old
# Recompute against OLD snapshot (668)
# We overwrote _daily_snapshot.json already; use the diff_tmp which still has old programs
# Actually _daily_diff_tmp has "current" from old extraction (785). Let's recompute.
