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.

3.6 KB
Raw Blame
"""Shared arXiv URL identifier parsing."""

import re
from typing import Final, NewType
from urllib.parse import urlparse

from .host_match import http_authority_host, is_host_or_subdomain

ArxivId = NewType("ArxivId", str)

_ID_PATTERN: Final = (
    r"(?:\d{4}\.\d{4,5}|[A-Za-z]+(?:-[A-Za-z]+)*(?:\.[A-Za-z]+)?/\d{7})"
    r"(?:v\d+)?"
)
_ARXIV_PATH: Final = re.compile(
    rf"^/(?:abs|pdf|html)/(?P<identifier>{_ID_PATTERN})(?:\.pdf)?/?$",
    re.IGNORECASE,
)
_AR5IV_DIRECT_PATH: Final = re.compile(
    rf"^/(?P<identifier>{_ID_PATTERN})(?:\.pdf)?/?$",
    re.IGNORECASE,
)
_ARXIV_FAMILY_DOMAINS: Final = ("arxiv.org", "ar5iv.org")


def _normalize_http_hostname(scheme: str, hostname: str | None) -> str | None:
    if scheme.lower() not in {"http", "https"} or hostname is None:
        return None
    return hostname.lower().rstrip(".")


def _normalized_http_hostname(url: str) -> str | None:
    try:
        parsed = urlparse(url)
    except ValueError:
        return None
    if parsed.scheme.lower() not in {"http", "https"}:
        return None
    return http_authority_host(parsed)


def is_arxiv_family_url(url: str) -> bool:
    """Return whether an HTTP(S) URL is on an arXiv-family host.

    This is host membership only, and the family includes the bare
    ``ar5iv.org`` domain, which arXiv does not serve. That makes it the right
    gate for deciding whether a *supplied* URL is worth parsing for an
    identifier (:func:`extract_arxiv_id` uses it for exactly that) and the
    wrong one for deciding whether a *response* came from arXiv: see
    ``ArxivDownloader._is_matching_arxiv_paper_url``, which deliberately
    tests the narrower ``.arxiv.org`` boundary so ``ar5iv.org`` can be an
    input identifier without ever being treated as authoritative.
    """
    hostname = _normalized_http_hostname(url)
    return hostname is not None and any(
        is_host_or_subdomain(hostname, domain)
        for domain in _ARXIV_FAMILY_DOMAINS
    )


def is_ar5iv_url(url: str) -> bool:
    """Return whether an HTTP(S) URL targets the ar5iv host family."""
    hostname = _normalized_http_hostname(url)
    return hostname is not None and is_host_or_subdomain(hostname, "ar5iv.org")


def is_arxiv_paper_url(url: str) -> bool:
    """Return whether an HTTP(S) URL identifies one specific arXiv paper.

    Host membership alone (:func:`is_arxiv_family_url`) is not enough to hand
    a URL to the arXiv downloader: ``arxiv.org/list/cs.AI/recent``,
    ``info.arxiv.org/help/...`` and ``arxiv.org/ftp/.../2301.12345.pdf`` are
    all on the family hosts but carry no identifier the downloader can act
    on, so they belong to the generic pipelines. Use this predicate for every
    routing and ownership decision. Response-URL trust is a separate and
    narrower question, answered by
    ``ArxivDownloader._is_matching_arxiv_paper_url`` rather than by either
    predicate here.
    """
    return extract_arxiv_id(url) is not None


def extract_arxiv_id(url: str) -> ArxivId | None:
    """Return a version-preserving ID from a supported arXiv-family URL."""
    try:
        parsed = urlparse(url)
        _ = parsed.port
        hostname = _normalize_http_hostname(parsed.scheme, parsed.hostname)
    except ValueError:
        return None

    if hostname is None:
        return None

    if not is_arxiv_family_url(url):
        return None
    is_ar5iv = is_ar5iv_url(url)

    match = _ARXIV_PATH.fullmatch(parsed.path)
    if match is None and is_ar5iv:
        match = _AR5IV_DIRECT_PATH.fullmatch(parsed.path)
    if match is None:
        return None
    return ArxivId(match.group("identifier"))