-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlecture_downloader.py
117 lines (92 loc) · 2.92 KB
/
lecture_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
#!/usr/bin/env python3
import argparse
import subprocess
import requests
import re
import os
# arguments
# ##################################################
# check if output file already exists
# if not check if dir does already exist
def validate_output_file(p, filepath) -> str:
abspath = os.path.abspath(filepath)
dirname = os.path.dirname(abspath)
if os.path.isfile(abspath):
p.error(f'The file {filepath} does already exist.')
elif not os.path.exists(dirname):
p.error(f'The directory {dirname} does not exist.')
else:
return filepath
def validate_time_string(p, time_string):
time_string_regex = re.compile(r'^[0-9]+:[0-5][0-9]:[0-5][0-9]$')
if not time_string_regex.match(time_string):
p.error(f'Time could not be parsed: {time_string}')
return time_string
def arguments():
parser = argparse.ArgumentParser(description="Download lectures")
parser.add_argument(
'-u',
dest='url',
required=True,
help='stream playlist url',
metavar='URL',
type=str
)
parser.add_argument(
'-o',
dest='output_file',
required=True,
help='output file name',
metavar='OUT_FILE',
type=lambda x: validate_output_file(parser, x),
)
parser.add_argument(
'-t',
dest='timeout',
required=False,
help='record time',
metavar='TIME',
type=lambda x: validate_time_string(parser, x),
)
return parser.parse_args()
# Playlist extraction
# ##################################################
def extract_playlist_links(url) -> list:
html = _fetch_html(url)
regex = re.compile(r'\bhttps://.*.m3u8\b')
lst = regex.findall(html)
return lst
def _fetch_html(url) -> str:
if not (url.startswith('http://') or url.startswith('https://')):
url = 'http://' + url
response = requests.get(url)
return response.text
# command line interaction
# ##################################################
def select_playlist(playlists) -> str:
for i, playlist in enumerate(playlists):
print(f'[{i + 1}] {playlist}')
n = -1
while n not in range(1, len(playlists) + 1):
try:
n = int(input('[!] select stream: '))
except:
n = -1
return playlists[n]
# main
# ##################################################
def main():
args = arguments()
playlists = extract_playlist_links(args.url)
if len(playlists) == 0:
print('[i] no stream found')
exit(0)
playlist_url = playlists[0]
if len(playlists) > 1:
print('[i] more than one stream found. select one')
playlist_url = select_playlist(playlists)
time_string = f' -t {args.timeout}' if args.timeout else ""
command = f'ffmpeg {time_string} -i "{playlist_url}" -codec copy {args.output_file}'
subprocess.call(command, shell=True)
if __name__ == '__main__':
main()