-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
602 lines (539 loc) · 20 KB
/
main.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
597
598
599
600
601
602
import base64, os, pickle, sys, urllib, urlparse
import xbmc, xbmcaddon, xbmcgui, xbmcplugin
try:
import simplejson as json
except ImportError:
import json
# http://mail.python.org/pipermail/python-list/2009-June/540579.html
import _strptime
from datetime import datetime
from dateutil import tz
from distutils.version import StrictVersion
from resources.lib.nhlgc import nhlgc
__addon__ = xbmcaddon.Addon()
__addonurl__ = sys.argv[0]
__addonhandle__ = int(sys.argv[1])
__addonargs__ = urlparse.parse_qs(sys.argv[2][1:])
__addonicon__ = __addon__.getAddonInfo('icon')
__addonfanart__ = __addon__.getAddonInfo('fanart')
__addonname__ = __addon__.getAddonInfo('name')
__cwd__ = __addon__.getAddonInfo('path').decode('utf-8')
__profile__ = __addon__.getAddonInfo('profile').decode('utf-8')
__language__ = __addon__.getLocalizedString
__teams_json__ = os.path.join(__cwd__, 'teams.json')
__cookiesfile__ = xbmc.translatePath(os.path.join(__profile__, 'cookies.lwp'))
__addonversion__ = __addon__.getAddonInfo('version')
__clear_cookies_before_version__ = '0.0.31'
game_time_format = xbmc.getRegion('dateshort') + ' ' + xbmc.getRegion('time').replace(':%S', '')
class NHL_GameCenter(object):
# This is the list of bitrates defined in settings.xml. These two sources
# should be kept in sync!
SETTINGS_BITRATES = [
# The following are for reference only, and should remain commented out:
# 'Always ask', # 0
# 'Best quality', # 1
'5000', # 2
'4500', # 3
'3000', # 4
'1600', # 5
'1200', # 6
'800', # 7
'400', # 8
'240', # 9
'150', # 10
]
MATCHUP_IMAGES_URL = 'http://nhl.cdn.neulion.net/u/nhlgc_roku/images/HD/%s_at_%s.jpg'
def __init__(self):
username = __addon__.getSetting('gc_username')
password = __addon__.getSetting('gc_password')
rogerslogin = __addon__.getSetting('gc_rogerslogin') == 'true'
proxy_config = None
if __addon__.getSetting('proxy_enabled') == 'true':
proxy_config = {
'scheme': __addon__.getSetting('proxy_scheme'),
'host': __addon__.getSetting('proxy_host'),
'port': __addon__.getSetting('proxy_port'),
'auth': {
'username': __addon__.getSetting('proxy_username'),
'password': __addon__.getSetting('proxy_password'),
},
}
if proxy_config['auth']['username'] == '' and proxy_config['auth']['password'] == '':
proxy_config['auth'] = None
hls_server = None
try:
self.has_hls_proxy = False
hls_proxy = xbmcaddon.Addon('service.nhl-hls-proxy')
hls_server = {
'host': hls_proxy.getSetting('hls_listen_host'),
'port': int(hls_proxy.getSetting('hls_listen_port')),
}
urllib.urlopen('http://%s:%d' % (hls_server['host'], hls_server['port']))
self.has_hls_proxy = True
except RuntimeError:
# Failed to get the handle for the add-on, so it probably hasn't
# been enabled.
pass
except IOError:
# Failed to connect to the proxy server, so it either hasn't been
# enabled, or it failed to start up.
pass
try:
clear_cookies = __addon__.getSetting('clear_cookies') == 'true' or StrictVersion(__addonversion__) < StrictVersion(__clear_cookies_before_version__)
self.game_center = nhlgc(username, password, rogerslogin, proxy_config, hls_server, __cookiesfile__, clear_cookies=clear_cookies)
if clear_cookies:
__addon__.setSetting('clear_cookies_last_version', __addonversion__)
__addon__.setSetting('clear_cookies', 'false')
except nhlgc.LogicError as error:
self.display_notification(error)
raise RuntimeError(error)
except nhlgc.NetworkError as error:
self.display_notification(error)
raise RuntimeError(error)
self.preferred_bitrate = int(__addon__.getSetting('preferred_bitrate'))
self.always_ask_bitrate = self.preferred_bitrate == 0
self.team_info = self.parse_teams_json(__teams_json__)
team_names_setting = int(__addon__.getSetting('team_names'))
if team_names_setting == 1:
self.team_info_key = 'team'
elif team_names_setting == 2:
self.team_info_key = 'full-name'
elif team_names_setting == 3:
self.team_info_key = None
else:
self.team_info_key = 'city'
self.show_scores = __addon__.getSetting('show_scores') == 'true'
self.at_instead_of_vs = __addon__.getSetting('at_instead_of_vs') == 'true'
self.show_stream_duration = __addon__.getSetting('show_stream_duration') == 'true'
def parse_teams_json(self, teams_file):
with open(teams_file) as file_obj:
teams_json = file_obj.read()
return json.loads(teams_json)
def display_notification(self, msg):
xbmcgui.Dialog().ok(__language__(30035), str(msg))
def build_game_info(self, game, title_suffix=''):
info = {
'genre': 'Hockey',
# 'year': int(game['season']),
'episode': int(game['id']),
'season': int(game['season']),
# 'cast': [
# self.team_info[game['home_team']]['full-name'],
# self.team_info[game['away_team']]['full-name'],
# ],
'title': self.game_title(game)[0],
'duration': 0,
}
if len(title_suffix) > 0:
info['title'] += ' ' + title_suffix
if self.show_stream_duration and game['start_time'] is not None:
info['aired'] = game['start_time'].astimezone(tz.tzlocal()).strftime('%Y-%m-%d')
if game['end_time'] is not None:
# FIXME: This is both correct and incorrect at the same time.
# It's correct because the start and end times are correct.
# It's incorrect because non-live streams don't have commercials.
#
# Times seem to be high by about 50 minutes, so lets just chop
# that time off and hope for the best.
time_delta = game['end_time'] - game['start_time']
info['duration'] = str((time_delta.seconds / 60) - 50)
return info
def add_folder(self, label=None, params={}, game=None):
item = xbmcgui.ListItem(label)
item.setProperty('fanart_image', __addonfanart__)
icon = None
if game is not None:
params['game'] = self.serialize_data(game)
icon = self.matchup_image(game)
item.setInfo('video', self.build_game_info(game))
if icon is None:
icon = __addonicon__
item.setIconImage(icon)
item.setArt({'thumb': icon})
url = __addonurl__
if len(params) > 0:
url += '?' + urllib.urlencode(params)
xbmcplugin.addDirectoryItem(
isFolder=True,
handle=__addonhandle__,
url=url,
listitem=item,
)
def add_item(self, label=None, url=__addonurl__, params={}, game=None):
item = xbmcgui.ListItem(label)
item.setProperty('fanart_image', __addonfanart__)
icon = None
if game is not None:
icon = self.matchup_image(game)
item.setInfo('video', self.build_game_info(game, '(' + label + ')'))
if icon is None:
icon = __addonicon__
item.setIconImage(icon)
item.setArt({'thumb': icon})
if len(params) > 0:
url += '?' + urllib.urlencode(params)
xbmcplugin.addDirectoryItem(
isFolder=False,
handle=__addonhandle__,
url=url,
listitem=item,
)
def select_bitrate(self, streams, prev_bitrate=None, adjust_bitrate=False):
sorted_streams = sorted(streams, key=int, reverse=True)
# Pick the best quality stream.
if self.preferred_bitrate == 1:
return sorted_streams[0]
# Handle setting prev_bitrate for "fuzzy" matching.
if prev_bitrate is None:
prev_bitrate = int(self.SETTINGS_BITRATES[self.preferred_bitrate - 2])
else:
prev_bitrate = int(prev_bitrate)
if adjust_bitrate is True:
prev_bitrate *= 1.10
# Pick a specific stream quality.
if (self.preferred_bitrate == 0 and prev_bitrate is not None) or self.preferred_bitrate > 1:
for bitrate in sorted_streams:
if int(bitrate) <= prev_bitrate:
return bitrate
# Ask what bitrate the user wants.
dialog = xbmcgui.Dialog()
xbmc.executebuiltin('Dialog.Close(busydialog)')
ret = dialog.select(__language__(30005), sorted_streams)
return sorted_streams[ret]
def game_title(self, game):
# Get the team names.
home_team = game['home_team']
away_team = game['away_team']
if self.team_info_key is not None:
if home_team in self.team_info:
home_team = self.team_info[home_team][self.team_info_key]
if away_team in self.team_info:
away_team = self.team_info[away_team][self.team_info_key]
# Get the score for the game.
home_team_score, away_team_score = game['home_goals'], game['away_goals']
# Get the required dates and times.
current_time_utc = datetime.utcnow().replace(tzinfo=tz.tzutc())
if game['start_time'] is not None:
start_time_local = game['start_time'].astimezone(tz.tzlocal()).strftime(game_time_format)
else:
start_time_local = game['date'].strftime(xbmc.getRegion('dateshort'))
# Start with the basic title of "Team vs Team".
lang_id = 30027
if self.at_instead_of_vs == True:
home_team, away_team = away_team, home_team
home_team_score, away_team_score = away_team_score, home_team_score
lang_id = 30028
title = home_team + __language__(lang_id) + away_team
# Handle game status flags.
status_flags = ''
if game['blocked']:
status_flags = __language__(30022)
else:
if game['live']:
status_flags = __language__(30023)
elif game['ended']:
time_delta = current_time_utc - game['start_time']
# Any game that has ended and started within the last 12 hours
# get flagged as having recently ended.
if time_delta.seconds + (time_delta.days * 86400) < 43200:
status_flags = __language__(30024)
# Handle showing the game score.
game_score = ''
if self.show_scores and (game['live'] or game['ended']) and home_team_score is not None and away_team_score is not None:
game_score = '(%s-%s)' % (home_team_score, away_team_score)
# Prepend the game start time.
return (
title,
start_time_local,
status_flags,
game_score,
)
def formatted_game_title(self, game):
title = self.game_title(game)
game_title = title[1] + ': '
if len(title[2]) > 0:
game_title += title[2] + ' '
game_title += title[0]
if len(title[3]) > 0:
game_title += ' ' + title[3]
return game_title
def matchup_image(self, game):
image = None
try:
home_team, away_team = game['home_team'], game['away_team']
if 'alt-abbr' in self.team_info[home_team]:
home_team = self.team_info[home_team]['alt-abbr']
if 'alt-abbr' in self.team_info[away_team]:
away_team = self.team_info[away_team]['alt-abbr']
image = self.MATCHUP_IMAGES_URL % (away_team, home_team)
except KeyError:
pass
return image
def serialize_data(self, data):
return base64.b64encode(pickle.dumps(data, pickle.HIGHEST_PROTOCOL))
def unserialize_data(self, data):
return pickle.loads(base64.b64decode(data))
def MODE_list(self, today_only):
retry_args = {'mode': 'list'}
if today_only == True:
retry_args['type'] = 'today'
else:
retry_args['type'] = 'recent'
try:
games = self.game_center.get_game_list(today_only)
for game in games:
if game['event_id'] is None:
continue
if game['streams'][self.game_center.STREAM_TYPE_CONDENSED] is not None or game['streams'][self.game_center.STREAM_TYPE_HIGHLIGHTS] is not None:
params = {'mode': 'view_options'}
else:
params = {'mode': 'live'}
self.add_folder(
label = self.formatted_game_title(game),
params = params,
game = game,
)
return
except (nhlgc.NetworkError, nhlgc.LoginError, nhlgc.LogicError) as error:
self.display_notification(error)
self.add_item(label=__language__(30030), params=retry_args)
def MODE_view_options(self, game):
retry_args = {
'mode': 'view_options',
'game': game,
}
try:
event_info = self.game_center.get_event_info(game['event_id'])
if event_info['blocked']:
self.display_notification(__language__(30066))
return
except (nhlgc.NetworkError, nhlgc.LoginError) as error:
self.display_notification(error)
self.add_item(label=__language__(30030), params=retry_args)
return
# Live stream
self.add_folder(
label = __language__(30059),
params = {'mode': 'live'},
game = game,
)
extra_options = [
(__language__(30060), self.game_center.STREAM_TYPE_CONDENSED),
(__language__(30061), self.game_center.STREAM_TYPE_HIGHLIGHTS),
]
use_bitrate = None
for idx, (label, stream_type) in enumerate(extra_options):
try:
if game['streams'][stream_type] is None:
continue
master_url = self.game_center.get_master_playlist(game['event_id'], game['streams'][stream_type])
if master_url is None:
continue
playlists = self.game_center.get_stream_playlist(master_url)
use_bitrate = self.select_bitrate(playlists, prev_bitrate=use_bitrate, adjust_bitrate=(idx == 1))
self.add_item(
label = label,
url = playlists[use_bitrate],
game = game,
)
except nhlgc.NetworkError as error:
if error.status_code != 404:
self.display_notification(error)
self.add_item(label=__language__(30030), params=retry_args)
except (nhlgc.LoginError, nhlgc.LogicError) as error:
self.display_notification(error)
self.add_item(label=__language__(30030), params=retry_args)
def MODE_live(self, game):
retry_args = {
'mode': 'live',
'game': game,
}
try:
event_info = self.game_center.get_event_info(game['event_id'])
if event_info['blocked']:
self.display_notification(__language__(30066))
return
except (nhlgc.NetworkError, nhlgc.LoginError) as error:
self.display_notification(error)
self.add_item(label=__language__(30030), params=retry_args)
return
perspectives = [
(__language__(30065), self.game_center.STREAM_PERSPECTIVE_NATIONAL),
(__language__(30025), self.game_center.STREAM_PERSPECTIVE_HOME),
(__language__(30026), self.game_center.STREAM_PERSPECTIVE_AWAY),
(__language__(30062), self.game_center.STREAM_PERSPECTIVE_FRENCH),
]
use_bitrate = None
for idx, (label, stream_perspective) in enumerate(perspectives):
if game['streams'][self.game_center.STREAM_TYPE_LIVE][stream_perspective] is None:
continue
try:
master_url = self.game_center.get_master_playlist(game['event_id'], game['streams'][self.game_center.STREAM_TYPE_LIVE][stream_perspective])
if master_url is None:
continue
playlists = self.game_center.get_stream_playlist(master_url)
use_bitrate = self.select_bitrate(playlists, prev_bitrate=use_bitrate, adjust_bitrate=(idx == 1))
self.add_item(
label = label,
url = playlists[use_bitrate],
game = game,
)
except nhlgc.NetworkError as error:
if error.status_code != 404:
self.display_notification(error)
self.add_item(label=__language__(30030), params=retry_args)
except (nhlgc.LoginError, nhlgc.LogicError) as error:
self.display_notification(error)
self.add_item(label=__language__(30030), params=retry_args)
def MODE_watch_OLD(self, game, stream_type):
retry_args = {
'mode': 'watch',
'game': game,
'stream_type': stream_type,
}
if stream_type == self.game_center.STREAM_TYPE_HIGHLIGHTS:
highlights = self.game_center.get_game_highlights(game['season'], game['id'], game['season_type'])
if 'home' in highlights and 'publishPoint' in highlights['home']:
self.add_item(label=__language__(30025), url=highlights['home']['publishPoint'], game=game)
if 'away' in highlights and 'publishPoint' in highlights['away']:
self.add_item(label=__language__(30026), url=highlights['away']['publishPoint'], game=game)
if 'french' in highlights and 'publishPoint' in highlights['away']:
self.add_item(label=__language__(30062), url=highlights['french']['publishPoint'], game=game)
return
perspectives = [
(__language__(30025), 'home', self.game_center.PERSPECTIVE_HOME),
(__language__(30026), 'away', self.game_center.PERSPECTIVE_AWAY),
]
if game['french_game'] == True:
perspectives += [(__language__(30062), 'french', self.game_center.PERSPECTIVE_FRENCH)]
seen_urls = {}
use_bitrate = None
for label, stream_key, perspective in perspectives:
try:
from_start = False
if stream_type == self.game_center.STREAM_TYPE_LIVE and game['streams'][stream_key] is not None:
playlists = self.game_center.get_playlists_from_m3u8_url(game['streams'][stream_key])
else:
if stream_type == self.game_center.STREAM_TYPE_LIVE:
from_start = True
playlists = self.game_center.get_video_playlists(game['season'], game['id'], game['season_type'], stream_type, perspective)
if len(playlists) == 1:
stream_url = playlists.values()[0]
else:
if use_bitrate is None or use_bitrate not in playlists:
use_bitrate = self.select_bitrate(playlists)
stream_url = playlists[use_bitrate]
if stream_url not in seen_urls:
self.add_item(
label=label,
url=self.game_center.get_authorized_stream_url(game, stream_url, False),
game=game,
)
if self.has_hls_proxy and from_start == True:
self.add_item(
label=label + __language__(30063),
url=self.game_center.get_authorized_stream_url(game, stream_url, True),
game=game,
)
seen_urls[stream_url] = True
except nhlgc.NetworkError as error:
if error.status_code != 404:
self.display_notification(error)
self.add_item(label=__language__(30030), params=retry_args)
except nhlgc.LoginError as error:
self.display_notification(error)
self.add_item(label=__language__(30030), params=retry_args)
def MODE_archives(self, season):
retry_args = {
'mode': 'archives',
'season': season,
}
try:
archives = self.game_center.get_archived_seasons()
if season is None:
for archive in archives:
title = '%d - %d' % (int(archive['season']), int(archive['season']) + 1)
self.add_folder(title, {
'mode': 'archives',
'season': archive['season'],
})
else:
title = '%d - %d: ' % (int(season), int(season) + 1)
for archive in archives:
if archive['season'] != season:
continue
for month in archive['months']:
self.add_folder(title + __language__(30037 + int(month) - 1), {
'mode': 'archives_month',
'season': season,
'month': month,
})
return
except nhlgc.NetworkError as error:
self.display_notification(error)
except nhlgc.LoginError as error:
self.display_notification(error)
except nhlgc.LogicError as error:
self.display_notification(error)
self.add_item(label=__language__(30030), params=retry_args)
def MODE_archives_month(self, season, month):
retry_args = {
'mode': 'archives_month',
'season': season,
'month': month,
}
try:
games = self.game_center.get_archived_month(season, month)
for game in games:
self.add_folder(
label=self.formatted_game_title(game, None),
params={'mode': 'view_options'},
game=game,
)
return
except nhlgc.NetworkError as error:
self.display_notification(error)
except nhlgc.LoginError as error:
self.display_notification(error)
except nhlgc.LogicError as error:
self.display_notification(error)
self.add_item(label=__language__(30030), params=retry_args)
##
# Addon menu system.
##
cache_folder = True
try:
mode = __addonargs__.get('mode', None)
if type(mode) == type(list()):
mode = mode[0]
game_center = NHL_GameCenter()
if mode is None:
cache_folder = False
game_center.add_folder(__language__(30029), {'mode': 'list', 'type': 'today'})
game_center.add_folder(__language__(30032), {'mode': 'list', 'type': 'recent'})
# game_center.add_folder(__language__(30036), {'mode': 'archives', 'season': None})
elif mode == 'list':
xbmcplugin.setContent(__addonhandle__, 'episodes')
cache_folder = False
today_only = __addonargs__.get('type')[0] == 'today'
game_center.MODE_list(today_only)
elif mode == 'view_options':
game = game_center.unserialize_data(__addonargs__.get('game')[0])
game_center.MODE_view_options(game)
elif mode == 'live':
xbmcplugin.setContent(__addonhandle__, 'episodes')
game = game_center.unserialize_data(__addonargs__.get('game')[0])
game_center.MODE_live(game)
# elif mode == 'archives':
# season = __addonargs__.get('season')[0]
# if season == 'None':
# season = None
# game_center.MODE_archives(season)
# elif mode == 'archives_month':
# xbmcplugin.setContent(__addonhandle__, 'episodes')
# season = __addonargs__.get('season')[0]
# month = __addonargs__.get('month')[0]
# game_center.MODE_archives_month(season, month)
except RuntimeError:
pass
xbmcplugin.endOfDirectory(__addonhandle__, cacheToDisc=cache_folder)