-
Notifications
You must be signed in to change notification settings - Fork 7
/
mlslive.py
596 lines (478 loc) · 21.4 KB
/
mlslive.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
'''
@author: Micah Galizia <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import os, re, urllib, urllib2, json, cookielib, datetime, uuid
from urlparse import urlparse
def log(msg, error = False):
try:
import xbmc
full_msg = "plugin.video.mlslive: {0}".format(msg)
xbmc.log(full_msg, level=xbmc.LOGERROR if error else xbmc.LOGINFO)
except:
print msg
class MLSLive:
def __init__(self):
"""
Initialize the MLSLive class.
"""
self.BEARER = 'Bearer 94vDO2IN1y963U8NO9Jw8omaG5q94Rht1ERjD6AEnKna90x04lf5Ty6brFsbYs8V'
self.BASIC = 'Basic bWF0Y2hkYXlfYW5kcm9pZDpKN3Q4dzhiRUJUYVVHWDJMZzJaTlZ5WXk='
self.USER_AGENT = 'BAMSDK/1.0.4 (mlsoccer-F73A6101; 1.0.0; google; handset) google Nexus 9 (N4F26Q; Linux; 7.1.1; API 25)'
self.TOKEN_PAGE = 'https://global-api.live-svcs.mlssoccer.com/token'
self.LOGIN_PAGE = 'https://global-api.live-svcs.mlssoccer.com/v2/user/identity'
self.CLUBS_PAGE = 'https://api.mlsdigital.net/www.mlssoccer.com/clubs?'
self.MATCHES_PAGE = 'https://api.mlsdigital.net/www.mlssoccer.com/matches?'
self.GRAPHGL_PAGE = 'https://cops-prod.live-svcs.mlssoccer.com/graphql'
self.GRAPHGL_QUERY= '?query={{%0A%20%20Schedule(gamePks:%20%22{0}%22)%20{{%0A%20%20%20%20dates%20{{%0A%20%20%20%20%20%20games%20{{%0A%20%20%20%20%20%20%20%20media%20{{%0A%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20videos%20{{%0A%20%20%20%20%20%20%20%20%20%20%20%20contentId%0A%20%20%20%20%20%20%20%20%20%20%20%20runTime%0A%20%20%20%20%20%20%20%20%20%20%20%20type%0A%20%20%20%20%20%20%20%20%20%20%20%20...on%20Video%20{{%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20media%20{{%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mediaId%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mediaState%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20playbackUrls%20{{%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20href%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20}}%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20}}%0A%20%20%20%20%20%20%20%20%20%20%20%20}}%0A%20%20%20%20%20%20%20%20%20%20%20%20...on%20Airing%20{{%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20mediaId%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20eventId%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20linear%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20playbackUrls%20{{%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20href%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20}}%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20mediaConfig%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20{{%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20state%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20productType%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20type%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20}}%0A%20%20%20%20%20%20%20%20%20%20%20%20}}%0A%20%20%20%20%20%20%20%20%20%20}}%0A%20%20%20%20%20%20%20%20}}%0A%20%20%20%20%20%20}}%0A%20%20%20%20}}%0A%20%20}}%0A}}'
def total_seconds(self, dt):
# Keep backward compatibility with Python 2.6 which doesn't have
# this method
if hasattr(dt, 'total_seconds'):
return dt.total_seconds()
else:
return (dt.microseconds + (dt.seconds + dt.days * 24 * 3600) * 10**6) / 10**6
def getAddonFolder(self):
try:
import xbmc, xbmcaddon
return xbmc.translatePath(xbmcaddon.Addon().getAddonInfo('profile'))
except:
return os.getcwd()
def getSettingsFile(self):
return os.path.join(self.getAddonFolder(), 'settings.json')
def getCookieFile(self):
return os.path.join(self.getAddonFolder(), 'cookies.lwp')
def createCookieJar(self):
cookie_file = self.getCookieFile()
return cookielib.LWPCookieJar(cookie_file)
def loadCookieJar(self):
jar = cookielib.LWPCookieJar()
cookie_file = self.getCookieFile()
jar.load(cookie_file,ignore_discard=True)
return jar
def postToken(self, xff, token=None):
"""
Get the token from MLBAM for MLS soccer.
@param token if specified, the toke to use for token authentication
"""
jar = self.createCookieJar()
#urllib2.HTTPHandler(debuglevel=1),urllib2.HTTPSHandler(debuglevel=1)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
opener.addheaders = [('Authorization', self.BEARER),
('User-Agent', urllib.quote(self.USER_AGENT))]
if not xff == None:
opener.addheaders.append(('X-Forwarded-For', xff))
values = { 'platform' : 'android',
'latitude' : '61.172110800000000',
'longitude' : '149.83626080000000',
'grant_type' : 'client_credentials',
'token' : 'BAMSDK_mlsoccer-F73A6101_prod_'}
if token == None:
values['token'] += str(uuid.uuid4())
else:
values['grant_type'] = 'urn:mlbam:params:oauth:grant_type:token'
values['token'] = token
try:
resp = opener.open(self.TOKEN_PAGE, urllib.urlencode(values))
except urllib2.HTTPError as err:
log("postToken {0}: '{1}'".format(err.code, err.reason), True)
return None
except urllib2.URLError as err:
log("postToken: '{0}'".format(err.reason), True)
return None
jar.save(filename=self.getCookieFile(), ignore_discard=True, ignore_expires=True)
resp_json = resp.read()
jsobj = json.loads(resp_json)
return jsobj
def postIdentity(self, xff, username, password, token):
"""
Post the user credentials to the identiy service.
@param username The username
@param password The password
@param token The token generated from the token service
"""
js_obj = {'email': {'address': username},
'password':{'value': password},
'type':'email-password'}
js_data = json.dumps(js_obj)
jar = self.createCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
rq_headers = {'Authorization': token,
'User-Agent': self.USER_AGENT,
'Content-Type': 'application/json',
'Accept': 'application/vnd.identity-service+json; version=1.0',
'Content-Length': len(js_data)}
if not xff == None:
rq_headers['X-Forwarded-For'] = xff
req = urllib2.Request(self.LOGIN_PAGE, data=js_data, headers=rq_headers)
try:
resp = opener.open(req)
except urllib2.HTTPError as err:
log("postIdentity {0}: '{1}'".format(err.code, err.reason), True)
return None
except urllib2.URLError as err:
log("postIdentity: '{0}'".format(err.reason), True)
return None
jar.save(filename=self.getCookieFile(), ignore_discard=True, ignore_expires=True)
js_obj = json.loads(resp.read())
return js_obj['code']
def getMatches(self, start, end, xff):
"""
Get the matches within a time frame
"""
jar = self.createCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
opener.addheaders = [('accept-version', '2.0.1'),
('Authorization', self.BASIC)]
if not xff == None:
opener.addheaders.append(('X-Forwarded-For', xff))
url = self.MATCHES_PAGE + urllib.urlencode({ 'startdate' : start, 'enddate' : end })
try:
resp = opener.open(url)
except urllib2.HTTPError as err:
log("getMatches {0}: '{1}'".format(err.code, err.reason), True)
return None
except urllib2.URLError as err:
log("getMatches: '{0}'".format(err.reason), True)
return None
jar.save(filename=self.getCookieFile(), ignore_discard=True, ignore_expires=True)
js_str = resp.read()
js_obj = json.loads(js_str)
return js_obj
def getClubs(self, xff = None):
"""
Get the list of clubs
"""
jar = self.createCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
opener.addheaders = [('accept-version', '2.0.1'),
('Authorization', self.BASIC)]
if not xff == None:
opener.addheaders.append(('X-Forwarded-For', xff))
url = self.CLUBS_PAGE + urllib.urlencode({ 'ttl' : 7200, 'pagesize' : 300 })
try:
resp = opener.open(url)
except urllib2.HTTPError as err:
log("getClubs {0}: '{1}'".format(err.code, err.reason), True)
return None
except urllib2.URLError as err:
log("getClubs: '{0}'".format(err.reason), True)
return None
jar.save(filename=self.getCookieFile(), ignore_discard=True, ignore_expires=True)
js_str = resp.read()
js_obj = json.loads(js_str)
return js_obj
def postGraphql(self, opta_id, token, xff):
query = self.GRAPHGL_QUERY.format(opta_id)
uri = self.GRAPHGL_PAGE + query
jar = self.createCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
opener.addheaders = [('Authorization', token),
('Accept', 'application/json'),
('User-Agent', urllib.quote(self.USER_AGENT))]
if not xff == None:
opener.addheaders.append(('X-Forwarded-For', xff))
try:
resp = opener.open(uri)
except urllib2.HTTPError as err:
log("postGraphql {0}: '{1}'".format(err.code, err.reason), True)
return None
except urllib2.URLError as err:
log("postGraphql: '{0}'".format(err.reason), True)
return None
jar.save(filename=self.getCookieFile(), ignore_discard=True, ignore_expires=True)
js_str = resp.read()
return json.loads(js_str)
def getEvents(self, uri, token, xff):
jar = self.createCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
opener.addheaders = [('Authorization', token),
('Accept', 'application/vnd.media-service+json; version=1'),
('x-mlbam-player-adapter', 'exoplayer-2.0.x'),
('User-Agent', urllib.quote(self.USER_AGENT))]
if not xff == None:
opener.addheaders.append(('X-Forwarded-For', xff))
try:
resp = opener.open(uri)
except urllib2.HTTPError as err:
log("getEvents {0}: '{1}'".format(err.code, err.reason), True)
log("Unable to get stream details")
try:
js_obj = json.loads(err.read())
except:
raise RuntimeError("Something went wrong parsing event resposne.")
if not 'errors' in js_obj:
raise RuntimeError("No errors in HTTP error")
raise RuntimeError(js_obj['errors'][0])
return None
except urllib2.URLError as err:
log("Url error: {0}".format(err.reason()), True)
raise RuntimeError("URL Error")
jar.save(filename=self.getCookieFile(), ignore_discard=True, ignore_expires=True)
js_str = resp.read()
return json.loads(js_str)
def login(self, username, password, xff = None):
"""
Login to the MLS Live streaming service.
@param username: the user name to log in with
@param password: the password to log in with.
@return: True if authentication is successful, otherwise, False.
"""
token = self.postToken(xff)
if token == None:
log("No token returned")
return False
token = token['access_token']
if token == None:
log("Unable to get token.", True)
return False
code = self.postIdentity(xff, username, password, token)
if code == None:
log("Unable to get code from identify", True)
return False
js_obj = self.postToken(xff, code)
try:
fp = open(self.getSettingsFile(), 'r')
settings = json.load(fp)
fp.close()
# update the auth settings
for key in js_obj.keys():
settings[key] = js_obj[key]
js_obj=settings
except:
pass
# store of the tokens
fp = open(self.getSettingsFile(), 'w')
json.dump(js_obj, fp)
fp.close()
return True
def getWeekRange(self, dt):
"""
Get the epoch values of monday morning at midnight to the following
monday at 11:59:59PM
"""
#roll back to the first day of the week (monday)
start = dt - datetime.timedelta(days=dt.weekday(), hours=dt.hour,
minutes=dt.minute, seconds=dt.second)
end = start + datetime.timedelta(days=8, seconds=-1)
epoch = datetime.datetime(1970,1,1)
return (int(self.total_seconds(start - epoch)),
int(self.total_seconds(end - epoch)))
def getGames(self, dt = None, xff = None):
"""
Get the list of games.
@return json game data
"""
# if no datetime specified just assume now
if dt == None:
dt = datetime.datetime.now()
week_range = self.getWeekRange(dt)
return self.getMatches(week_range[0], week_range[1], xff)
def getTitle(self, game, fmt):
"""
Get the title string
@param game the game json
@param separator text to separate the teams
"""
return fmt.format(game['home']['name']['short'],
game['away']['name']['short'])
def getFullTitle(self, game, fmt):
"""
Get the title string
@param game the game json
@param separator text to separate the teams
"""
return fmt.format(game['home']['name']['full'],
game['away']['name']['full'])
def getDescription(self, game, fmt, def_blackouts, def_venue):
blackouts = def_blackouts
if not game['blackouts'] == None:
blackouts = ''
for blackout in game['blackouts']:
blackouts += blackout + ', '
venue = def_venue
if game['venue'] != None:
venue = game['venue']['name']
return fmt.format(game['home']['name']['full'],
game['away']['name']['full'],
venue, blackouts[0:-2])
def getGameString(self, game, separator):
"""
Get the game title string
@param game the game data dictionary
@param separator string containing the word to separate the home and
away side (eg "at")
@return the game title
"""
game_dt = datetime.datetime.fromtimestamp(game['date'])
dt_str = game_dt.strftime("%m/%d %H:%M")
title = self.getTitle(game, separator)
game_str = '{0} [I]{1}[/I] [B]{2}[/B]'.format(title, game['period'],
dt_str)
return game_str.encode('utf-8').strip()
def getImage(self, game, fav = None):
"""
Get the team image for the game. If the favorite is set and they are one
of the teams in the game, their image will be used, otherwise, the home
team's image will be used.
@param game The game
@param fav the [optional] favorite team
@return the game image or None if something went wrong.
"""
# use the home team unless the away team if the favorite
team = game['home']
if fav and game['away']['id'] == fav:
team = game['away']
if 'logo' in team:
if 'original' in team['logo']:
return team['logo']['original']
return None
def parsePlaylist(self, uri, token, xff):
"""
Parse the playlist and split it by bitrate.
"""
streams = {}
jar = self.createCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
opener.addheaders = [('Authorization', token),
('User-Agent', urllib.quote(self.USER_AGENT))]
if not xff == None:
opener.addheaders.append(('X-Forwarded-For', xff))
try:
resp = opener.open(uri)
except urllib2.HTTPError as err:
log("parsePlaylist {0}: '{1}'".format(err.code, err.reason), True)
return None
except urllib2.URLError as err:
log("parsePlaylist: '{0}'".format(err.reason), True)
return None
jar.save(filename=self.getCookieFile(), ignore_discard=True, ignore_expires=True)
m3u8 = resp.read();
url = urlparse(uri)
prefix = url.scheme + "://" + url.netloc + url.path[:url.path.rfind('/')+1]
suffix = '?' + url.params + url.query + url.fragment
lines = m3u8.split('\n')
bandwidth = ""
for line in lines:
if line == "#EXTM3U":
continue
if line[:17] == '#EXT-X-STREAM-INF':
bandwidth = re.search(".*,?BANDWIDTH\=(.*?),.*", line)
if bandwidth:
bandwidth = bandwidth.group(1)
else:
log("Unable to parse bandwidth")
elif line[-5:] == ".m3u8":
stream = '{0}{1}|User-Agent={2}&Authorization={3}'.format(prefix,
line, urllib.quote(self.USER_AGENT), token)
if not xff == None:
stream += '&X-Forwarded-For={0}'.format(urllib.quote(xff))
streams[bandwidth] = stream
return streams
def getStreams(self, opta_id, xff=None):
"""
Get the stream
@param opta_id the optaId from the games list
"""
try:
fp = open(self.getSettingsFile(), 'r')
settings = json.load(fp)
token = settings['access_token']
fp.close()
except:
log("Unable to load settings so couldn't get access_token", True)
return None
js_obj = self.postGraphql(opta_id, token, xff)
if js_obj == None:
return None
dates = js_obj['data']['Schedule']['dates']
if len(dates) == 0:
log("Unable to load stream metadata. No dates", True)
return None
return dates[0]['games'][0]['media']
def getStreamURIs(self, media, xff=None):
try:
fp = open(self.getSettingsFile(), 'r')
settings = json.load(fp)
token = settings['access_token']
fp.close()
except:
log("Unable to load settings so couldn't get access_token", True)
return None
videos = media['videos'][0]
if 'media' in videos:
videos = videos['media'][0]
uri = videos['playbackUrls'][0]['href']
uri = uri.replace('{scenario}', 'android')
js_obj = self.getEvents(uri, token, xff)
if js_obj == None:
return None
playlist = js_obj['stream']['complete']
streams = self.parsePlaylist(playlist, token, xff)
return streams
def setFavoriteClub(self, id):
try:
fp = open(self.getSettingsFile(), 'r')
settings = json.load(fp)
fp.close()
except:
settings = {}
settings['id'] = id;
fp = open(self.getSettingsFile(), 'w')
json.dump(settings, fp)
fp.close()
def getFavoriteClub(self):
try:
fp = open(self.getSettingsFile(), 'r')
settings = json.load(fp)
fp.close()
except:
log("Unable to load settings so couldn't get favorite club")
return None
if settings == None:
log("No settings")
return None
# if the id was specified return it
if 'id' in settings.keys():
return settings['id']
return None
def getAccessToken(self):
"""
Get the local access token
"""
try:
fp = open(self.getSettingsFile(), 'r')
settings = json.load(fp)
token = settings['access_token']
fp.close()
except:
return None
return token
def deleteAccesstoken(self):
"""
Remove the local access token to force logging in
"""
try:
# load the settings
filename = self.getSettingsFile()
fp = open(filename, 'r')
settings = json.load(fp)
fp.close()
#delete the access token
settings.pop('access_token')
log(settings, True)
# save the settings without the access token
fp = open(filename, 'w')
json.dump(settings, fp)
fp.close()
except:
return None
return None