#!/usr/bin/env python3
"""Full fetch with retry for empty subs - try different URLs."""
import xml.etree.ElementTree as ET
import urllib.request
import urllib.error
import time

SUBS = {
    "Luchtvaart & radio": ["ADSBexchange","ADSB","flightradar24","aviation","airplanes","Gliding","Baofeng","RTLSDR","meshtastic"],
    "Cybersecurity & OSINT": ["cybersecurity","OSINT","bellingcat","Kalilinux","tryhackme","flipperzero","lockpicking","wardrivers","Adguard"],
    "AI & modellen": ["OpenAI","ClaudeAI","DeepSeek"],
    "Wearables, sport & gezondheid": ["Garmin","Garmininstinct","GarminEdge","GarminTactix","fitbit","Strava","Zwift","running","peloton","Biohackers"],
    "Outdoor, bikepacking & reizen": ["bikepacking","Ultralight","CamperVans","VanlifeEurope","orienteering","Meteograms"],
    "Tactisch, defensie & uitrusting": ["TacticalMedicine","511tactical","Military","Glock19"],
    "België, Europa & lokaal": ["Antwerpen","belgium","europe"],
    "Gaming & entertainment": ["GhostRecon","Doom","doorkickers","TombRaider"],
    "Maker, hardware & niche": ["BambuLab","Xennials"]
}

HEADERS = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"}
NS = {"atom": "http://www.w3.org/2005/Atom"}

def fetch_sub(sub, retries=0):
    # Try RSS first
    urls = [
        f"https://old.reddit.com/r/{sub}/hot/.rss",
    ]
    for url in urls:
        try:
            req = urllib.request.Request(url, headers=HEADERS)
            with urllib.request.urlopen(req, timeout=15) as resp:
                body = resp.read().decode()
            root = ET.fromstring(body)
            entries = root.findall("atom:entry", NS)
            posts = []
            for entry in entries[:3]:
                title_el = entry.find("atom:title", NS)
                title = title_el.text.strip().replace("\n", " ").replace("|", "/") if title_el is not None and title_el.text else "[geen titel]"
                link_el = entry.find("atom:link", NS)
                url_str = link_el.get("href", "") if link_el is not None else ""
                posts.append((title, url_str))
            if posts:
                return posts
        except:
            continue
    return []

all_results = {}
for cat, sub_list in SUBS.items():
    cat_posts = []
    for sub in sub_list:
        posts = fetch_sub(sub)
        for title, url in posts:
            cat_posts.append((sub, title, url))
        time.sleep(0.15)
    all_results[cat] = cat_posts[:5]

# Output
for cat, posts in all_results.items():
    if not posts:
        print(f"CATEGORY|{cat}|NONE")
    else:
        for sub, title, url in posts:
            print(f"CATEGORY|{cat}|{sub}|{title}|{url}")
