"""扫描小体积详情页（<6KB = 早期简单版，可升级六卡）"""
import os, re, json

details_dir = 'details'
files = []
for f in os.listdir(details_dir):
    if f.endswith('.html'):
        path = os.path.join(details_dir, f)
        size = os.path.getsize(path)
        files.append((f, size))

# 小文件（<6KB = 早期简单版）
small = [(f, s) for f, s in files if s < 6000]
small.sort(key=lambda x: x[1])
print(f"总详情页: {len(files)}, 小文件(<6KB): {len(small)}")

# 读 index_main.html 找这些文件对应的 PROGRAMS
with open('index_main.html', 'r', encoding='utf-8') as f:
    html = f.read()
m = re.search(r'const PROGRAMS=(\[[\s\S]*?\]);', html)
prog_str = m.group(1)
entries = re.findall(r'\{id:"([^"]+)"([\s\S]*?)(?=\{id:"|,?\n\];)', prog_str)

# 建立 url -> (id, s, n, deg) 映射
url_map = {}
for pid, body in entries:
    s_m = re.search(r's:"([^"]*)"', body)
    n_m = re.search(r'n:"([^"]*)"', body)
    deg_m = re.search(r'deg:"([^"]*)"', body)
    url_m = re.search(r'url:"([^"]*)"', body)
    ddl_m = re.search(r'ddl:"([^"]*)"', body)
    if url_m:
        url = url_m.group(1)
        url_map[url] = {
            'id': pid,
            's': s_m.group(1) if s_m else '',
            'n': n_m.group(1) if n_m else '',
            'deg': deg_m.group(1) if deg_m else '',
            'ddl': ddl_m.group(1) if ddl_m else ''
        }

# 热门学校
hot = ['cmu', 'gatech', 'illinois', 'umich', 'usc', 'nyu', 'col', 'up', 'cornell', 'berkeley', 'ucla', 'mit', 'princeton', 'jhu', 'wustl', 'duke', 'notre', 'emory', 'geo', 'uva', 'unc', 'ucsd']

# 匹配
candidates = []
for f, size in small:
    url = f'details/{f}'
    if url in url_map:
        info = url_map[url]
        candidates.append({**info, 'size': size, 'url': url})

# 按热门学校排序
def score(c):
    if c['s'] in hot:
        return hot.index(c['s'])
    return 99
candidates.sort(key=score)

print(f"\n=== 小文件详情页对应 PROGRAMS（前 30）===")
for i, c in enumerate(candidates[:30]):
    print(f"{i+1}. [{c['s']}] {c['id']} | {c['n'][:30]} | {c['deg']} | {c['size']}B | ddl={c['ddl'][:20]}")
