import re, json, os
from datetime import datetime

html = open('index_main.html', encoding='utf-8').read()
pm = re.search(r'const PROGRAMS=\[([\s\S]*?)\n\];', html)
block = pm.group(1)
entries = re.split(r'(?=\{id:")', block)
current = {}
for e in entries:
    e = e.strip().rstrip(',')
    if not e.startswith('{'): continue
    mid = re.search(r'\{id:"([^"]+)"', e)
    if not mid: continue
    pid = mid.group(1)
    s = re.search(r's:"([^"]*)"', e)
    n = re.search(r'n:"([^"]*)"', e)
    en = re.search(r'en:"([^"]*)"', e)
    deg = re.search(r'deg:"([^"]*)"', e)
    url = re.search(r'url:"([^"]*)"', e)
    current[pid] = {
        's': s.group(1) if s else '',
        'n': n.group(1) if n else '',
        'en': en.group(1) if en else '',
        'deg': deg.group(1) if deg else '',
        'url': url.group(1) if url else '',
    }

prev = json.load(open('_daily_snapshot.json', encoding='utf-8'))
# Snapshot was already overwritten by previous run; we need to recover from the snapshot BEFORE the overwrite.
# Actually the previous run already overwrote it. Let me just output new entries by reading current and comparing to... 
# Wait, the previous run wrote the new snapshot already. We lost the old snapshot. But we have the old count (1349) from the output.
# The new snapshot now has 1448 entries. The diff of 99 new ids was printed.
# Let me just output the 99 new entries list from the saved output file.

# Actually, let me read the saved tool output to get the NEW ENTRIES section
with open(r'C:\Users\buf001\AppData\Local\Doubao\User Data\Default\.doubao\agent_mode\workspace\.sessions\38452333715834626\agents\m_0cw3alUqktb\system\tool-results\bash_1ECFB048A900.txt', encoding='utf-8') as f:
    content = f.read()

# Extract NEW ENTRIES section
m = re.search(r'=== NEW ENTRIES ===\n(.*?)\n=== URL CHANGED ===', content, re.DOTALL)
if m:
    new_entries_text = m.group(1).strip()
    lines = [l for l in new_entries_text.split('\n') if l.strip()]
    print(f'Total new entries: {len(lines)}')
    print()
    # Group by school
    from collections import Counter
    schools = []
    for l in lines:
        parts = l.split(' | ')
        if parts:
            schools.append(parts[0])
    print('By school:')
    for s, c in Counter(schools).most_common():
        print(f'  {s}: {c}')
    print()
    # Save to file
    with open('_r310_new_entries.txt', 'w', encoding='utf-8') as f:
        for l in lines:
            f.write(l + '\n')
    print(f'Saved to _r310_new_entries.txt')
    print()
    print('=== ALL NEW ENTRIES ===')
    for l in lines:
        print(l)
