-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch_paper.py
More file actions
749 lines (613 loc) · 29.4 KB
/
fetch_paper.py
File metadata and controls
749 lines (613 loc) · 29.4 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#!/usr/bin/env python3
"""
fetch_paper.py - Fetch full text of scientific papers from multiple sources
This module provides the PaperFetcher class which retrieves paper text given a DOI.
It tries multiple sources in a fallback chain:
1. Europe PMC (XML full text)
2. NCBI PubMed Central (XML full text)
3. CrossRef (metadata + references)
4. Unpaywall (OA PDF)
5. Publisher HTML (direct scraping)
6. Playwright-based browser fetching (bioRxiv, PMC, publisher)
Text is cached locally to avoid redundant API calls.
"""
import json
import re
import sys
import time
from datetime import datetime
from pathlib import Path
from typing import Optional
from urllib.parse import quote
import requests
from bs4 import BeautifulSoup
# Try to import playwright for bioRxiv/medRxiv full text
try:
from playwright.sync_api import sync_playwright
PLAYWRIGHT_AVAILABLE = True
except ImportError:
PLAYWRIGHT_AVAILABLE = False
# Default cache directory for storing paper full text
DEFAULT_CACHE_DIR = Path(__file__).parent / '.paper_cache'
class PaperFetcher:
"""Fetch full text of scientific papers from multiple sources."""
def __init__(
self,
verbose: bool = False,
use_cache: bool = True,
cache_dir: str | Path | None = None,
):
self.verbose = verbose
self.use_cache = use_cache
self.cache_dir = Path(cache_dir) if cache_dir else DEFAULT_CACHE_DIR
self.session = requests.Session()
self.session.headers.update({
'User-Agent': 'ArchiveFinder/1.0 (https://github.com/dandi; mailto:ben.dichter@catalystneuro.com)'
})
# Ensure cache directory exists
if self.use_cache:
self.cache_dir.mkdir(parents=True, exist_ok=True)
def log(self, message: str):
"""Print message if verbose mode is enabled."""
if self.verbose:
print(f"[DEBUG] {message}", file=sys.stderr)
# ------------------------------------------------------------------ #
# Cache helpers
# ------------------------------------------------------------------ #
def _get_cache_path(self, doi: str) -> Path:
"""Get cache file path for a DOI."""
safe_doi = doi.replace('/', '_').replace(':', '_').replace('\\', '_')
return self.cache_dir / f"{safe_doi}.json"
def _get_cached_text(self, doi: str) -> Optional[tuple[str, str]]:
"""Get cached paper text if available."""
if not self.use_cache:
return None
cache_path = self._get_cache_path(doi)
if cache_path.exists():
try:
with open(cache_path, 'r') as f:
data = json.load(f)
self.log(f"Cache hit for DOI: {doi}")
return data.get('text'), data.get('source', '')
except Exception as e:
self.log(f"Cache read error: {e}")
return None
def _cache_text(self, doi: str, text: str, source: str):
"""Cache paper text."""
if not self.use_cache:
return
cache_path = self._get_cache_path(doi)
try:
with open(cache_path, 'w') as f:
json.dump({
'doi': doi,
'text': text,
'source': source,
'cached_at': datetime.now().isoformat()
}, f)
self.log(f"Cached text for DOI: {doi}")
except Exception as e:
self.log(f"Cache write error: {e}")
# ------------------------------------------------------------------ #
# Utility helpers
# ------------------------------------------------------------------ #
@staticmethod
def is_preprint_doi(doi: str) -> bool:
"""Check if a DOI is from a preprint server (bioRxiv/medRxiv)."""
return doi.startswith('10.1101/')
def get_pmcid_for_doi(self, doi: str) -> Optional[str]:
"""
Get PMCID for a DOI using NCBI ID converter.
Returns the PMCID if found, None otherwise.
"""
converter_url = "https://pmc.ncbi.nlm.nih.gov/tools/idconv/api/v1/articles/"
params = {
'ids': doi,
'format': 'json',
'tool': 'dandi_finder',
'email': 'info@dandiarchive.org'
}
try:
resp = self.session.get(converter_url, params=params, timeout=30)
resp.raise_for_status()
data = resp.json()
records = data.get('records', [])
if records and records[0].get('pmcid'):
return records[0]['pmcid']
except Exception as e:
self.log(f"Error getting PMCID: {e}")
return None
# ------------------------------------------------------------------ #
# Source-specific fetchers
# ------------------------------------------------------------------ #
def get_text_from_europe_pmc(self, doi: str) -> tuple[Optional[str], Optional[str]]:
"""
Get full text from Europe PMC.
Supports both PMC articles (via PMCID) and preprints (via PPR ID).
Abstract-only results are skipped since DANDI refs are rarely in abstracts.
Returns tuple of (text, pmcid) - pmcid is returned even if text fetch fails,
for potential Playwright fallback.
"""
self.log(f"Trying Europe PMC for DOI: {doi}")
pmcid_found = None
search_url = "https://www.ebi.ac.uk/europepmc/webservices/rest/search"
params = {
'query': f'DOI:"{doi}"',
'format': 'json',
'resultType': 'core'
}
try:
resp = self.session.get(search_url, params=params, timeout=30)
resp.raise_for_status()
data = resp.json()
if data.get('resultList', {}).get('result'):
result = data['resultList']['result'][0]
pmcid = result.get('pmcid')
# Try PMCID first (for published articles)
if pmcid:
pmcid_found = pmcid
self.log(f"Found PMCID: {pmcid}, fetching full text")
fulltext_url = f"https://www.ebi.ac.uk/europepmc/webservices/rest/{pmcid}/fullTextXML"
try:
ft_resp = self.session.get(fulltext_url, timeout=30)
if ft_resp.status_code == 200:
soup = BeautifulSoup(ft_resp.content, 'lxml-xml')
text = soup.get_text(separator=' ', strip=True)
# Also extract hyperlink URLs (ext-link elements contain href attributes)
ext_links = []
for link in soup.find_all('ext-link'):
href = link.get('xlink:href', '')
if href:
ext_links.append(href)
if ext_links:
self.log(f"Found {len(ext_links)} hyperlinks in XML")
text = text + '\n\n[HYPERLINKS]\n' + '\n'.join(ext_links)
return text, pmcid_found
except Exception as e:
self.log(f"Error fetching full text: {e}")
# Try PPR ID for preprints (bioRxiv, medRxiv, etc.)
full_text_ids = result.get('fullTextIdList', {}).get('fullTextId', [])
for ft_id in full_text_ids:
if ft_id.startswith('PPR'):
self.log(f"Found preprint ID: {ft_id}, fetching full text")
fulltext_url = f"https://www.ebi.ac.uk/europepmc/webservices/rest/{ft_id}/fullTextXML"
try:
ft_resp = self.session.get(fulltext_url, timeout=30)
if ft_resp.status_code == 200:
soup = BeautifulSoup(ft_resp.content, 'lxml-xml')
text = soup.get_text(separator=' ', strip=True)
ext_links = []
for link in soup.find_all('ext-link'):
href = link.get('xlink:href', '')
if href:
ext_links.append(href)
if ext_links:
self.log(f"Found {len(ext_links)} hyperlinks in preprint XML")
text = text + '\n\n[HYPERLINKS]\n' + '\n'.join(ext_links)
return text, pmcid_found
except Exception as e:
self.log(f"Error fetching preprint full text: {e}")
if not pmcid and not full_text_ids:
self.log("No PMCID or preprint ID available, skipping abstract-only result")
except Exception as e:
self.log(f"Europe PMC error: {e}")
return None, pmcid_found
def get_text_from_pmc(self, doi: str) -> tuple[Optional[str], Optional[str]]:
"""
Get full text from NCBI PubMed Central.
Returns tuple of (text, pmcid) - pmcid is returned for potential Playwright fallback.
"""
self.log(f"Trying NCBI PMC for DOI: {doi}")
converter_url = "https://pmc.ncbi.nlm.nih.gov/tools/idconv/api/v1/articles/"
params = {
'ids': doi,
'format': 'json',
'tool': 'dandi_finder',
'email': 'info@dandiarchive.org'
}
try:
resp = self.session.get(converter_url, params=params, timeout=30)
resp.raise_for_status()
data = resp.json()
records = data.get('records', [])
if records and records[0].get('pmcid'):
pmcid = records[0]['pmcid']
self.log(f"Found PMCID: {pmcid}")
efetch_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi"
params = {
'db': 'pmc',
'id': pmcid,
'rettype': 'xml',
'tool': 'dandi_finder',
'email': 'info@dandiarchive.org'
}
ft_resp = self.session.get(efetch_url, params=params, timeout=30)
if ft_resp.status_code == 200:
soup = BeautifulSoup(ft_resp.content, 'lxml-xml')
return soup.get_text(separator=' ', strip=True), pmcid
except Exception as e:
self.log(f"NCBI PMC error: {e}")
return None, None
def get_text_from_crossref(self, doi: str) -> Optional[str]:
"""
Get metadata from CrossRef (title, abstract, references).
This is a fallback that provides limited text.
"""
self.log(f"Trying CrossRef for DOI: {doi}")
url = f"https://api.crossref.org/works/{quote(doi, safe='')}"
try:
resp = self.session.get(url, timeout=30)
resp.raise_for_status()
data = resp.json()
message = data.get('message', {})
text_parts = []
# Title
if message.get('title'):
text_parts.extend(message['title'])
# Abstract
if message.get('abstract'):
abstract = BeautifulSoup(message['abstract'], 'html.parser').get_text()
text_parts.append(abstract)
# References (might contain DANDI DOIs)
for ref in message.get('reference', []):
if ref.get('DOI'):
text_parts.append(ref['DOI'])
if ref.get('unstructured'):
text_parts.append(ref['unstructured'])
if text_parts:
return '\n\n'.join(text_parts)
except Exception as e:
self.log(f"CrossRef error: {e}")
return None
def get_text_from_pmc_playwright(self, pmcid: str) -> Optional[str]:
"""
Get full text from PMC using Playwright.
The PMC API sometimes returns incomplete text (e.g., author manuscripts
missing data availability sections). This method scrapes the full HTML
page which often contains more complete content.
Args:
pmcid: The PMC ID (e.g., 'PMC11093107')
Requires: pip install playwright && playwright install chromium
"""
if not PLAYWRIGHT_AVAILABLE:
self.log("Playwright not available, skipping PMC browser fetch")
return None
self.log(f"Trying PMC via Playwright for PMCID: {pmcid}")
try:
with sync_playwright() as p:
browser = p.chromium.launch(
headless=True,
args=['--disable-blink-features=AutomationControlled']
)
context = browser.new_context(
user_agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
)
page = context.new_page()
url = f'https://pmc.ncbi.nlm.nih.gov/articles/{pmcid}/'
self.log(f"Navigating to: {url}")
page.goto(url, wait_until='domcontentloaded', timeout=30000)
page.wait_for_timeout(5000)
title = page.title()
if 'not found' in title.lower() or '404' in title or 'error' in title.lower():
self.log(f"Page not found for {pmcid}")
browser.close()
return None
text = page.inner_text('body')
if text and len(text) > 1000:
self.log(f"Got {len(text)} chars from PMC via Playwright")
browser.close()
return text
browser.close()
except Exception as e:
self.log(f"PMC Playwright error: {e}")
return None
def get_text_from_biorxiv_playwright(self, doi: str) -> Optional[str]:
"""
Get full text from bioRxiv/medRxiv using Playwright to bypass Cloudflare.
Requires: pip install playwright && playwright install chromium
"""
if not PLAYWRIGHT_AVAILABLE:
self.log("Playwright not available, skipping bioRxiv browser fetch")
return None
if not self.is_preprint_doi(doi):
return None
self.log(f"Trying bioRxiv/medRxiv via Playwright for DOI: {doi}")
try:
with sync_playwright() as p:
browser = p.chromium.launch(
headless=True,
args=['--disable-blink-features=AutomationControlled']
)
context = browser.new_context(
user_agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
)
page = context.new_page()
for server in ['biorxiv', 'medrxiv']:
url = f'https://www.{server}.org/content/{doi}v1.full'
self.log(f"Navigating to: {url}")
try:
page.goto(url, wait_until='domcontentloaded', timeout=30000)
page.wait_for_timeout(10000)
title = page.title()
if 'not found' in title.lower() or '404' in title:
self.log(f"Page not found on {server}")
continue
article = page.query_selector('article')
if article:
text = article.inner_text()
else:
text = page.inner_text('body')
if text and len(text) > 1000:
self.log(f"Got {len(text)} chars from {server} via Playwright")
browser.close()
return text
except Exception as e:
self.log(f"Error fetching from {server}: {e}")
continue
browser.close()
except Exception as e:
self.log(f"Playwright error: {e}")
return None
def get_text_from_publisher_playwright(self, doi: str) -> Optional[str]:
"""
Scrape full text from publisher's HTML page using Playwright.
This is a fallback for when regular HTTP requests fail (403 Forbidden, etc).
Requires: pip install playwright && playwright install chromium
"""
if not PLAYWRIGHT_AVAILABLE:
self.log("Playwright not available, skipping publisher browser fetch")
return None
self.log(f"Trying publisher HTML via Playwright for DOI: {doi}")
try:
with sync_playwright() as p:
browser = p.chromium.launch(
headless=True,
args=['--disable-blink-features=AutomationControlled']
)
context = browser.new_context(
user_agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
)
page = context.new_page()
doi_url = f'https://doi.org/{doi}'
self.log(f"Navigating to: {doi_url}")
page.goto(doi_url, wait_until='domcontentloaded', timeout=30000)
page.wait_for_timeout(5000)
title = page.title()
if 'not found' in title.lower() or '404' in title or 'error' in title.lower():
self.log(f"Page not found for {doi}")
browser.close()
return None
text = page.inner_text('body')
if text and len(text) > 1000:
self.log(f"Got {len(text)} chars from publisher via Playwright")
browser.close()
return text
browser.close()
except Exception as e:
self.log(f"Publisher Playwright error: {e}")
return None
def get_text_from_publisher_html(self, doi: str) -> Optional[str]:
"""
Scrape full text from publisher's open access HTML page.
Works with Nature, Springer, Cell, Elsevier, and other open access papers.
Falls back to Playwright if regular HTTP request fails.
"""
self.log(f"Trying publisher HTML for DOI: {doi}")
doi_url = f"https://doi.org/{doi}"
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
}
resp = self.session.get(doi_url, headers=headers, timeout=30, allow_redirects=True)
resp.raise_for_status()
content_type = resp.headers.get('content-type', '')
if 'text/html' not in content_type:
self.log(f"Not HTML content: {content_type}")
return None
soup = BeautifulSoup(resp.content, 'html.parser')
for element in soup(['script', 'style', 'nav', 'header', 'footer']):
element.decompose()
article_content = None
selectors = [
'article',
'[role="main"]',
'.article-content',
'.article__body',
'#article-body',
'.c-article-body', # Nature
'.article-section',
'main',
]
for selector in selectors:
article_content = soup.select_one(selector)
if article_content:
break
if article_content:
text = article_content.get_text(separator=' ', strip=True)
else:
text = soup.get_text(separator=' ', strip=True)
if len(text) > 1000:
self.log(f"Got {len(text)} chars from publisher HTML")
return text
else:
self.log(f"Insufficient content from HTML ({len(text)} chars)")
self.log("Trying Playwright fallback for insufficient HTML content")
return self.get_text_from_publisher_playwright(doi)
except Exception as e:
self.log(f"Publisher HTML error: {e}")
self.log("Trying Playwright fallback for publisher HTML")
return self.get_text_from_publisher_playwright(doi)
return None
def extract_text_from_pdf_url(self, url: str) -> Optional[str]:
"""Download a PDF from a URL and extract text using PyMuPDF."""
import tempfile
try:
resp = self.session.get(url, timeout=60, stream=True)
if resp.status_code != 200:
self.log(f"PDF download failed: HTTP {resp.status_code}")
return None
content_type = resp.headers.get('content-type', '')
if 'pdf' not in content_type and not url.endswith('.pdf'):
self.log(f"Not a PDF: {content_type}")
return None
with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as tmp:
for chunk in resp.iter_content(chunk_size=65536):
tmp.write(chunk)
tmp_path = tmp.name
import fitz # PyMuPDF
pages = []
with fitz.open(tmp_path) as doc:
for page in doc:
pages.append(page.get_text())
Path(tmp_path).unlink(missing_ok=True)
text = '\n'.join(pages).strip()
if len(text) > 500:
return text
self.log(f"PDF text too short ({len(text)} chars)")
return None
except Exception as e:
self.log(f"PDF extraction error: {e}")
return None
def get_text_from_unpaywall(self, doi: str) -> Optional[str]:
"""Get paper text via Unpaywall OA PDF lookup."""
self.log(f"Trying Unpaywall for DOI: {doi}")
try:
resp = self.session.get(
f"https://api.unpaywall.org/v2/{doi}",
params={'email': 'ben.dichter@catalystneuro.com'},
timeout=15,
)
if resp.status_code != 200:
return None
data = resp.json()
if not data.get('is_oa'):
self.log("Unpaywall: not OA")
return None
for loc in data.get('oa_locations', []):
pdf_url = loc.get('url_for_pdf')
if not pdf_url:
continue
# Skip PMC PDFs — they return HTML redirects; we use PMC Playwright instead
if 'pmc.ncbi.nlm.nih.gov' in pdf_url:
continue
self.log(f"Unpaywall PDF: {pdf_url[:80]}")
text = self.extract_text_from_pdf_url(pdf_url)
if text:
return text
except Exception as e:
self.log(f"Unpaywall error: {e}")
return None
# ------------------------------------------------------------------ #
# Main orchestrator
# ------------------------------------------------------------------ #
def get_paper_text(self, doi: str) -> tuple[Optional[str], str, bool]:
"""
Get paper text from multiple sources with a fallback chain.
Returns tuple of (text, source_name, from_cache) or (None, '', False) if not found.
Combines text from multiple sources to maximize coverage.
"""
# Check cache first
cached = self._get_cached_text(doi)
if cached and cached[0]:
return cached[0], cached[1], True
text_parts = []
sources_used = []
pmcid = None # Track PMCID for potential Playwright fallback
# For bioRxiv/medRxiv preprints (10.1101/...), use dedicated method first
if self.is_preprint_doi(doi):
self.log(f"Preprint DOI detected, trying bioRxiv/medRxiv Playwright first: {doi}")
playwright_text = self.get_text_from_biorxiv_playwright(doi)
if playwright_text and len(playwright_text) > 1000:
self.log(f"Got text from bioRxiv Playwright ({len(playwright_text)} chars)")
text_parts.append(playwright_text)
sources_used.append('playwright_biorxiv')
# Also try CrossRef for references
crossref_text = self.get_text_from_crossref(doi)
if crossref_text and len(crossref_text) > 100:
self.log(f"Got text from crossref ({len(crossref_text)} chars)")
text_parts.append(crossref_text)
if 'crossref' not in sources_used:
sources_used.append('crossref')
# If bioRxiv Playwright failed, try Europe PMC (some preprints are indexed there)
if not sources_used or sources_used == ['crossref']:
text, europe_pmc_pmcid = self.get_text_from_europe_pmc(doi)
if text and len(text) > 100:
self.log(f"Got text from europe_pmc ({len(text)} chars)")
text_parts.insert(0, text)
sources_used.insert(0, 'europe_pmc')
else:
# For non-preprint DOIs, try Europe PMC first
text, europe_pmc_pmcid = self.get_text_from_europe_pmc(doi)
if europe_pmc_pmcid:
pmcid = europe_pmc_pmcid
if text and len(text) > 100:
self.log(f"Got text from europe_pmc ({len(text)} chars)")
text_parts.append(text)
sources_used.append('europe_pmc')
else:
time.sleep(0.5)
# Try NCBI PMC
text, ncbi_pmcid = self.get_text_from_pmc(doi)
if ncbi_pmcid:
pmcid = ncbi_pmcid
if text and len(text) > 100:
self.log(f"Got text from ncbi_pmc ({len(text)} chars)")
text_parts.append(text)
sources_used.append('ncbi_pmc')
time.sleep(0.5)
# Always try CrossRef for references (they often contain DANDI DOIs)
crossref_text = self.get_text_from_crossref(doi)
if crossref_text and len(crossref_text) > 100:
self.log(f"Got text from crossref ({len(crossref_text)} chars)")
text_parts.append(crossref_text)
if 'crossref' not in sources_used:
sources_used.append('crossref')
time.sleep(0.5)
# If PMC text is short, try Playwright for more complete content
MIN_PMC_TEXT_FOR_COMPLETENESS = 15000
pmc_text_length = len(text_parts[0]) if text_parts and sources_used and sources_used[0] in ('europe_pmc', 'ncbi_pmc') else 0
if pmc_text_length > 0 and pmc_text_length < MIN_PMC_TEXT_FOR_COMPLETENESS:
if not pmcid:
pmcid = self.get_pmcid_for_doi(doi)
if pmcid:
self.log(f"PMC text seems short ({pmc_text_length} chars), trying Playwright for {pmcid}")
playwright_text = self.get_text_from_pmc_playwright(pmcid)
if playwright_text and len(playwright_text) > pmc_text_length:
self.log(f"Got better text from PMC Playwright ({len(playwright_text)} chars vs {pmc_text_length})")
text_parts[0] = playwright_text
sources_used[0] = 'pmc_playwright'
# If we don't have PMC full text, try other sources
if not sources_used or sources_used == ['crossref']:
# Try Unpaywall for OA PDF
unpaywall_text = self.get_text_from_unpaywall(doi)
if unpaywall_text and len(unpaywall_text) > 1000:
self.log(f"Got text from Unpaywall ({len(unpaywall_text)} chars)")
text_parts.append(unpaywall_text)
sources_used.append('unpaywall')
if not sources_used or sources_used == ['crossref']:
# Try scraping publisher HTML as fallback
publisher_text = self.get_text_from_publisher_html(doi)
if publisher_text and len(publisher_text) > 1000:
self.log(f"Got text from publisher ({len(publisher_text)} chars)")
text_parts.append(publisher_text)
sources_used.append('publisher_html')
# If publisher HTML failed but we have a PMCID, try PMC Playwright
if (not sources_used or sources_used == ['crossref']) and pmcid:
self.log(f"Publisher blocked, trying PMC Playwright for {pmcid}")
pmc_playwright_text = self.get_text_from_pmc_playwright(pmcid)
if pmc_playwright_text and len(pmc_playwright_text) > 1000:
self.log(f"Got text from PMC Playwright ({len(pmc_playwright_text)} chars)")
text_parts.append(pmc_playwright_text)
sources_used.append('pmc_playwright')
if text_parts:
combined_text = '\n\n'.join(text_parts)
source_str = '+'.join(sources_used)
# Only cache if we have more than just crossref metadata
if sources_used != ['crossref']:
self._cache_text(doi, combined_text, source_str)
else:
self.log(f"Skipping cache for crossref-only result: {doi}")
return combined_text, source_str, False
return None, '', False