-
Notifications
You must be signed in to change notification settings - Fork 2
/
cambridge_core_downloader.py
309 lines (291 loc) · 11.7 KB
/
cambridge_core_downloader.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
import re
import string
from argparse import ArgumentParser
from io import BytesIO
from pathlib import Path
import sys
import pypdf
import requests
import roman
from bs4 import BeautifulSoup
from ebooklib import epub
from tqdm import tqdm
class CambridgeCoreBook:
doi = ""
title = ""
author = ""
chapters = []
total_directory_pages = 0
current_directory_page = 0
html = ""
base_url = "https://www.cambridge.org"
book_url = ""
output_dir_base = "output/"
output_dir = ""
chapter_dir = ""
output_filename = ""
page_index = 0
valid_characters = f"-_.() {string.ascii_letters}{string.digits}"
def __init__(self, doi, epub_generation):
self.doi = doi
self.epub_generation = epub_generation
self.get_html()
self.get_title()
self.get_author()
self.make_output_dir()
self.output_filename = (
f'{self.author.replace(" ", "-")}_{self.title.replace(" ", "-")}'
)
self.get_chapters()
self.download_files()
self.merge_pdfs()
if self.epub_generation:
self.make_epub()
def get_html(self):
print(f"Getting book information from DOI.")
doi_url = "https://doi.org/" + self.doi
response = ""
try:
response = requests.get(doi_url)
except not response.status_code == 200:
raise
self.html = BeautifulSoup(response.text, "html.parser")
if self.html.find(attrs={"data-test-id": "paginationSearchResult"}) is None:
self.total_directory_pages = 1
else:
self.total_directory_pages = int(
self.html.find(attrs={"data-test-id": "paginationSearchResult"})
.find("p")
.get_text()
.split()[-1]
)
self.current_directory_page = 1
self.book_url = response.url
def get_chapters(self):
all_chapters_html = self.html.find_all("ul", class_="details")
re_pdf = re.compile(r"\.pdf")
re_core_reader = re.compile("core-reader")
for single_chapter_html in all_chapters_html:
if single_chapter_html.find(href=re_pdf) is None:
continue
chapter_dict = {
"title": (
single_chapter_html.find("a", class_="part-link")
.get_text()
.strip()
.split("\n")
)[0],
"pdf_link": single_chapter_html.find(href=re_pdf)["href"],
"pages": "",
"html_link": "",
"indentation_level": int(
[
re.split("indent-", classname)
for classname in single_chapter_html.parent["class"]
if re.match("indent", classname)
][0][-1]
),
}
if (
len(
single_chapter_html.find("a", class_="part-link")
.get_text()
.strip()
.split("\n")
)
== 2
):
chapter_dict["pages"] = (
single_chapter_html.find("a", class_="part-link")
.get_text()
.strip()
.split("\n")
)[1].replace("pp ", "")
chapter_dict["first_page"] = chapter_dict["pages"].split("-")[0].strip()
chapter_dict["last_page"] = chapter_dict["pages"].split("-")[1].strip()
try:
chapter_dict["first_page"] = int(chapter_dict["first_page"])
chapter_dict["last_page"] = int(chapter_dict["last_page"])
chapter_dict["pagination_type"] = "arabic"
except (TypeError, ValueError):
chapter_dict["first_page"] = roman.fromRoman(
chapter_dict["first_page"].upper()
)
chapter_dict["last_page"] = roman.fromRoman(
chapter_dict["last_page"].upper()
)
chapter_dict["pagination_type"] = "roman"
chapter_dict["pages_length"] = (
chapter_dict["last_page"] - chapter_dict["first_page"] + 1
)
if single_chapter_html.find(href=re_core_reader) is not None:
chapter_dict["html_link"] = single_chapter_html.find(
href=re_core_reader
)["href"]
self.chapters.append(chapter_dict)
if self.current_directory_page < self.total_directory_pages:
response = ""
next_page_url = (
self.book_url + f"?pageNum={self.current_directory_page + 1}"
)
try:
response = requests.get(next_page_url)
except not response.status_code == 200:
raise
self.html = BeautifulSoup(response.text, "html.parser")
self.current_directory_page = self.current_directory_page + 1
self.get_chapters()
def get_author(self):
if not self.html.find("meta", {"name": "citation_author"}):
author_string = self.html.find("meta", {"name": "citation_editor"})[
"content"
]
else:
author_string = self.html.find("meta", {"name": "citation_author"})[
"content"
]
self.author = "".join(
letter for letter in author_string if letter in self.valid_characters
)
def get_title(self):
title_string = self.html.find("meta", {"name": "citation_title"})["content"]
self.title = "".join(
letter for letter in title_string if letter in self.valid_characters
)
def make_output_dir(self):
try:
self.output_dir = (
self.output_dir_base
+ f'{self.author.replace(" ", "-")}_{self.title.replace(" ", "-")}/'
)
self.chapter_dir = self.output_dir + "chapters/"
Path(self.output_dir_base).mkdir(exist_ok=True)
Path(self.output_dir).mkdir(exist_ok=False)
Path(self.chapter_dir).mkdir(exist_ok=False)
except FileExistsError:
print(
f'The output folder "{self.output_dir}" already exists! Please rename or remove and start again.'
)
sys.exit(1)
def download_files(self):
if self.epub_generation:
filetypes = ["pdf", "html"]
else:
filetypes = ["pdf"]
for filetype in filetypes:
if self.chapters[0][f"{filetype}_link"] == "":
continue
else:
print(
f'Downloading {filetype.upper()}s for "{self.title}" by {self.author}.'
)
sequence_number = 1
response = ""
for chapter in tqdm(self.chapters):
try:
response = requests.get(
self.base_url + chapter[f"{filetype}_link"]
)
except not response.status_code == 200:
raise
chapter[filetype] = response.content
if filetype == "html":
self.extract_html(chapter)
# Ensure only valid characters
chapter_title_for_filename = chapter["title"].replace(" ", "-")
valid_chapter_filename = "".join(
ch
for ch in chapter_title_for_filename
if ch in self.valid_characters
)
with open(
self.chapter_dir
+ f'{sequence_number:02}_{valid_chapter_filename}_{chapter["pages"]}.{filetype}',
"wb",
) as output_file:
output_file.write(chapter[filetype])
sequence_number += 1
def merge_pdfs(self):
print(f"Merging PDFs.")
writer = pypdf.PdfWriter()
last_parents = {0: None}
for chapter in self.chapters:
pdf = pypdf.PdfReader(BytesIO(chapter["pdf"]))
chapter["pdf_length"] = len(pdf.pages)
# Unfortunately, length in pages is not necessarily the same as length of the PDF file, as Cambridge Core
# sometimes inserts blank or copyright pages
writer.append(fileobj=pdf, import_outline=False)
page_index_first = self.page_index
page_index_last = self.page_index + chapter["pdf_length"] - 1
last_parent = writer.add_outline_item(
title=chapter["title"],
page_number=page_index_first,
parent=last_parents[chapter["indentation_level"]],
)
last_parents[chapter["indentation_level"] + 1] = last_parent
match chapter["pagination_type"]:
case "arabic":
pagination_style = pypdf.constants.PageLabelStyle.DECIMAL
case "roman":
pagination_style = pypdf.constants.PageLabelStyle.LOWERCASE_ROMAN
case _:
raise KeyError
writer.set_page_label(
page_index_from=page_index_first,
page_index_to=page_index_last,
style=pagination_style,
start=chapter["first_page"],
)
self.page_index = self.page_index + chapter["pdf_length"]
writer.write(self.output_dir + "/" + self.output_filename + ".pdf")
writer.close()
print("Done.")
def extract_html(self, chapter):
chapter_html = BeautifulSoup(chapter["html"], "html.parser")
chapter["extracted_html"] = chapter_html.find(id="content-container").prettify()
def make_epub(self):
if "extracted_html" not in self.chapters[0]:
print("No HTML available, no EPUB can be made.")
return
print("Making EPUB.")
book = epub.EpubBook()
book.set_identifier(self.output_filename)
book.set_title(self.title)
book.set_language("en")
book.add_author(self.author)
book.toc = []
book.spine = []
for chapter in self.chapters:
epub_chapter = epub.EpubHtml(
title=chapter["title"],
file_name=chapter["title"].replace(" ", "-") + ".html",
lang="en",
)
epub_chapter.set_content(chapter["extracted_html"])
book.add_item(epub_chapter)
book.toc.append(epub_chapter)
book.spine.append(epub_chapter)
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())
epub.write_epub(self.output_dir + "/" + self.output_filename + ".epub", book)
print("Done.")
def check_python_version():
if not sys.version_info >= (3, 10):
print(
"At least python version 3.10 is required to run this script. Please consider updating your python environment ({}.{})".format(
sys.version_info.major, sys.version_info.minor
)
)
sys.exit(1)
if __name__ == "__main__":
check_python_version()
parser = ArgumentParser("Download a book from Cambridge Core.")
parser.add_argument(
"doi", type=str, help="Digital Object Identifier (DOI)", nargs="?"
)
parser.add_argument("-e", "--epub", action="store_true")
args = parser.parse_args()
print("Welcome to Cambridge Core Book Downloader!")
if not args.doi:
args.doi = input("Enter Digital Object Identifier (DOI): ")
cambridge_book = CambridgeCoreBook(doi=args.doi, epub_generation=args.epub)