File size: 9,947 Bytes
9d0fd45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"""Academic content fetchers with smart URL routing."""
from __future__ import annotations

import logging
import re
from typing import Literal
from urllib.parse import urlparse

import httpx

from frontier_agent.infra.config import get_config

logger = logging.getLogger(__name__)

Route = Literal["pmc", "pubmed", "biorxiv", "paywall", "jina"]

# Paywall / anti-bot domains that need Unpaywall detour.
PAYWALL_DOMAINS: frozenset[str] = frozenset({
    "www.sciencedirect.com", "linkinghub.elsevier.com",
    "onlinelibrary.wiley.com", "chemistry-europe.onlinelibrary.wiley.com",
    "advanced.onlinelibrary.wiley.com", "analyticalsciencejournals.onlinelibrary.wiley.com",
    "nph.onlinelibrary.wiley.com", "faseb.onlinelibrary.wiley.com",
    "link.aps.org", "journals.aps.org",
    "www.cell.com",
    "academic.oup.com",
    "www.tandfonline.com",
    "pubs.rsc.org", "www.rsc.org",
    "www.science.org",
    "www.jneurosci.org",
    "ashpublications.org",
    "pubs.aip.org",
    "journals.sagepub.com",
    "pubs.acs.org",
})

_GARBAGE_SIGNALS: tuple[str, ...] = (
    "security verification", "captcha", "cloudflare",
    "access denied", "please verify", "robot",
    "enable javascript", "browser check",
    "cookies are required", "please enable cookies",
    "sign in to access", "institutional login",
    "subscribe to read", "purchase this article",
)

# Unpaywall requires a contact address and Crossref asks for one in the
# User-Agent; both throttle or block anonymous traffic. There is deliberately
# no default: a shared fallback address would collect every deployment's
# traffic, and the polite-pool courtesy only works if the address is yours.
_CONTACT_UA_TEMPLATE = "FrontierAgent/1.0 (mailto:{email})"
_ANON_CROSSREF_UA = "FrontierAgent/1.0"


# ── URL extraction helpers ────────────────────────────────────────────────

def extract_pmcid(url: str) -> str:
    """Extract PMC id from URL. e.g. ``pmc.ncbi.nlm.nih.gov/articles/PMC1234567/``."""
    match = re.search(r"(PMC\d+)", url)
    return match.group(1) if match else ""


def extract_doi(url: str) -> str:
    """Try to extract DOI from URL. Returns empty string if not found."""
    match = re.search(r"doi\.org/(10\.\d{4,}/[^\s]+)", url)
    if match:
        return match.group(1).rstrip("/")
    match = re.search(r"/(10\.\d{4,}/[^\s?#]+)", url)
    if match:
        return match.group(1).rstrip("/")
    return ""


def route_url(url: str) -> Route:
    """Classify URL for backend selection. Pure β€” no I/O."""
    domain = urlparse(url).netloc
    if "pmc.ncbi.nlm.nih.gov" in domain:
        return "pmc"
    if "pubmed.ncbi.nlm.nih.gov" in domain:
        return "pubmed"
    if "biorxiv.org" in domain or "medrxiv.org" in domain:
        return "biorxiv"
    if domain in PAYWALL_DOMAINS:
        return "paywall"
    return "jina"


def is_garbage_content(text: str) -> bool:
    """Detect anti-bot pages, login walls, CAPTCHAs, empty pages."""
    if not text:
        return True
    low = text[:2000].lower()
    return any(sig in low for sig in _GARBAGE_SIGNALS)


def biorxiv_to_pdf(url: str) -> str:
    """Convert bioRxiv/medRxiv URL to full PDF URL. Returns empty if not applicable."""
    if not ("biorxiv.org" in url or "medrxiv.org" in url):
        return ""
    clean = url.split("?")[0].split("#")[0].rstrip("/")
    if clean.endswith(".pdf"):
        return url
    if "/content/" in clean:
        return clean + ".full.pdf"
    return ""


# ── Async fetch helpers ───────────────────────────────────────────────────

def _unpaywall_email() -> str:
    """Contact address for Unpaywall, or "" when unconfigured.

    Callers treat "" as "skip Unpaywall": querying it without an address is a
    terms violation, and a placeholder address just gets the deployment
    rate-limited.
    """
    return get_config().unpaywall_email or ""


def _crossref_ua() -> str:
    """Crossref User-Agent, with the contact address when one is configured."""
    email = get_config().unpaywall_email
    return _CONTACT_UA_TEMPLATE.format(email=email) if email else _ANON_CROSSREF_UA


def _ncbi_params(base: dict | None = None) -> dict:
    """Attach ``api_key`` if configured β€” raises PubMed/E-utils rate limit."""
    params = dict(base or {})
    config = get_config()
    if config.ncbi_api_key:
        params["api_key"] = config.ncbi_api_key
    return params


async def fetch_pmc_fulltext(pmcid: str, *, timeout: int = 30) -> str:
    """Fetch full text from PMC Open Access API (BioC JSON format)."""
    if not pmcid:
        return ""
    url = (
        "https://www.ncbi.nlm.nih.gov/research/bionlp/RESTful/pmcoa.cgi/BioC_json/"
        f"{pmcid}/unicode"
    )
    try:
        async with httpx.AsyncClient(timeout=timeout) as client:
            resp = await client.get(url, params=_ncbi_params())
    except Exception as exc:
        logger.warning("[PMC API] Failed for %s: %s", pmcid, exc)
        return ""

    if resp.status_code != 200 or len(resp.text) < 500:
        return ""
    # PMC returns "[Error]..." or HTML for non-OA content.
    preview = resp.text[:100].lower()
    if resp.text.strip().startswith("[Error]") or "<html" in preview:
        logger.info("[PMC API] %s not in OA subset", pmcid)
        return ""
    try:
        data = resp.json()
    except Exception as exc:
        logger.warning("[PMC API] JSON parse failed for %s: %s", pmcid, exc)
        return ""
    items = data if isinstance(data, list) else [data]
    parts: list[str] = []
    for item in items:
        if not isinstance(item, dict):
            continue
        for doc in item.get("documents", []):
            for passage in doc.get("passages", []):
                text = passage.get("text", "")
                if text:
                    parts.append(text)
    fulltext = "\n\n".join(parts)
    if fulltext:
        logger.info("[PMC API] Fetched %d chars for %s", len(fulltext), pmcid)
    return fulltext


async def pubmed_to_pmc(url: str, *, timeout: int = 15) -> str:
    """Convert PubMed URL β†’ PMC id via E-utilities elink. Empty on failure."""
    match = re.search(r"pubmed\.ncbi\.nlm\.nih\.gov/(\d+)", url)
    if not match:
        return ""
    pmid = match.group(1)
    try:
        async with httpx.AsyncClient(timeout=timeout) as client:
            resp = await client.get(
                "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi",
                params=_ncbi_params({
                    "dbfrom": "pubmed",
                    "db": "pmc",
                    "id": pmid,
                    "retmode": "json",
                }),
            )
    except Exception as exc:
        logger.warning("[PubMed->PMC] Failed for %s: %s", pmid, exc)
        return ""

    if resp.status_code != 200:
        return ""
    try:
        data = resp.json()
    except Exception:
        return ""
    for ls in data.get("linksets", []):
        for ldb in ls.get("linksetdbs", []):
            if ldb.get("dbto") == "pmc":
                links = ldb.get("links", [])
                if links:
                    return f"PMC{links[0]}"
    return ""


async def fetch_unpaywall_oa_url(doi: str, *, timeout: int = 15) -> str:
    """Query Unpaywall API for an OA PDF URL. Empty string on any failure."""
    if not doi:
        return ""
    if not (email := _unpaywall_email()):
        # Unpaywall rejects requests without a contact address. Degrade to
        # "no OA copy found" rather than spending a request that cannot succeed.
        logger.debug("[Unpaywall] Skipped for DOI %s: UNPAYWALL_EMAIL unset", doi)
        return ""
    try:
        async with httpx.AsyncClient(timeout=timeout) as client:
            resp = await client.get(
                f"https://api.unpaywall.org/v2/{doi}",
                params={"email": email},
            )
    except Exception as exc:
        logger.warning("[Unpaywall] Failed for DOI %s: %s", doi, exc)
        return ""

    if resp.status_code != 200:
        return ""
    try:
        data = resp.json()
    except Exception:
        return ""

    best = data.get("best_oa_location") or {}
    pdf_url = best.get("url_for_pdf") or best.get("url") or ""
    if pdf_url:
        logger.info("[Unpaywall] Found OA for DOI %s: %s", doi, pdf_url)
        return pdf_url
    for loc in data.get("oa_locations", []) or []:
        pdf_url = loc.get("url_for_pdf") or loc.get("url") or ""
        if pdf_url:
            logger.info("[Unpaywall] Found OA for DOI %s: %s", doi, pdf_url)
            return pdf_url
    return ""


async def crossref_doi_lookup(
    url: str, title_hint: str = "", *, timeout: int = 10
) -> str:
    """Try to find DOI via CrossRef when URL doesn't expose one."""
    query = title_hint if title_hint else url
    try:
        async with httpx.AsyncClient(timeout=timeout) as client:
            resp = await client.get(
                "https://api.crossref.org/works",
                params={"query": query, "rows": 1},
                headers={"User-Agent": _crossref_ua()},
            )
    except Exception as exc:
        logger.warning("[CrossRef] Lookup failed: %s", exc)
        return ""

    if resp.status_code != 200:
        return ""
    try:
        items = resp.json().get("message", {}).get("items", [])
    except Exception:
        return ""
    if items:
        doi = items[0].get("DOI", "")
        if doi:
            logger.info("[CrossRef] Found DOI %s for query: %.60s", doi, query)
            return doi
    return ""


async def resolve_doi(url: str, *, title_hint: str = "") -> str:
    """Convenience: extract DOI from URL, fall back to CrossRef lookup."""
    doi = extract_doi(url)
    if doi:
        return doi
    return await crossref_doi_lookup(url, title_hint)