-
Notifications
You must be signed in to change notification settings - Fork 22
/
addon.py
555 lines (493 loc) · 22.9 KB
/
addon.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
# -*- coding: utf-8 -*-
import http.client
import json
import os
import random
import sys
import time
import traceback
import urllib.request, urllib.parse, urllib.error
from collections import defaultdict
from datetime import datetime, timedelta
from urllib.parse import urlparse
import xbmc
import xbmcaddon
import xbmcgui
import xbmcplugin
import xbmcvfs
from xbmcplugin import addDirectoryItem
import ivysilani
###############################################################################
REMOTE_DBG = False
# append pydev remote debugger
if REMOTE_DBG:
try:
# sys.path.append(os.environ['HOME'] + r'/.kodi/system/python/Lib/pysrc') # Linux
sys.path.append("C:\\eclipse\\plugins\\org.python.pydev_4.0.0.201504132356\\pysrc") # Windows
import pydevd
pydevd.settrace('localhost', port=5678, stdoutToServer=True, stderrToServer=True)
except ImportError:
sys.stderr.write("Error: Could not load pysrc!")
sys.exit(1)
###############################################################################
params = None
_addon_ = xbmcaddon.Addon('plugin.video.ivysilani.cz')
_lang_ = _addon_.getLocalizedString
_scriptname_ = _addon_.getAddonInfo('name')
_version_ = _addon_.getAddonInfo('version')
_first_error_ = False
_send_errors_ = False
###############################################################################
def log(msg, level=xbmc.LOGDEBUG):
if type(msg).__name__ == 'unicode':
msg = msg.encode('utf-8')
xbmc.log("[%s] %s" % (_scriptname_, msg.__str__()), level)
def logDbg(msg):
log(msg, level=xbmc.LOGDEBUG)
def logErr(msg):
log(msg, level=xbmc.LOGERROR)
###############################################################################
def _exception_log(exc_type, exc_value, exc_traceback):
global _first_error_
global _send_errors_
logErr(traceback.format_exception(exc_type, exc_value, exc_traceback))
xbmcgui.Dialog().notification(_scriptname_, _toString(exc_value), xbmcgui.NOTIFICATION_ERROR)
if not _first_error_:
if xbmcgui.Dialog().yesno(_scriptname_, _lang_(30500) + "\n" + _lang_(30501)):
_addon_.setSetting("send_errors", "true")
_send_errors_ = (_addon_.getSetting('send_errors') == "true")
_addon_.setSetting("first_error", "true")
_first_error_ = (_addon_.getSetting('first_error') == "true")
if _send_errors_:
if _sendError(params, exc_type, exc_value, exc_traceback):
xbmcgui.Dialog().notification(_scriptname_, _lang_(30502), xbmcgui.NOTIFICATION_INFO)
else:
xbmcgui.Dialog().notification(_scriptname_, _lang_(30503), xbmcgui.NOTIFICATION_ERROR)
try:
# First run
if not (_addon_.getSetting("settings_init_done") == "true"):
DEFAULT_SETTING_VALUES = {"quality": "576p",
"auto_quality": "true",
"quality_fallback": "true",
"auto_view_mode": "true",
"send_errors": "false",
"show_subtitles": "false"}
for setting in list(DEFAULT_SETTING_VALUES.keys()):
val = _addon_.getSetting(setting)
if not val:
_addon_.setSetting(setting, DEFAULT_SETTING_VALUES[setting])
_addon_.setSetting("settings_init_done", "true")
###############################################################################
_auto_quality_ = (_addon_.getSetting('auto_quality') == "true")
_quality_ = _addon_.getSetting('quality')
_quality_fallback_ = (_addon_.getSetting('quality_fallback') == "true")
_first_error_ = (_addon_.getSetting('first_error') == "true")
_send_errors_ = (_addon_.getSetting('send_errors') == "true")
_auto_view_mode_ = (_addon_.getSetting('auto_view_mode') == "true")
_show_subtitles_ = (_addon_.getSetting('show_subtitles') == "true")
_subtitles_path_ = xbmcvfs.translatePath(os.path.join(_addon_.getAddonInfo('profile'), "subtitles.str"))
_icon_ = xbmcvfs.translatePath(os.path.join(_addon_.getAddonInfo('path'), 'icon.png'))
_next_ = xbmcvfs.translatePath(os.path.join(_addon_.getAddonInfo('path'), 'resources', 'media', 'next.png'))
_previous_ = xbmcvfs.translatePath(os.path.join(_addon_.getAddonInfo('path'), 'resources', 'media', 'previous.png'))
_fanArt = xbmcvfs.translatePath(os.path.join(_addon_.getAddonInfo('path'), 'resources', 'media', 'fanart1.png'))
_handle_ = int(sys.argv[1])
_baseurl_ = sys.argv[0]
well_known_error_messages = [('Programme not found!', 30550),
('Playlisturl is empty!', 30550),
('Non playable programme!', 30550),
('error_nonEncoded', 30551)]
SKIN_DATA = defaultdict(list, {
'skin.confluence': [
{'name': 'List', 'id': 50},
{'name': 'Big List', 'id': 51},
{'name': 'Thumbnail', 'id': 500},
{'name': 'Media info', 'id': 504},
{'name': 'Media info 2', 'id': 503}
]
})
def _toString(text):
if type(text).__name__ == 'unicode':
output = text.encode('utf-8')
else:
output = str(text)
return output
def _fanart():
fanartFolder = os.path.join(_addon_.getAddonInfo('path'), 'resources', 'media', 'fanart')
listedDir = os.listdir(fanartFolder)
r = random.randint(0, len(listedDir) - 1)
selected = os.path.join(_addon_.getAddonInfo('path'), 'resources', 'media', 'fanart', listedDir[r])
return xbmcvfs.translatePath(selected)
def _setViewMode(view_mode):
if _auto_view_mode_:
skin_dir = xbmc.getSkinDir()
for sd in SKIN_DATA[skin_dir]:
if sd['name'] == view_mode:
view_mode_id = sd['id']
xbmc.executebuiltin('Container.SetViewMode(%d)' % view_mode_id)
def mainMenu():
spotlight_labels = {"tipsMain": 30019,
"topDay": 30020,
"topWeek": 30021,
"tipsNote": 30022,
"tipsArchive": 30023,
"watching": 30024}
addDirectoryItem(_lang_(30015), _baseurl_ + "?menu=live")
addDirectoryItem(_lang_(30016), _baseurl_ + "?menu=byDate")
addDirectoryItem(_lang_(30017), _baseurl_ + "?menu=byLetter")
addDirectoryItem(_lang_(30018), _baseurl_ + "?menu=byGenre")
for spotlight in ivysilani.SPOTLIGHTS:
addDirectoryItem(_lang_(spotlight_labels[spotlight.ID]), _baseurl_ + "?menu=" + spotlight.ID)
xbmcplugin.endOfDirectory(_handle_, updateListing=True)
def addDirectoryItem(label, url, ID=None, related=False, episodes=False, plot=None, title=None, date=None,
duration=None,
icon=_icon_, image=None, fanart=None, isFolder=True):
li = xbmcgui.ListItem(label)
if not title:
title = label
liVideo = {'title': title}
if duration:
liVideo['duration'] = duration
if plot:
liVideo['plot'] = plot
if date:
dt = datetime.fromtimestamp(time.mktime(time.strptime(date, "%d. %m. %Y")))
liVideo['premiered'] = dt.strftime("%Y-%m-%d")
if image:
li.setArt({'thumb': image})
li.setArt({'icon': icon})
li.setInfo("video", liVideo)
if not fanart:
fanart = _fanart()
li.setProperty('fanart_image', fanart)
if episodes:
url = _baseurl_ + "?episodes=" + ID
if ID:
cm = []
cm.append((_lang_(30013), "Container.Update(" + _baseurl_ + "?play=" + ID + "&skip_auto=1)"))
if related:
cm.append((_lang_(30003), "Container.Update(" + _baseurl_ + "?related=" + ID + ")"))
cm.append((_lang_(30004), "Container.Update(" + _baseurl_ + "?episodes=" + ID + ")"))
cm.append((_lang_(30005), "Container.Update(" + _baseurl_ + "?bonuses=" + ID + ")"))
li.addContextMenuItems(cm)
xbmcplugin.addDirectoryItem(handle=_handle_, url=url, listitem=li, isFolder=isFolder)
def listProgrammelist(programmelist, episodes=False):
xbmcplugin.setContent(_handle_, "episodes")
pList = programmelist.list()
for item in pList:
plot = None
date = None
if hasattr(item, "synopsis") and item.synopsis:
plot = item.synopsis
url = _baseurl_ + "?play=" + item.ID
title = item.title
if hasattr(item, 'time'):
title = "[" + item.time + "] " + title
active = True
if hasattr(item, 'active'):
active = (item.active == '1')
if active:
addDirectoryItem(title, url, ID=item.ID, related=True, episodes=episodes, plot=plot, date=date,
image=item.imageURL)
xbmcplugin.endOfDirectory(_handle_, updateListing=False, cacheToDisc=False)
def playProgramme(ID, skipAutoQuality=False):
programme = ivysilani.Programme(ID)
if _auto_quality_ and not skipAutoQuality:
u = autoSelectQuality(programme)
if u:
playUrl(_toString(programme.title), u, programme.imageURL)
return
for quality in programme.available_qualities():
url = programme.url(quality)
addDirectoryItem(quality.label(), url=url, title=_toString(programme.title), image=programme.imageURL,
isFolder=False)
xbmcplugin.endOfDirectory(_handle_, updateListing=False, cacheToDisc=False)
def autoSelectQuality(playable):
setting_quality = ivysilani.Quality(_quality_)
url = playable.url(setting_quality)
if url or not _quality_fallback_:
return url
all_qualities = ["1080p", "720p", "576p", "404p", "288p", "144p"]
for q in all_qualities:
quality = ivysilani.Quality(q)
if setting_quality.height < quality.height:
continue
url = playable.url(quality)
if url:
return url
return None
def listLiveChannels():
xbmcplugin.setContent(_handle_, "episodes")
for liveChannel in ivysilani.LIVE_CHANNELS:
live_programme = liveChannel.programme()
title = _toString(liveChannel.title)
if hasattr(live_programme, "title") and live_programme.title:
title += ": " + _toString(live_programme.title)
plot = None
if hasattr(live_programme, "time") and live_programme.time:
plot = _toString(_lang_(30001)) + " " + _toString(live_programme.time)
if hasattr(live_programme, "elapsedPercentage") and live_programme.elapsedPercentage:
plot += " (" + _toString(live_programme.elapsedPercentage) + "%)"
if hasattr(live_programme, "synopsis") and live_programme.synopsis:
plot += "\n\n" + _toString(live_programme.synopsis)
if hasattr(live_programme, "ID") and live_programme.ID:
try:
url = _baseurl_ + "?play=" + liveChannel.ID
addDirectoryItem(title, url, ID=liveChannel.ID, plot=plot, image=live_programme.imageURL)
continue
except:
pass
if liveChannel.permanent:
title += " [" + _toString(_lang_(30002)) + "]"
url = _baseurl_ + "?menu=live"
image = None
if hasattr(live_programme, 'imageURL') and live_programme.imageURL:
image = live_programme.imageURL
addDirectoryItem(title, url, image=image)
xbmcplugin.endOfDirectory(_handle_, updateListing=False, cacheToDisc=False)
def playUrl(title, url, image, subtitles=False):
li = xbmcgui.ListItem(title)
li.setArt({'thumb': image})
res = urllib.request.urlopen(url)
try:
url = res.geturl()
finally:
res.close()
player = xbmc.Player()
player.play(url, li)
if subtitles:
while not player.isPlaying():
xbmc.sleep(2000)
player.setSubtitles(_subtitles_path_)
def playPlayable(playable, skipAutoQuality=False, forceQuality=None):
image = xbmcvfs.translatePath(os.path.join(_addon_.getAddonInfo('path'), 'resources', 'media',
'logo_' + playable.ID.lower() + '_400x225.png'))
if isinstance(playable, ivysilani.Programme):
image = playable.imageURL
if _auto_quality_ and not skipAutoQuality and not forceQuality:
url = autoSelectQuality(playable)
if url:
playUrl(playable.title, url, image,
subtitles=getattr(playable, 'subs_available', False))
return
if forceQuality:
quality = ivysilani.Quality(forceQuality)
url = playable.url(quality)
if url:
playUrl(playable.title, url, image,
subtitles=getattr(playable, 'subs_available', False))
return
qualities = playable.available_qualities()
for quality in qualities:
addDirectoryItem(quality.label(), url=_baseurl_ + "?force_quality=" + str(quality) + "&play=" + playable.ID,
title=_toString(playable.title), image=image, isFolder=False)
xbmcplugin.endOfDirectory(_handle_, updateListing=False, cacheToDisc=False)
def playLiveChannel(liveChannel, skipAutoQuality=False):
image = xbmcvfs.translatePath(os.path.join(_addon_.getAddonInfo('path'), 'resources', 'media',
'logo_' + liveChannel.ID.lower() + '_400x225.png'))
if _auto_quality_ and not skipAutoQuality:
url = autoSelectQuality(liveChannel)
if url:
playUrl(liveChannel.title, url, image)
return
qualities = liveChannel.available_qualities()
for quality in qualities:
url = liveChannel.url(quality)
addDirectoryItem(quality.label(), url=url, title=_toString(liveChannel.title), image=image, isFolder=False)
xbmcplugin.endOfDirectory(_handle_, updateListing=False, cacheToDisc=False)
def selectLiveChannel(ID):
for liveChannel in ivysilani.LIVE_CHANNELS:
if liveChannel.ID == ID:
return liveChannel
def listAlphabet():
for letter in ivysilani.alphabet():
addDirectoryItem(letter.title, _baseurl_ + "?letter=" + urllib.parse.quote_plus(_toString(letter.link)))
xbmcplugin.endOfDirectory(_handle_, updateListing=False, cacheToDisc=False)
def listGenres():
for genre in ivysilani.genres():
addDirectoryItem(genre.title, _baseurl_ + "?genre=" + urllib.parse.quote_plus(_toString(genre.link)))
xbmcplugin.endOfDirectory(_handle_, updateListing=False, cacheToDisc=False)
def listDates():
day_names = []
listing = []
for i in range(7):
day_names.append(_lang_(31000 + i))
dt = datetime.now();
min_date = datetime.fromtimestamp(time.mktime(time.strptime(ivysilani.DATE_MIN, "%Y-%m-%d")))
while dt > min_date:
pretty_date = day_names[dt.weekday()] + " " + dt.strftime("%d.%m.%Y")
formated_date = dt.strftime("%Y-%m-%d")
list_item = xbmcgui.ListItem(label=pretty_date)
listing.append((_baseurl_ + "?date=" + urllib.parse.quote_plus(formated_date), list_item, True))
dt = dt - timedelta(days=1)
xbmcplugin.addDirectoryItems(_handle_, listing, len(listing))
xbmcplugin.endOfDirectory(_handle_, updateListing=False, cacheToDisc=False)
def listChannelsForDate(date):
for channel in ivysilani.LIVE_CHANNELS:
if channel.permanent:
image = xbmcvfs.translatePath(os.path.join(_addon_.getAddonInfo('path'), 'resources', 'media',
'logo_' + channel.ID.lower() + '_400x225.png'))
url = _baseurl_ + "?date=" + urllib.parse.quote_plus(date) + "&channel=" + channel.ID
addDirectoryItem(_toString(channel.title), url, image=image)
xbmcplugin.endOfDirectory(_handle_, updateListing=False, cacheToDisc=False)
def listContext(what, ID, page):
xbmcplugin.setContent(_handle_, "episodes")
programme = ivysilani.Programme(ID)
l = []
if what == "related":
l = programme.related(page)
elif what == "episodes":
l = programme.episodes(page)
elif what == "bonuses":
l = programme.bonuses(page)
if page > 1:
addDirectoryItem('[B]<< ' + _lang_(30007) + '[/B]',
_baseurl_ + "?" + what + "=" + ID + "&page=" + str(page - 1), image=_previous_)
for item in l:
plot = None
if hasattr(item, "synopsis") and item.synopsis:
plot = item.synopsis
addDirectoryItem(item.title, _baseurl_ + "?play=" + item.ID, ID=item.ID, related=True, plot=plot,
image=item.imageURL)
if len(l) == ivysilani.PAGE_SIZE:
addDirectoryItem('[B]' + _lang_(30006) + ' >>[/B]',
_baseurl_ + "?" + what + "=" + ID + "&page=" + str(page + 1), image=_next_)
_setViewMode("Media info")
xbmcplugin.endOfDirectory(_handle_, updateListing=(page > 1), cacheToDisc=False)
def _sendError(params, exc_type, exc_value, exc_traceback):
status = "no status"
try:
conn = http.client.HTTPSConnection('script.google.com')
req_data = urllib.parse.urlencode(
{'addon': _scriptname_, 'version': _version_, 'params': _toString(params), 'type': exc_type,
'value': exc_value,
'traceback': _toString(traceback.format_exception(exc_type, exc_value, exc_traceback))})
headers = {"Content-type": "application/x-www-form-urlencoded"}
conn.request(method='POST', url='/macros/s/AKfycbyZfKhi7A_6QurtOhcan9t1W0Tug-F63_CBUwtfkBkZbR2ysFvt/exec',
body=req_data, headers=headers)
resp = conn.getresponse()
while resp.status >= 300 and resp.status < 400:
location = resp.getheader('Location')
o = urlparse(location, allow_fragments=True)
host = o.netloc
conn = http.client.HTTPSConnection(host)
url = o.path + "?" + o.query
conn.request(method='GET', url=url)
resp = conn.getresponse()
if resp.status >= 200 and resp.status < 300:
resp_body = resp.read()
json_body = json.loads(resp_body)
status = json_body['status']
if status == 'ok':
return True
else:
logErr(status)
except:
pass
logErr(status)
return False
def get_params():
param = []
paramstring = sys.argv[2]
if len(paramstring) >= 2:
params = sys.argv[2]
cleanedparams = params.replace('?', '')
if (params[len(params) - 1] == '/'):
params = params[0:len(params) - 2]
pairsofparams = cleanedparams.split('&')
param = {}
for i in range(len(pairsofparams)):
splitparams = {}
splitparams = pairsofparams[i].split('=')
if (len(splitparams)) == 2:
param[splitparams[0]] = splitparams[1]
return param
def assign_params(params):
for param in params:
try:
globals()[param] = urllib.parse.unquote_plus(params[param])
except:
pass
menu = None
play = None
play_live = None
genre = None
letter = None
date = None
channel = None
related = None
episodes = None
bonuses = None
skip_auto = None
force_quality = None
page = 1
params = get_params()
assign_params(params)
page = int(page)
try:
if play:
skip_auto = (skip_auto is not None and skip_auto != "0")
playable = selectLiveChannel(play)
if not playable:
if _show_subtitles_:
playable = ivysilani.Programme(play, subtitles_path=_subtitles_path_)
else:
playable = ivysilani.Programme(play)
playPlayable(playable, skip_auto, force_quality)
elif genre:
for g in ivysilani.genres():
if g.link == genre:
listProgrammelist(g, episodes=True)
_setViewMode("Media info")
break
elif letter:
for l in ivysilani.alphabet():
if _toString(l.link) == _toString(letter):
listProgrammelist(l, episodes=True)
_setViewMode("Media info")
break
elif date and channel:
listProgrammelist(ivysilani.Date(date, selectLiveChannel(channel)))
_setViewMode("Media info")
else:
if date:
listChannelsForDate(date)
_setViewMode('Media info 2')
elif related:
listContext("related", related, page)
_setViewMode("Media info")
elif episodes:
listContext("episodes", episodes, page)
_setViewMode("Media info")
elif bonuses:
listContext("bonuses", bonuses, page)
_setViewMode("Media info")
elif menu:
_setViewMode('List')
if menu == "live":
listLiveChannels()
elif menu == "byDate":
listDates()
elif menu == "byLetter":
listAlphabet()
elif menu == "byGenre":
listGenres()
else:
for spotlight in ivysilani.SPOTLIGHTS:
if spotlight.ID == menu:
listProgrammelist(spotlight)
break
else:
mainMenu()
except Exception as ex:
exc_type, exc_value, exc_traceback = sys.exc_info()
logErr(traceback.format_exception(exc_type, exc_value, exc_traceback))
found = False
for wnm in well_known_error_messages:
if str(ex) == wnm[0]:
xbmcgui.Dialog().notification(_scriptname_, _lang_(wnm[1]), xbmcgui.NOTIFICATION_ERROR)
found = True
if not found:
_exception_log(exc_type, exc_value, exc_traceback)
except Exception as ex:
exc_type, exc_value, exc_traceback = sys.exc_info()
_exception_log(exc_type, exc_value, exc_traceback)