import json, re

with open('index_main.html','r',encoding='utf-8') as f:
    html = f.read()

# 提取 PROGRAMS 数组中的 id 和 url
prog_block = re.search(r'const PROGRAMS\s*=\s*\[(.*?)\n\];', html, re.S)
if not prog_block:
    prog_block = re.search(r'PROGRAMS\s*=\s*\[(.*?)\n\];', html, re.S)

prog_ids = set()
prog_urls = set()
if prog_block:
    block = prog_block.group(1)
    prog_ids = set(re.findall(r'\{id:"([^"]+)"', block))
    prog_urls = set(re.findall(r'url:"(details/[^"]+)"', block))

print(f'PROGRAMS 已有 id 数: {len(prog_ids)}')
print(f'PROGRAMS url 数: {len(prog_urls)}')

with open('todo_upgrade.json','r',encoding='utf-8') as f:
    todo = json.load(f)
print(f'todo_upgrade 总数: {len(todo)}')

# 筛选未核验的 todo 项（det 不在 PROGRAMS url 里）
pending = []
for item in todo:
    det = item.get('det','')
    if det and det not in prog_urls:
        pending.append(item)

print(f'未核验 todo 项数: {len(pending)}')

# 按学科优先级排序
priority = {
    'cs': 1, 'ee': 2, 'fin': 3, 'econ': 4, 'ba': 5, 'ds': 6,
    'stat': 7, 'mech': 8, 'bio': 9, 'mgmt': 10, 'biz': 11,
    'soc': 12, 'art': 13, 'hum': 14
}

def disc_key(item):
    d = item.get('disc','other')
    return priority.get(d, 99)

pending.sort(key=disc_key)

print()
print('=== 按优先级前20条待核验 ===')
for i, item in enumerate(pending[:20]):
    print(f"{i+1}. [{item['disc']}] {item['s']} - {item['en']} ({item['deg']}) det={item['det']}")
    print(f"   url: {item.get('url','')}")
