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.

10.6 KB
Raw Blame
"""
Generic PDF Downloader for unspecified sources
"""

from typing import Dict, Optional
import requests
from urllib.parse import urlparse, urlunparse
from loguru import logger

from ...library.download_management.failure_classifier import (
    access_denied_reason,
)
from ...security.ssrf_validator import redact_url_for_log
from .base import BaseDownloader, ContentType, DownloadResult


HTML_NOT_PDF_REASON = "Source is an HTML page, not a PDF"


def _with_pdf_suffix(url: str) -> Optional[str]:
    """Return ``url`` with ``.pdf`` appended to its path, or ``None`` if
    no suffix is needed / the URL cannot be parsed.

    Three edge cases the previous implementation got wrong:

    1. **Bare host.** A URL like ``https://pmc.ncbi.nlm.nih.gov/`` has an
       empty path. Naive string concatenation
       (``url.rstrip("/") + ".pdf"``) produced
       ``https://pmc.ncbi.nlm.nih.gov.pdf`` -- ``.pdf`` ended up appended
       to the hostname, the SSRF validator rejected it as an unresolvable
       hostname, and the URL was logged at ``ERROR`` even though the
       fallback was a normal part of the download path.
    2. **Trailing slash on a path.** ``https://example.com/paper/`` ended
       up as ``https://example.com/paper.pdf`` by accident; that worked,
       but only because ``rstrip("/")`` happened to delete exactly the
       slash before appending.
    3. **Query string.** ``https://example.com/paper?q=1`` became
       ``https://example.com/paper?q=1.pdf``, putting ``.pdf`` after the
       query separator and corrupting the URL.

    We rebuild the URL via ``urlunparse(parsed._replace(path=...))`` so
    the suffix lands in the path component and scheme/netloc/query/
    fragment/userinfo are preserved verbatim. An empty path becomes
    ``/index.pdf`` -- a deterministic placeholder that still describes
    a resource at the host root.

    The ``.pdf`` guard is case-insensitive and runs on the trailing
    slashes-stripped path, so ``paper.pdf/``, ``paper.PDF``, and
    ``paper.Pdf/`` are all recognised as already-suffixed and short-
    circuit to ``None`` instead of producing ``paper.pdf.pdf``.
    """
    try:
        parsed = urlparse(url)
    except ValueError:
        return None

    stripped_path = parsed.path.rstrip("/") if parsed.path else ""
    if stripped_path.lower().endswith(".pdf"):
        return None

    new_path = (stripped_path or "/index") + ".pdf"
    return urlunparse(parsed._replace(path=new_path))


class GenericDownloader(BaseDownloader):
    """Generic downloader for any URL - attempts basic PDF download."""

    def can_handle(self, url: str) -> bool:
        """Generic downloader can handle any URL as a fallback."""
        return True

    def download(
        self, url: str, content_type: ContentType = ContentType.PDF
    ) -> Optional[bytes]:
        """Attempt to download content from any URL."""
        if content_type == ContentType.TEXT:
            # For generic sources, we can only extract text from PDF
            pdf_content = self._download_pdf(url)
            if pdf_content:
                text = self.extract_text_from_pdf(pdf_content)
                if text:
                    return text.encode("utf-8")
            return None
        # Try to download as PDF
        return self._download_pdf(url)

    def download_with_result(
        self, url: str, content_type: ContentType = ContentType.PDF
    ) -> DownloadResult:
        """Download content and return detailed result with skip reason."""
        if content_type == ContentType.TEXT:
            # For generic sources, we can only extract text from PDF
            pdf_content = self._download_pdf(url)
            if pdf_content:
                text = self.extract_text_from_pdf(pdf_content)
                if text:
                    return DownloadResult(
                        content=text.encode("utf-8"),
                        is_success=True,
                        pdf_content=pdf_content,
                    )
                return DownloadResult(
                    skip_reason="PDF downloaded but text extraction failed"
                )
            return DownloadResult(skip_reason="Could not download PDF from URL")
        # Try to download as PDF
        logger.info(
            f"Attempting generic download from {redact_url_for_log(url)}"
        )

        # Try direct download
        pdf_content = super()._download_pdf(url)

        if pdf_content:
            logger.info(
                f"Successfully downloaded PDF from {redact_url_for_log(url)}"
            )
            return DownloadResult(
                content=pdf_content, is_success=True, pdf_content=pdf_content
            )

        # If the URL doesn't end with .pdf, try adding it
        pdf_url = _with_pdf_suffix(url)
        if pdf_url is not None:
            logger.debug(
                f"Trying with .pdf extension: {redact_url_for_log(pdf_url)}"
            )
            pdf_content = super()._download_pdf(pdf_url)
        else:
            pdf_content = None

        if pdf_content:
            logger.info(
                f"Successfully downloaded PDF from {redact_url_for_log(pdf_url)}"
            )
            return DownloadResult(
                content=pdf_content, is_success=True, pdf_content=pdf_content
            )

        # Diagnostic request: determine WHY the download failed.
        #
        # IMPORTANT: stream=True is intentional here. DO NOT remove it.
        # This block only inspects response.status_code and headers
        # to determine why a download failed (404, 403, paywall, etc.).
        # Without stream=True, the full response body would be downloaded
        # into memory. Since GenericDownloader.can_handle() returns True
        # for ALL URLs, this could mean downloading multi-GB files just
        # to check a status code.
        #
        # The context manager (with ... as response) ensures the streamed
        # connection is properly closed on all code paths, preventing
        # file descriptor leaks (each unclosed stream=True response
        # holds an open socket FD).
        try:
            with self.session.get(
                url, timeout=5, allow_redirects=True, stream=True
            ) as response:
                # Check status code
                if response.status_code == 200:
                    # HTML identifies a format mismatch, not a paywall.
                    response_content_type = response.headers.get(
                        "content-type", ""
                    ).lower()
                    if "text/html" in response_content_type:
                        return DownloadResult(
                            skip_reason=HTML_NOT_PDF_REASON,
                            status_code=response.status_code,
                        )
                    return DownloadResult(
                        skip_reason=f"Unexpected content type: {response_content_type} - expected PDF",
                        status_code=response.status_code,
                    )
                if response.status_code == 404:
                    return DownloadResult(
                        skip_reason="Article not found (404) - may have been removed or URL is incorrect",
                        status_code=404,
                    )
                if response.status_code == 403:
                    # ``url`` is set per instance by requests.Response, not
                    # declared on the class, so a response-like object may
                    # lack it; fall back to the requested URL.
                    destination = getattr(response, "url", None)
                    if not isinstance(destination, str) or not destination:
                        destination = url
                    return DownloadResult(
                        skip_reason=access_denied_reason(destination),
                        status_code=403,
                    )
                if response.status_code == 429:
                    return DownloadResult(
                        skip_reason="Rate limited (429) - retry later",
                        status_code=429,
                    )
                if response.status_code == 401:
                    return DownloadResult(
                        skip_reason="Authentication required - please login to access this article",
                        status_code=401,
                    )
                if response.status_code >= 500:
                    return DownloadResult(
                        skip_reason=f"Server error ({response.status_code}) - website is experiencing technical issues",
                        status_code=response.status_code,
                    )
                return DownloadResult(
                    skip_reason=f"Unable to access article - server returned error code {response.status_code}",
                    status_code=response.status_code,
                )
        except requests.exceptions.Timeout:
            return DownloadResult(
                skip_reason="Connection timed out - server took too long to respond"
            )
        except requests.exceptions.ConnectionError:
            return DownloadResult(
                skip_reason="Could not connect to server - website may be down"
            )
        except requests.RequestException as e:
            logger.opt(exception=False).warning(
                "Unexpected error checking URL: {} ({})",
                redact_url_for_log(url),
                type(e).__name__,
            )
            return DownloadResult(
                skip_reason="Network error - could not reach the website"
            )

    def _download_pdf(
        self, url: str, headers: Optional[Dict[str, str]] = None
    ) -> Optional[bytes]:
        """Attempt to download PDF from URL."""
        logger.info(
            f"Attempting generic download from {redact_url_for_log(url)}"
        )

        # Try direct download
        pdf_content = super()._download_pdf(url)

        if pdf_content:
            logger.info(
                f"Successfully downloaded PDF from {redact_url_for_log(url)}"
            )
            return pdf_content

        # If the URL doesn't end with .pdf, try adding it
        pdf_url = _with_pdf_suffix(url)
        if pdf_url is not None:
            logger.debug(
                f"Trying with .pdf extension: {redact_url_for_log(pdf_url)}"
            )
            pdf_content = super()._download_pdf(pdf_url)
        else:
            pdf_content = None

        if pdf_content:
            logger.info(
                f"Successfully downloaded PDF from {redact_url_for_log(pdf_url)}"
            )
            return pdf_content

        logger.warning(f"Failed to download PDF from {redact_url_for_log(url)}")
        return None