forked from alexgand/springer_free_books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
59 lines (39 loc) · 1.72 KB
/
main.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
import os
import requests
import pandas as pd
from tqdm import tqdm
# insert here the folder you want the books to be downloaded:
folder = os.getcwd() + '/download/'
if not os.path.exists(folder):
os.mkdir(folder)
books = pd.read_excel('https://resource-cms.springernature.com/springer-cms/rest/v1/content/17858272/data/v4')
# save table:
books.to_excel(folder + 'table.xlsx')
# debug:
# books = books.head()
print('Download started.')
for url, title, author, pk_name in tqdm(books[['OpenURL', 'Book Title', 'Author', 'English Package Name']].values):
new_folder = folder + pk_name + '/'
if not os.path.exists(new_folder):
os.mkdir(new_folder)
r = requests.get(url)
new_url = r.url
new_url = new_url.replace('/book/','/content/pdf/')
new_url = new_url.replace('%2F','/')
new_url = new_url + '.pdf'
final = new_url.split('/')[-1]
final = title.replace(',','-').replace('.','').replace('/',' ') + ' - ' + author.replace(',','-').replace('.','').replace('/',' ') + ' - ' + final
myfile = requests.get(new_url, allow_redirects=True)
open(new_folder+final, 'wb').write(myfile.content)
#download epub version too if exists
new_url = r.url
new_url = new_url.replace('/book/','/download/epub/')
new_url = new_url.replace('%2F','/')
new_url = new_url + '.epub'
final = new_url.split('/')[-1]
final = title.replace(',','-').replace('.','').replace('/',' ') + ' - ' + author.replace(',','-').replace('.','').replace('/',' ') + ' - ' + final
request = requests.get(new_url)
if request.status_code == 200:
myfile = requests.get(new_url, allow_redirects=True)
open(new_folder+final, 'wb').write(myfile.content)
print('Download finished.')