import json, re

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

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

existing_urls = set(re.findall(r'url:"(details/[^"]+\.html)"', html))
existing_en = set(re.findall(r'en:"([^"]+)"', html))

# 已知排除黑名单（关键词匹配）
blacklist_keywords = [
    'EECS – Computer Science', 'EECS – Electrical Engineering and Computer Sciences',
    'Information and Data Science', 'Master of Artificial Intelligence',
    'Master of Computational Social Science', 'Statistics and Data Science, MS',  # Berkeley 403
    'Data Science in Biomedicine',  # UCLA online
    'Master of Applied Geospatial',  # UCLA MAGIST online
    'Applied Data Science',  # UNC online
    'Data Science and Engineering',  # UCSD MAS 职业
    'Cybersecurity Risk Management',  # Georgetown SCS
    'Quantitative Financial Economics',  # Duke MQFE 停招
    'Statistics and Data Science, MA, Statistics for Political Science',  # Wustl polsci conc
    'Data Science | MS',  # Notre online
]

# 已知排除的学校+学科组合
blacklist_schools = {'berkeley'}  # Berkeley 全系站403

disc_order = ['cs','ee','fin','econ','ba','ds','stat','mech','bio','mgmt']
candidates = []

for item in todo:
    det = item.get('det','')
    en = item.get('en','')
    deg = item.get('deg','')
    disc = item.get('disc','')
    s = item.get('s','')
    cn = item.get('cn','')
    
    if det in existing_urls or en in existing_en:
        continue
    if s in blacklist_schools:
        continue
    if any(kw.lower() in en.lower() for kw in blacklist_keywords):
        continue
    
    deg_lower = deg.lower()
    if not any(k in deg_lower for k in ['ms','ma','meng','mps','master']):
        continue
    
    # 跳过博士
    if 'phd' in deg_lower or 'ph.d' in deg_lower:
        continue
    
    candidates.append({
        's': s, 'en': en, 'cn': cn, 'deg': deg,
        'disc': disc, 'det': det, 'col': item.get('col',''), 'url': item.get('url','')
    })

candidates.sort(key=lambda x: disc_order.index(x['disc']) if x['disc'] in disc_order else 99)

print(f'过滤后候选总数: {len(candidates)}')
print()
print('=== 前 40 条（按学科优先级）===')
for i, c in enumerate(candidates[:40]):
    print(f'{i+1:2d}. [{c["disc"]:5s}] {c["s"]:12s} | {c["cn"][:24]:24s} | {c["en"][:58]} | {c["deg"]}')
