#!/usr/bin/env python3
"""Fetch underrepresented subs more aggressively."""
import xml.etree.ElementTree as ET
import urllib.request
import urllib.error
import time

# Subs that returned nothing in first pass
SUBS = {
    "Baofeng": "Luchtvaart & radio",
    "RTLSDR": "Luchtvaart & radio",
    "meshtastic": "Luchtvaart & radio",
    "bellingcat": "Cybersecurity & OSINT",
    "Kalilinux": "Cybersecurity & OSINT",
    "tryhackme": "Cybersecurity & OSINT",
    "flipperzero": "Cybersecurity & OSINT",
    "lockpicking": "Cybersecurity & OSINT",
    "wardrivers": "Cybersecurity & OSINT",
    "Adguard": "Cybersecurity & OSINT",
    "DeepSeek": "AI & modellen",
    "GarminEdge": "Wearables, sport & gezondheid",
    "GarminTactix": "Wearables, sport & gezondheid",
    "fitbit": "Wearables, sport & gezondheid",
    "Strava": "Wearables, sport & gezondheid",
    "Zwift": "Wearables, sport & gezondheid",
    "running": "Wearables, sport & gezondheid",
    "peloton": "Wearables, sport & gezondheid",
    "Biohackers": "Wearables, sport & gezondheid",
    "CamperVans": "Outdoor, bikepacking & reizen",
    "VanlifeEurope": "Outdoor, bikepacking & reizen",
    "orienteering": "Outdoor, bikepacking & reizen",
    "Meteograms": "Outdoor, bikepacking & reizen",
    "Military": "Tactisch, defensie & uitrusting",
    "Glock19": "Tactisch, defensie & uitrusting",
    "europe": "België, Europa & lokaal",
    "doorkickers": "Gaming & entertainment",
    "TombRaider": "Gaming & entertainment",
    "BambuLab": "Maker, hardware & niche",
    "Xennials": "Maker, hardware & niche",
}

HEADERS = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"}

for sub, cat in sorted(SUBS.items()):
    url = f"https://old.reddit.com/r/{sub}/hot/.rss"
    try:
        req = urllib.request.Request(url, headers=HEADERS)
        with urllib.request.urlopen(req, timeout=15) as resp:
            body = resp.read().decode()
        ns = {"atom": "http://www.w3.org/2005/Atom"}
        root = ET.fromstring(body)
        entries = root.findall("atom:entry", ns)
        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 ""
            print(f"CATEGORY|{cat}|{sub}|{title}|{url_str}")
    except Exception as e:
        print(f"CATEGORY|{cat}|{sub}|[ERROR: {e}]|", file=open('/dev/null','w'))  # skip silently
    time.sleep(0.2)
