forked from pigskin/kodi-gamepass
/
default.py
335 lines (292 loc) · 11.8 KB
/
default.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
import urllib
import urllib2
import re
import os
import json
import cookielib
import time
import xbmcplugin
import xbmcgui
import xbmcvfs
import xbmcaddon
import StorageServer
import xml.etree.ElementTree as ET
import random
import md5
from uuid import getnode as get_mac
from datetime import datetime, timedelta
from traceback import format_exc
from urlparse import urlparse, parse_qs
from BeautifulSoup import BeautifulSoup
from BeautifulSoup import BeautifulStoneSoup
addon = xbmcaddon.Addon(id='plugin.video.nfl.gamepass')
addon_path = xbmc.translatePath(addon.getAddonInfo('path'))
addon_profile = xbmc.translatePath(addon.getAddonInfo('profile'))
cookie_file = os.path.join(addon_profile, 'cookie_file')
cookie_jar = cookielib.LWPCookieJar(cookie_file)
icon = os.path.join(addon_path, 'icon.png')
fanart = os.path.join(addon_path, 'fanart.jpg')
base_url = ''
debug = addon.getSetting('debug')
addon_version = addon.getAddonInfo('version')
cache = StorageServer.StorageServer("nfl_game_pass", 24)
username = addon.getSetting('email')
password = addon.getSetting('password')
def addon_log(string):
if debug == 'true':
xbmc.log("[addon.nfl.gamepass-%s]: %s" %(addon_version, string))
def cache_seasons_and_weeks(login_data):
soup = BeautifulSoup(login_data, convertEntities=BeautifulSoup.HTML_ENTITIES)
try:
seasons_soup = soup.find('select', id='seasonSelect').findChildren()
seasons = []
for season in seasons_soup:
seasons.append(season.string)
cache.set('seasons', repr(seasons))
addon_log('Seasons cached')
except:
addon_log('Season cache failed')
return False
try:
weeks_soup = soup.find('select', id='weekSelect').findChildren()
weeks = {}
for week in weeks_soup:
week_code = week['value']
weeks[week_code] = week.string
cache.set('weeks', repr(weeks))
addon_log('Weeks cached')
except:
addon_log('Week cache failed')
return False
return True
def display_games(season, week_code):
games = get_weeks_games(season, week_code)
if games:
for game_id, game_info in games.iteritems():
add_dir(game_info[0], game_id, 4, icon, game_info[1], game_info[2], False)
else:
dialog = xbmcgui.Dialog()
dialog.ok("Fetching Games Failed", "Fetching Game Data Failed.")
addon_log('Fetching games failed.')
def display_seasons(seasons):
for season in seasons:
add_dir(season, season, 2, icon)
def display_weeks(season, weeks):
for week_code, week_name in sorted(weeks.iteritems()):
add_dir(week_name, season + ';' + week_code, 3, icon)
def gamepass_login():
url = 'https://id.s.nfl.com/login'
post_data = {
'username': username,
'password': password,
'vendor_id': 'nflptnrnln',
'error_url': 'https://gamepass.nfl.com/nflgp/secure/login?redirect=loginform&redirectnosub=packages&redirectsub=schedule',
'success_url': 'https://gamepass.nfl.com/nflgp/secure/login?redirect=loginform&redirectnosub=packages&redirectsub=schedule'
}
login_data = make_request(url, urllib.urlencode(post_data))
cache_success = cache_seasons_and_weeks(login_data)
if cache_success:
addon_log('login success')
return True
else: # if cache failed, then login failed or the login page's HTML changed
dialog = xbmcgui.Dialog()
dialog.ok("Login Failed", "Logging into NFL Game Pass failed. Make sure your account information is correct.")
addon_log('login failed')
return False
# The plid parameter used when requesting the video path appears to be an MD5 of... something.
# However, I don't knwo what it is an "id" of, since the value seems to change constantly.
# Reusing a plid doesn't work, so, I assume it's a unique identifier for the player as we request a stream.
# This, pseudorandom approach seems to work for now.
def gen_plid():
rand = random.getrandbits(10)
mac_address = str(get_mac())
m = md5.new(str(rand) + mac_address)
return m.hexdigest()
# the XML manifest of all available streams for a game
def get_manifest(video_path):
url, port, path = video_path.partition(':443')
path = path.replace('?', '&')
url = url.replace('adaptive://', 'http://') + port + '/play?' + urllib.quote_plus('url=' + path, ':&=')
manifest_data = make_request(url)
return manifest_data
def get_stream_url(game_id):
video_path = get_video_path(game_id)
manifest = get_manifest(video_path)
stream_url = parse_manifest(manifest)
return stream_url
# the "video path" provides the info neccesary to request the stream's manifest
def get_video_path(game_id):
url = 'https://gamepass.nfl.com/nflgp/servlets/encryptvideopath'
plid = gen_plid()
post_data = {
'path': game_id,
'plid': plid,
'type': 'fgpa',
'isFlex': 'true'
}
video_path_data = make_request(url, urllib.urlencode(post_data))
try:
soup = BeautifulStoneSoup(video_path_data, convertEntities=BeautifulSoup.XML_ENTITIES)
video_path = soup.find('path')
addon_log('Video Path Acquired Successfully.')
return video_path.string
except:
addon_log('Video Path Acquisition Failed.')
return False
# season is in format: YYYY
# week is in format 101 (1st week preseason) or 213 (13th week of regular season)
def get_weeks_games(season, week):
url = 'https://gamepass.nfl.com/nflgp/servlets/games'
post_data = {
'isFlex': 'true',
'season': season,
'week': week
}
games = {}
game_data = make_request(url, urllib.urlencode(post_data))
soup = BeautifulStoneSoup(game_data, convertEntities=BeautifulSoup.XML_ENTITIES)
games_soup = soup('game')
for game in games_soup:
game_id = ''
try:
game_id = game.programid.string
games[game_id] = ''
except AttributeError:
addon_log('No program id: %s' %game)
format_exc()
continue
away_team = game.awayteam('city')[0].string + ' ' + game.awayteam('name')[0].string
home_team = game.hometeam('city')[0].string + ' ' + game.hometeam('name')[0].string
try:
start_time = datetime.fromtimestamp(time.mktime(time.strptime(game.gametimegmt.string, '%Y-%m-%dT%H:%M:%S.000')))
end_time = datetime.fromtimestamp(time.mktime(time.strptime(game.gameendtimegmt.string, '%Y-%m-%dT%H:%M:%S.000')))
duration = (end_time - start_time).seconds / 60
except:
addon_log(format_exc())
duration = None
try:
game_datetime = datetime.fromtimestamp(time.mktime(time.strptime(game.date.string, '%Y-%m-%dT%H:%M:%S.000')))
game_date_string = game_datetime.strftime('%A, %b %d %I:%M %p')
except:
addon_log(format_exc())
game_date_string = ''
try:
scores = '%s %s\n%s %s' %(away_team, game.awayteam('score')[0].string, home_team, game.hometeam('score')[0].string)
except:
addon_log(format_exc())
scores = ''
description = "%s\n\n %s" %(game_date_string, scores)
games[game_id] = (away_team + ' at ' + home_team, description, duration)
return games
def make_request(url, data=None, headers=None):
addon_log('Request URL: %s' %url)
if headers is None:
headers = {'User-agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0',
'Referer' : base_url}
if not xbmcvfs.exists(cookie_file):
addon_log('Creating cookie_file!')
cookie_jar.save()
cookie_jar.load(cookie_file, ignore_discard=True, ignore_expires=True)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
urllib2.install_opener(opener)
try:
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
cookie_jar.save(cookie_file, ignore_discard=True, ignore_expires=False)
data = response.read()
addon_log(str(response.info()))
redirect_url = response.geturl()
response.close()
if redirect_url != url:
addon_log('Redirect URL: %s' %redirect_url)
return data
except urllib2.URLError, e:
addon_log('We failed to open "%s".' %url)
if hasattr(e, 'reason'):
addon_log('We failed to reach a server.')
addon_log('Reason: %s' %e.reason)
if hasattr(e, 'code'):
addon_log('We failed with error code - %s.' %e.code)
def parse_manifest(manifest):
try:
soup = BeautifulStoneSoup(manifest, convertEntities=BeautifulStoneSoup.XML_ENTITIES)
items = [{'servers': [{'name': x['name'], 'port': x['port']} for x in i('httpserver')],
'url': i['url'],
'info': '%sx%s Bitrate: %s' %(i.video['height'], i.video['width'], i['bitrate'])}
for i in soup('streamdata')]
dialog = xbmcgui.Dialog()
ret = dialog.select('Choose a stream', [i['info'] for i in items])
if ret >= 0:
addon_log('Selected: %s' %items[ret])
stream_url = 'http://%s%s' %(items[ret]['servers'][1]['name'], items[ret]['url'])
addon_log('Stream URL: %s' %stream_url)
return stream_url
else: raise
except:
addon_log(format_exc())
return False
def add_dir(name, url, mode, iconimage, discription="", duration=None, isfolder=True):
params = {'name': name, 'url': url, 'mode': mode}
url = '%s?%s' %(sys.argv[0], urllib.urlencode(params))
listitem = xbmcgui.ListItem(name, iconImage=iconimage, thumbnailImage=iconimage)
listitem.setProperty("Fanart_Image", fanart)
if not isfolder:
# IsPlayable tells xbmc that there is more work to be done to resolve a playable url
listitem.setProperty('IsPlayable', 'true')
listitem.setInfo(type="Video", infoLabels={"Title": name, "Plot": discription, "Duration": duration})
xbmcplugin.addDirectoryItem(int(sys.argv[1]), url, listitem, isfolder)
def get_params():
p = parse_qs(sys.argv[2][1:])
for i in p.keys():
p[i] = p[i][0]
return p
if debug == 'true':
cache.dbg = True
params = get_params()
addon_log("params: %s" %params)
try:
mode = int(params['mode'])
except:
mode = None
if mode == None:
seasons = None
if username and password:
login_success = gamepass_login()
if login_success:
seasons = eval(cache.get('seasons'))
# in some instances logging in is not necessary
elif not username:
try:
seasons = eval(cache.get('seasons'))
except SyntaxError:
addon_log('No season cache')
data = make_request('https://gamepass.nfl.com/nflgp/secure/schedule')
ok = cache_seasons_and_weeks(data)
if ok:
seasons = eval(cache.get('seasons'))
if seasons:
display_seasons(seasons)
else:
dialog = xbmcgui.Dialog()
dialog.ok("Account Info Not Set", "Please set your Game Pass username and password", "in Add-on Settings.")
addon_log('No account settings detected.')
xbmcplugin.endOfDirectory(int(sys.argv[1]))
elif mode == 1:
# unused for the time being
# will be used later when/if NFL Network and NFL RedZone support is added
xbmcplugin.endOfDirectory(int(sys.argv[1]))
elif mode == 2:
weeks = eval(cache.get('weeks'))
season = params['name']
display_weeks(season, weeks)
xbmcplugin.endOfDirectory(int(sys.argv[1]))
elif mode == 3:
season, week_code = params['url'].split(';', 1)
display_games(season, week_code)
xbmcplugin.endOfDirectory(int(sys.argv[1]))
elif mode == 4:
game_id = params['url']
resolved_url = get_stream_url(game_id)
addon_log('Resolved URL: %s.' %resolved_url)
item = xbmcgui.ListItem(path=resolved_url)
xbmcplugin.setResolvedUrl(int(sys.argv[1]), True, item)