-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudiobook_download.py
425 lines (279 loc) · 12.4 KB
/
audiobook_download.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/python3
# -*- coding: utf-8 -*-
""" Script for downloading multiple resources from a WWW page (e.g. chapters of an audiobook) """
import sys
import os
import re
import urllib.request
from random import randint
import music_tag
HELP="""
audiobook_download [options] [URLs|PHRASE]
This script downloads MP3 files from provided URLs. It tags them and creates playlist out of them.
Useful for downloading audiobook chapters from a WWW page
Options:
--search [PHRASE] Search for available audiobooks
--output_dir To what location should the files be downloaded?
--pls Create playlist file in target directory
--no_tag Skip retagging. By default, title tag will be updated with chapter titles to
help dispaying and sorting in media players
--dry_run No downloading, only file list and hrefs will be displayed to check
if everything is in order (e.g. if REGEX found some valid resource URLs)
--supported List supported audiobook providers
--version Display version
--help, -h Display this message
Dependencies:
This program uses music_tag library. Install it by typing: pip3 install music_tag
"""
USER_AGENT="Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20100101 Firefox/10.0"
PROVIDERS = {
'https://fulllengthaudiobooks.com/': {
'ext':'mp3',
'rx_chapters':re.compile(' src="(https://.*?mp3.*?)"'),
'search':'https://fulllengthaudiobooks.com/?s=%q',
'rx_slinks':re.compile('<h2 class="entry-title post-title"><a href="(.*?)".*?rel="bookmark">.*?</a></h2>'),
'rx_stitles':re.compile('<h2 class="entry-title post-title"><a href=".*?".*?rel="bookmark">(.*?)</a></h2>')
},
'https://librivox.org/': {'ext':'mp3',
'rx_chapters':re.compile('<tr>(.*?)</tr>', re.DOTALL),
'rx_href':re.compile('</td>.*?<td><a href="(.*?\.mp3.*?)" class="chapter-name">', re.DOTALL),
'rx_title':re.compile('class="chapter-name">(.*?)</a></td>', re.DOTALL),
'title_from_href':True
},
'https://goldenaudiobooks.com/': {'copy_from':'https://fulllengthaudiobooks.com/',
'search': 'https://goldenaudiobooks.com/?s=%q',
'rx_slinks':re.compile('<h2 class="entry-title"><a href="(.*?)".*?rel="bookmark">.*?</a></h2>'),
'rx_stitles':re.compile('<h2 class="entry-title"><a href=".*?".*?rel="bookmark">(.*?)</a></h2>')
},
'https://bookaudiobooks.com/': {'copy_from':'https://fulllengthaudiobooks.com/',
'search': 'https://bookaudiobooks.com/?s=%q',
'rx_slinks':re.compile('<h2 class="entry-title"><a href="(.*?)".*?rel="bookmark">.*?</a></h2>'),
'rx_stitles':re.compile('<h2 class="entry-title"><a href=".*?".*?rel="bookmark">(.*?)</a></h2>')
},
'https://101audiobooks.com': {'ext':'mp3',
'rx_chapters':re.compile(' src="(https://.*?mp3.*?)"'),
'search': 'https://101audiobooks.com/?s=%q',
'rx_slinks':re.compile('<h2 class="entry-title"><a href="(.*?)".*?rel="bookmark">.*?</a></h2>'),
'rx_stitles':re.compile('<h2 class="entry-title"><a href=".*?".*?rel="bookmark">(.*?)</a></h2>')
},
'https://archive.org/': {'ext':'mp3',
'rx_chapters':re.compile('<div itemprop="(.*?)</div>', re.DOTALL),
'rx_href':re.compile('<link itemprop="associatedMedia" href="([^<]*?\.mp3)">', re.DOTALL),
'rx_title':re.compile('content="(.*?)"'),
'title_from_href':False,
'search':'https://archive.org/search.php?query=%q&and[]=mediatype%3A%22audio%22&and[]=subject%3A%22audiobook%22',
'rx_slinks':re.compile('<a href="([^<]*?)" title=".*?".*?data-event-click-tracking="GenericNonCollection|ItemTile">', re.DOTALL),
'rx_stitles':re.compile('<a href="[^<]*?" title="(.*?)".*?data-event-click-tracking="GenericNonCollection|ItemTile">', re.DOTALL),
'slinks_prepend': 'https://archive.org'
},
'https://etc.usf.edu/lit2go/': { 'ext':'mp3',
'rx_chapters':re.compile('<source src="([^<"]*?\.mp3[^<"]*?)".*?/>'),
'title_from_href':True,
'search':'https://etc.usf.edu/lit2go/search/?q=%q',
'rx_slinks':re.compile('<h4><a href="([^<]*?)">.*?</a></h4>'),
'rx_stitles':re.compile('<h4><a href="[^<]*?">(.*?)</a></h4>')
}
}
VERSION="1.0.0"
ERR_COL='\033[1;31m'
DEF_COL='\033[0m'
LINK_COL='\033[0;34m'
def slist(lst, idx, default):
""" Safely extract list element or give default """
try: return lst[idx]
except (IndexError, TypeError, ValueError, KeyError) as e: return default
def scast(value, target_type, default_value):
""" Safely cast into a given type or give default value """
try:
if value is None: return default_value
return target_type(value)
except (ValueError, TypeError): return default_value
def download_html(url:str):
""" Download main page into str"""
try:
req = urllib.request.Request(url, None, {'User-Agent':USER_AGENT})
response = urllib.request.urlopen(req)
html = response.read().decode("utf-8")
except (urllib.error.URLError, ValueError, TypeError, OSError) as e:
print(f"""\n{ERR_COL}Error downloading main page from {url}: {e}{DEF_COL}""")
sys.exit(1)
return html
def prepare_phrase(phrase:str):
phrase = phrase.replace(' ','+')
phrase = phrase.replace('/','+')
return phrase
def search(phrase:str, **kargs):
""" Search providers for audiobooks and display results """
phrase = prepare_phrase(phrase)
results = []
for pr, body in PROVIDERS.items():
search = body.get('search')
rx_slinks = body.get('rx_slinks')
rx_stitles = body.get('rx_stitles')
prepend = body.get('slinks_prepend','')
if search is None or rx_slinks is None or rx_stitles is None: continue
search = search.replace('%q',phrase)
html = download_html(search)
links = re.findall(rx_slinks, html)
titles = re.findall(rx_stitles, html)
for i,l in enumerate(links):
if l in (None,''): continue
l = f'{prepend}{l}'
t = slist(titles, i, '<ERROR>')
t = re.sub('&#.*?;','',t)
results.append ( (l, t) )
if len(results) == 0: print('No matching audiobooks found!')
else:
print(f'Found {len(results)} matching audiobooks:\n')
for r in results:
print(f'{r[1]} ---->{LINK_COL}{r[0]}{DEF_COL}')
return results
def create_manifest(url:str):
""" Create manifest of chapters contained in WWW page at given URL """
patterns = []
for pr, body in PROVIDERS.items():
if body.get('copy_from') is not None:
body = PROVIDERS.get(body.get('copy_from'))
if url.startswith(pr): patterns.append(body)
if patterns == []:
print(f"{ERR_COL}Provider not supported. Aborting...{DEF_COL}")
sys.exit(1)
html = download_html(url)
manifest = {}
i = 0
for p in patterns:
chapters = re.findall(p.get('rx_chapters',''), html)
for ch in chapters:
if p.get('rx_href','') == '': ch_href = ch
else:
ch_href = slist( re.findall(p.get('rx_href',''), ch), 0, '')
if ch_href == '': continue
if p.get('title_from_href',False):
ch_title = slist( ch_href.split('/'), -1, '')
ch_title = slist( ch_title.split('.'), 0, '')
else:
if p.get('rx_title','') == '': ch_title = ''
else: ch_title = slist( re.findall(p.get('rx_title',''), ch), 0, '')
i += 1
if ch_title == '':
basename = f'Part {str(i).zfill(3)}.{p.get("ext","unknown")}'
ch_title = basename
else: basename = f'{ch_title} ({str(i).zfill(3)}).{p.get("ext","unknown")}'
manifest[basename] = { 'href': ch_href, 'title': ch_title }
return manifest
def download_ress(manifest:dict, output_dir:str, **kargs):
""" Download and save chapters from manifest """
dry_run = kargs.get('dry_run', False)
if not os.path.isdir(output_dir):
print(f"""{ERR_COL}Target directory does not exist! Aborting...{DEF_COL}""")
sys.exit(1)
for basename, item in manifest.items():
manifest[basename]['downloaded'] = False
href = item.get('href','')
if href == '':
print(f"""Empty URL for {basename}. Ignoring...""")
continue
file = f'{output_dir}/{basename}'
manifest[basename]['file'] = file
if dry_run:
print(f'{file}: <---{LINK_COL}{href}{DEF_COL}')
continue
print(f'\nDownloading: {href}')
if os.path.isfile(file) or os.path.isdir(file):
print(f"""{ERR_COL}File {file} already exists! Ignoring...{DEF_COL}""")
continue
try:
req = urllib.request.Request(href, None, {'User-Agent':USER_AGENT})
response = urllib.request.urlopen(req)
with open(file, 'wb') as f:
f.write(response.read())
manifest[basename]['downloaded'] = True
print('Done...')
except (urllib.error.URLError, ValueError, TypeError, OSError) as e:
print(f"""\n{ERR_COL}Error downloading {href}: {e}{DEF_COL}\n\n""")
def gen_playlist(manifest:dict, output_dir:str, format:str, **kargs):
""" Generate playlist file """
dry_run = kargs.get('dry_run',False)
file = f'{output_dir}/playlist.{format}'
if format == 'pls':
pls = f"""[playlist]\n\n"""
i = 1
for basename, item in manifest.items():
item = item.get('title')
pls = f"""{pls}File{i}={basename}\nTitle{i}={item}\n\n"""
i += 1
if os.path.isfile(file) or os.path.isdir(file):
print(f"""\n\nFile {file} already exists! Ignoring...""")
return 1
if dry_run:
print(f"""Playlist saved to {file}...""")
return 0
try:
with open(file, 'w') as f:
f.write(pls)
print(f"""Playlist saved to {file}...""")
except OSError as e:
print(f"""\n{ERR_COL}Error wriring to {file}: {e}{DEF_COL}\n\n""")
def retag(manifest:list, **kargs):
""" Append chapter names to Title tags """
for basename, item in manifest.items():
file = item.get('file')
try:
f = music_tag.load_file(file)
title = f['title']
if item.get('title','') not in scast(title, str, ''):
title = f"""{title} {item.get('title','')}"""
f['title'] = title
f.save()
print(f'File {file}: Title tag changed to "{title}"')
except Exception as e:
print(f"""\n{ERR_COL}Error tagging {file}: {e}{DEF_COL}""")
def process(url, **kargs):
""" Main processing function """
dry_run = kargs.get('dry_run',False)
output_dir = kargs.get('output_dir','')
playlist = kargs.get('playlist',False)
pl_format = kargs.get('pl_format','pls')
no_tag = kargs.get('no_tag',False)
manifest = create_manifest(url)
download_ress(manifest, output_dir, dry_run=dry_run)
if playlist: gen_playlist(manifest, output_dir, pl_format, dry_run=dry_run)
if not no_tag and not dry_run: retag(manifest)
def main():
output_dir = os.getcwd()
dry_run = False
playlist = False
pl_format = None
no_tag = False
if len(sys.argv) > 1:
for i, arg in enumerate(sys.argv):
if i == 0: continue
if arg.startswith('--output_dir='):
args = arg.split('=',1)
if len(args) > 1: output_dir = args[1]
continue
elif arg == '--version': print(VERSION)
elif arg in ('--help','-h'): print(HELP)
elif arg == '--supported':
for s in PROVIDERS.keys(): print(s)
elif arg == '--search':
phrase = slist(sys.argv, i+1, '')
if phrase == '':
print('No search phrase given!')
break
search(phrase)
break
elif arg == '--dry_run':
dry_run = True
continue
elif arg == '--no_tag':
no_tag = True
continue
elif arg == '--pls':
playlist = True
pl_format = 'pls'
continue
else:
process(arg, dry_run=dry_run, output_dir=output_dir, playlist=playlist, pl_format=pl_format, no_tag=no_tag)
if __name__ == '__main__':
main()