-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
80 lines (58 loc) · 2.13 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import requests
import json
from pprint import pprint as pp
import urllib
import config as cfg
import os
def createFolder(directory):
try:
if not os.path.exists(directory):
os.makedirs(directory)
except OSError:
print('Error: Creating directory. ' + directory)
def getAllAudiobooks():
audiobooks = []
url = "https://iceportal.de/api1/rs/pages/hoerbuecher_und_hoerspiele"
response = requests.get(url, headers=cfg.headers)
# extract titles
json_data = json.loads(response.text)
items = json_data["teasersMain"]["items"]
for item in items:
name = str(item["navigation"]["href"])[28:]
audiobooks.append(name)
return audiobooks
def downloadAudiobook(title):
print("Downloading audiobook: {}".format(title))
url = "https://iceportal.de/api1/rs/pages/hoerbuecher_und_hoerspiele/{}".format(
title)
responseChapter = requests.get(url, headers=cfg.headers)
# extract chapters
json_data = json.loads(responseChapter.text)
playlist = json_data["modules"]["playlist"]
# extract downloadPath for each chapter
downloadPath = []
for chapter in playlist:
chapterPath = chapter["path"]
url = "https://iceportal.de/api1/rs/audiobooks/path{}".format(
chapterPath)
responseDownloadPath = requests.get(
url, headers=cfg.headers, cookies=cfg.cookies)
path = json.loads(responseDownloadPath.text)["path"]
downloadPath.append(path)
createFolder('./audiobooks/{}'.format(title))
# download each track
for counter, track in enumerate(downloadPath):
print("{}/{}".format(counter+1, len(downloadPath)))
url = "https://iceportal.de{}".format(track)
audio = requests.get(url)
savePath = "audiobooks/{}/{}_".format(title,
title)+str(counter+1)+".mp3"
with open(savePath, "wb+") as code:
code.write(audio.content)
# MAIN
# extract all audiobooks
audiobooks = getAllAudiobooks()
createFolder('./audiobooks')
# download all audibooks
for book in audiobooks:
downloadAudiobook(str(book))