-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_utils.py
More file actions
323 lines (277 loc) · 10.8 KB
/
Copy pathpdf_utils.py
File metadata and controls
323 lines (277 loc) · 10.8 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
"""PDF (and HTML) download and text extraction utilities."""
import re
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
import requests
from bs4 import BeautifulSoup
from pypdf import PdfReader
_TEXT_SUFFIXES = {".txt", ".md", ".markdown", ".text"}
_HTML_BLOCK_TAGS = ("h1", "h2", "h3", "h4", "p", "li", "blockquote", "pre")
_ARXIV_ABS_RE = re.compile(
r"https?://(?:www\.)?arxiv\.org/(?:abs|html)/"
r"(\d{4}\.\d{4,5}(?:v\d+)?)(?:\.pdf)?(?:[?#].*)?$"
)
_OPENREVIEW_FORUM_RE = re.compile(
r"https?://openreview\.net/forum\?(.*&)?id=([A-Za-z0-9_-]+)(?:&.*)?$"
)
def _normalize_pdf_url(url):
"""Normalize common paper URLs into direct PDF URLs when possible."""
raw = url or ""
match = _ARXIV_ABS_RE.match(raw)
if match:
return f"https://arxiv.org/pdf/{match.group(1)}.pdf"
# OpenReview: /forum?id=X returns HTML, but /pdf?id=X returns PDF.
match = _OPENREVIEW_FORUM_RE.match(raw)
if match:
return f"https://openreview.net/pdf?id={match.group(2)}"
return raw
# Many publisher and academic servers reject Python's default
# User-Agent (e.g. werbos.com returns 465, others return 403/451).
# Sending a real browser UA fixes the majority of these without
# affecting servers that don't care.
_BROWSER_UA = (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
)
def download_pdf(url, timeout=60):
"""Download a PDF from a URL to a temporary file.
Args:
url: URL pointing to a PDF file.
timeout: Request timeout in seconds.
Returns:
Path to the downloaded temporary file.
"""
resolved_url = _normalize_pdf_url(url)
if resolved_url != url:
print(f"[PDF] Normalized {url} -> {resolved_url}", file=sys.stderr)
print(f"[PDF] Downloading {resolved_url}...", file=sys.stderr)
headers = {"User-Agent": _BROWSER_UA, "Accept": "application/pdf,*/*"}
try:
resp = requests.get(
resolved_url, timeout=timeout, stream=True, headers=headers,
)
except requests.exceptions.SSLError as exc:
# Many academic servers (university personal pages, preprint
# mirrors) ship incomplete TLS chains that Python's certifi
# bundle doesn't trust. These are not MITM signals — they're
# configuration gaps on hosts like www2.math.uu.se. Fall back
# once with verification disabled after logging loudly.
print(
f"[PDF] TLS verification failed for {resolved_url}: {exc}. "
"Retrying with verify=False (academic-server fallback).",
file=sys.stderr,
)
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
resp = requests.get(
resolved_url, timeout=timeout, stream=True, verify=False,
headers=headers,
)
resp.raise_for_status()
content_type = (resp.headers.get("content-type") or "").lower()
if content_type and "pdf" not in content_type and "octet-stream" not in content_type:
raise ValueError(
f"URL did not return a PDF: {resolved_url} "
f"(content-type: {content_type})"
)
suffix = ".pdf"
tmp = tempfile.NamedTemporaryFile(suffix=suffix, delete=False)
for chunk in resp.iter_content(chunk_size=8192):
if chunk:
tmp.write(chunk)
tmp.close()
path = Path(tmp.name)
with path.open("rb") as fh:
magic = fh.read(5)
if magic != b"%PDF-":
path.unlink(missing_ok=True)
raise ValueError(f"URL did not return a valid PDF: {resolved_url}")
print(f"[PDF] Saved to {tmp.name}", file=sys.stderr)
return path
def extract_text(pdf_path):
"""Extract text from a PDF file using pypdf, with OCR fallback.
Older scanned papers (e.g. pre-2000 IEEE / academic preprints)
are image-based — pypdf returns an empty string. When that
happens we fall back to rasterizing each page and running
tesseract OCR. The fallback only kicks in when pypdf can't
extract anything meaningful, so the fast path stays fast.
Args:
pdf_path: Path to the PDF file.
Returns:
Extracted text as a string.
"""
reader = PdfReader(str(pdf_path))
pages_text = []
for page in reader.pages:
text = page.extract_text()
if text:
pages_text.append(text)
full_text = "\n\n".join(pages_text)
if full_text.strip():
print(
f"[PDF] Extracted {len(full_text)} chars from "
f"{len(reader.pages)} pages", file=sys.stderr,
)
return full_text
print(
f"[PDF] No text via pypdf ({len(reader.pages)} pages) — "
f"trying OCR fallback (image-based PDF)", file=sys.stderr,
)
ocr_text = _extract_via_ocr(pdf_path, page_count=len(reader.pages))
if ocr_text.strip():
print(
f"[PDF] OCR extracted {len(ocr_text)} chars", file=sys.stderr,
)
return ocr_text
print(
f"[PDF] Warning: OCR also returned no text for {pdf_path}",
file=sys.stderr,
)
return ""
def _extract_via_ocr(pdf_path, page_count=None):
"""Rasterize each page of an image-based PDF and run tesseract.
Requires pdftoppm (poppler-utils) and tesseract on $PATH. Both
are packaged on the worker host. Best-effort: if either binary
is missing or fails, returns "" so the caller can surface a
clean error.
Page renders go to a per-PDF tempdir that is unconditionally
removed on exit so we don't leak hundreds of MB into /tmp.
"""
if not shutil.which("pdftoppm"):
print("[PDF] OCR fallback unavailable: pdftoppm not on PATH",
file=sys.stderr)
return ""
if not shutil.which("tesseract"):
print("[PDF] OCR fallback unavailable: tesseract not on PATH",
file=sys.stderr)
return ""
workdir = Path(tempfile.mkdtemp(prefix="pdf_ocr_"))
try:
# 200 DPI is the typical tesseract sweet spot — readable but
# not so huge that runtime explodes for long papers.
prefix = str(workdir / "page")
try:
subprocess.run(
["pdftoppm", "-r", "200", "-png", str(pdf_path), prefix],
check=True, capture_output=True, timeout=300,
)
except subprocess.CalledProcessError as exc:
print(f"[PDF] pdftoppm failed: {exc.stderr[:200]!r}",
file=sys.stderr)
return ""
except subprocess.TimeoutExpired:
print("[PDF] pdftoppm timed out", file=sys.stderr)
return ""
page_pngs = sorted(workdir.glob("page*.png"))
if not page_pngs:
print("[PDF] pdftoppm produced no pages", file=sys.stderr)
return ""
page_texts = []
for png in page_pngs:
try:
proc = subprocess.run(
["tesseract", str(png), "-", "-l", "eng"],
check=True, capture_output=True, text=True,
timeout=120,
)
if proc.stdout.strip():
page_texts.append(proc.stdout)
except subprocess.CalledProcessError as exc:
print(
f"[PDF] tesseract failed for {png.name}: "
f"{exc.stderr[:200]!r}", file=sys.stderr,
)
except subprocess.TimeoutExpired:
print(f"[PDF] tesseract timed out for {png.name}",
file=sys.stderr)
return "\n\n".join(page_texts)
finally:
try:
shutil.rmtree(workdir, ignore_errors=True)
except OSError:
pass
def _extract_html_main_text(html_bytes, *, source_url=""):
"""Pull the main article text out of an HTML page using bs4.
Drops script/style/nav/header/footer/aside/noscript/iframe/form,
then prefers <article>, <main>, or <body> as the content root.
Concatenates heading and block-level text only, so we skip
navigation links and sidebar boilerplate.
"""
soup = BeautifulSoup(html_bytes, "html.parser")
for tag in soup(["script", "style", "nav", "header", "footer",
"aside", "noscript", "iframe", "form"]):
tag.decompose()
root = soup.find("article") or soup.find("main") or soup.body or soup
blocks = []
title = soup.find("title")
if title and title.get_text(strip=True):
blocks.append(title.get_text(strip=True))
for el in root.find_all(_HTML_BLOCK_TAGS):
text = el.get_text(" ", strip=True)
if text:
blocks.append(text)
extracted = "\n\n".join(blocks)
print(
f"[HTML] Extracted {len(extracted)} chars from {source_url}",
file=sys.stderr,
)
return extracted
def download_html_text(url, timeout=60):
"""Download an HTML page and return its main article text.
Used as a fallback when a submission URL points at an article
landing page (e.g. a research blog post) rather than a PDF.
"""
resolved_url = _normalize_pdf_url(url)
print(f"[HTML] Downloading {resolved_url}...", file=sys.stderr)
headers = {
"User-Agent": _BROWSER_UA,
"Accept": "text/html,application/xhtml+xml,*/*",
}
resp = requests.get(resolved_url, timeout=timeout, headers=headers)
resp.raise_for_status()
content_type = (resp.headers.get("content-type") or "").lower()
if "html" not in content_type and "xml" not in content_type:
raise ValueError(
f"URL did not return HTML: {resolved_url} "
f"(content-type: {content_type})"
)
return _extract_html_main_text(resp.content, source_url=resolved_url)
def download_and_extract(url):
"""Download a PDF (or HTML page) and extract its text.
Local `.txt` / `.md` files are treated as already-extracted source
text. Remote URLs are tried as PDFs first; if the server returns
HTML instead, the main article text is extracted via bs4.
Args:
url: URL pointing to a PDF or HTML article, or a local
filesystem path.
Returns:
Extracted text as a string.
"""
local = Path(url).expanduser()
if local.is_file():
if local.suffix.lower() in _TEXT_SUFFIXES:
text = local.read_text(encoding="utf-8")
print(
f"[PDF] Loaded {len(text)} chars from text source {local}",
file=sys.stderr,
)
return text
return extract_text(local)
try:
pdf_path = download_pdf(url)
except ValueError as exc:
if "did not return a PDF" not in str(exc):
raise
text = download_html_text(url)
if not text.strip():
raise ValueError(
f"URL returned HTML but no extractable article text: {url}"
) from exc
return text
try:
return extract_text(pdf_path)
finally:
pdf_path.unlink(missing_ok=True)