forked from micahg/plugin.video.mlslive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlslive.py
360 lines (276 loc) · 12.2 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
'''
@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 urllib, urllib2, xml.dom.minidom, cookielib, time, datetime
import _strptime
class MLSLive:
def __init__(self):
"""
Initialize the MLSLive class.
"""
self.CED_CONFIG = 'http://static.mlsdigital.net/mobile/v20/config.json'
self.PUBLISH_POINT = 'http://live.mlssoccer.com/mlsmdl/servlets/publishpoint'
self.LOGIN_PAGE = 'https://live.mlssoccer.com/mlsmdl/secure/login'
self.GAMES_PAGE_PREFIX = 'http://mobile.cdn.mlssoccer.com/iphone/v5/prod/games_for_week_'
self.GAME_PREFIX = 'http://live.mlssoccer.com/mlsmdl/schedule?'
# resolution for images
self.RES = '560x320'
self.timeOffset = None
def getCookieFile(self):
import os
try:
import xbmc, xbmcaddon
base = xbmc.translatePath(xbmcaddon.Addon().getAddonInfo('profile'))
except:
base = os.getcwd()
return os.path.join(base, '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 login(self, username, password):
"""
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.
"""
# setup the login values
values = { 'username' : username,
'password' : password }
jar = self.createCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))#,
#urllib2.HTTPSHandler(debuglevel=1))
try:
resp = opener.open(self.LOGIN_PAGE, urllib.urlencode(values))
except:
print "Unable to login"
return False
jar.save(ignore_discard=True)
resp_xml = resp.read()
dom = xml.dom.minidom.parseString(resp_xml)
result_node = dom.getElementsByTagName('result')[0]
code_node = result_node.getElementsByTagName('code')[0]
if code_node.firstChild.nodeValue == 'loginsuccess':
return True
return False
def getTimeOffset(self, games_xml):
# there are no games in the first month, but it still returns the time
now = datetime.datetime.now()
# parse the xml for the server time
dom = xml.dom.minidom.parseString(games_xml)
result_node = dom.getElementsByTagName('result')[0]
cur_date_node = result_node.getElementsByTagName('currentDate')[0]
cur_date = cur_date_node.firstChild.nodeValue
try:
t = time.strptime(cur_date, '%a %b %d %H:%M:%S EST %Y')
server = datetime.datetime.fromtimestamp(time.mktime(t))
except ValueError:
print "ERROR: Unable to get server time"
return None
# calculate the time delta between the server and the local time zone
# accommodating for network delay
td = server - now
seconds = td.seconds
modsecs = seconds % 100
if modsecs < 100:
seconds += (100 - modsecs)
self.timeOffset = datetime.timedelta(days = td.days, seconds = seconds)
return None
def getGamesXML(self, month, year = '2015'):
values = {'format' : 'xml',
'year' : year,
'month' : month,
'checksubscription' : 'true' }
jar = self.loadCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
try:
resp = opener.open(self.GAME_PREFIX, urllib.urlencode(values))
except:
print "Unable get games xml"
return None
jar.save(filename=self.getCookieFile(), ignore_discard=True)
xml_data = resp.read()
return xml_data
def getGames(self, month):
"""
Get the list of games.
@param games_url the url of the weeks games
@return json game data
"""
games_xml = self.getGamesXML(month)
if games_xml == None:
return None
self.getTimeOffset(games_xml)
dom = xml.dom.minidom.parseString(games_xml)
result_node = dom.getElementsByTagName('result')[0]
games_node = result_node.getElementsByTagName('games')[0]
games = []
for game_node in games_node.getElementsByTagName('game'):
game = {}
# parse each element we'll need
for str in ['gid', 'type', 'id', 'gameTimeGMT', 'awayTeam',
'homeTeam', 'awayTeamName', 'homeTeamName', 'programId',
'gs', 'result', 'isLive']:
nodes = game_node.getElementsByTagName(str)
if len(nodes) > 0:
game[str] = nodes[0].firstChild.nodeValue
games.append(game)
return games
def getGameDateTimeStr(self, game_date_time):
"""
Convert the date time stamp from GMT to local time
@param game_date_time the game time (in GMT)
@return a string containing the local game date and time.
"""
try:
game_t = time.strptime(game_date_time, "%Y-%m-%d %H:%M:%S.%f")
except ValueError:
return None
game = datetime.datetime.fromtimestamp(time.mktime(game_t))
# attempt to calculate the local time
if not self.timeOffset == None:
game -= self.timeOffset
# return a nice string
return game.strftime("%m/%d %H:%M")
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
"""
# create the base string
game_str = game['awayTeamName'] + ' ' + separator + ' ' + \
game['homeTeamName']
# add the result
if 'result' in game.keys():
if game['result'] == 'F':
game_str += ' ([B]Final[/B])'
return game_str.encode('utf-8').strip()
if 'isLive' in game.keys():
if game['isLive'] == 'true':
game_str = '[I]' + game_str + '[/I]'
# if we can get the date/time of the game add it
dt = self.getGameDateTimeStr(game['gameTimeGMT'])
if not dt == None:
game_str += ' ([B]' + dt + '[/B])'
return game_str.encode('utf-8').strip()
def getFinalStreams(self, game_id):
"""
Get the streams for matches that have ended.
@param game_id the game id
@return a dictionary containing the streams with keys for the stream
type
"""
game_xml = self.getGameXML(game_id)
try:
dom = xml.dom.minidom.parseString(game_xml)
except:
return None
rss_node = dom.getElementsByTagName('rss')[0]
chan_node = rss_node.getElementsByTagName('channel')[0]
games = {}
for item in chan_node.getElementsByTagName('item'):
# get the game type
game_type = item.getElementsByTagName('nl:type')[0].firstChild.nodeValue
# get the group list and make sure its valid
group_list = item.getElementsByTagName('media:group')
if group_list == None or len(group_list) == 0:
continue
# get the content node and then the URL
content_node = group_list[0].getElementsByTagName('media:content')[0]
games[game_type] = content_node.getAttribute('url')
return games
def getStream(self, adaptive):
return None
def getGameLiveStream(self, game_id, condensed = False):
"""
Get the game streams. This method will parse the game XML for the
HLS playlist, and then parse that playlist for the different bitrate
streams.
@param game_id the game id
@return the live stream
"""
values = { 'type' : 'game',
'gt' : 'condensed' if condensed else 'live',
'id' : game_id,
'nt' : '1'}
uri = self.PUBLISH_POINT + '?' + urllib.urlencode(values)
jar = self.loadCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
# set the user agent to get the HLS stream
opener.addheaders = [('User-Agent', urllib.quote('PS3Application libhttp/4.5.5-000 (CellOS)'))]
print uri
try:
resp = opener.open(uri)
except urllib2.URLError as error:
print 'ERROR: ' + error.reason + '(' + uri + ')'
return ""
jar.save(filename=self.getCookieFile(), ignore_discard=True)
game_xml = resp.read()
try:
dom = xml.dom.minidom.parseString(game_xml)
except:
print "Unable to parse game XML for game " + game_id
return ""
result_node = dom.getElementsByTagName('result')[0]
m3u8URL = result_node.getElementsByTagName('path')[0]
print game_xml
#hacked together stuff
header = {'Cookie' : 'nlqptid=' + m3u8URL.split('?', 1)[1], 'User-Agent' : 'Safari/537.36 Mozilla/5.0 AppleWebKit/537.36 Chrome/31.0.1650.57', 'Accept-Encoding' : 'gzip,deflate', 'Connection' : 'Keep-Alive'}
if "live" in uri:
#Download the m3u8
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('Cookie', 'nlqptid=' + m3u8URL.split('?', 1)[1])]
values = {}
login_data = urllib.urlencode(values)
response = opener.open(m3u8URL, login_data)
m3u8File = response.read()
#Download the keys
url2=''
for line in m3u8File.split("\n"):
searchTerm = "#EXT-X-KEY:METHOD=AES-128,URI="
if searchTerm in line:
url2=line.strip().replace(searchTerm,'')[1:-1]
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
values = {}
login_data = urllib.urlencode(values)
#This line was causing errors for buf/edm game 11/7/2014
#Seemed to run fine without a proper response...
try:
response = opener.open(url2, login_data)
except:
pass
#Remove unneeded cookies
remove = []
for cookie in jar:
if cookie.name != "nlqptid" and "as-live" not in cookie.name:
remove.append(cookie)
for cookie in remove:
cj.clear(cookie.domain, cookie.path, cookie.name)
#Create header needed for playback
cookies = ''
for cookie in cj:
cookies = cookies + cookie.name + "=" + cookie.value + "; "
header = {'Cookie' : cookies, 'User-Agent' : 'Safari/537.36 Mozilla/5.0 AppleWebKit/537.36 Chrome/31.0.1650.57', 'Accept-Encoding' : 'gzip,deflate', 'Connection' : 'Keep-Alive'}
#header = {'Cookie' : cookies, 'User-Agent' : 'NHL1415/4.1030 CFNetwork/711.1.12 Darwin/14.0.0', 'Accept-Encoding' : 'gzip,deflate', 'Connection' : 'Keep-Alive'}
print header
result_node = dom.getElementsByTagName('result')[0]
path_node = result_node.getElementsByTagName('path')[0]
stream_url = path_node.firstChild.nodeValue + "|" + urllib.urlencode(header)
return stream_url