~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""
arXiv PDF and Text Downloader
"""
import re
from dataclasses import dataclass
from enum import StrEnum
from typing import Callable, Dict, Final, Iterator, Optional
from urllib.parse import urlparse
from bs4 import BeautifulSoup, NavigableString, PageElement, Tag
from loguru import logger
from ...security import redact_url_for_log
from ...constants import USER_AGENT
from ...security.egress.policy import PolicyDeniedError
from ...utilities.arxiv import extract_arxiv_id, is_arxiv_paper_url
from ...utilities.arxiv_api import arxiv_api_request_gate
from .base import ContentType, DownloadResult
from .html import HTMLDownloader
class ArxivTextSource(StrEnum):
"""Canonical source used to produce arXiv text."""
ARXIV_HTML = "arxiv_html"
LOCAL_PDF = "local_pdf"
ARXIV_API = "arxiv_api"
@dataclass(frozen=True, slots=True)
class ArxivTextResult:
"""Text paired with its exact arXiv production source."""
text: str
source: ArxivTextSource
def attribute_arxiv_text(text: str, arxiv_id: str) -> str:
"""Append the ``Source: https://arxiv.org/abs/<id>`` attribution line.
Applied to PDF and API metadata text (not to the HTML rendition), by
the downloader and by ``DownloadService`` for text it extracts from a
stored PDF, so text saved with the same provenance labels reads the
same whichever path produced it.
"""
return f"{text.rstrip()}\n\nSource: https://arxiv.org/abs/{arxiv_id}"
class ArxivFullTextStatus(StrEnum):
"""How a full-text-only fetch (``download_full_text``) ended."""
# Full text was produced (``ArxivFullTextOutcome.result`` holds it).
TEXT = "text"
# The PDF was downloaded but extraction produced no text: an
# image-only scan, or the extraction ceilings hit before any text.
# The download and the extraction were both paid for.
PDF_WITHOUT_TEXT = "pdf_without_text"
# No full text and no PDF bytes, and arXiv answered every request:
# the HTML leg found no usable rendition (404/410, a non-HTML answer,
# or a page that is not a rendition) and the PDF request was answered
# 404/410 or with something that is not a PDF. The paper has no full
# text to fetch, so a caller budgeting fetches can refund this one.
NOT_FETCHED = "not_fetched"
# No full text and no PDF bytes because a request failed rather than
# being answered: a timeout, a connection error, a 5xx (other than a
# PDF 503) or other unexpected status, or the decoded-body cap, on
# either leg. Nothing says the paper lacks full text; the host is
# failing, so a caller fetching several papers should stop sending it
# requests instead of paying a timeout per paper.
FETCH_FAILED = "fetch_failed"
# As FETCH_FAILED, but arXiv answered the PDF request with HTTP 429 or
# 503: the host is rate limiting or unavailable, so a caller fetching
# several papers should stop sending it requests. The PDF leg of
# ``download_full_text`` makes a single attempt and does not retry
# into the rate limit.
RATE_LIMITED = "rate_limited"
@dataclass(frozen=True, slots=True)
class ArxivFullTextOutcome:
"""Result of ``download_full_text``: the text, and how the fetch ended."""
status: ArxivFullTextStatus
result: ArxivTextResult | None = None
# arXiv asks harvesters to use this host, which carries an up-to-date copy
# of the corpus and is "specifically set aside for programmatic access"
# (https://info.arxiv.org/help/bulk_data.html), rather than arxiv.org,
# whose capacity is kept for interactive readers. It serves the same
# /html/{id} and /pdf/{id} paths. Automated callers (the search engine's
# full-text step) fetch from it; user-initiated library downloads keep
# arxiv.org.
ARXIV_EXPORT_HOST: Final = "export.arxiv.org"
# HTML must be at least this fraction of the PDF text already in hand before it
# is allowed to replace it. arXiv answers /html/{id} with a stub for papers that
# have no HTML rendition, and such a stub clears the extraction pipeline's 50
# character floor while carrying none of the paper.
MIN_HTML_TO_PDF_TEXT_RATIO = 0.5
# Absolute floor for HTML accepted with no PDF text to compare against.
# The ratio above only protects callers that already hold PDF bytes; the
# text-first paths (ContentFetcher.fetch, pipeline.fetch_and_extract, and
# download_service's first-ever text extraction) call in with none, and a
# stub clears the extraction pipeline's 50 character floor easily. The
# known stub extracts to ~260 characters and real LaTeXML renditions run to
# tens of thousands, so this sits about 8x above the former and an order of
# magnitude below the latter. It is a heuristic -- arXiv does not mark stub
# pages distinctly -- but failing it is cheap: download_text_with_source
# then falls through to downloading the PDF, which is exactly what these
# paths did before HTML-first existed.
MIN_STANDALONE_HTML_TEXT_LENGTH = 2000
# Upper bound on the number of <math> elements a rendition may carry before
# the TeX rewrite is abandoned in favour of the PDF. The rewrite itself is
# linear, nested <math> included (see _rewrite_math_to_tex, which rewrites
# only outermost elements), so this is not a complexity guard: it
# caps the work the rewrite can be made to do, and nothing more. It does not
# bound the BeautifulSoup parse that precedes it, whose cost scales with the
# page's total element count rather than its equation count and which every
# HTML download has always paid; the only limits there are the HTTP timeout
# and SafeSession's 1 GiB body cap. Real arXiv papers sit orders of magnitude
# below the bound -- a heavy mathematics paper runs to a few hundred
# equations -- so exceeding it marks a page the PDF serves better.
MAX_MATH_ELEMENTS = 5000
# MathML reaches these pages namespace-prefixed as often as not, and lxml's
# HTML parser keeps the prefix in the tag name (``<m:math>`` parses to a tag
# named "m:math"). A literal "math" match therefore finds nothing on such a
# page and the rendition is accepted with its MathML left in place. What
# the extractors then make of the raw MathML depends on the page rather than
# on which extractor wins the tiebreak: either one may drop the element, so
# the equation is missing from the stored text (with no PDF fallback once
# the text clears the standalone floor), or emit its text nodes glued to the
# TeX ("x\\frac{1}{2}" for an equation whose visible symbol is "x").
# Matching on the local name rewrites both spellings, so the stored text is
# the clean TeX either way.
_MATHML_LOCAL_NAME: Final = re.compile(r"(?:^|:)math$", re.IGNORECASE)
_TEX_ANNOTATION_LOCAL_NAME: Final = re.compile(
r"(?:^|:)annotation$", re.IGNORECASE
)
# The visible-text tier of the ladder below must not read annotation
# markup: <annotation> children of other encodings (application/x-asy, ...)
# and <annotation-xml> (Content MathML) carry hidden machine-readable
# state, not rendered characters. Anchored and suffixed so a literal
# "annotation" matches, "annotation-xml" matches, and nothing longer does.
_HIDDEN_ANNOTATION_LOCAL_NAME: Final = re.compile(
r"(?:^|:)annotation(?:-xml)?$", re.IGNORECASE
)
class ArxivDownloader(HTMLDownloader):
"""Download arXiv PDFs and produce text from canonical arXiv sources."""
# Set per instance by __init__; the class default covers instances
# built without it (test doubles, ``__new__``).
_fetch_host: str = "arxiv.org"
def __init__(
self,
timeout: int = 30,
language: str = "English",
*,
fetch_host: str = "arxiv.org",
**kwargs,
):
"""``fetch_host`` is the host the HTML and PDF legs request from.
It is ``arxiv.org`` or ``ARXIV_EXPORT_HOST``; any other value is
rejected. Attribution (``Source: https://arxiv.org/abs/...``) and
the response checks are independent of it: both hosts sit inside
the ``.arxiv.org`` trust boundary ``_is_matching_arxiv_paper_url``
applies.
"""
if fetch_host not in ("arxiv.org", ARXIV_EXPORT_HOST):
raise ValueError("fetch_host must be arxiv.org or export.arxiv.org")
self._fetch_host = fetch_host
super().__init__(timeout=timeout, language=language, **kwargs)
session = self.session
if session is not None:
session.headers.update({"User-Agent": USER_AGENT})
def can_handle(self, url: str) -> bool:
"""Check if URL names a specific arXiv paper.
Host membership is not enough. Every entry point below needs an
identifier to build a canonical URL from, so a family URL without one
-- a listing, an author page, or the legacy ``/ftp/`` PDF layout --
would be claimed only to be skipped, starving the downloader that can
actually serve it (``DirectPDFDownloader`` for ``/ftp/`` PDFs).
"""
return is_arxiv_paper_url(url)
def download(
self, url: str, content_type: ContentType = ContentType.PDF
) -> Optional[bytes]:
"""Download content from arXiv."""
if content_type == ContentType.TEXT:
text = self.download_text(url)
return text.encode("utf-8", errors="ignore") if text else None
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."""
# Extract arXiv ID
arxiv_id = self._extract_arxiv_id(url)
if not arxiv_id:
return DownloadResult(
skip_reason="Invalid arXiv URL - could not extract article ID"
)
if content_type == ContentType.TEXT:
result = self.download_text_with_source(url)
if result and result.text:
return DownloadResult(
content=result.text.encode("utf-8", errors="ignore"),
is_success=True,
content_is_complete=result.source
!= ArxivTextSource.ARXIV_API,
)
return DownloadResult(
skip_reason=f"Could not retrieve full text for arXiv:{arxiv_id}"
)
# Download PDF
pdf_url = self._pdf_url(arxiv_id)
logger.info(f"Downloading arXiv PDF: {arxiv_id}")
pdf_content = super()._download_pdf(pdf_url)
if pdf_content:
return DownloadResult(content=pdf_content, is_success=True)
return DownloadResult(
skip_reason=f"Failed to download PDF for arXiv:{arxiv_id} - server may be unavailable"
)
def _download_pdf(
self,
url: str,
headers: Optional[Dict[str, str]] = None,
*,
max_attempts: int = 3,
on_rate_limited: Optional[Callable[[], None]] = None,
on_transport_failure: Optional[Callable[[], None]] = None,
) -> Optional[bytes]:
"""Download PDF from arXiv.
``max_attempts``, ``on_rate_limited`` and ``on_transport_failure``
are passed through to ``BaseDownloader._download_pdf``.
"""
# Extract arXiv ID
arxiv_id = self._extract_arxiv_id(url)
if not arxiv_id:
logger.error(
f"Could not extract arXiv ID from {redact_url_for_log(url)}"
)
return None
# Construct PDF URL
pdf_url = self._pdf_url(arxiv_id)
logger.info(f"Downloading arXiv PDF: {arxiv_id}")
# Use honest user agent - arXiv supports academic tools with proper identification
enhanced_headers = {
"User-Agent": USER_AGENT,
"Accept": "application/pdf,application/octet-stream,*/*",
"Accept-Language": "en-US,en;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
}
return super()._download_pdf(
pdf_url,
headers=enhanced_headers,
max_attempts=max_attempts,
on_rate_limited=on_rate_limited,
on_transport_failure=on_transport_failure,
)
def download_text(
self, url: str, pdf_content: bytes | None = None
) -> str | None:
"""Return official HTML, PDF text, or API metadata for an arXiv URL.
Supplied PDF bytes are reused even when empty. When they are omitted,
the canonical arXiv PDF is downloaded once. ar5iv URLs are accepted
only for identifier normalization and are never fetched directly.
"""
result = self.download_text_with_source(url, pdf_content=pdf_content)
return result.text if result is not None else None
def _pdf_url(self, arxiv_id: str) -> str:
"""The PDF URL on ``fetch_host``.
``export.arxiv.org`` answers ``/pdf/{id}.pdf`` with a 301 to the
suffix-less path, so on that host the suffix is left off to avoid
an unnecessary redirect. (``SafeSession.resolve_redirects`` rejects
an oversized redirect ``Content-Length`` and discards redirect bodies
before Requests can buffer them.) ``arxiv.org`` keeps the URL it has
always been sent.
"""
if self._fetch_host == ARXIV_EXPORT_HOST:
return f"https://{ARXIV_EXPORT_HOST}/pdf/{arxiv_id}"
return f"https://arxiv.org/pdf/{arxiv_id}.pdf"
def download_text_with_source(
self, url: str, pdf_content: bytes | None = None
) -> ArxivTextResult | None:
"""Return arXiv text with the exact source that produced it.
Transport failures on every leg are handled here, not raised: a
paper whose HTML and PDF legs both fail yields the API metadata
(``ArxivTextSource.ARXIV_API``) or ``None``.
"""
arxiv_id = self._extract_arxiv_id(url)
if not arxiv_id:
return None
result, _ = self._html_then_pdf_text(url, arxiv_id, pdf_content)
if result is not None:
return result
api_text = self._fetch_from_arxiv_api(arxiv_id)
if api_text:
return ArxivTextResult(
attribute_arxiv_text(api_text, arxiv_id),
ArxivTextSource.ARXIV_API,
)
return None
def download_full_text(self, url: str) -> ArxivFullTextOutcome:
"""Return full text only (HTML rendition, then PDF), and how it ended.
For callers that already hold the abstract: the API metadata leg is
never requested, because it yields only the abstract. The status
tells the ways a fetch can end without text apart, so a caller
budgeting fetches can refund only a paper that has no full text:
``NOT_FETCHED`` when arXiv answered that there is none (no
rendition, and a PDF request answered 404/410 or with no PDF),
``FETCH_FAILED`` when a request failed instead of being answered
(timeout, connection error, a failed DNS lookup or other refusal by
the URL validator, 5xx, the decoded-body cap, any other request
error, on either leg), ``RATE_LIMITED`` for a PDF answered 429/503, and
``PDF_WITHOUT_TEXT`` for a PDF that downloaded but yielded no text
(the download and extraction were paid for). A URL naming no paper
is ``NOT_FETCHED``.
The PDF leg makes a single attempt: it is not retried on HTTP
429/503, timeouts or connection errors, so a fetch costs at most
two requests (HTML, then PDF). ``FETCH_FAILED`` and
``RATE_LIMITED`` tell a caller fetching several papers to stop
instead of sending a failing host a request (and a timeout) per
paper. Those give-ups are logged as warnings, not errors.
"""
arxiv_id = self._extract_arxiv_id(url)
if not arxiv_id:
return ArxivFullTextOutcome(ArxivFullTextStatus.NOT_FETCHED)
rate_limited = False
transport_failed = False
def _note_rate_limited() -> None:
nonlocal rate_limited
rate_limited = True
def _note_transport_failure() -> None:
nonlocal transport_failed
transport_failed = True
result, pdf_in_hand = self._html_then_pdf_text(
url,
arxiv_id,
None,
pdf_max_attempts=1,
on_pdf_rate_limited=_note_rate_limited,
on_transport_failure=_note_transport_failure,
)
if result is not None:
return ArxivFullTextOutcome(ArxivFullTextStatus.TEXT, result)
if pdf_in_hand:
return ArxivFullTextOutcome(ArxivFullTextStatus.PDF_WITHOUT_TEXT)
if rate_limited:
return ArxivFullTextOutcome(ArxivFullTextStatus.RATE_LIMITED)
if transport_failed:
return ArxivFullTextOutcome(ArxivFullTextStatus.FETCH_FAILED)
return ArxivFullTextOutcome(ArxivFullTextStatus.NOT_FETCHED)
def _html_then_pdf_text(
self,
url: str,
arxiv_id: str,
pdf_content: bytes | None,
*,
pdf_max_attempts: int | None = None,
on_pdf_rate_limited: Callable[[], None] | None = None,
on_transport_failure: Callable[[], None] | None = None,
) -> tuple[ArxivTextResult | None, bool]:
"""Produce text from the HTML rendition, then the PDF.
Returns the text (or ``None``) and whether PDF bytes were in hand,
supplied or downloaded, whether or not they yielded any text.
``pdf_max_attempts`` and ``on_pdf_rate_limited`` are forwarded to
``_download_pdf`` when the PDF is downloaded; left unset, the PDF
download keeps its default retries. ``on_transport_failure`` is
forwarded to the HTML fetch and, with ``pdf_max_attempts``, to the
PDF download.
"""
# PDF bytes already in hand set the quality floor for HTML, so their
# text is extracted before HTML is considered.
pdf_text = (
self.extract_text_from_pdf(pdf_content)
if pdf_content is not None
else None
)
pdf_in_hand = pdf_content is not None
# HTML is fetched even when PDF bytes are already in hand: preferring
# the official HTML rendition is the point of this path, and the PDF
# text above is what sets the quality floor it has to clear
# (_html_text_beats_pdf_text). That costs one request the PDF-only
# path did not make; the caller's retry budget is what bounds it.
if on_transport_failure is None:
html_text = self._download_html_text(arxiv_id)
else:
html_text = self._download_html_text(
arxiv_id, on_transport_failure=on_transport_failure
)
if html_text and self._html_text_beats_pdf_text(
html_text, pdf_text, arxiv_id
):
return (
ArxivTextResult(html_text, ArxivTextSource.ARXIV_HTML),
pdf_in_hand,
)
if pdf_content is None:
logger.info(f"Downloading arXiv PDF for full text: {arxiv_id}")
if pdf_max_attempts is None:
downloaded_pdf = self._download_pdf(url)
else:
downloaded_pdf = self._download_pdf(
url,
max_attempts=pdf_max_attempts,
on_rate_limited=on_pdf_rate_limited,
on_transport_failure=on_transport_failure,
)
if downloaded_pdf is not None:
pdf_in_hand = True
pdf_text = self.extract_text_from_pdf(downloaded_pdf)
if pdf_text:
return (
ArxivTextResult(
attribute_arxiv_text(pdf_text, arxiv_id),
ArxivTextSource.LOCAL_PDF,
),
pdf_in_hand,
)
return None, pdf_in_hand
def _download_html_text(
self,
arxiv_id: str,
*,
on_transport_failure: Callable[[], None] | None = None,
) -> str | None:
"""Extract normalized text from official arXiv HTML when usable.
``on_transport_failure`` is forwarded to the HTML fetch (see
``HTMLDownloader._fetch_html_with_final_url``).
"""
html_url = f"https://{self._fetch_host}/html/{arxiv_id}"
# Single-shot HTML leg: the full-text flow implements its own
# backoff/circuit-breaking across papers (NOT_FETCHED vs
# FETCH_FAILED accounting), so the generic fetch must not
# retry timeouts/429s underneath it and triple the per-paper
# cost against a rate-limiting host.
if on_transport_failure is None:
html, final_url = self._fetch_html_with_final_url(
html_url, max_attempts=1
)
else:
html, final_url = self._fetch_html_with_final_url(
html_url,
on_transport_failure=on_transport_failure,
max_attempts=1,
)
if not html:
return None
if not self._stayed_on_arxiv_html(final_url, arxiv_id):
logger.info(
"arXiv HTML request for {} redirected to {}; not a rendition",
arxiv_id,
redact_url_for_log(final_url),
)
return None
try:
soup = BeautifulSoup(html, "lxml")
if not self._rewrite_math_to_tex(soup, arxiv_id):
return None
canonical_url = f"https://arxiv.org/abs/{arxiv_id}"
extracted = self._extract_content(str(soup), canonical_url)
if not extracted:
return None
text = self._format_extracted_content(extracted)
return text if text.strip() else None
except Exception:
logger.warning(
"arXiv HTML unusable; trying fallbacks: {}", arxiv_id
)
return None
@staticmethod
def _tex_for_math_element(math: Tag) -> str:
"""The TeX one <math> element carries, or ``""`` when it carries none.
Four sources, each a strictly weaker guarantee than the one before.
An ``<annotation encoding="application/x-tex">`` child first because
it is the explicit one; then an
``<annotation encoding="application/x-tex+html">`` child, the same
TeX with the HTML LaTeXML kept around it; then the ``alttext``
attribute LaTeXML always writes onto the element itself; and finally
the element's visible MathML text, read with the annotation
subtrees skipped so annotation state is never mistaken for rendered
characters, so a bare unannotated equation
still yields its rendered characters instead of its annotation state
(#4783). Only annotations are removed: presentation elements that
lay out without drawing, such as ``<mphantom>``, keep their text in
this tier. An empty source falls through rather than abandoning the
page -- an element can carry an empty annotation and a usable one
beside it, or an empty annotation and a usable attribute, and there
is no reason to prefer the empty one. Every annotation of each
encoding is examined for that reason, not just the first descendant
-- but only the outermost of a nested run: an annotation nested in a
same-encoding annotation contributes a subset of its ancestor's
text, so once the ancestor reads as empty every annotation inside it
does too. Reading only outermost matches keeps their subtrees
disjoint, so each encoding's search, text extraction included, is
one linear pass over the element; re-reading every level of a
nested chain would be quadratic in its depth, and this HTML is
author-controlled.
``<annotation-xml>`` is deliberately not a source: it holds a
different encoding, the anchored matcher above already excludes it,
and the visible-text tier skips it — along with every other
non-TeX ``<annotation>`` — in the text it reads, so its hidden
text can neither supply an empty equation nor pollute a rendered
one. A node whose only text is annotation text reads as empty and
takes the PDF exit.
"""
for encoding in ("application/x-tex", "application/x-tex+html"):
for annotation in ArxivDownloader._outermost_tex_annotations(
math, encoding
):
tex = annotation.get_text().strip()
if tex:
return tex
alttext = math.get("alttext")
if isinstance(alttext, str) and alttext.strip():
return alttext.strip()
return ArxivDownloader._visible_math_text(math)
@staticmethod
def _outermost_tex_annotations(math: Tag, encoding: str) -> Iterator[Tag]:
"""``<annotation encoding=...>`` descendants with no such ancestor.
Yielded lazily in document order. One iterative walk that does not
descend into a match, so every node is visited at most once and the
yielded subtrees are disjoint -- reading each one's text is linear
in the element's size in total, however deeply matches nest.
"""
stack: list[Tag] = [
child for child in reversed(math.contents) if isinstance(child, Tag)
]
while stack:
node = stack.pop()
if (
_TEX_ANNOTATION_LOCAL_NAME.search(node.name or "")
and node.get("encoding") == encoding
):
yield node
continue
stack.extend(
child
for child in reversed(node.contents)
if isinstance(child, Tag)
)
@staticmethod
def _visible_math_text(math: Tag) -> str:
"""``math.get_text(" ", strip=True)`` minus annotation subtrees.
Hidden machine-readable state -- Content MathML inside
``<annotation-xml>``, other encodings inside ``<annotation>`` -- is
never emitted as the equation's rendered characters. One iterative
walk that skips those subtrees, so each node is visited once and the
tree is left untouched. (A ``copy.deepcopy`` of the element with the
annotations decomposed gives the same text, but bs4's deepcopy
re-walks the copy on every insert, which is quadratic in the depth
of nested markup.) Strings are filtered by the same types
``get_text`` keeps, so comments and the like stay out.
"""
types = math.interesting_string_types or Tag.MAIN_CONTENT_STRING_TYPES
parts: list[str] = []
stack: list[PageElement] = list(reversed(math.contents))
while stack:
node = stack.pop()
if isinstance(node, Tag):
if _HIDDEN_ANNOTATION_LOCAL_NAME.search(node.name or ""):
continue
stack.extend(reversed(node.contents))
elif isinstance(node, NavigableString) and type(node) in types:
stripped = node.strip()
if stripped:
parts.append(stripped)
return " ".join(parts)
@staticmethod
def _rewrite_math_to_tex(soup: BeautifulSoup, arxiv_id: str) -> bool:
"""Replace every <math> element with its TeX annotation, in place.
Returns False when the rendition must be abandoned for the PDF. Two
distinct cases do that, each logged at debug so they are told apart in
a report: the page carries more than MAX_MATH_ELEMENTS equations, or a
<math> offers no TeX at all.
"No TeX at all" is narrower than it used to be. The annotation child
is not the only place LaTeXML writes the TeX -- it always writes it
into the ``alttext`` attribute as well, while the parallel
<annotation> markup depends on how arXiv invokes it, so a rendition
carrying only ``alttext`` used to take the PDF exit with its TeX
sitting right there on the element (#6414). Both sources are read
now, annotation first. Partially annotated renditions go further and
mix TeX-annotated equations with bare MathML (#4783): the recovery
ladder reads x-tex annotations, then x-tex+html ones, then
``alttext``, then the element's visible MathML text, so a node kills
the page only when it is genuinely empty -- no annotation, no
attribute, and no rendered characters to keep.
Each element is rewritten in place -- renamed to an inline <span>
holding the TeX -- rather than with ``Tag.replace_with``. replace_with
locates the element with a linear scan of its parent's contents, so
rewriting n equations that share one parent costs O(n^2); arXiv
renders this HTML from author-submitted LaTeX, and it is reached on
the search path. Rewriting in place is linear and keeps bs4's own
escaping on serialization.
Only outermost <math> elements are rewritten. Each one's TeX comes
from its own subtree (the annotation search, and the visible-text
tier's walk), so a <math> nested inside another is covered by its
ancestor and is discarded with the ancestor's children. Visiting
the nested ones as well would re-read every level of a nested
chain, making the rewrite quadratic in the nesting depth (nested
<math> is not valid MathML, but this HTML is author-controlled).
Outermost elements have disjoint subtrees, so the total work is
linear in the page size.
"""
math_elements = soup.find_all(_MATHML_LOCAL_NAME)
if len(math_elements) > MAX_MATH_ELEMENTS:
logger.debug(
"arXiv HTML for {} carries {} <math> elements, above the "
"{} element bound; falling back to the PDF",
arxiv_id,
len(math_elements),
MAX_MATH_ELEMENTS,
)
return False
for math in ArxivDownloader._outermost_math_elements(soup):
tex = ArxivDownloader._tex_for_math_element(math)
if not tex:
logger.debug(
"arXiv HTML for {} has a <math> element carrying no "
"annotation of either TeX encoding, no alttext "
"attribute, and no visible MathML text; falling back "
"to the PDF",
arxiv_id,
)
return False
math.name = "span"
math.attrs.clear()
math.clear()
math.append(NavigableString(tex))
return True
@staticmethod
def _outermost_math_elements(soup: BeautifulSoup) -> list[Tag]:
"""<math> elements with no <math> ancestor, in document order.
One iterative walk that does not descend into a matched element,
so every node is visited at most once.
"""
outermost: list[Tag] = []
# Children are pushed in reverse so they pop in document order.
stack: list[Tag] = [
child for child in reversed(soup.contents) if isinstance(child, Tag)
]
while stack:
node = stack.pop()
if _MATHML_LOCAL_NAME.search(node.name or ""):
outermost.append(node)
continue
stack.extend(
child
for child in reversed(node.contents)
if isinstance(child, Tag)
)
return outermost
@staticmethod
def _is_matching_arxiv_paper_url(
response_url: str | None, arxiv_id: str
) -> bool:
"""Require an official URL for the requested paper and version.
An unversioned request may resolve to a versioned rendition of the
same paper. An explicit version must remain exact. The host test is
the ``.arxiv.org`` trust boundary, so ``ar5iv.labs.arxiv.org`` -- the
host the real ar5iv service runs on -- is inside it and accepted,
while the bare ``ar5iv.org`` domain is not: it is an input identifier
only and is never treated as an authoritative response. That is also
why this does not reuse ``is_arxiv_family_url``, whose family takes
in ``ar5iv.org``: widening the boundary to match it would accept a
response from a host arXiv does not serve.
"""
if not response_url:
return False
try:
parsed = urlparse(response_url)
hostname = (parsed.hostname or "").lower().rstrip(".")
expected_port = {"https": 443, "http": 80}.get(parsed.scheme)
if (
expected_port is None
or parsed.port not in {None, expected_port}
or parsed.username is not None
or parsed.password is not None
or not (
hostname == "arxiv.org" or hostname.endswith(".arxiv.org")
)
):
return False
except ValueError:
return False
actual_id = extract_arxiv_id(response_url)
if actual_id is None:
return False
if re.search(r"v\d+$", arxiv_id, re.IGNORECASE):
return actual_id.casefold() == arxiv_id.casefold()
return (
re.sub(r"v\d+$", "", actual_id, flags=re.IGNORECASE).casefold()
== arxiv_id.casefold()
)
@classmethod
def _stayed_on_arxiv_html(
cls, final_url: str | None, arxiv_id: str
) -> bool:
"""Accept HTTPS HTML only for the requested arXiv paper/version."""
if not final_url or not cls._is_matching_arxiv_paper_url(
final_url, arxiv_id
):
return False
parsed = urlparse(final_url)
return parsed.scheme == "https" and parsed.path.startswith("/html/")
def _html_text_beats_pdf_text(
self, html_text: str, pdf_text: str | None, arxiv_id: str
) -> bool:
"""Report whether HTML text may replace PDF text already extracted.
With no PDF text in hand there is nothing to compare against, so the
ratio cannot help; an absolute floor stands in for it. Returning True
unconditionally here is what let a stub win on every text-first path.
"""
if not pdf_text:
if len(html_text) >= MIN_STANDALONE_HTML_TEXT_LENGTH:
return True
logger.warning(
"arXiv HTML for {} yielded only {} characters and no PDF text "
"is available to compare against; below the {} character floor "
"for standalone HTML, so falling back to the PDF",
arxiv_id,
len(html_text),
MIN_STANDALONE_HTML_TEXT_LENGTH,
)
return False
if len(html_text) >= MIN_HTML_TO_PDF_TEXT_RATIO * len(pdf_text):
return True
logger.warning(
"arXiv HTML for {} yielded only {} characters against {} "
"characters of PDF text; keeping the PDF text",
arxiv_id,
len(html_text),
len(pdf_text),
)
return False
def _extract_arxiv_id(self, url: str) -> Optional[str]:
"""Extract arXiv ID from URL."""
return extract_arxiv_id(url)
def get_metadata(self, url: str) -> dict[str, str]:
"""Return canonical arXiv attribution without making a request."""
arxiv_id = self._extract_arxiv_id(url)
if arxiv_id is None:
return {}
return {"url": f"https://arxiv.org/abs/{arxiv_id}"}
def _fetch_from_arxiv_api(self, arxiv_id: str) -> Optional[str]:
"""Fetch abstract and metadata from arXiv API."""
engine_type = "arxiv_api"
session = self.session
if session is None:
self.rate_tracker.record_outcome(
engine_type=engine_type,
wait_time=0.0,
success=False,
retry_count=1,
error_type="SessionUnavailable",
)
return None
adaptive_wait = self.rate_tracker.apply_rate_limit(engine_type)
api_text: str | None = None
error_type: str | None = None
try:
with arxiv_api_request_gate():
response = session.get(
"https://export.arxiv.org/api/query",
params={"id_list": arxiv_id},
timeout=10,
allow_redirects=False,
)
if response.status_code != 200:
error_type = f"HTTP_{response.status_code}"
else:
# Parse the Atom feed response
# Use defusedxml to prevent XXE attacks
from defusedxml import ElementTree as ET
root = ET.fromstring(response.text)
# Define namespaces (URIs are identifiers, not URLs to fetch)
ns = {
"atom": "http://www.w3.org/2005/Atom", # DevSkim: ignore DS137138
"arxiv": "http://arxiv.org/schemas/atom", # DevSkim: ignore DS137138
}
# Find the entry
entry = root.find("atom:entry", ns)
if entry is not None:
entry_id = entry.find("atom:id", ns)
response_url = (
entry_id.text.strip()
if entry_id is not None and entry_id.text
else None
)
if (
response_url is None
or not self._is_matching_arxiv_paper_url(
response_url, arxiv_id
)
or not urlparse(response_url).path.startswith("/abs/")
):
entry = None
if entry is not None:
# Extract text content
text_parts: list[str] = []
# Title
title = entry.find("atom:title", ns)
if title is not None and title.text:
title_text = title.text.strip()
if title_text:
text_parts.append(f"Title: {title_text}")
# Authors
authors = entry.findall("atom:author", ns)
if authors:
author_names: list[str] = []
for author in authors:
name = author.find("atom:name", ns)
if name is not None and name.text:
author_name = name.text.strip()
if author_name:
author_names.append(author_name)
if author_names:
text_parts.append(
f"Authors: {', '.join(author_names)}"
)
# Abstract
summary = entry.find("atom:summary", ns)
if summary is not None and summary.text:
summary_text = summary.text.strip()
if summary_text:
text_parts.append(f"\nAbstract:\n{summary_text}")
# Categories
categories = entry.findall("atom:category", ns)
if categories:
cat_terms: list[str] = []
for category in categories:
term = category.get("term")
if term and term.strip():
cat_terms.append(term.strip())
if cat_terms:
text_parts.append(
f"\nCategories: {', '.join(cat_terms)}"
)
if text_parts:
api_text = "\n".join(text_parts)
if api_text is None:
error_type = "UnusableFeed"
except PolicyDeniedError:
raise
except Exception as e:
logger.debug(f"Failed to fetch from arXiv API ({type(e).__name__})")
error_type = type(e).__name__
if api_text is not None:
self.rate_tracker.record_outcome(
engine_type=engine_type,
wait_time=adaptive_wait,
success=True,
retry_count=1,
search_result_count=1,
)
logger.info(f"Retrieved text content from arXiv API for {arxiv_id}")
return api_text
self.rate_tracker.record_outcome(
engine_type=engine_type,
wait_time=adaptive_wait,
success=False,
retry_count=1,
error_type=error_type,
)
return None