forked from sweatyeggs69/Bookie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
355 lines (305 loc) · 12.6 KB
/
Copy pathscraper.py
File metadata and controls
355 lines (305 loc) · 12.6 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
"""Metadata scraping from Open Library, Apple Books, and Goodreads."""
import ipaddress
import re
import time
import logging
from concurrent.futures import ThreadPoolExecutor, as_completed
from urllib.parse import urlparse
import requests
from bs4 import BeautifulSoup
logger = logging.getLogger(__name__)
def _is_safe_url(url: str) -> bool:
"""Return False for non-HTTPS URLs or those resolving to private/loopback addresses."""
try:
parsed = urlparse(url)
if parsed.scheme != "https":
return False
host = parsed.hostname or ""
try:
addr = ipaddress.ip_address(host)
if addr.is_private or addr.is_loopback or addr.is_link_local or addr.is_reserved:
return False
except ValueError:
# hostname — block obvious internal names
if host in ("localhost",) or host.endswith(".local") or host.endswith(".internal"):
return False
return True
except Exception:
return False
HEADERS = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/120.0.0.0 Safari/537.36"
)
}
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _clean_list_to_str(val, limit=None) -> str:
"""Safely format lists or single values into a clean comma-separated string."""
if not val:
return ""
if isinstance(val, list):
items = [str(x).strip() for x in val if x]
if limit:
items = items[:limit]
return ", ".join(items)
return str(val).strip()
# ---------------------------------------------------------------------------
# Open Library
# ---------------------------------------------------------------------------
def search_open_library(query: str, max_results: int = 10) -> list[dict]:
"""Search Open Library."""
url = "https://openlibrary.org/search.json"
params = {
"q": query,
"limit": max_results,
"fields": "key,title,author_name,isbn,publisher,first_publish_year,language,number_of_pages_median,subject,cover_i,ratings_average",
}
try:
r = requests.get(url, params=params, timeout=15)
r.raise_for_status()
data = r.json()
results = []
for doc in data.get("docs", []):
try:
results.append(_parse_ol_doc(doc))
except Exception as e:
logger.warning("Failed to parse Open Library doc: %s", e)
return results
except Exception as exc:
logger.warning("Open Library search failed: %s", exc)
return []
def fetch_open_library_by_isbn(isbn: str) -> dict | None:
results = search_open_library(f"isbn:{isbn}", max_results=1)
return results[0] if results else None
def _parse_ol_doc(doc: dict) -> dict:
cover_id = doc.get("cover_i")
cover_url = f"https://covers.openlibrary.org/b/id/{cover_id}-L.jpg" if cover_id else None
isbns = doc.get("isbn") or []
if isinstance(isbns, list):
isbn10 = next((str(i).strip() for i in isbns if i and len(str(i).strip()) == 10), None)
isbn13 = next((str(i).strip() for i in isbns if i and len(str(i).strip()) == 13), None)
elif isbns:
isbn_str = str(isbns).strip()
isbn10 = isbn_str if len(isbn_str) == 10 else None
isbn13 = isbn_str if len(isbn_str) == 13 else None
else:
isbn10 = isbn13 = None
return {
"source": "open_library",
"title": doc.get("title"),
"author": _clean_list_to_str(doc.get("author_name")),
"publisher": _clean_list_to_str(doc.get("publisher"), limit=2),
"published_date": str(doc.get("first_publish_year") or ""),
"page_count": doc.get("number_of_pages_median"),
"categories": _clean_list_to_str(doc.get("subject"), limit=5),
"language": _clean_list_to_str(doc.get("language")),
"isbn": isbn10,
"isbn13": isbn13,
"rating": doc.get("ratings_average"),
"cover_url": cover_url,
}
# ---------------------------------------------------------------------------
# Apple Books (great high-res covers, no auth required)
# ---------------------------------------------------------------------------
def search_itunes(query: str, max_results: int = 10) -> list[dict]:
"""Search Apple Books — reliable source of high-res covers."""
url = "https://itunes.apple.com/search"
params = {"term": query, "media": "ebook", "limit": max_results}
try:
r = requests.get(url, params=params, timeout=10)
r.raise_for_status()
data = r.json()
results = []
for item in data.get("results", []):
try:
raw_cover = item.get("artworkUrl100", "")
# Upgrade thumbnail to high-res
cover_url = raw_cover.replace("100x100bb", "1500x1500bb") if raw_cover else None
results.append({
"source": "itunes",
"title": item.get("trackName"),
"author": item.get("artistName"),
"publisher": item.get("sellerName"),
"published_date": (item.get("releaseDate") or "")[:4],
"page_count": None,
"categories": ", ".join(item.get("genres", [])),
"language": None,
"isbn": None,
"isbn13": None,
"rating": None,
"cover_url": cover_url,
})
except Exception as e:
logger.warning("Failed to parse iTunes item: %s", e)
return results
except Exception as exc:
logger.warning("Apple Books search failed: %s", exc)
return []
# ---------------------------------------------------------------------------
# Goodreads (scrape)
# ---------------------------------------------------------------------------
def search_goodreads(query: str, max_results: int = 10) -> list[dict]:
"""Search Goodreads (web scrape)."""
url = f"https://www.goodreads.com/search?q={requests.utils.quote(query)}&search_type=books"
try:
r = requests.get(url, headers=HEADERS, timeout=15)
r.raise_for_status()
soup = BeautifulSoup(r.text, "lxml")
results = []
for row in soup.select("tr[itemtype='http://schema.org/Book']")[:max_results]:
item = _parse_gr_row(row)
if item:
results.append(item)
return results
except Exception as exc:
logger.warning("Goodreads search failed: %s", exc)
return []
def fetch_goodreads_book(book_id: str) -> dict | None:
url = f"https://www.goodreads.com/book/show/{book_id}"
try:
r = requests.get(url, headers=HEADERS, timeout=15)
r.raise_for_status()
return _parse_gr_book_page(r.text, book_id)
except Exception as exc:
logger.warning("Goodreads fetch failed: %s", exc)
return None
def _parse_gr_row(row) -> dict | None:
try:
title_el = row.select_one("a.bookTitle span")
author_el = row.select_one("a.authorName span")
cover_el = row.select_one("img")
link_el = row.select_one("a.bookTitle")
gr_id = None
if link_el and link_el.get("href"):
m = re.search(r"/show/(\d+)", link_el["href"])
gr_id = m.group(1) if m else None
cover_url = None
if cover_el:
src = cover_el.get("src", "")
# Strip size constraints to get largest available
cover_url = re.sub(r"\._\w+_\.jpg", ".jpg", src)
cover_url = re.sub(r"SX\d+|SY\d+|CR\d+,\d+,\d+,\d+", "", cover_url)
return {
"source": "goodreads",
"goodreads_id": gr_id,
"title": title_el.text.strip() if title_el else None,
"author": author_el.text.strip() if author_el else None,
"cover_url": cover_url,
"publisher": None, "published_date": None,
"page_count": None, "categories": None, "language": None,
"isbn": None, "isbn13": None, "rating": None,
}
except Exception:
return None
def _parse_gr_book_page(html: str, book_id: str) -> dict:
soup = BeautifulSoup(html, "lxml")
def txt(sel):
el = soup.select_one(sel)
return el.get_text(strip=True) if el else None
title = txt("h1[data-testid='bookTitle']") or txt("h1.Text__title1")
author = txt("span.ContributorLink__name")
cover_el = soup.select_one("img.ResponsiveImage")
cover_url = cover_el["src"] if cover_el and cover_el.get("src") else None
rating_el = soup.select_one("div.RatingStatistics__rating")
rating = None
if rating_el:
try:
rating = float(rating_el.text.strip())
except ValueError:
pass
pages_el = soup.select_one("p[data-testid='pagesFormat']")
page_count = None
if pages_el:
m = re.search(r"(\d+)\s+pages", pages_el.text)
page_count = int(m.group(1)) if m else None
genre_els = soup.select("span.BookPageMetadataSection__genreButton a")
categories = ", ".join(el.text.strip() for el in genre_els[:5]) if genre_els else None
isbn_el = soup.select_one("div[itemprop='isbn']")
isbn13_val = isbn_el.text.strip() if isbn_el else None
return {
"source": "goodreads",
"goodreads_id": book_id,
"title": title,
"author": author,
"cover_url": cover_url,
"publisher": None, "published_date": None,
"page_count": page_count,
"categories": categories,
"language": None,
"isbn": None,
"isbn13": isbn13_val,
"rating": rating,
}
# ---------------------------------------------------------------------------
# Cover search by ISBN (dedicated cover lookup)
# ---------------------------------------------------------------------------
def fetch_cover_urls_for_isbn(isbn: str) -> list[str]:
"""Return a prioritized list of cover image URLs for a given ISBN."""
clean = re.sub(r"[^0-9X]", "", isbn.upper())
if len(clean) in (10, 13):
return [f"https://covers.openlibrary.org/b/isbn/{clean}-L.jpg"]
return []
# ---------------------------------------------------------------------------
# Unified parallel search
# ---------------------------------------------------------------------------
SOURCE_FNS = {
"open_library": search_open_library,
"itunes": search_itunes,
"goodreads": search_goodreads,
}
DEFAULT_SOURCE_ORDER = ["open_library", "itunes", "goodreads"]
SOURCE_LABELS = {
"open_library": "Open Library",
"itunes": "Apple Books",
"goodreads": "Goodreads",
}
def search_all_sources(
query: str,
sources: list[str] | None = None,
api_keys: dict | None = None,
) -> list[dict]:
"""Search all requested sources in parallel."""
if sources is None:
sources = DEFAULT_SOURCE_ORDER
if api_keys is None:
api_keys = {}
active_sources = [s for s in sources if s in SOURCE_FNS]
results_by_source: dict[str, list[dict]] = {}
with ThreadPoolExecutor(max_workers=4) as ex:
futures = {ex.submit(SOURCE_FNS[s], query): s for s in active_sources}
for fut in as_completed(futures):
src = futures[fut]
try:
results_by_source[src] = fut.result()
except Exception as exc:
logger.warning("Source %s failed: %s", src, exc)
results_by_source[src] = []
# Return in priority order
flat: list[dict] = []
for src in sources:
flat.extend(results_by_source.get(src, []))
return flat
_COVER_MAX_BYTES = 10 * 1024 * 1024 # 10 MB — guard against huge/malicious cover URLs
def fetch_cover_image(url: str) -> bytes | None:
"""Download a cover image from a URL, refusing responses larger than 10 MB."""
if not _is_safe_url(url):
logger.warning("Blocked cover download from unsafe URL: %s", url)
return None
try:
r = requests.get(url, headers=HEADERS, timeout=15, stream=True)
r.raise_for_status()
chunks: list[bytes] = []
total = 0
for chunk in r.iter_content(chunk_size=65536):
total += len(chunk)
if total > _COVER_MAX_BYTES:
logger.warning("Cover download from %s aborted: response exceeded %d bytes", url, _COVER_MAX_BYTES)
return None
chunks.append(chunk)
return b"".join(chunks)
except Exception as exc:
logger.warning("Cover download failed from %s: %s", url, exc)
return None