"""
GLAMWalk -- Volume 4: Libraries & Archives
07_directory_listing_rescue.py

The last-resort rescue technique: when no known URL pattern works, fetch the server's own raw directory listing and rank the real files found there by extension quality.

Source: Volume Four, Section 1.7 -- When Even the Fallback Chain Fails
Targets Python 3.11.4 -- see Volume One, Chapter 1.5/1.6.

NOTE: This script makes real network requests to a live public API.
It's safe to run as-is, but requires an internet connection.
"""

import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin

def score_link(href):
    """Higher is better; a hard negative score rules a link out entirely."""
    href_low = href.lower()
    score = 0

    if href_low.endswith(".txt"):
        score += 200
    if "-0.txt" in href_low:
        score += 100
    if "utf-8" in href_low:
        score += 50
    if href_low.endswith((".htm", ".html")):
        score += 20

    bad_extensions = (".zip", ".gz", ".jpg", ".png", ".mp3", ".pdf", ".epub")
    if any(href_low.endswith(ext) for ext in bad_extensions):
        score -= 1000

    return score

def find_best_file_in_directory(gid):
    directory_url = f"https://www.gutenberg.org/files/{gid}/"
    response = requests.get(directory_url, timeout=20)
    if response.status_code != 200:
        return None

    soup = BeautifulSoup(response.text, "html.parser")
    candidates = []

    for link in soup.find_all("a"):
        href = link.get("href")
        if not href:
            continue
        full_url = urljoin(directory_url, href)
        score = score_link(href)
        if score > 0:
            candidates.append((score, full_url))

    if not candidates:
        return None

    candidates.sort(reverse=True)   # highest score first
    return candidates[0][1]
