-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaudible.py
More file actions
236 lines (192 loc) · 6.22 KB
/
Copy pathaudible.py
File metadata and controls
236 lines (192 loc) · 6.22 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
"""
audible.py — Pure Audible scraper.
Stateless functions. Takes region as parameter. No decisions, no title cleaning.
Functions:
search(query, region) -> list[dict]
get_by_asin(asin, region) -> dict | None
get_details(url) -> dict
"""
import re
from typing import Optional
from urllib.parse import quote_plus
import requests
from bs4 import BeautifulSoup
DOMAINS = {
"us": "www.audible.com",
"uk": "www.audible.co.uk",
"de": "www.audible.de",
"fr": "www.audible.fr",
}
REGIONS = list(DOMAINS.keys())
_UA = (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/120.0.0.0 Safari/537.36"
)
_s = requests.Session()
_s.headers["User-Agent"] = _UA
_s.headers["Accept-Language"] = "en-US,en;q=0.9"
# ============================================================
# Search
# ============================================================
def search(query: str, region: str = "us", max_results: int = 5) -> list:
"""Search Audible. Returns list of result dicts."""
domain = DOMAINS.get(region, DOMAINS["us"])
url = f"https://{domain}/search?keywords={quote_plus(query)}"
try:
r = _s.get(url, timeout=15)
r.raise_for_status()
except Exception:
return []
soup = BeautifulSoup(r.text, "html.parser")
results = []
items = soup.select("li.productListItem, div.adbl-impression-container")
if not items:
items = soup.select("[data-widget='productList'] li")
for el in items[:max_results]:
try:
parsed = _parse_result(el, domain)
if parsed:
results.append(parsed)
except Exception:
continue
# Deduplicate by ASIN
seen, out = set(), []
for r in results:
asin = r.get("asin", "")
if asin and asin in seen:
continue
if asin:
seen.add(asin)
out.append(r)
return out
# ============================================================
# ASIN Lookup
# ============================================================
def get_by_asin(asin: str, region: str = "us") -> Optional[dict]:
"""Fetch a book directly by ASIN."""
domain = DOMAINS.get(region, DOMAINS["us"])
url = f"https://{domain}/pd/{asin}"
try:
r = _s.get(url, timeout=15, allow_redirects=True)
if r.status_code != 200:
return None
except Exception:
return None
details = _parse_detail_page(r.text)
if details:
details["asin"] = asin
details["url"] = url
return details if details else None
# ============================================================
# Book Details
# ============================================================
def get_details(url: str) -> dict:
"""Fetch full metadata from a book's Audible page."""
try:
r = _s.get(url, timeout=15)
r.raise_for_status()
except Exception:
return {}
return _parse_detail_page(r.text)
def _parse_detail_page(html: str) -> dict:
"""Parse an Audible book detail page."""
soup = BeautifulSoup(html, "html.parser")
d = {}
# Description
desc = soup.select_one(
"#center-1 .bc-expander-content, "
"div.productPublisherSummary span.bc-text"
)
if desc:
d["description"] = desc.get_text(strip=True)
# Genres
genres = [g.get_text(strip=True)
for g in soup.select("a.bc-chip, .categoriesLabel a")]
if genres:
d["genres"] = genres
# Publisher, date, language
for sel, key in [
(".publisherLabel a, a[href*='publisher']", "publisher"),
(".releaseDateLabel span.bc-text", "publishedDate"),
(".languageLabel span.bc-text", "language"),
]:
found = soup.select_one(sel)
if found:
d[key] = found.get_text(strip=True)
# Cover image
hero = soup.select_one(
"img.bc-pub-block[src*='images'], "
"img[alt*='cover'][src*='images']"
)
if hero:
d["cover_url"] = re.sub(r'_SL\d+_', '_SL500_', hero.get("src", ""))
# ASIN from page
asin_el = soup.select_one("input[name='asin'], [data-asin]")
if asin_el:
d["asin"] = asin_el.get("value") or asin_el.get("data-asin")
# Title from page
title_el = soup.select_one("h1.bc-heading, h1")
if title_el:
t = title_el.get_text(strip=True)
if t:
d["title"] = t
# Author from page
author_el = soup.select_one(".authorLabel a, a[href*='author']")
if author_el:
d["author"] = author_el.get_text(strip=True)
# Narrator from page
narrator_el = soup.select_one(".narratorLabel a, a[href*='narrator']")
if narrator_el:
d["narrator"] = narrator_el.get_text(strip=True)
return d
# ============================================================
# Internal Parsing
# ============================================================
def _parse_result(el, domain: str) -> Optional[dict]:
"""Parse a single search result element."""
r = {}
title_el = el.select_one("h3 a, .bc-heading a, a.bc-link[href*='/pd/']")
if not title_el:
return None
r["title"] = title_el.get_text(strip=True)
r["url"] = title_el.get("href", "")
if r["url"] and not r["url"].startswith("http"):
r["url"] = f"https://{domain}{r['url']}"
# Extract ASIN from URL
asin_match = re.search(r'/([A-Z0-9]{10})(?:\?|$)', r["url"])
if asin_match:
r["asin"] = asin_match.group(1)
# Extract title from URL slug if element text was empty
if not r["title"] and r["url"]:
slug = re.search(r'/pd/([^/]+?)(?:-Audiobook)?/', r["url"])
if slug:
r["title"] = slug.group(1).replace('-', ' ')
if not r["title"]:
return None
# Author, narrator
for sel, key in [
(".authorLabel a, .bc-color-secondary a[href*='author']", "author"),
(".narratorLabel a, a[href*='narrator']", "narrator"),
]:
found = el.select_one(sel)
if found:
r[key] = found.get_text(strip=True)
# Series
series_el = el.select_one(".seriesLabel a, a[href*='series']")
if series_el:
r["series"] = series_el.get_text(strip=True)
parent = series_el.parent.get_text(
strip=True) if series_el.parent else ""
seq = re.search(r'Book\s+(\d+(?:\.\d+)?)', parent)
if seq:
r["series_sequence"] = seq.group(1)
# Cover
img = el.select_one("img[src*='images']")
if img:
r["cover_url"] = re.sub(r'_SL\d+_', '_SL500_', img.get("src", ""))
# Runtime
rt = el.select_one(".runtimeLabel, .bc-text:-soup-contains('hrs')")
if rt:
r["runtime"] = rt.get_text(strip=True)
return r