Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

8.7 KB
Raw Blame
"""URL-based Wayback availability lookup shared by archive callers."""

import posixpath
import re
from datetime import datetime
from urllib.parse import unquote, urlsplit

from requests import Request

from ..security.safe_requests import safe_get
from ..utilities.resource_utils import safe_close
from .rate_limiting import RateLimitError

AVAILABLE_API = "https://archive.org/wayback/available"
UNSUPPORTED_REPLAY_CAPTURE_URL = (
    "Unsupported archive replay capture URL. Use the exact capture format "
    "https://web.archive.org/web/<timestamp>/<url>, where timestamp is 14 "
    "digits (UTC) and url is the original HTTP(S) URL."
)


def _archive_target_key(url: str):
    """Ignore scheme/www/default ports/fragments, retaining prepared path/query."""
    prepared = Request("GET", validate_archive_url(url)).prepare().url or ""
    parsed = urlsplit(prepared)
    default_port = 443 if parsed.scheme == "https" else 80
    port = parsed.port if parsed.port != default_port else None
    return (
        (parsed.hostname or "").removeprefix("www."),
        port,
        parsed.path or "/",
        parsed.query,
    )


def validate_capture_fetch_url(
    url: str, timestamp: str, expected_url: str | None = None
) -> None:
    """Keep page retrieval and every redirect inside the selected capture."""
    parsed = urlsplit(url)
    if (
        parsed.scheme != "https"
        or parsed.hostname != "web.archive.org"
        or parsed.port not in {None, 443}
        or parsed.username is not None
        or parsed.password is not None
        or not re.match(
            rf"^/web/{re.escape(timestamp)}(?:id_|if_)?/https?://", parsed.path
        )
    ):
        raise ValueError("Archive replay left the selected capture")
    if expected_url is not None:
        target = url.split(f"/web/{timestamp}", 1)[1].split("/", 1)[1]
        if _archive_target_key(target) != _archive_target_key(expected_url):
            raise ValueError("Archive replay changed the captured URL")


def capture_metadata_from_replay_url(url: str) -> dict | None:
    """Identify an exact supplied capture without an availability request.

    A direct replay read needs the same boundary as a discovered capture,
    even before any citation exists. Unsupported capture forms fail before
    network access instead of becoming an ordinary live-page fetch.
    """
    parsed = urlsplit(url)
    if parsed.scheme not in {"http", "https"} or not parsed.hostname:
        return None
    # Detection includes the spelling Requests will send. These aliases
    # must not fall through to an unrestricted live-page fetch, but only
    # the original, canonical capture spelling is accepted below.
    prepared = urlsplit(Request("GET", url).prepare().url or "")
    prepared_host = (prepared.hostname or "").rstrip(".")
    if prepared_host not in {"web.archive.org", "archive.org"}:
        return None

    def has_replay_path(path):
        # Inspect the wrapper, not the embedded target's path. Decode one
        # layer for detection only; never fetch a normalized substitute.
        wrapper = unquote(path.split("://", 1)[0]).replace("\\", "/")
        return any(
            candidate.lstrip("/").partition("/")[0].lower() == "web"
            for candidate in (wrapper, posixpath.normpath(wrapper))
        )

    if not any(has_replay_path(path) for path in (parsed.path, prepared.path)):
        return None
    if prepared_host != "web.archive.org":
        raise ValueError(UNSUPPORTED_REPLAY_CAPTURE_URL)
    replay = re.match(r"^/web/([0-9]{14})(?:id_|if_)?/https?://", parsed.path)
    if replay is None:
        raise ValueError(UNSUPPORTED_REPLAY_CAPTURE_URL)
    timestamp = replay.group(1)
    captured_date = datetime.strptime(timestamp, "%Y%m%d%H%M%S")
    target = url.split(f"/web/{timestamp}", 1)[1].split("/", 1)[1]
    validate_archive_url(target)
    validate_capture_fetch_url(url, timestamp, target)
    return {
        "source_type": "archive",
        "original_url": target,
        "requested_url": target,
        "timestamp": timestamp,
        "formatted_date": captured_date.strftime("%Y-%m-%d %H:%M:%S UTC"),
    }


def validate_archive_url(url: str) -> str:
    """Require one absolute HTTP(S) URL without embedded credentials."""
    url = url.strip()
    if not url or any(
        character.isspace() or ord(character) < 32 for character in url
    ):
        raise ValueError(
            "Supply a single absolute HTTP(S) URL to fetch_archive"
        )
    parsed = urlsplit(url)
    if (
        parsed.scheme.lower() not in {"http", "https"}
        or not parsed.hostname
        or parsed.username is not None
        or parsed.password is not None
    ):
        raise ValueError("Supply an absolute HTTP(S) URL without credentials")
    # Accessing port validates malformed/non-numeric/out-of-range ports.
    parsed.port
    return url


def normalize_archive_date(value: str | None) -> str | None:
    """Convert a calendar date to the availability API's YYYYMMDD input."""
    if value is None:
        return None
    if re.fullmatch(r"\d{4}-\d{2}-\d{2}", value):
        return datetime.strptime(value, "%Y-%m-%d").strftime("%Y%m%d")
    if re.fullmatch(r"\d{8}", value):
        return datetime.strptime(value, "%Y%m%d").strftime("%Y%m%d")
    raise ValueError("Archive date must be a valid YYYY-MM-DD or YYYYMMDD date")


def get_availability_response(url: str, timestamp=None, *, request_get=None):
    """Request the fixed archive API without following redirects."""
    params = {"url": url}
    if timestamp is not None:
        params["timestamp"] = timestamp
    getter = request_get if request_get is not None else safe_get
    return getter(
        AVAILABLE_API, params=params, timeout=30, allow_redirects=False
    )


def lookup_archive_snapshot(url: str, timestamp=None, *, request_get=None):
    """Return an accessible snapshot, preserving its actual capture time.

    With no timestamp the API selects the most recent available capture;
    otherwise it selects a capture near the requested date. Never substitute
    the live page when the archive has no capture.
    """
    url = validate_archive_url(url)
    response = get_availability_response(
        url, timestamp, request_get=request_get
    )
    try:
        if response.status_code == 429:
            raise RateLimitError(
                "Wayback archive lookup rate limit exceeded",
                retry_after=response.headers.get("Retry-After"),
            )
        response.raise_for_status()
        if response.status_code != 200:
            raise ValueError(
                "Archive availability API returned an unexpected status"
            )
        data = response.json()
        if not isinstance(data, dict) or not isinstance(
            data.get("archived_snapshots"), dict
        ):
            raise ValueError("Invalid archive availability response")
        closest = data["archived_snapshots"].get("closest")
        if closest is None:
            return None
        if not isinstance(closest, dict):
            raise ValueError("Invalid archive snapshot")
        if (
            closest.get("available") is not True
            or str(closest.get("status")) != "200"
        ):
            return None
        captured = closest.get("timestamp")
        if not isinstance(captured, str) or not re.fullmatch(
            r"\d{14}", captured
        ):
            raise ValueError("Invalid archive capture timestamp")
        captured_date = datetime.strptime(captured, "%Y%m%d%H%M%S")
        replay = closest.get("url")
        if not isinstance(replay, str):
            raise ValueError("Invalid archive replay URL")
        parsed = urlsplit(replay)
        prefix = f"/web/{captured}/"
        if (
            parsed.scheme.lower() not in {"http", "https"}
            or parsed.hostname != "web.archive.org"
            or parsed.port not in {None, 443, 80}
            or parsed.username is not None
            or parsed.password is not None
            or not parsed.path.startswith(prefix)
        ):
            raise ValueError("Invalid archive replay URL")
        # Preserve the embedded target's query, punctuation and fragment.
        embedded = replay.split(prefix, 1)[1]
        validate_archive_url(embedded)
        if _archive_target_key(embedded) != _archive_target_key(url):
            raise ValueError("Archive capture does not match the requested URL")
        replay = "https://web.archive.org" + replay[replay.index(prefix) :]
        return {
            "original_url": embedded,
            "requested_url": url,
            "url": replay,
            "timestamp": captured,
            "formatted_date": captured_date.strftime("%Y-%m-%d %H:%M:%S UTC"),
            "available": True,
            "status": "200",
        }
    finally:
        safe_close(response, "Wayback availability response")