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.1 KB
Raw Blame
"""
URL Classifier for content fetching.

Determines the type of URL to route to the appropriate downloader.
"""

import re
from enum import Enum
from typing import Optional
from urllib.parse import ParseResult, urlparse

from ..utilities.arxiv import (
    extract_arxiv_id as extract_arxiv_id_from_url,
    is_arxiv_paper_url,
)
from ..utilities.host_match import http_authority_host, is_host_or_subdomain


class URLType(Enum):
    """Types of URLs we can handle."""

    ARXIV = "arxiv"
    PUBMED = "pubmed"
    PMC = "pmc"  # PubMed Central
    SEMANTIC_SCHOLAR = "semantic_scholar"
    BIORXIV = "biorxiv"
    MEDRXIV = "medrxiv"
    DOI = "doi"
    PDF = "pdf"  # Direct PDF link
    HTML = "html"  # Generic web page
    INVALID = "invalid"  # Dangerous/unsupported URL schemes


# URL schemes that should be rejected (security risk)
DANGEROUS_SCHEMES = {"javascript", "data", "file", "vbscript", "about"}


class URLClassifier:
    """Classifies URLs to determine the appropriate downloader."""

    # Each source is a list of (host, path) pairs: the URL's host must be that
    # host or a subdomain of it, and its path must start with the pattern.
    # Both are read from the parsed URL. The patterns used to be searched for
    # anywhere in the URL string, which also found them in another site's
    # path or query (https://evil.example/doi.org/10.1234/x) and inside
    # look-alike hosts (fakedoi.org), so such a URL was routed to the source's
    # downloader and cited under its trusted tag (#6835).
    # Note: paths are matched lowercased, as the whole URL was before.

    # PubMed patterns
    PUBMED_PATTERNS = [
        ("pubmed.ncbi.nlm.nih.gov", r"/\d+"),
        ("ncbi.nlm.nih.gov", r"/pubmed/\d+"),
    ]

    # PMC patterns (PubMed Central - full text)
    PMC_PATTERNS = [
        ("ncbi.nlm.nih.gov", r"/pmc/articles/pmc"),
        ("europepmc.org", r"/article/pmc"),
        ("europepmc.org", r"/articles/pmc"),
    ]

    # Semantic Scholar patterns
    SEMANTIC_SCHOLAR_PATTERNS = [
        ("semanticscholar.org", r"/paper/"),
        ("api.semanticscholar.org", r"/"),
    ]

    # BioRxiv/MedRxiv patterns
    BIORXIV_PATTERNS = [
        ("biorxiv.org", r"/content/"),
    ]

    MEDRXIV_PATTERNS = [
        ("medrxiv.org", r"/content/"),
    ]

    # DOI patterns (dx.doi.org is a subdomain of doi.org)
    DOI_PATTERNS = [
        ("doi.org", r"/10\."),
    ]

    @classmethod
    def classify(cls, url: str) -> URLType:
        """
        Classify a URL to determine its type.

        Args:
            url: The URL to classify

        Returns:
            URLType enum indicating the type of content
        """
        url_lower = url.lower().strip()

        # Check for dangerous URL schemes first (security)
        try:
            parsed = urlparse(url_lower)
            if parsed.scheme in DANGEROUS_SCHEMES:
                return URLType.INVALID
            # Only allow http/https schemes
            if parsed.scheme and parsed.scheme not in ("http", "https", ""):
                return URLType.INVALID
        except Exception:
            return URLType.INVALID

        # Only a URL that names one paper belongs to arXiv. Other pages on
        # the family hosts (listings, author pages, info./blog./static.
        # subdomains, the legacy /ftp/ PDF layout) carry no identifier the
        # arXiv downloader can act on, and ARXIV ownership is terminal for
        # every fetch entry point, so they stay with the generic pipelines.
        if is_arxiv_paper_url(url.strip()):
            return URLType.ARXIV

        # Check direct PDF links after arXiv-family routing
        if cls._is_pdf_url(url_lower):
            return URLType.PDF

        # Check specific academic sources
        host, path = cls._host_and_path(parsed, url_lower)
        for url_type, patterns in (
            (URLType.PMC, cls.PMC_PATTERNS),
            (URLType.PUBMED, cls.PUBMED_PATTERNS),
            (URLType.SEMANTIC_SCHOLAR, cls.SEMANTIC_SCHOLAR_PATTERNS),
            (URLType.BIORXIV, cls.BIORXIV_PATTERNS),
            (URLType.MEDRXIV, cls.MEDRXIV_PATTERNS),
            (URLType.DOI, cls.DOI_PATTERNS),
        ):
            for domain, path_pattern in patterns:
                if is_host_or_subdomain(host, domain) and re.match(
                    path_pattern, path
                ):
                    return url_type

        # Default to HTML for web pages
        return URLType.HTML

    @staticmethod
    def _host_and_path(parsed: ParseResult, url_lower: str) -> tuple[str, str]:
        """The host and path the source patterns are matched against.

        A URL without a scheme ("doi.org/10.1234/x"), or with too few slashes
        after it ("https:/doi.org/10.1234/x"), passes the scheme check in
        ``classify``, and ``urlparse`` reads its host as part of the path. So
        what follows the scheme is read again as a network-path reference,
        which is how browsers read it. An authority the HTTP stack would read
        differently has no host (``http_authority_host``) and matches nothing.
        """
        if not parsed.netloc:
            rest = url_lower.split(":", 1)[1] if parsed.scheme else url_lower
            try:
                parsed = urlparse("//" + rest.lstrip("/"))
            except ValueError:
                return "", ""
        return http_authority_host(parsed) or "", parsed.path

    @classmethod
    def _is_pdf_url(cls, url: str) -> bool:
        """Check if URL points directly to a PDF.

        Note: Academic source PDFs (arXiv, bioRxiv, etc.) are handled
        by their respective patterns, not as generic PDFs.
        """
        url_lower = url.lower()

        # Academic sources with PDF URLs should use their specialized downloaders
        academic_domains = [
            "arxiv.org",
            "biorxiv.org",
            "medrxiv.org",
            "ncbi.nlm.nih.gov",
            "europepmc.org",
            "semanticscholar.org",
        ]
        if any(domain in url_lower for domain in academic_domains):
            return False

        parsed = urlparse(url_lower)
        path = parsed.path

        # Check file extension
        if path.endswith(".pdf"):
            return True

        # Check common PDF URL patterns
        if "/pdf/" in path:
            return True

        return False

    @classmethod
    def extract_id(
        cls, url: str, url_type: Optional[URLType] = None
    ) -> Optional[str]:
        """
        Extract the identifier from a URL.

        Args:
            url: The URL to extract from
            url_type: Optional pre-classified type

        Returns:
            Extracted ID or None
        """
        if url_type is None:
            url_type = cls.classify(url)

        if url_type == URLType.ARXIV:
            return extract_arxiv_id_from_url(url)

        if url_type == URLType.PUBMED:
            # Extract PMID
            match = re.search(r"/(\d+)/?", url)
            if match:
                return match.group(1)

        elif url_type == URLType.PMC:
            # Extract PMC ID
            match = re.search(r"PMC(\d+)", url, re.IGNORECASE)
            if match:
                return f"PMC{match.group(1)}"

        elif url_type == URLType.SEMANTIC_SCHOLAR:
            # Extract paper ID (40-char hex)
            match = re.search(
                r"/paper/(?:[^/]+/)?([a-f0-9]{40})", url, re.IGNORECASE
            )
            if match:
                return match.group(1)

        elif url_type == URLType.DOI:
            # Extract DOI
            match = re.search(r"(10\.\d{4,}/[^\s]+)", url)
            if match:
                return match.group(1)

        return None

    @classmethod
    def get_source_name(cls, url_type: URLType) -> str:
        """Get human-readable source name."""
        names = {
            URLType.ARXIV: "arXiv",
            URLType.PUBMED: "PubMed",
            URLType.PMC: "PubMed Central",
            URLType.SEMANTIC_SCHOLAR: "Semantic Scholar",
            URLType.BIORXIV: "bioRxiv",
            URLType.MEDRXIV: "medRxiv",
            URLType.DOI: "DOI",
            URLType.PDF: "PDF",
            URLType.HTML: "Web Page",
            URLType.INVALID: "Invalid URL",
        }
        return names.get(url_type, "Unknown")