This repository has been archived by the owner on Mar 31, 2019. It is now read-only.
/
ustvnow.py
419 lines (350 loc) · 14 KB
/
ustvnow.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
#!/usr/bin/env python
from __future__ import print_function, division, unicode_literals
try:
input = raw_input
except NameError:
pass
from collections import defaultdict
import m3u8
import requests
###################
# USTVnow API calls
###################
def get_auth_token(username, password, include_resp=False):
params = {
'username': username,
'password': password,
'device': 'gtv',
'redir': '0'
}
resp = requests.get('https://m.ustvnow.com/gtv/1/live/login',
params=params)
resp.raise_for_status()
try:
token = resp.json()['token']
if not token:
raise RuntimeError('No token was returned')
except Exception as e:
setattr(e, 'response', resp)
raise e
return (token, resp) if include_resp else token
def get_channel_list(auth_token, include_resp=False):
params = {
'token': auth_token
}
resp = requests.get('https://m.ustvnow.com/gtv/1/live/listchannels',
params=params)
resp.raise_for_status()
try:
channels = [{
'name': channel['sname'],
'img': 'https://m.ustvnow.com/' + channel['img'],
'code': channel['scode'],
'callsign': channel['callsign'],
'available': bool(channel['t'])
} for channel in resp.json()['results']['streamnames']]
except Exception as e:
setattr(e, 'response', resp)
raise e
return (channels, resp) if include_resp else channels
def get_channel_playlist_url(auth_token, channel_code, include_resp=False):
params = {
'token': auth_token,
'scode': channel_code
}
resp = requests.get('https://m.ustvnow.com/stream/1/live/view',
params=params)
resp.raise_for_status()
try:
url = resp.json()['stream']
except Exception as e:
setattr(e, 'response', resp)
raise e
return (url, resp) if include_resp else url
###############################
# M3U8 playlist-related helpers
###############################
def get_playlist(url, include_resp=False):
resp = requests.get(url)
resp.raise_for_status()
try:
playlist = m3u8.loads(resp.text)
except Exception as e:
setattr(e, 'response', resp)
raise e
return (playlist, resp) if include_resp else playlist
def group_playlists_by_quality(playlist):
qualities = defaultdict(list)
for p in playlist.playlists:
qualities[p.stream_info].append(p)
return qualities
def make_playlist(stream_playlists):
playlist = m3u8.M3U8()
for p in stream_playlists:
playlist.add_playlist(p)
return playlist.dumps()
#####
# CLI
#####
if __name__ == '__main__':
import argparse
from getpass import getpass
import os
from pprint import pformat
import subprocess
import sys
try:
from codec_names import codec_names
except ImportError:
codec_names = {}
def indent(text, columns):
return '\n'.join(' '*columns + line for line in text.splitlines())
def format_si(num):
for unit in ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z']:
if abs(num) < 1024:
return '{:3.1f}{}'.format(num, unit)
num /= 1024
return '{:.1f}{}'.format(num, 'Y')
def describe_codecs(codecs_str):
descriptions = []
for c in codecs_str.split(','):
c = c.strip().lower()
if c in codec_names:
descriptions.append(codec_names[c])
else:
descriptions.append('Unknown Codec ({})'.format(c))
return ', '.join(descriptions)
def describe_quality_info(quality):
if quality.resolution is None:
resolution_str = '(unknown resolution)'
else:
resolution_str = '{}x{}'.format(quality.resolution[0],
quality.resolution[1])
return '{}, {}bps, codecs: {}'.format(resolution_str,
format_si(quality.bandwidth),
describe_codecs(quality.codecs))
def prompt_number(low, high):
while True:
num = input('({}-{}): '.format(low, high))
try:
num = int(num)
if num >= low and num <= high:
return num
else:
raise ValueError()
except ValueError:
print('Invalid selection. ', end='')
def prompt_yes_no():
while True:
val = input('(Y/N): ').lower()
if val == 'y':
return True
elif val == 'n':
return False
else:
print('Invalid selection. ', end='')
def prompt_username_password(username, password):
if not username or not password:
print('[i] Enter your USTVnow login credentials:')
if username:
print(indent('Using username {}'.format(username), 6))
while not username:
username = input(indent('Username: ', 6))
if password:
print(indent('Using password from arguments', 6))
while not password:
password = getpass(str(indent('Password: ', 6)))
return (username, password)
def prompt_channel_code(channel_list, channel_code):
if channel_code is not None:
try:
coded_channel = next(channel for channel in channel_list
if channel['code'] == channel_code)
if coded_channel['available']:
return channel_code
else:
print("[!] Channel '{}' exists but is marked as "
"unavailable. Use anyway? "
.format(channel_code), end='')
if prompt_yes_no():
return channel_code
except StopIteration:
print("[!] Unknown channel '{}'.".format(channel_code))
available_channels = [channel for channel in channel_list
if channel['available']]
print('[i] Select a channel:')
for i, channel in enumerate(available_channels):
print(indent('{}) {}: {} ({})'
.format(i + 1, channel['code'], channel['name'],
channel['callsign']), 6))
print(' ', end='')
channel_num = prompt_number(1, len(available_channels))
return available_channels[channel_num - 1]['code']
def prompt_channel_quality(qualities, bitrate):
qualities.sort(key=lambda quality: quality.bandwidth, reverse=True)
if bitrate is not None:
if bitrate.lower() == 'highest':
print('[i] The highest-bitrate stream was: {}.'
.format(describe_quality_info(qualities[0])))
return qualities[0]
else:
try:
bitrate = int(bitrate)
selected_quality = next((quality for quality in qualities
if quality.bandwidth == bitrate),
None)
except ValueError:
selected_quality = None
if selected_quality is not None:
return selected_quality
else:
print('[!] Channel bitrate {} does not exist.'
.format(bitrate))
print('[i] Select desired quality:')
for i, quality in enumerate(qualities):
print(indent('{}) {}: {}'
.format(i + 1, quality.bandwidth,
describe_quality_info(quality)), 6))
print(' ', end='')
quality_num = prompt_number(1, len(qualities))
return qualities[quality_num - 1]
def prompt_filename_if_needed(filename):
if os.path.isfile(filename):
print("[!] File '{}' already exists, overwrite? "
.format(filename), end='')
if not prompt_yes_no():
filename = ''
while not filename:
prompt_filename = input('[i] Enter new filename: ').strip()
filename = prompt_filename + '.m3u8'
return filename
def format_request(req):
head = '{} {}\n{}'.format(
req.method, req.url,
'\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items())
)
return (head + '\n\n{}'.format(req.body)) if req.body else head
def format_response(resp):
head = '{} {}\n{}'.format(
resp.status_code, resp.reason,
'\n'.join('{}: {}'.format(k, v) for k, v in resp.headers.items())
)
return (head + '\n\n{}'.format(resp.text)) if resp.text else head
def format_playlist_qualities(playlists_by_quality):
output = ''
for quality in playlists_by_quality:
output += str(quality) + '\n'
for playlist in playlists_by_quality[quality]:
output += indent('- ' + playlist.uri, 2) + '\n'
return output
def print_request_response(resp):
print('[*] Sent request:')
print(indent(format_request(resp.request), 6))
print('[*] Received response:')
print(indent(format_response(resp), 6))
def make_api_request(api_func, *params):
try:
(value, resp) = api_func(*params, include_resp=True)
except Exception as e:
print('failed.')
print('[!] Error: {}.'.format(e))
print_request_response(e.response)
sys.exit(1)
print('done.')
if args.verbose:
print_request_response(resp)
return value
def open_with_default_program(filename):
if sys.platform.startswith('darwin'):
subprocess.call(('open', filename))
elif os.name == 'nt':
os.startfile(filename)
elif os.name == 'posix':
subprocess.call(('xdg-open', filename))
#
# read command-line arguments
#
parser = argparse.ArgumentParser(description='A simple command-line '
'client for USTVnow.')
parser.add_argument('-u', '--username',
help='USTVnow username (email address)')
parser.add_argument('-p', '--password', help='USTVnow password')
parser.add_argument('-c', '--channel',
help='channel code '
'(run without this argument to see a list)')
parser.add_argument('-q', '--quality',
help='channel bitrate in bps '
'(run without this argument to see a list, '
'or use \'highest\' to use the highest-bitrate '
'stream available)')
parser.add_argument('-d', '--download-only', action='store_true',
help='just download the m3u8 file for the channel '
'without playing')
parser.add_argument('-o', '--output-filename',
help='override the output m3u8 filename')
parser.add_argument('-v', '--verbose', action='store_true',
help='show some debugging information')
args = parser.parse_args()
#
# login, get token
#
(username, password) = prompt_username_password(args.username,
args.password)
print('[+] Logging in... ', end='')
token = make_api_request(get_auth_token, username, password)
if args.verbose:
print('[*] Got token: {}'.format(token))
#
# get channel list
#
print('[+] Retrieving channel list... ', end='')
channel_list = make_api_request(get_channel_list, token)
num_avail_channels = sum(channel['available'] for channel in channel_list)
if args.verbose:
print('[*] Got channel data ({} available, {} total):'
.format(num_avail_channels, len(channel_list)))
print(indent(pformat(channel_list), 6))
else:
print('[*] Got {} available channels ({} total).'
.format(num_avail_channels, len(channel_list)))
#
# select channel, get playlist data
#
channel_code = prompt_channel_code(channel_list, args.channel)
print('[+] Retrieving channel playlist URL... ', end='')
playlist_url = make_api_request(get_channel_playlist_url, token,
channel_code)
if args.verbose:
print('[*] Got playlist URL (for channel {}): {}'
.format(channel_code, playlist_url))
print('[+] Retrieving channel playlist... ', end='')
playlist = make_api_request(get_playlist, playlist_url)
playlists_by_quality = group_playlists_by_quality(playlist)
if args.verbose:
print('[*] Got playlists by quality (for channel {}):'
.format(channel_code))
print(indent(format_playlist_qualities(playlists_by_quality), 6))
#
# select playlists, create m3u8 file
#
selected_quality = prompt_channel_quality(list(playlists_by_quality
.keys()), args.quality)
selected_playlists = playlists_by_quality[selected_quality]
output_data = make_playlist(selected_playlists)
if args.verbose:
print('[*] Created output m3u8 data:')
print(indent(output_data, 6))
default_filename = '{}_{}.m3u8'.format(channel_code,
selected_quality.bandwidth)
output_filename = prompt_filename_if_needed(args.output_filename or
default_filename)
with open(output_filename, 'wb') as f:
f.write(output_data.encode('utf-8'))
print("[+] Wrote channel playlist as '{}'.".format(output_filename))
#
# play output file
#
if not args.download_only:
print("[+] Opening '{}'... ".format(output_filename), end='')
open_with_default_program(output_filename)
print('done.')