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
"""
Open-Access DOI Downloader

Resolves a canonical DOI (https://doi.org/... or https://dx.doi.org/...) to an
open-access copy before falling back to the publisher landing page.

Closes the gap where a DOI record was treated as a direct PDF URL: the
publisher landing page (HTML) was misclassified as a paywall and no open-access
discovery was performed first. Uses OpenAlex, which aggregates OA locations
(publisher, PMC, arXiv, author manuscripts) into ``best_oa_location`` and the
per-location ``is_oa`` flag on ``locations``.

Classification of outcomes (drives retry vs. permanent via the skip reason text,
see ``library/download_management/failure_classifier.py``):
- OA PDF found and downloaded  -> success
- OA PDF found but download failed -> retryable (the copy exists)
- Confirmed not open access    -> permanent paywall (genuinely no free copy)
- Could not determine          -> retryable (API error / not in OpenAlex)
"""

from typing import Optional, Tuple
from urllib.parse import unquote, urlsplit

import requests
from loguru import logger

from ...security.ssrf_validator import redact_url_for_log
from ...utilities.resource_utils import safe_close
from .base import BaseDownloader, ContentType, DownloadResult


class OpenAccessDOIDownloader(BaseDownloader):
    """Resolve a DOI to an open-access PDF via OpenAlex before declaring paywall."""

    def __init__(
        self,
        timeout: int = 30,
        polite_pool_email: Optional[str] = None,
        **kwargs,
    ):
        """
        Initialize the DOI open-access downloader.

        Args:
            timeout: Request timeout in seconds
            polite_pool_email: Optional email for the OpenAlex polite pool
        """
        super().__init__(timeout, **kwargs)
        self.polite_pool_email = polite_pool_email
        self.base_api_url = "https://api.openalex.org"

    def can_handle(self, url: str) -> bool:
        """Handle canonical DOI URLs (doi.org / dx.doi.org)."""
        try:
            hostname = (urlsplit(url).hostname or "").lower()
        except (ValueError, AttributeError, TypeError):
            return False
        return hostname in ("doi.org", "dx.doi.org")

    def download(
        self, url: str, content_type: ContentType = ContentType.PDF
    ) -> Optional[bytes]:
        """Download content from a DOI URL."""
        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:
        """Resolve the DOI to an OA PDF and return it (or extracted text)."""
        doi = self._extract_doi(url)
        if not doi:
            return DownloadResult(skip_reason="Could not extract DOI from URL")

        pdf_url, status = self._resolve_oa_pdf(doi)

        if status == "unknown":
            # Could not determine OA status (API error / not in OpenAlex) -> retry.
            return DownloadResult(
                skip_reason="Could not resolve open access status for DOI (retry later)"
            )
        if status != "oa" or not pdf_url:
            # Confirmed not open access -> genuine paywall (permanent).
            return DownloadResult(
                skip_reason="No open access copy available (paywall)"
            )

        logger.info(
            "Downloading resolved open access PDF from {}",
            redact_url_for_log(pdf_url),
        )
        pdf_content = super()._download_pdf(pdf_url)
        if not pdf_content:
            # The OA copy exists but this download attempt failed -> retry.
            return DownloadResult(
                skip_reason="Open access PDF found but download failed"
            )

        if content_type == ContentType.PDF:
            return DownloadResult(content=pdf_content, is_success=True)

        # TEXT: extract from the OA PDF.
        text = self.extract_text_from_pdf(pdf_content)
        if text:
            return DownloadResult(content=text.encode("utf-8"), is_success=True)
        return DownloadResult(
            skip_reason="Open access PDF downloaded but text extraction failed"
        )

    @staticmethod
    def _extract_doi(url: str) -> Optional[str]:
        """Extract the DOI from a doi.org / dx.doi.org URL.

        ``urlsplit`` keeps a semicolon inside the path. ``urlparse`` would
        move a legacy SICI suffix such as ``;2-8`` into ``params`` and drop
        it. The query and fragment are removed before percent-decoding, so
        ``?query`` / ``#fragment`` stay out of the identifier while ``%3C``,
        ``%3E``, and ``%3B`` in the path become ``<``, ``>``, and ``;``.
        """
        try:
            parsed = urlsplit(url)
        except (ValueError, AttributeError, TypeError):
            return None
        hostname = (parsed.hostname or "").lower()
        if hostname not in ("doi.org", "dx.doi.org"):
            return None
        doi = unquote(parsed.path.lstrip("/")).strip()
        return doi or None

    def _resolve_oa_pdf(self, doi: str) -> Tuple[Optional[str], str]:
        """
        Resolve a DOI to an open-access PDF URL via OpenAlex.

        Returns:
            (pdf_url, status) where status is one of:
            - "oa": an OA PDF URL was found
            - "not_oa": OpenAlex confirms the work is not open access
            - "unknown": could not determine (API error / work not in OpenAlex)
        """
        try:
            # No `select`: the full work object is needed for the OA-locations
            # scan over `locations` (catches PMC / author-manuscript PDFs that
            # `best_oa_location` misses).
            params = {"filter": f"doi:{doi}"}
            headers = {}
            if self.polite_pool_email:
                headers["User-Agent"] = f"mailto:{self.polite_pool_email}"

            response = self.session.get(
                f"{self.base_api_url}/works",
                params=params,
                headers=headers,
                timeout=self.timeout,
            )

            try:
                if response.status_code != 200:
                    logger.warning(
                        "OpenAlex DOI lookup failed: HTTP {}",
                        response.status_code,
                    )
                    return None, "unknown"

                results = response.json().get("results", [])
            finally:
                safe_close(response, "OpenAlex DOI response")
            if not results:
                logger.info("DOI not found in OpenAlex")
                return None, "unknown"

            work = results[0]
            if not work.get("open_access", {}).get("is_oa", False):
                logger.info("DOI is not open access per OpenAlex")
                return None, "not_oa"

            # 1. Prefer the best OA location's direct PDF.
            best = work.get("best_oa_location") or {}
            if best.get("pdf_url"):
                return str(best["pdf_url"]), "oa"

            # 2. Scan all OA locations for a direct PDF (catches PMC, arXiv, AM).
            # OpenAlex returns `locations` (not `oa_locations`); filter by the
            # per-location `is_oa` flag.
            for loc in work.get("locations", []) or []:
                if loc and loc.get("is_oa") and loc.get("pdf_url"):
                    return str(loc["pdf_url"]), "oa"

            # 3. Fall back to a landing page only if it is actually a PDF.
            landing = best.get("landing_page_url")
            if landing:
                head = None
                try:
                    head = self.session.head(
                        landing,
                        timeout=self.timeout,
                        allow_redirects=True,
                    )
                    if (
                        "application/pdf"
                        in head.headers.get("Content-Type", "").lower()
                    ):
                        return str(landing), "oa"
                except (
                    requests.exceptions.RequestException,
                    ValueError,
                ) as exc:
                    logger.opt(exception=False).warning(
                        "Failed to validate open access landing page ({})",
                        type(exc).__name__,
                    )
                finally:
                    safe_close(head, "OpenAlex DOI landing response")

            # OA work but no retrievable PDF this time -> retryable (not a paywall).
            logger.info("DOI is open access but no direct PDF URL was found")
            return None, "unknown"

        except (requests.exceptions.RequestException, ValueError) as exc:
            logger.opt(exception=False).warning(
                "OpenAlex DOI lookup failed ({})", type(exc).__name__
            )
            return None, "unknown"