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.

11.9 KB
Raw Blame
"""
OpenAlex PDF Downloader

Downloads PDFs from OpenAlex using their API to find open access PDFs.
OpenAlex aggregates open access information from multiple sources.
"""

import re
from typing import Optional
from urllib.parse import urlparse

import requests
from loguru import logger

from ...security import redact_url_for_log
from ...security.log_sanitizer import scrub_error
from ...utilities.openalex_enrichment import (
    normalize_openalex_api_key,
    send_with_key_fallback,
)
from .base import BaseDownloader, ContentType, DownloadResult


class OpenAlexDownloader(BaseDownloader):
    """Downloader for OpenAlex papers with open access PDF support."""

    def __init__(
        self,
        timeout: int = 30,
        polite_pool_email: Optional[str] = None,
        api_key: Optional[str] = None,
        **kwargs,
    ):
        """
        Initialize OpenAlex downloader.

        Args:
            timeout: Request timeout in seconds
            polite_pool_email: Optional email included in the User-Agent for
                identification; retained under its existing name for compatibility
            api_key: Optional OpenAlex API key for a higher daily request budget.
                Placeholders and sentinel values normalize to None so the
                downloader falls back to the keyless free tier instead of
                sending a Bearer token OpenAlex will reject. A real key
                OpenAlex *does* reject is dropped on first use and every
                later lookup goes out keyless, so a bad key never turns
                an available PDF into "no PDF available".
        """
        super().__init__(timeout, **kwargs)
        self.polite_pool_email = polite_pool_email
        self.api_key: Optional[str] = normalize_openalex_api_key(api_key)
        # Set only once OpenAlex refuses the key; kept so ``_scrub``
        # still has the literal to redact after ``api_key`` is cleared.
        self._rejected_api_key: Optional[str] = None
        self.base_api_url = "https://api.openalex.org"

    def _scrub(self, error: Exception) -> str:
        """Return a log-safe rendering of *error*.

        logger.warning + this helper replaces logger.exception throughout
        ``_get_pdf_url``: that frame's locals hold ``headers`` (the
        "Authorization: Bearer <key>" value), which loguru's diagnose
        would render into the traceback. Same documented trade-off as
        ``search_engine_nasa_ads``. This class is not a
        ``BaseSearchEngine``, so it does its own literal-key redaction
        instead of relying on ``_scrub_error``/``_secret_attrs``.

        ``getattr`` with a default, like ``BaseSearchEngine._scrub_error``:
        this runs inside ``except`` handlers, and a partially-constructed
        instance (``__new__``, or a ``super().__init__`` that raised)
        must degrade to a shape-only scrub rather than raise
        ``AttributeError`` from inside the handler.
        """
        return scrub_error(
            error,
            getattr(self, "api_key", None),
            getattr(self, "_rejected_api_key", None),
        )

    def can_handle(self, url: str) -> bool:
        """Check if URL is from OpenAlex."""
        try:
            hostname = urlparse(url).hostname
            return bool(
                hostname
                and (
                    hostname == "openalex.org"
                    or hostname.endswith(".openalex.org")
                )
            )
        except (ValueError, AttributeError, TypeError):
            return False

    def download(
        self, url: str, content_type: ContentType = ContentType.PDF
    ) -> Optional[bytes]:
        """Download content from OpenAlex."""
        result = self.download_with_result(url, content_type)
        return result.content if result.is_success else None

    def download_with_result(
        self, url: str, content_type: ContentType = ContentType.PDF
    ) -> DownloadResult:
        """Download PDF and return detailed result with skip reason."""
        # Only support PDF downloads for now
        if content_type != ContentType.PDF:
            return DownloadResult(
                skip_reason="Text extraction not yet supported for OpenAlex"
            )

        # Extract work ID from URL
        work_id = self._extract_work_id(url)
        if not work_id:
            return DownloadResult(
                skip_reason="Invalid OpenAlex URL - could not extract work ID"
            )

        logger.info(f"Looking up OpenAlex work: {work_id}")

        # Get work details from API to find PDF URL
        pdf_url = self._get_pdf_url(work_id)

        if not pdf_url:
            return DownloadResult(
                skip_reason="Not open access - no free PDF available"
            )

        # Download the PDF from the open access URL
        logger.info(
            f"Downloading open access PDF from: {redact_url_for_log(pdf_url)}"
        )
        pdf_content = super()._download_pdf(pdf_url)

        if pdf_content:
            return DownloadResult(content=pdf_content, is_success=True)
        return DownloadResult(
            skip_reason="Open access PDF URL found but download failed"
        )

    def _extract_work_id(self, url: str) -> Optional[str]:
        """
        Extract OpenAlex work ID from URL.

        Handles formats like:
        - https://openalex.org/W123456789
        - https://openalex.org/works/W123456789

        Returns:
            Work ID (e.g., W123456789) or None if not found
        """
        # Use urlparse for more robust URL handling (handles query strings, fragments)
        parsed = urlparse(url)
        if not parsed.netloc or "openalex.org" not in parsed.netloc:
            return None

        # Extract work ID from path (W followed by digits)
        # Handles /works/W123 or /W123
        path = parsed.path
        match = re.search(r"(?:/works/)?(W\d+)", path)
        return match.group(1) if match else None

    def _get_pdf_url(self, work_id: str) -> Optional[str]:
        """
        Get open access PDF URL from OpenAlex API.

        Args:
            work_id: OpenAlex work ID (e.g., W123456789)

        Returns:
            PDF URL if available, None otherwise
        """
        try:
            # Construct API request
            api_url = f"{self.base_api_url}/works/{work_id}"
            params = {"select": "id,open_access,best_oa_location"}

            # Identify the client by email when configured.
            headers = {}
            if self.polite_pool_email:
                headers["User-Agent"] = f"mailto:{self.polite_pool_email}"

            def _send(with_api_key: bool) -> requests.Response:
                request_headers = dict(headers)
                if with_api_key and self.api_key:
                    # Bearer header rather than the equally-supported
                    # ?api_key= query parameter, so the key stays out of
                    # URLs. https://help.openalex.org/api/authentication/
                    request_headers["Authorization"] = f"Bearer {self.api_key}"
                return self.session.get(
                    api_url,
                    params=params,
                    headers=request_headers,
                    timeout=self.timeout,
                )

            def _drop_rejected_key() -> None:
                # The single mutation point: this runs *before* the
                # keyless retry, so even if that retry raises, the
                # refused key is already gone and cannot be sent again.
                # ``or self._rejected_api_key``: a second call must not
                # store None over the literal and drop it out of
                # ``_scrub``'s redaction set.
                self._rejected_api_key = self.api_key or self._rejected_api_key
                self.api_key = None

            # Make API request. The shared helper retries keylessly once
            # if OpenAlex refuses the key and drops the key for this
            # downloader's lifetime, so a stale key costs one extra
            # request rather than every open-access PDF. If the keyless
            # retry fails too, the status branches below apply the
            # pre-existing behaviour (warn, return None).
            response, _ = send_with_key_fallback(
                _send,
                api_key=self.api_key,
                context="OpenAlex download",
                on_key_rejected=_drop_rejected_key,
            )

            if response.status_code == 200:
                data = response.json()

                # Check if it's open access
                open_access_info = data.get("open_access", {})
                is_oa = open_access_info.get("is_oa", False)

                if not is_oa:
                    logger.info(f"Work {work_id} is not open access")
                    return None

                # Get PDF URL from best open access location
                best_oa_location = data.get("best_oa_location", {})
                if best_oa_location:
                    # Try pdf_url first, fall back to landing_page_url
                    pdf_url = best_oa_location.get("pdf_url")
                    if pdf_url:
                        logger.info(
                            f"Found open access PDF for work {work_id}: {redact_url_for_log(pdf_url)}"
                        )
                        return str(pdf_url)

                    # Some works have landing page but no direct PDF
                    landing_url = best_oa_location.get("landing_page_url")
                    if landing_url:
                        logger.info(
                            f"Found landing page for work {work_id}: {redact_url_for_log(landing_url)}"
                        )
                        # Validate that landing page is actually a PDF before returning
                        try:
                            head_response = self.session.head(
                                landing_url,
                                timeout=self.timeout,
                                allow_redirects=True,
                            )
                            content_type = head_response.headers.get(
                                "Content-Type", ""
                            ).lower()
                            if "application/pdf" in content_type:
                                logger.info(
                                    f"Landing page is a direct PDF link for work {work_id}"
                                )
                                return str(landing_url)
                            logger.info(
                                f"Landing page is not a PDF (Content-Type: {content_type}), skipping"
                            )
                        except Exception as exc:
                            # Type only: urllib3 errors embed the scheme-less
                            # path and query, which the scrubber misses.
                            logger.warning(
                                f"Failed to validate landing page URL "
                                f"({type(exc).__name__}) for work {work_id}: "
                                f"{redact_url_for_log(landing_url)}"
                            )

                logger.info(
                    f"No PDF URL available for open access work {work_id}"
                )
                return None

            if response.status_code == 404:
                logger.warning(f"Work not found in OpenAlex: {work_id}")
                return None
            logger.warning(f"OpenAlex API error: {response.status_code}")
            return None

        except requests.exceptions.RequestException as exc:
            safe_msg = self._scrub(exc)
            logger.warning(f"Failed to query OpenAlex API: {safe_msg}")
            return None
        except ValueError as exc:
            # JSON decode errors are expected runtime errors
            safe_msg = self._scrub(exc)
            logger.warning(f"Failed to parse OpenAlex API response: {safe_msg}")
            return None
        # Note: KeyError and TypeError are not caught - they indicate programming
        # bugs that should propagate for debugging