import json, re

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

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

# 提取 PROGRAMS 里的 url
prog_urls = set(re.findall(r'url:"(details/[^"]+\.html)"', html))
print(f'PROGRAMS 已有 url 数: {len(prog_urls)}')

# 提取 VERIFIED_DETAILS 集合里的 url
vd_match = re.search(r'const VERIFIED_DETAILS=new Set\(\[([^\]]+)\]', html)
vd_urls = set()
if vd_match:
    vd_urls = set(re.findall(r'"(details/[^"]+\.html)"', vd_match.group(1)))
print(f'VERIFIED_DETAILS 集合数: {len(vd_urls)}')

# 硕士学位关键词
master_degs = {'MS','MA','MEng','MPS','MFA','MBA','MPH','MPP','MSW','MLA','MSB','MM','MEd','LLM','MArch','MCP','MHA','MSM','MSc','MAS','MEngS','MPS'}

# 筛选未核验的硕士项目（det 不在 PROGRAMS 也不在 VD 里）
prio = {'cs':0,'eng':1,'fin':2,'econ':3,'ba':4,'ds':5,'stat':6,'sci':7,'bio':8,'mgmt':9,'biz':10,'soc':11,'art':12,'hum':13}
candidates = []
for item in todo:
    deg = item.get('deg','').upper()
    # 只保留硕士级
    if not any(m in deg for m in ['MS','MA','MEng','MPS','MFA','MPH','MPP','MSW','MLA','MArch','MCP','MHA','MSc','MAS','LLM','MEd']):
        continue
    # 排除 PhD/博士
    if 'PHD' in deg or 'DR' in deg or 'DOC' in deg:
        continue
    det = item.get('det','')
    if not det:
        continue
    # 跳过已在 PROGRAMS 或 VD 的
    if det in prog_urls or det in vd_urls:
        continue
    disc = item.get('disc','other')
    rank = prio.get(disc, 99)
    candidates.append((rank, item))

candidates.sort(key=lambda x: x[0])
print(f'未核验硕士候选: {len(candidates)}')
print()
print('=== 前 30 条候选（按学科优先级）===')
for i, (rank, item) in enumerate(candidates[:30]):
    s = item.get('s','?')
    cn = item.get('cn','?')
    en = item.get('en','?')
    deg = item.get('deg','?')
    disc = item.get('disc','?')
    det = item.get('det','?')
    print(f'{i+1}. [{s}] {cn} ({deg}) | {en} | disc={disc} | det={det}')
