forked from maxgalbu/xbmc.plugin.video.nba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
games.py
316 lines (257 loc) · 12 KB
/
games.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
import json
import datetime, time
from datetime import timedelta
import urllib,urllib2
import xbmc,xbmcplugin,xbmcgui
from xml.dom.minidom import parseString
import re
import sys
from utils import *
from common import *
import vars
import SimpleDownloader as downloader
def getGameUrl(video_id, video_type, video_ishomefeed):
log("cookies: %s %s" % (video_type, vars.cookies), xbmc.LOGDEBUG)
# video_type could be archive, live, condensed or oldseason
if video_type not in ["live", "archive", "condensed"]:
video_type = "archive"
url = 'http://watch.nba.com/nba/servlets/publishpoint'
headers = {
'Cookie': vars.cookies,
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'iPad' if video_type == "live"
else "AppleCoreMedia/1.0.0.8C148a (iPad; U; CPU OS 6_2_1 like Mac OS X; en_us)",
}
body = {
'id': str(video_id),
'gt': video_type + ("away" if not video_ishomefeed else ""),
'type': 'game',
'plid': vars.player_id,
'nt': '1'
}
if video_type != "live":
body['format'] = 'xml'
body = urllib.urlencode(body)
log("the body of publishpoint request is: %s" % body, xbmc.LOGDEBUG)
try:
request = urllib2.Request(url, body, headers)
response = urllib2.urlopen(request)
content = response.read()
except urllib2.HTTPError as e:
log("Failed to get video url. The url was %s, the content was %s" % (url, e.read()))
littleErrorPopup( xbmcaddon.Addon().getLocalizedString(50020) )
return ''
xml = parseString(str(content))
url = xml.getElementsByTagName("path")[0].childNodes[0].nodeValue
log(url, xbmc.LOGDEBUG)
selected_video_url = ''
if video_type == "live":
# transform the url
match = re.search('http://([^:]+)/([^?]+?)\?(.+)$', url)
domain = match.group(1)
arguments = match.group(2)
querystring = match.group(3)
livecookies = "nlqptid=%s" % (querystring)
livecookiesencoded = urllib.quote(livecookies)
log("live cookie: %s %s" % (querystring, livecookies), xbmc.LOGDEBUG)
url = "http://%s/%s?%s" % (domain, arguments, querystring)
url = getGameUrlWithBitrate(url, video_type)
selected_video_url = "%s|Cookie=%s" % (url, livecookiesencoded)
else:
# Archive and condensed flow: We now work with HLS.
# The cookies are already in the URL and the server will supply them to ffmpeg later.
selected_video_url = getGameUrlWithBitrate(url, video_type)
if selected_video_url:
log("the url of video %s is %s" % (video_id, selected_video_url), xbmc.LOGDEBUG)
return selected_video_url
def getHighlightGameUrl(video_id):
url = 'http://watch.nba.com/nba/servlets/publishpoint'
headers = {
'Cookie': vars.cookies,
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': "AppleCoreMedia/1.0.0.8C148a (iPad; U; CPU OS 6_2_1 like Mac OS X; en_us)",
}
body = urllib.urlencode({
'id': str(video_id),
'gt': "recapf",
'type': 'game',
'plid': vars.player_id,
'isFlex': "true",
'bitrate': "1600" # forced bitrate
})
log("the body of publishpoint request is: %s" % body, xbmc.LOGDEBUG)
try:
request = urllib2.Request(url, body, headers)
response = urllib2.urlopen(request)
content = response.read()
except urllib2.HTTPError:
return ''
xml = parseString(str(content))
url = xml.getElementsByTagName("path")[0].childNodes[0].nodeValue
# Remove everything after ? otherwise XBMC fails to read the rtpm stream
url, _,_ = url.partition("?")
log("highlight video url: %s" % url, xbmc.LOGDEBUG)
return url
def addGamesLinks(fromDate = '', video_type = "archive"):
try:
schedule = 'http://smb.cdnak.neulion.com/fs/nba/feeds_s2012/schedule/' +fromDate + '.js?t=' + "%d" %time.time()
log('Requesting %s' % schedule, xbmc.LOGDEBUG)
now_datetime_est = nowEST()
# http://smb.cdnak.neulion.com/fs/nba/feeds_s2012/schedule/2013/10_7.js?t=1381054350000
req = urllib2.Request(schedule, None);
response = str(urllib2.urlopen(req).read())
js = json.loads(response[response.find("{"):])
for game in js['games']:
log(game, xbmc.LOGDEBUG)
for details in game:
h = details.get('h', '')
v = details.get('v', '')
game_id = details.get('id', '')
game_start_date_est = details.get('d', '')
vs = details.get('vs', '')
hs = details.get('hs', '')
gs = details.get('gs', '')
video_has_away_feed = False
video_details = details.get('video', {})
video_has_away_feed = video_details.get("af", False)
# Try to convert start date to datetime
try:
game_start_datetime_est = datetime.datetime.strptime(game_start_date_est, "%Y-%m-%dT%H:%M:%S.%f" )
except:
game_start_datetime_est = datetime.datetime.fromtimestamp(time.mktime(time.strptime(game_start_date_est, "%Y-%m-%dT%H:%M:%S.%f")))
#Set game start date in the past if python can't parse the date
#so it doesn't get flagged as live or future game and you can still play it
#if a video is available
if type(game_start_datetime_est) is not datetime.datetime:
game_start_datetime_est = now_datetime_est + timedelta(-30)
#guess end date by adding 4 hours to start date
game_end_datetime_est = game_start_datetime_est + timedelta(hours=4)
if game_id != '':
# Get pretty names for the team names
if v.lower() in vars.teams:
visitor_name = vars.teams[v.lower()]
else:
visitor_name = v
if h.lower() in vars.teams:
host_name = vars.teams[h.lower()]
else:
host_name = h
has_video = "video" in details
future_video = game_start_datetime_est > now_datetime_est and \
game_start_datetime_est.date() == now_datetime_est.date()
live_video = game_start_datetime_est < now_datetime_est < game_end_datetime_est
# Create the title
name = game_start_datetime_est.strftime("%Y-%m-%d")
if video_type == "live":
name = toLocalTimezone(game_start_datetime_est).strftime("%Y-%m-%d (at %I:%M %p)")
#Add the teams' names and the scores if needed
name += ' %s vs %s' % (visitor_name, host_name)
if vars.scores == '1' and not future_video:
name += ' %s:%s' % (str(vs), str(hs))
thumbnail_url = ("http://e1.cdnl3.neulion.com/nba/player-v4/nba/images/teams/%s.png" % h)
if video_type == "live":
if future_video:
name = "UPCOMING: " + name
elif live_video:
name = "LIVE: " + name
add_link = True
if video_type == "live" and not (live_video or future_video):
add_link = False
elif video_type != "live" and (live_video or future_video):
add_link = False
elif not future_video and not has_video:
add_link = False
if add_link == True:
params = {
'video_id': game_id,
'video_type': video_type,
'video_hasawayfeed': 1 if video_has_away_feed else 0
}
# Add a directory item that contains home/away/condensed items
addListItem(name, url="", mode="gamechoosevideo",
iconimage=thumbnail_url, isfolder=True, customparams=params)
except Exception, e:
xbmc.executebuiltin('Notification(NBA League Pass,'+str(e)+',5000,)')
log(str(e))
pass
def playGame():
# Authenticate
if vars.cookies == '':
vars.cookies = login()
if not vars.cookies:
return
currentvideo_id = vars.params.get("video_id")
currentvideo_type = vars.params.get("video_type")
currentvideo_ishomefeed = vars.params.get("video_ishomefeed", "1")
currentvideo_ishomefeed = currentvideo_ishomefeed == "1"
# Get the video url.
# Authentication is needed over this point!
currentvideo_url = getGameUrl(currentvideo_id, currentvideo_type, currentvideo_ishomefeed)
if currentvideo_url:
item = xbmcgui.ListItem(path=currentvideo_url)
xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item)
dw = downloader.SimpleDownloader()
params = { "url": currentvideo_url, "download_path": "c:/", "Title": "my video" }
dw.download("myvideo.mp4", params)
def chooseGameVideoMenu():
currentvideo_id = vars.params.get("video_id")
currentvideo_type = vars.params.get("video_type")
currentvideo_hasawayfeed = vars.params.get("video_hasawayfeed", "0")
currentvideo_hasawayfeed = currentvideo_hasawayfeed == "1"
if currentvideo_hasawayfeed:
# Create the "Home" and "Away" list items
for ishomefeed in [True, False]:
listitemname = "Full game, " + ("away feed" if not ishomefeed else "home feed")
params = {
'video_id': currentvideo_id,
'video_type': currentvideo_type,
'video_ishomefeed': 1 if ishomefeed else 0
}
addListItem(listitemname, url="", mode="playgame", iconimage="", customparams=params)
else:
#Add a "Home" list item
params = {
'video_id': currentvideo_id,
'video_type': currentvideo_type
}
addListItem("Full game", url="", mode="playgame", iconimage="", customparams=params)
# Create the "Condensed" list item
if currentvideo_type != "live":
params = {
'video_id': currentvideo_id,
'video_type': 'condensed'
}
addListItem("Condensed game", url="", mode="playgame", iconimage="", customparams=params)
# Get the highlights video if available
highlights_url = getHighlightGameUrl(currentvideo_id)
if highlights_url:
addVideoListItem("Highlights", highlights_url, iconimage="")
xbmcplugin.endOfDirectory(handle = int(sys.argv[1]) )
def chooseGameMenu(mode, url, date2Use = None):
try:
if mode == "selectdate":
tday = getDate()
elif mode == "oldseason":
tday = date2Use
else:
tday = nowEST()
log("current date (america timezone) is %s" % str(tday), xbmc.LOGDEBUG)
# parse the video type
video_type = url
day = tday.isoweekday()
# starts on mondays
tday = tday - timedelta(day -1)
now = tday
default = "%04d/%d_%d" % (now.year, now.month, now.day)
if mode == "lastweek":
tday = tday - timedelta(7)
now = tday
default = "%04d/%d_%d" % (now.year, now.month, now.day)
addGamesLinks(default, video_type)
# Can't sort the games list correctly because XBMC treats file items and directory
# items differently and puts directory first, then file items (home/away feeds
# require a directory item while only-home-feed games is a file item)
#xbmcplugin.addSortMethod( handle=int(sys.argv[1]), sortMethod=xbmcplugin.SORT_METHOD_DATE )
except:
xbmcplugin.endOfDirectory(handle = int(sys.argv[1]),succeeded=False)
return None