-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_pdf.py
508 lines (414 loc) · 14.7 KB
/
find_pdf.py
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
from dataclasses import dataclass
from urllib.parse import urljoin, urlparse
import logging
import re
import os
from base64 import b64decode
import requests
from find_shared import get_base_url_from_soup
logger = logging.getLogger(__name__)
@dataclass
class PdfLink:
href: str
anchor: str
source: str = None
error: str = None
def find_pdf_link(soup):
"""find a single potential PDF link in BeautifulSoup object, prioritizing meta tags."""
try:
base_url = get_base_url_from_soup(soup)
print(f"Base URL: {base_url}")
# try meta tags first
meta_pdf = get_pdf_from_meta(soup)
if meta_pdf:
print(f"Meta PDF: {meta_pdf}")
if base_url:
meta_pdf.href = urljoin(base_url, meta_pdf.href)
meta_pdf.href = transform_pdf_url(meta_pdf.href)
if is_valid_pdf_link(meta_pdf.href):
if validate_pdf(meta_pdf):
return meta_pdf
else:
print(f"No meta PDF")
# try JavaScript next
js_pdf = get_pdf_from_javascript(str(soup))
if js_pdf:
print(f"JS PDF: {js_pdf}")
if base_url:
js_pdf.href = urljoin(base_url, js_pdf.href)
js_pdf.href = transform_pdf_url(js_pdf.href)
if is_valid_pdf_link(js_pdf.href):
if validate_pdf(js_pdf):
return js_pdf
else:
print(f"No JS PDF")
# try content links
for link in get_pdf_links_from_content(soup):
if base_url:
link.href = urljoin(base_url, link.href)
link.href = transform_pdf_url(link.href)
if is_valid_pdf_link(link.href):
if validate_pdf(link):
return link
print(f"No content PDF")
for button_link in get_pdf_links_from_buttons(soup):
if base_url:
button_link.href = urljoin(base_url, button_link.href)
button_link.href = transform_pdf_url(button_link.href)
if is_valid_pdf_link(button_link.href):
if validate_pdf(button_link):
return button_link
except Exception as e:
logger.error(f"Error finding PDF link: {str(e)}")
return None
def get_pdf_from_meta(soup):
"""Extract PDF link from meta tags."""
for meta in soup.find_all('meta'):
if meta.get('name') == 'citation_pdf_url' or meta.get('property') == 'citation_pdf_url':
if 'content' in meta.attrs:
return PdfLink(
href=meta['content'],
anchor="<meta citation_pdf_url>",
source="meta"
)
return None
def get_pdf_from_javascript(html_content):
"""Extract PDF link from JavaScript variables."""
patterns = [
r'"pdfUrl":"(.*?)"',
r'"exportPdfDownloadUrl": ?"(.*?)"',
r'"downloadPdfUrl":"(.*?)"',
r'"fullTextPdfUrl":"(.*?)"'
]
for pattern in patterns:
matches = re.findall(pattern, html_content)
if matches:
href = matches[0]
if '\\u' in href:
try:
href = href.encode().decode('unicode-escape')
except UnicodeDecodeError:
continue
return PdfLink(
href=href,
anchor=pattern.split('"')[1],
source="javascript"
)
return None
def get_pdf_links_from_content(soup):
"""Extract PDF links from BeautifulSoup content."""
pdf_links = []
bad_sections = [
"div.relatedItem",
"div.citedBySection",
"div.references",
"div#supplementary-material",
"div.table-of-content",
"div.footnotes",
"section#article-references",
]
for section in soup.select(', '.join(bad_sections)):
section.decompose()
for link in soup.find_all('a', href=True):
href = link['href']
if has_bad_pattern(href):
continue
anchor_text = link.get_text().strip().lower()
if has_bad_anchor_word(anchor_text):
continue
if is_pdf_link(link):
pdf_links.append(PdfLink(
href=href,
anchor=anchor_text or '<no text>',
source="content"
))
return pdf_links
def get_pdf_links_from_buttons(soup):
"""Extract PDF links from button elements."""
pdf_links = []
for button in soup.find_all('button', {'onclick': True}):
onclick = button['onclick']
match = re.search(r"(https?:\/\/[^\s'\"]+\.pdf)", onclick)
if match:
href = match.group(1)
anchor_text = button.get_text().strip() or '<button>'
pdf_links.append(PdfLink(
href=href,
anchor=anchor_text,
source="button"
))
return pdf_links
def is_pdf_link(link):
"""Check if link is likely a PDF download with stricter validation."""
href = link['href'].lower()
anchor_text = link.get_text().strip().lower()
# Direct PDF file links are most reliable
if href.endswith('.pdf'):
return True
# Check for PDF extensions in paths
path_components = urlparse(href).path.lower().split('/')
if any(comp.endswith('.pdf') for comp in path_components):
return True
# Check PDF indicators in query parameters
if 'pdf' in href and any(param in href for param in [
'download=',
'format=pdf',
'type=pdf',
'mimeType=pdf',
'/pdf/',
'action=download'
]):
return True
# If "PDF" is in anchor text, require additional download indicators
if "pdf" in anchor_text:
download_indicators = [
"download",
"télécharger",
"get",
"view",
"access",
"full text"
]
return any(indicator in anchor_text for indicator in download_indicators)
# Check for download buttons/links with PDF context
if any(text in anchor_text for text in ["download", "télécharger"]):
# Must have PDF context either in href or nearby elements
has_pdf_context = (
'pdf' in href or
any('pdf' in img.get('src', '').lower() for img in link.find_all('img')) or
any('pdf' in cls for cls in link.get('class', [])) or
link.find_parent(class_=lambda x: x and 'pdf' in x.lower())
)
return has_pdf_context and "citation" not in anchor_text
# Check for PDF-specific icons
pdf_icons = [
"fa-file-pdf",
"fa-pdf",
"pdf-icon",
"icon-pdf"
]
if any(cls in ' '.join(link.get('class', [])) for cls in pdf_icons):
return True
# Check parent elements for PDF context
parent_pdf_classes = [
"pdf-download",
"download-pdf",
"pdf-options",
"pdf-container"
]
for parent in link.parents:
if any(cls in ' '.join(parent.get('class', [])) for cls in parent_pdf_classes):
return True
return False
def has_bad_pattern(href):
"""Check if URL contains patterns indicating it's not a valid PDF link."""
bad_patterns = [
# Supplementary materials
'/suppl_file/',
'supplementary+file',
'_supplement',
'/Appendix',
'supinfo.pdf',
'supplementary-materials',
# Navigation/UI elements
'/faq',
'figures',
'_toc_',
'download_statistics',
# Archives/compressed files
'.zip',
'.tar.',
'.gz',
# Sample/example content
'/samples/',
'example.pdf',
# Purchase/subscription
'showsubscriptions',
'price-lists',
'libraryrequestform',
'pricing.pdf',
# Administrative
'content_policy.pdf',
'BookTOC.pdf',
'BookBackMatter.pdf',
'Deposit_Agreement',
'ethicspolicy.pdf',
'TermsOfUse.pdf',
'license_agreement.pdf',
'authors_guide.pdf',
# Other
'first-page.pdf',
'preview.pdf'
]
return any(pattern in href.lower() for pattern in bad_patterns)
def has_bad_anchor_word(anchor_text):
"""Check if anchor text contains words indicating it's not a valid PDF link."""
bad_words = [
# Supplementary content
'supplement',
'appendix',
'supporting information',
'additional files',
# Figures/media
'figure',
'table',
'video',
'image',
# Administrative
'faq',
'help',
'checklist',
'guidelines',
'instructions',
'policy',
'agreement',
'terms',
# Navigation
'abstract',
'toc',
'contents',
'index',
# Statistics
'download statistics',
'citation statistics',
'metrics',
# Other
'purchase',
'subscribe',
'preview'
]
return any(word in anchor_text.lower() for word in bad_words)
def transform_pdf_url(url):
"""Transform PDF URLs based on publisher-specific patterns."""
if not url:
return url
# Recyt
if re.match(r'https?://recyt\.fecyt\.es/index\.php/EPI/article/view/', url):
url = url.replace('/article/view/', '/article/download/')
# MIT Press Journals and Chicago
if (re.match(r'https?://(www.)?mitpressjournals\.org/doi/full/10\.+', url) or
re.match(r'https?://(www.)?journals\.uchicago\.edu/doi/full/10\.+', url)):
url = url.replace('/doi/full/', '/doi/pdf/')
# ASCO
if re.match(r'https?://(www.)?ascopubs\.org/doi/full/10\.+', url):
url = url.replace('/doi/full/', '/doi/pdfdirect/')
# AHA Journals
if re.match(r'https?://(www\.)?ahajournals\.org/doi/reader/10\..+', url):
url = url.replace('/doi/reader/', '/doi/pdf/')
# SAGE
if re.match(r'https?://(www\.)?journals.sagepub.com/doi/reader/10\..+', url):
url = url.replace('/doi/reader/', '/doi/pdf/')
# Taylor & Francis
if re.match(r'https?://(www\.)?tandfonline.com/doi/epdf/10\..+', url):
url = url.replace('/doi/epdf/', '/doi/pdf/')
# American Journal of Roentgenology
if re.match(r'https?://(www\.)?ajronline.org/doi/epdf/10\..+', url):
url = url.replace('/doi/epdf/', '/doi/pdf/')
# ACS Publications
if re.match(r'https?://(www\.)?pubs.acs.org/doi/epdf/10\..+', url):
url = url.replace('/doi/epdf/', '/doi/pdf/')
# Royal Society Publishing
if re.match(r'https?://(www\.)?royalsocietypublishing.org/doi/epdf/10\..+', url):
url = url.replace('/doi/epdf/', '/doi/pdf/')
# Wiley
if re.match(r'https?://(www\.)?onlinelibrary.wiley.com/doi/epdf/10\..+', url):
url = url.replace('/epdf/', '/pdfdirect/')
url = re.sub(r'(onlinelibrary\.wiley\.com/doi/)pdf(/.+)', r'\1pdfdirect\2', url)
# Healio
if re.match(r'https?://(journals\.)?healio.com/doi/epdf/10\..+', url):
url = url.replace('/doi/epdf/', '/doi/pdf/')
# RSNA
if re.match(r'https?://(pubs\.)?rsna.org/doi/epdf/10\..+', url):
url = url.replace('/doi/epdf/', '/doi/pdf/')
# General epdf to pdf conversion
if '/epdf/' in url:
url = url.replace('/epdf/', '/pdf/')
# Science Direct
if url.startswith('https://www.sciencedirect.com/science/article/pii/'):
url = url.replace('/article/pii/', '/article/am/pii/')
url = re.sub(r'(science)direct.com/science/article/pii/(.*?)/pdf\?md5=.*?-main\.pdf$',
r'\1direct.com/science/article/pii/\2/pdfft', url)
# Nature
if '/articles/' in url and url.endswith('.pdf'):
url = url.replace('.pdf', '_reference.pdf')
# IEEE
url = re.sub(r'(ieeexplore\.ieee\.org/stamp/stamp\.jsp\?tp=&arnumber=\d+)',
r'\1&tag=1', url)
# JSTOR
url = re.sub(r'(jstor\.org/stable/)(.*?)$',
r'\1pdfplus/\2.pdf', url)
return url
def is_valid_pdf_link(url):
"""Check if PDF link is valid."""
bad_domains = [
'exlibrisgroup.com',
'citeseerx.ist.psu.edu',
'deepdyve.com',
'researchgate.net',
'academia.edu'
]
parsed_url = urlparse(url)
if any(domain in parsed_url.netloc.lower() for domain in bad_domains):
return False
if 'temporary' in url.lower() or 'temp' in url.lower():
return False
if 'expired' in url.lower():
return False
return True
def validate_pdf(pdf_link):
"""Validate PDF link using Zyte API, status code, and content type."""
if not pdf_link:
return False
href = pdf_link.href.lower()
# Special cases that can be directly trusted
trusted_direct_patterns = [
'onlinelibrary.wiley.com/pdfdirect',
'onlinelibrary.wiley.com/doi/pdfdirect',
'/article/download/',
'/index.php/',
'/download/'
]
if any(pattern in href for pattern in trusted_direct_patterns):
return True
try:
# Use HEAD request to check the link
response = requests.head(href, allow_redirects=True, timeout=5)
# Validate content type
content_type = response.headers.get('Content-Type', '').lower()
if 'application/pdf' in content_type:
return True
# Validate content start (fallback to GET if necessary)
with requests.get(href, stream=True, timeout=10, headers={'User-Agent': 'Mozilla/5.0'}) as get_response:
get_response.raise_for_status() # Raise an exception for non-2xx responses
content_start = get_response.raw.read(5)
if content_start == b'%PDF-':
return True
if validate_with_zyte_api(href):
return True
logger.warning(f"PDF link ({pdf_link.href}) does not appear to be a valid PDF.")
return False
except Exception as e:
logger.error(f"Error validating PDF link: {str(e)}")
# Fallback to trusted patterns if Zyte validation fails
trusted_fallback_patterns = [
r'onlinelibrary\.wiley\.com/doi/pdfdirect/',
r'science\.org/doi/pdf/',
r'springer\.com/content/pdf/',
r'tandfonline\.com/doi/pdf/'
]
return any(re.search(pattern, pdf_link.href) for pattern in trusted_fallback_patterns)
def validate_with_zyte_api(url):
"""Validate PDF link using Zyte API."""
print("Validating PDF link with Zyte API.")
zyte_api_key = os.getenv("ZYTE_API_KEY")
api_response = requests.post(
"https://api.zyte.com/v1/extract",
auth=(zyte_api_key, ""),
json={
"url": url,
"httpResponseBody": True
},
)
http_response_body: bytes = b64decode(api_response.json()["httpResponseBody"])
if http_response_body.startswith(b'%PDF'):
return True