# -*- coding: utf-8 -*-
import re, io, sys, os, json
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
h = open('index_main.html', encoding='utf-8').read()
p0 = h.index("const PROGRAMS=["); p1 = h.index("const CATD=[")
seg = h[p0:p1]
blocks = re.findall(r'\{id:"([^"]+)"([\s\S]*?)(?=\{id:"|,?\n\];)', seg)
print('PROGRAMS 权威条数:', len(blocks))

def g(b, k):
    m = re.search(k + r':"([^"]*)"', b)
    return m.group(1) if m else ''

cand = []
for pid, b in blocks:
    test = g(b, 'cls'); tui = g(b, 'tui'); ddl = g(b, 'ddl'); lang = g(b, 'lang')
    miss = []
    if not test.strip(): miss.append('test')
    elif '待补' in test or '待确认' in test: miss.append('test?')
    if not tui.strip(): miss.append('tui')
    elif '待补' in tui: miss.append('tui?')
    if not ddl.strip(): miss.append('ddl')
    cand.append({'id': pid, 's': g(b,'s'), 'n': g(b,'n'), 'en': g(b,'en'),
                 'deg': g(b,'deg'), 'url': g(b,'url'), 'miss': miss,
                 'test': test, 'tui': tui, 'ddl': ddl})

print('半核验/待补:', sum(1 for c in cand if c['miss']))
prio = ['col','up','nyu','gatech','duke','ucla','ut','ubc','stanford','usc','cmu','illinois','unc','uva','mcg','uw']
kw = re.compile(r'(computer|electrical|EE|finance|economics|business analytics|analytics|data scien|statistic|mechanical|biomedical|management|MSBA|MAFN|ML|machine learning)', re.I)
out = [c for c in cand if c['miss'] and c['s'] in prio and kw.search(c['en']+' '+c['n'])]
print('优先级候选:', len(out))
for c in out[:30]:
    print(f"{c['id']} | {c['s']} | {c['deg']} | {c['en']} | miss={','.join(c['miss'])} | {c['url']}")

# 保存全量候选供后续
with open('_r320_cand.json','w',encoding='utf-8') as f:
    json.dump(cand, f, ensure_ascii=False, indent=1)
