This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
core.py
221 lines (185 loc) · 8.26 KB
/
core.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
import datetime
import pathlib
import re
import time
from datetime import date
import cloudscraper
from bs4 import BeautifulSoup
from cloudscraper import CloudflareChallengeError
from requests import Response
from dailyblink.media import (
set_m4a_meta_data,
save_media,
save_text,
)
from dailyblink.settings import (
BASE_URL,
COVER_FILE_NAME_STEM,
PLAYLIST_FILE_NAME,
LANGUAGES,
MAX_CLOUDFLARE_ATTEMPTS,
CLOUDFLARE_WAIT_TIME,
MAX_LINE_LENGTH,
)
class BlinkistScraper:
def __init__(self):
self._reset_cloudscraper()
def download_daily_blinks(self, languages: list, base_path: pathlib.Path):
current_date = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
print(f"Downloading the free daily Blinks on {current_date}...")
try:
self._attempt_daily_blinks_download(languages, base_path)
except CloudflareChallengeError as e:
print(e)
print("Retrying...")
for index in range(1, MAX_CLOUDFLARE_ATTEMPTS + 1):
print(f"Attempt {index}/{MAX_CLOUDFLARE_ATTEMPTS}... ", end="")
time.sleep(CLOUDFLARE_WAIT_TIME)
self._reset_cloudscraper()
try:
self._attempt_daily_blinks_download(languages, base_path)
break
except CloudflareChallengeError:
print("FAILED")
def _reset_cloudscraper(self):
self.scraper = cloudscraper.create_scraper()
def _attempt_daily_blinks_download(self, languages: list, base_path: pathlib.Path):
for language_code in languages:
self._download_daily_blinks(language_code, base_path)
print()
print(f"All blinks were saved under {base_path.absolute()}")
def _download_daily_blinks(self, language_code, base_path):
language = LANGUAGES.get(language_code)
blink_info = self._get_daily_blink_info(language=language_code)
blink_url = blink_info["url"]
print()
print(f"{language} ({language_code}):")
print(f"{blink_info['title']} - {blink_info['author']}\n")
blink = self._request_blinkist_book_text(blink_url=blink_url)
book_id = blink["book-id"]
chapter_ids = blink["chapter-ids"]
chapters = blink["chapters"]
valid_title = re.sub(r"([^\s\w]|_)+", "", blink_info["title"])
valid_author = re.sub(r"([^\s\w]|_)+", "", blink_info["author"])
book_path = base_path / language / f"{date.today()} - {valid_title}"
print("Saving book cover...")
cover_response = self.scraper.get(blink_info["cover_url"])
file_extension = _determine_file_extension(cover_response)
cover = cover_response.content
cover_path = book_path / f"{COVER_FILE_NAME_STEM}.{file_extension}"
save_media(media=cover, file_path=cover_path)
print("Saving book text...")
markdown_text = _create_markdown_text(blink_info, chapters, cover_path)
markdown_book_path = book_path / f"{valid_title} - {valid_author}.md"
save_text(text=markdown_text, file_path=markdown_book_path)
try:
file_list = []
for number, chapter_id in enumerate(chapter_ids):
status = f"Saving audio track #{number + 1} - {chapters[number][0]}"
status_limited = f"{status[:MAX_LINE_LENGTH - 3]}..."
print(status_limited)
file_name = f"{number:02d} - {valid_title}.m4a"
file_path = book_path / file_name
file_list.append(file_name)
audio_response = self._request_audio(
book_id=book_id, chapter_id=chapter_id
)
save_media(media=audio_response.content, file_path=file_path)
set_m4a_meta_data(
filename=file_path,
artist=blink_info["author"],
title=chapters[number][0],
album=blink_info["title"],
track_number=number + 1,
total_track_number=len(chapter_ids),
genre="Blinkist audiobook",
)
with open(book_path / PLAYLIST_FILE_NAME, "w") as f:
print("Creating playlist file...")
f.write("\n".join(file_list))
except ValueError:
print("No audio tracks are available.")
def _get_daily_blink_info(self, language="en"):
daily_blink_url = f"{BASE_URL}/{language}/nc/daily/"
response = self.scraper.get(daily_blink_url)
if "complete the security check" in response.text:
raise CloudflareChallengeError()
return _create_blink_info(response.text)
def _request_blinkist_book_text(self, blink_url):
response = self.scraper.get(blink_url)
soup = BeautifulSoup(response.text, "html.parser")
book_id = soup.find_all("div", {"class": "reader__container"})[0][
"data-book-id"
]
chapter_list_elements = soup.find_all("li", {"class": "chapters"})
chapter_ids = [
chapter_element["data-chapterid"]
for chapter_element in chapter_list_elements
if "data-chapterid" in chapter_element.attrs
]
chapters = soup.find_all(None, {"class": "chapter chapter"})
book_text = [chapter.text.strip() for chapter in chapters]
chapter_texts = [
(chapter.split("\n", 1)[0], chapter.split("\n", 1)[1])
for chapter in book_text
]
return {
"book-id": book_id,
"chapter-ids": chapter_ids,
"chapters": chapter_texts,
}
def _request_audio(self, book_id, chapter_id):
url = f"{BASE_URL}/api/books/{book_id}/chapters/{chapter_id}/audio"
headers = {"x-requested-with": "XMLHttpRequest"}
response = self.scraper.get(url, headers=headers)
if response.status_code == 404: # Text only book, no audio
raise ValueError(
f"Audio track does not exist for book {book_id} chapter {chapter_id}"
)
audio_url = response.json().get("url")
return self.scraper.get(audio_url)
def _create_blink_info(response_text):
soup = BeautifulSoup(response_text, "html.parser")
daily_book_href = soup.find_all("a", {"class": "daily-book__cta"})[0]["href"]
title = soup.find_all(None, {"class": "daily-book__headline"})[0].text.strip()
author = (
soup.find_all(None, {"class": "daily-book__author"})[0]
.text.strip()
.split(" ", 1)[1]
)
read_time = soup.find_all(None, {"class": "book-stats__label"})[0].text.strip()
synopsis = soup.find_all(None, {"class": "book-tabs__content"})[0].text.strip()
for_who = soup.find_all(None, {"class": "book-tabs__content"})[1].text.strip()
about_author = soup.find_all(None, {"class": "book-tabs__content"})[2].text.strip()
cover_url = soup.find_all("img", {"class": "legacy-book-cover__image"})[0]["src"]
return {
"url": BASE_URL + daily_book_href,
"title": title,
"author": author,
"read_time": read_time,
"synopsis": synopsis,
"for_who": for_who,
"about_author": about_author,
"cover_url": cover_url,
}
def _create_markdown_text(blink_info, chapters, cover_path):
markdown_text = f"# {blink_info['title']}\n\n"
markdown_text += f"_{blink_info['author']}_\n\n"
markdown_text += f"{blink_info['read_time']}\n\n"
markdown_text += f"![cover]({cover_path})\n\n"
markdown_text += f"### Synopsis\n\n{blink_info['synopsis']}\n\n"
markdown_text += f"### Who is it for?\n\n{blink_info['for_who']}\n\n"
markdown_text += f"### About the author\n\n{blink_info['about_author']}\n\n"
for number, chapter in enumerate(chapters):
if number != 0 and number != len(chapters) - 1:
markdown_text += f"## Blink {number} - {chapter[0]}\n\n"
else:
markdown_text += f"## {chapter[0]}\n\n"
markdown_text += f"{chapter[1]}\n\n"
markdown_text += f"Source: {blink_info['url']}\n\n"
return markdown_text
def _determine_file_extension(cover_response: Response) -> str:
headers = cover_response.headers
content_type = headers["content-type"]
_, file_extension = content_type.split("/")
return file_extension