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

# PROGRAMS 记录按 {id:" chunk 切分
prog_chunks = re.split(r'\{id:"', html)
prog_en, prog_url, prog_s = set(), set(), {}
for c in prog_chunks[1:]:
    m_en = re.search(r'en:"([^"]*)"', c)
    m_url = re.search(r'url:"([^"]*)"', c)
    m_s = re.search(r's:"([^"]*)"', c)
    if m_en: prog_en.add(m_en.group(1))
    if m_url: prog_url.add(m_url.group(1))
    if m_s and m_url: prog_s[m_url.group(1)] = m_s.group(1)

# VD 集合：只取 VERIFIED_DETAILS 段内
m_vd = re.search(r'const VERIFIED_DETAILS=new Set\(\[(.*?)\]\)', html, re.S)
vd = set(re.findall(r'"(details/[^"]+\.html)"', m_vd.group(1))) if m_vd else set()

todo = json.load(open(os.path.join(BASE, "todo_upgrade.json"), encoding="utf-8"))
items = todo if isinstance(todo, list) else todo.get("items") or todo.get("todo") or []

disc_rank = {"cs": 0, "eng": 1, "biz": 2, "soc": 3, "sci": 4, "med": 5, "art": 6, "edu": 7}
buckets = {k: [] for k in disc_rank}
skipped = 0
for it in items:
    deg = (it.get("deg") or "").strip()
    if "PhD" in deg or "Doctor" in deg or "doctoral" in deg.lower():
        continue  # 批次5
    det = it.get("det") or ""
    en = (it.get("en") or "").strip()
    s = it.get("s")
    if det in vd or det in prog_url:
        skipped += 1
        continue
    disc = it.get("disc") or "sci"
    buckets.setdefault(disc, []).append((s, en, deg, disc, det, it.get("url")))

print("todo", len(items), "| 已登记/已入库跳过:", skipped)
for k in sorted(buckets, key=lambda x: disc_rank.get(x, 9)):
    rows = buckets[k]
    if not rows: continue
    print(f"\n===== disc={k}  未核验硕士候选 {len(rows)} =====")
    for s, en, deg, disc, det, url in rows[:25]:
        print(f"  [{s}] {deg} | {en} | {det}")
