# -*- coding: utf-8 -*-
import io, sys, json, re
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")

html = open("index_main.html", encoding="utf-8").read()

# PROGRAMS 段
pm = re.search(r"const PROGRAMS=\[(.*?)\n\];", html, re.S)
pseg = pm.group(1)
progs = []
for chunk in re.split(r"\n(?=\{id:)", pseg):
    mid = re.search(r'\{id:"([^"]+)".*?\bs:"([^"]+)".*?\ben:"([^"]+)".*?\bdet:'
                    , chunk, re.S)
    # det 在 PROGRAMS 是 url 字段
    i = re.search(r'id:"([^"]+)"', chunk)
    s = re.search(r'\bs:"([^"]+)"', chunk)
    en = re.search(r'\ben:"([^"]+)"', chunk)
    url = re.search(r'\burl:"([^"]+)"', chunk)
    if i:
        progs.append({"id": i.group(1), "s": s.group(1) if s else "",
                      "en": en.group(1) if en else "", "url": url.group(1) if url else ""})

print("已入库 PROGRAMS 总数:", len(progs))
cornell_in = [p for p in progs if p["s"] == "cornell"]
print("Cornell 已入库:", len(cornell_in))
for p in cornell_in:
    print("  ", p["id"], "|", p["en"], "|", p["url"])

todo = json.load(open("todo_upgrade.json", encoding="utf-8"))
print("\ntodo 类型:", type(todo), "长度:", len(todo) if hasattr(todo, "__len__") else "?")
if isinstance(todo, dict):
    print("keys:", list(todo.keys())[:10])
    todo = todo.get("items") or todo.get("list") or todo.get("todos") or []

cornell_todo = [t for t in todo if t.get("s") == "cornell"]
print("\nCornell todo:", len(cornell_todo))
for t in cornell_todo:
    print("  ", t.get("det"), "|", t.get("disc"), "|", t.get("deg"), "|", t.get("en"), "|", t.get("col"))
