import re, json
from datetime import datetime, timezone

CUTOFF_MS = 1780272000000  # June 1, 2026 08:00 UTC

files = {
    'top': '/home/hermes/workspace/reddit_top.html',
    'hot': '/home/hermes/workspace/reddit_hot.html',
    'new': '/home/hermes/workspace/reddit_new.html',
}

all_posts = {}
for source, path in files.items():
    with open(path) as f:
        content = f.read()
    
    # Extract title blocks: <p class="title"> ... </p>
    title_blocks = re.findall(r'<p class="title">(.*?)</p>', content, re.DOTALL)
    
    print(f"\n=== {source.upper()} ===")
    
    for block in title_blocks:
        # Extract permalink (for self posts) or outbound href
        # self post: href="/r/boardgames/comments/..."
        permalink_m = re.search(r'href="(/r/boardgames/comments/[^"/]+/)', block)
        if not permalink_m:
            # alternative: gallery or i.redd.it with data-href-url
            permalink_m = re.search(r'href="(/r/boardgames/comments/[^"/]+)"', block)
        if not permalink_m:
            # outbound: look for data-href-url
            permalink_m = re.search(r'data-href-url="([^"]*r/boardgames/comments/[^"]*)"', block)
        
        if not permalink_m:
            continue
            
        permalink = permalink_m.group(1)
        
        # Extract title text
        title_m = re.search(r'<a[^>]*>(.*?)</a>', block)
        title = title_m.group(1).strip() if title_m else 'Unknown'
        
        # Flair
        flair_m = re.search(r'title="([^"]*)"', block.split('</a>')[0])
        flair = ''
        flair_label = re.search(r'linkflairlabel[^>]*title="([^"]*)"', block)
        if flair_label:
            flair = flair_label.group(1)
        
        # Find data attributes for this post (search nearby in the full content)
        # Build a short ID from the permalink
        post_id = permalink.split('/comments/')[1].split('/')[0] if '/comments/' in permalink else permalink.split('/')[-2]
        
        # Search for attribute block near this permalink
        pattern = re.compile(
            r'data-permalink="[^"]*' + re.escape(post_id) + r'[^"]*".*?'
            r'(?:data-comments-count="(\d+)".*?)?'
            r'(?:data-score="(\d+)".*?)?'
            r'(?:data-timestamp="(\d+)".*?)?'
            r'(?:data-author="([^"]*)".*?)?',
            re.DOTALL
        )
        attr_match = pattern.search(content)
        if attr_match:
            comments = int(attr_match.group(1)) if attr_match.group(1) else 0
            score = int(attr_match.group(2)) if attr_match.group(2) else 0
            timestamp = int(attr_match.group(3)) if attr_match.group(3) else 0
            author = attr_match.group(4) or 'unknown'
        else:
            comments = score = timestamp = 0
            author = 'unknown'
        
        if timestamp < CUTOFF_MS:
            continue
        
        key = post_id
        if key in all_posts:
            # Keep highest score
            if score > all_posts[key]['score']:
                all_posts[key] = {
                    'title': title,
                    'flair': flair,
                    'permalink': permalink,
                    'source': source,
                    'score': score,
                    'comments': comments,
                    'timestamp': timestamp,
                    'author': author,
                }
        else:
            all_posts[key] = {
                'title': title,
                'flair': flair,
                'permalink': permalink,
                'source': source,
                'score': score,
                'comments': comments,
                'timestamp': timestamp,
                'author': author,
            }

print(f"\n\n=== TOTAL UNIQUE POSTS (last 72h): {len(all_posts)} ===")

sorted_posts = sorted(all_posts.values(), key=lambda x: x['score'], reverse=True)

for i, p in enumerate(sorted_posts):
    dt = datetime.fromtimestamp(p['timestamp']/1000, tz=timezone.utc)
    age_h = (datetime.now(timezone.utc) - dt).total_seconds() / 3600
    flair_str = f" [{p['flair']}]" if p['flair'] else ""
    print(f"{i+1}. [{p['source'].upper()}{flair_str}] Score:{p['score']} 💬{p['comments']} | {age_h:.0f}h ago")
    print(f"   {p['title']}")
    print(f"   u/{p['author']} | https://old.reddit.com{p['permalink']}")
    print()

