#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = [
#   "bs4",
#   "requests",
# ]
# ///

from bs4 import BeautifulSoup
import sys
import requests
import os

def download_page(url, filename):
    """Download HTML page and save to local file"""
    if os.path.exists(filename):
        print(f"Using cached {filename}")
        return open(filename).read()
    
    print(f"Downloading {url}...")
    try:
        response = requests.get(url, timeout=30)
        response.raise_for_status()
        with open(filename, 'w', encoding='utf-8') as f:
            f.write(response.text)
        return response.text
    except requests.RequestException as e:
        print(f"Error downloading {url}: {e}")
        sys.exit(1)

# Download the main index page
index_url = 'https://wiki.studiominus.nl/index.html'
html = download_page(index_url, 'index.html')
soup = BeautifulSoup(html, 'html.parser')

with open('sidebar.txt', 'w') as f:
    for link in soup.select('.sidebar-links a[href]'):
        url = f'https://wiki.studiominus.nl{link['href']}'
        title = link.get_text().strip()
        f.write(f'{url} - {title}\n')
        if 'texturePackSystem.html' in url:
            break
