-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.py
executable file
·60 lines (46 loc) · 2.07 KB
/
audio.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
#!/usr/bin/env python3
import os
import re
from argparse import ArgumentParser
# width of the download progress bar
BAR_SIZE = 50
def download_audio(audio_url, session, title=None):
res = session.get(audio_url)
onelined = re.sub(r"\s+", " ", res.text)
if not title:
title = re.findall(r"<div class=\"card-header\">\s*<h3>(.*)</h3>", onelined)[0]
stream_url = re.findall(r".*src:\ '(https://stream5.uni-regensburg.de/audio.*\.mp3.*)'}\);.*", onelined)[0]
print(stream_url)
output_filename = f"{title}.mp3"
if os.path.exists(output_filename):
print(output_filename, "already exists")
return
# https://stackoverflow.com/a/15645088
print("Downloading", title)
with open(output_filename, "wb") as f:
res = session.get(stream_url, stream=True)
total_length = res.headers.get('content-length')
if total_length is None:
# no content length header
f.write(res.content)
else:
dl = 0
total_length = int(total_length)
for data in res.iter_content(chunk_size=4096):
dl += len(data)
f.write(data)
done = int(BAR_SIZE * dl / total_length)
print(f"\r[", '=' * done, ' ' * (BAR_SIZE - done), "]", sep="", end="", flush=True)
print()
if __name__ == "__main__":
from util import get_authenticated_session, get_credentials
parser = ArgumentParser(description="""A tool to download audio from the UR Mediathek.
To use it, you must have a credentials.json file in the current directory which contains the keys 'account' and 'password'.
""")
parser.add_argument("url",
help="The URL of the site that shows the audio. Usually begins with https://mediathek2.uni-regensburg.de/playthis/")
parser.add_argument("--title", default=None, help="Overwrite the title derived from the mediathek.")
args = parser.parse_args()
print("Starting session")
session = get_authenticated_session(get_credentials())
download_audio(args.url, session, args.title)