import json
import re

# 读取 todo_upgrade.json
with open('todo_upgrade.json','r',encoding='utf-8') as f:
    todo = json.load(f)

# 读取 index_main.html 中的 PROGRAMS url 集合
with open('index_main.html','r',encoding='utf-8') as f:
    html = f.read()

prog_urls = set(re.findall(r'url:\s*"(details/[^"]+)"', html))

# 学科优先级
disc_priority = ['cs','ee','fin','econ','ba','ds','stat','mech','bio','mgmt']

# 筛选未核验硕士项目：排除带 PhD 的
candidates = []
for item in todo:
    det = item.get('det','')
    if not det:
        continue
    if det in prog_urls:
        continue
    en = item.get('en','')
    # 排除 PhD 项目
    if 'PhD' in en or 'Ph.D' in en or 'phd' in en.lower():
        continue
    # 排除博士层级
    lvl = item.get('lvl','m')
    if lvl == 'phd':
        continue
    disc = item.get('disc','other')
    candidates.append({
        'school': item.get('s',''),
        'name_cn': item.get('n',''),
        'name_en': en,
        'det': det,
        'disc': disc,
        'deg': item.get('deg',''),
        'dept': item.get('dept',''),
    })

print(f'未核验纯硕士候选数: {len(candidates)}')

def disc_rank(c):
    d = c['disc']
    if d in disc_priority:
        return disc_priority.index(d)
    return len(disc_priority)

candidates.sort(key=disc_rank)

print('\n=== 前 20 条候选（纯硕士，按学科优先级）===')
for i, c in enumerate(candidates[:20]):
    print(f"{i+1}. [{c['disc']}] {c['school']} - {c['name_cn']} | {c['name_en']} -> {c['det']}")
