import json, re

html = open('index_main.html', encoding='utf-8').read()
existing_ids = set(re.findall(r'id:"([^"]+)"', html))
existing_urls = set(re.findall(r'url:"(details/[^"]+)"', html))

# Also extract existing program English names for fuzzy matching
existing_en_names = set()
for m in re.finditer(r'id:"([^"]+)",s:"([^"]+)",n:"([^"]+)",en:"([^"]+)"', html):
    existing_en_names.add(m.group(4).lower().strip())

print(f"Existing IDs: {len(existing_ids)}, URLs: {len(existing_urls)}, EN names: {len(existing_en_names)}")

todo = json.load(open('todo_upgrade.json', encoding='utf-8'))
pending = [item for item in todo if item.get('det','') not in existing_urls and item.get('url','') not in existing_urls]

# Filter by hot priority and not already in PROGRAMS by name match
hot_keywords = [
    'computer science', 'data science', 'analytics', 'business analytics',
    'electrical engineering', 'computer engineering', 'finance', 'financial',
    'economics', 'statistic', 'machine learning', 'mechanical engineering',
    'biomedical', 'biology', 'management', 'information systems',
    'operations research', 'industrial engineering', 'systems engineering',
    'quantitative', 'accounting', 'marketing', 'entrepreneurship',
    'cancer biology', 'genomics', 'microbiology', 'immunology',
    'physics', 'chemistry', 'mathematics',
]

# Exclude clearly excluded programs
exclude_keywords = [
    'event management', 'hospitality', 'human capital', 'project management',
    'travel and tourism', 'sports management', 'real estate', 'sustainability management',
    'translation', 'creative writing', 'teaching', 'education', 'art history',
    'religion', 'music', 'theater', 'dance', 'film', 'cinema', 'journalism',
    'communication', 'english', 'history', 'philosophy', 'sociology',
    'political science', 'international relations', 'public policy', 'social work',
    'divinity', 'theology', 'law', 'near and middle eastern', 'east asian',
    'middle eastern', 'slavic', 'german', 'french', 'italian', 'spanish',
    'portuguese', 'africana', 'jewish', 'islamic', 'buddhist', 'hindu',
    'anthropology', 'archaeology', 'classics', 'comparative literature',
    'women', 'gender', 'sexuality', 'migration', 'refugee',
    'executive', 'mid-career', 'part-time', 'online', 'distance',
    'master of liberal', 'master of science in liberal', 'professional studies',
    'event', 'public relations', 'advertising', 'public relations',
    'nonprofit', 'social innovation', 'social entrepreneurship',
    'medical physics', 'health administration', 'health management',
    'hospital', 'nursing', 'pharmacy', 'public health',
    'urban planning', 'urban design', 'architecture', 'landscape',
    'fine arts', 'studio art', 'design', 'music', 'theater', 'dance',
    'writing', 'journalism', 'media studies', 'communication',
    'international business', 'global business',
]

hot_pending = []
for p in pending:
    en_lower = p.get('en', '').lower()
    disc = p.get('disc', '').lower()
    
    # Check if English name already exists in PROGRAMS
    if en_lower in existing_en_names:
        continue
    
    # Check exclude keywords
    if any(x in en_lower for x in exclude_keywords):
        continue
    
    # Check hot keywords
    if any(kw in en_lower or kw in disc for kw in hot_keywords):
        hot_pending.append(p)

print(f"\nHot pending (after name match): {len(hot_pending)}")

# Group by school
from collections import Counter
school_counts = Counter(p.get('s', '?') for p in hot_pending)
print("\nHot pending by school:")
for s, c in school_counts.most_common(20):
    print(f"  {s}: {c}")

print("\n--- First 30 hot pending ---")
for i, p in enumerate(hot_pending[:30]):
    print(f"{i+1}. [{p.get('s','?')}] {p.get('en','?')} | {p.get('deg','?')} | disc={p.get('disc','?')} | det={p.get('det','')}")
