-
Notifications
You must be signed in to change notification settings - Fork 0
/
drdown.py
executable file
·243 lines (184 loc) · 7.16 KB
/
drdown.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
#!/usr/bin/python
import sys
import os
import re
import logging
import json
if sys.version_info < (3, 0):
from urllib2 import Request
from urllib2 import urlopen
from urlparse import urlparse
else:
from urllib.request import Request
from urllib.request import urlopen
from urllib.parse import urlparse
raw_input = input
useragent = 'Mozilla/5.0'
headers = {'User-Agent': useragent}
intro = """
Usage: drdown.py url
This script finds the stream URL from a dr.dk page so you can
download the tv program.
"""
def fetch(url):
"""Download body from url"""
req = Request(url, headers=headers)
response = urlopen(req)
body = response.read()
response.close()
# convert to string - it is easier to convert here
if isinstance(body, bytes):
body = body.decode('utf8')
return body
class StreamExtractor:
def __init__(self, url):
self.url = url.lower()
self.urlp = urlparse(self.url)
def get_stream_data(self):
"""supply a URL to a video page on dr.dk and get back a streaming
url"""
if self.urlp.netloc not in ('dr.dk', 'www.dr.dk'):
raise Exception("Must be an URL from dr.dk")
self.html = fetch(self.url)
logging.info("Player fetched: " + self.url)
# Standalone show?
if self.urlp.path.startswith('/tv/se/'):
return self.get_stream_data_from_standalone()
# Bonanza?
elif self.urlp.path.startswith('/bonanza/'):
return self.get_stream_data_from_bonanza()
# Live tv?
elif self.urlp.path.startswith('/tv/live'):
return self.get_stream_data_from_live()
else:
return self.get_stream_data_from_series()
def get_stream_data_from_rurl(self, rurl):
"""Helper method to parse resource JSON document"""
body = fetch(rurl)
resource_data = json.loads(body)
qualities = resource_data.get('links')
# sort by quality
qualities = sorted(qualities, key=lambda x: x['bitrateKbps'],
reverse=True)
stream_data = qualities[0]
stream_url = stream_data.get('uri')
logging.info("Stream data fetched: " + stream_url)
playpath, filename = self.get_metadata_from_stream_url(stream_url)
stream_data = {'stream_url': stream_url,
'playpath': playpath,
'filename': filename,
'is_live': False}
return stream_data
def get_metadata_from_stream_url(self, stream_url):
"""Helper method to extacts playpath and filename suggestion from a
rtmp url"""
parsed = urlparse(stream_url)
playpath_s = parsed.path.split('/')[2:]
playpath = '/'.join(playpath_s)
# rerun to split the parameter
path = urlparse(parsed.path).path
filename = path.split('/')[-1]
return playpath, filename
def get_stream_data_from_standalone(self):
"""Extracts stream data from a normal single program page.
The data is hidden in a resource URL, that we need to download
and parse.
"""
mu_regex = re.compile('resource: "([^"]+)"')
m = mu_regex.search(self.html)
if m and m.groups():
resource_meta_url = m.groups()[0]
return self.get_stream_data_from_rurl(resource_meta_url)
def get_stream_data_from_bonanza(self):
"""Finds stream URL from bonanza section. Just pick up the first RTMP
url.
"""
stream_regex = re.compile('rtmp://.*?\.mp4')
m = stream_regex.search(self.html)
if m and m.group():
stream_url = m.group()
else:
raise Exception("Could not find Bonanza stream URL")
playpath, filename = self.get_metadata_from_stream_url(stream_url)
stream_data = {'stream_url': stream_url,
'playpath': playpath,
'filename': filename,
'is_live': False}
return stream_data
def get_stream_data_from_live(self):
stream_url = 'rtmp://livetv.gss.dr.dk/live'
quality = '3'
playpaths = {'dr1': 'livedr01astream',
'dr2': 'livedr02astream',
'dr-ramasjang': 'livedr05astream',
'dr-k': 'livedr04astream',
'dr-update-2': 'livedr03astream',
'dr3': 'livedr06astream'}
urlend = self.urlp.path.split('/')[-1]
playpath = playpaths.get(urlend)
filename = 'live.mp4'
if playpath:
playpath += quality
filename = urlend + '.mp4'
stream_data = {'stream_url': stream_url,
'playpath': playpath,
'filename': filename,
'is_live': True}
return stream_data
def get_stream_data_from_series(self):
"""dr.dk has a special player for multi episode series. This is the
fall back parser, as there seems to be no pattern in the URL."""
slug_regex = re.compile('seriesSlug=([^"]+)"')
m = slug_regex.search(self.html)
if m and m.groups():
slug_id = m.groups()[0]
else:
raise Exception("Could not find program slug")
logging.info("found slug: " + slug_id)
program_meta_url = 'http://www.dr.dk/nu/api/programseries/%s/videos'\
% slug_id
body = fetch(program_meta_url)
program_data = json.loads(body)
if not program_data:
raise Exception("Could not find data about the program series")
fragment = self.urlp.fragment
if fragment.startswith('/'):
fragment = fragment[1:]
fragment = fragment.split('/')
video_id = fragment[0]
logging.info("Video ID: " + video_id)
video_data = None
if video_id:
for item in program_data:
if item['id'] == int(video_id):
video_data = item
if not video_data:
video_data = program_data[0]
resource_meta_url = video_data.get('videoResourceUrl')
return self.get_stream_data_from_rurl(resource_meta_url)
def generate_cmd(self):
"""Build command line to download stream with the rtmpdump tool"""
sdata = self.get_stream_data()
if not sdata:
return "Not found"
filename = sdata['filename']
custom_filename = raw_input("Type another filename or press <enter> to keep default [%s]: " % filename)
if custom_filename:
filename = custom_filename
cmd_live = 'rtmpdump --live --rtmp="%s" --playpath="%s" -o %s'
cmd_rec = 'rtmpdump -e --rtmp="%s" --playpath="%s" -o %s'
if sdata['is_live'] is True:
cmd = cmd_live % (sdata['stream_url'], sdata['playpath'], filename)
else:
cmd = cmd_rec % (sdata['stream_url'], sdata['playpath'], filename)
return cmd
def main():
if len(sys.argv) > 1:
url = sys.argv[1]
extractor = StreamExtractor(url)
cmd = extractor.generate_cmd()
os.system(cmd)
else:
print(intro)
if __name__ == "__main__":
main()