-
Notifications
You must be signed in to change notification settings - Fork 20
/
vrtapihelper.py
453 lines (388 loc) · 21.1 KB
/
vrtapihelper.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
# -*- coding: utf-8 -*-
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, unicode_literals
from resources.lib.helperobjects.helperobjects import TitleItem
from resources.lib.vrtplayer import actions, metadatacreator, statichelper
try:
from urllib.parse import urlencode, unquote
from urllib.request import build_opener, install_opener, ProxyHandler, urlopen
except ImportError:
from urllib2 import build_opener, install_opener, ProxyHandler, urlopen, unquote
from urllib import urlencode # pylint: disable=ungrouped-imports
class VRTApiHelper:
_VRT_BASE = 'https://www.vrt.be'
_VRTNU_SEARCH_URL = 'https://vrtnu-api.vrt.be/search'
_VRTNU_SUGGEST_URL = 'https://vrtnu-api.vrt.be/suggest'
_VRTNU_SCREENSHOT_URL = 'https://vrtnu-api.vrt.be/screenshots'
def __init__(self, _kodi, _favorites):
self._kodi = _kodi
self._proxies = _kodi.get_proxies()
install_opener(build_opener(ProxyHandler(self._proxies)))
self._showpermalink = _kodi.get_setting('showpermalink') == 'true'
self._favorites = _favorites
def get_tvshow_items(self, category=None, channel=None, filtered=False):
import json
params = dict()
if category:
params['facets[categories]'] = category
cache_file = 'category.%s.json' % category
if channel:
params['facets[programBrands]'] = channel
cache_file = 'channel.%s.json' % channel
# If no facet-selection is done, we return the A-Z listing
if not category and not channel:
params['facets[transcodingStatus]'] = 'AVAILABLE'
cache_file = 'programs.json'
api_url = self._VRTNU_SUGGEST_URL + '?' + urlencode(params)
# Try the cache if it is fresh
data = self._kodi.get_cache(cache_file, ttl=60 * 60)
if not data:
self._kodi.log_notice('URL get: ' + unquote(api_url), 'Verbose')
data = urlopen(api_url).read()
self._kodi.update_cache(cache_file, data)
api_json = json.loads(data)
return self._map_to_tvshow_items(api_json, filtered=statichelper.is_filtered(filtered))
def _map_to_tvshow_items(self, tvshows, filtered=False):
tvshow_items = []
if statichelper.is_filtered(filtered):
favorite_names = self._favorites.names()
for tvshow in tvshows:
if statichelper.is_filtered(filtered) and tvshow.get('programName') not in favorite_names:
continue
metadata = metadatacreator.MetadataCreator()
metadata.tvshowtitle = tvshow.get('title', '???')
metadata.plot = statichelper.unescape(tvshow.get('description', '???'))
metadata.brands = tvshow.get('brands')
metadata.permalink = statichelper.shorten_link(tvshow.get('targetUrl'))
# NOTE: This adds episode_count to label, would be better as metadata
# title = '%s [LIGHT][COLOR yellow]%s[/COLOR][/LIGHT]' % (tvshow.get('title', '???'), tvshow.get('episode_count', '?'))
label = tvshow.get('title', '???')
thumbnail = statichelper.add_https_method(tvshow.get('thumbnail', 'DefaultAddonVideo.png'))
program_path = statichelper.unique_path(tvshow.get('targetUrl'))
if self._favorites.is_activated():
program = tvshow.get('title').encode('utf-8')
if self._favorites.is_favorite(program_path):
params = dict(action='unfollow', program=program, path=program_path)
context_menu = [(self._kodi.localize(30412), 'RunPlugin(plugin://plugin.video.vrt.nu?%s)' % urlencode(params))]
else:
params = dict(action='follow', program=program, path=program_path)
context_menu = [(self._kodi.localize(30411), 'RunPlugin(plugin://plugin.video.vrt.nu?%s)' % urlencode(params))]
else:
context_menu = []
# Cut vrtbase url off since it will be added again when searching for episodes
# (with a-z we dont have the full url)
video_url = statichelper.add_https_method(tvshow.get('targetUrl')).replace(self._VRT_BASE, '')
tvshow_items.append(TitleItem(
title=label,
url_dict=dict(action=actions.LISTING_EPISODES, video_url=video_url),
is_playable=False,
art_dict=dict(thumb=thumbnail, icon='DefaultAddonVideo.png', fanart=thumbnail),
video_dict=metadata.get_video_dict(),
context_menu=context_menu,
))
return tvshow_items
def _get_season_items(self, api_url, api_json):
season_items = []
sort = 'label'
ascending = True
content = 'seasons'
episodes = api_json.get('results')
facets = api_json.get('facets', dict()).get('facets')
if episodes and facets:
try:
# Check if program has seasons
facet = next(f for f in facets if f.get('name') == 'seasons' and len(f.get('buckets', [])) > 1)
# Found multiple seasons, make list of seasons
season_items, sort, ascending, content = self._map_to_season_items(api_url, facet.get('buckets', []), episodes)
except StopIteration:
pass
return season_items, sort, ascending, content
def get_episode_items(self, path=None, page=None, all_seasons=False, filtered=False, variety=None):
import json
episode_items = []
sort = 'episode'
ascending = True
# Recent items
if variety in ('offline', 'recent'):
page = statichelper.realpage(page)
params = {
'from': ((page - 1) * 50) + 1,
'i': 'video',
'size': 50,
}
if variety == 'offline':
from datetime import datetime
import dateutil.tz
params['facets[assetOffTime]'] = datetime.now(dateutil.tz.gettz('Europe/Brussels')).strftime('%Y-%m-%d')
if statichelper.is_filtered(filtered):
params['facets[programName]'] = '[%s]' % (','.join(self._favorites.names()))
else:
params['facets[programBrands]'] = '[een,canvas,sporza,vrtnws,vrtnxt,radio1,radio2,klara,stubru,mnm]'
api_url = self._VRTNU_SEARCH_URL + '?' + urlencode(params)
self._kodi.log_notice('URL get: ' + unquote(api_url), 'Verbose')
api_json = json.loads(urlopen(api_url).read())
episode_items, sort, ascending, content = self._map_to_episode_items(api_json.get('results', []), titletype='recent', filtered=statichelper.is_filtered(filtered))
if path:
if '.relevant/' in path:
params = {
'facets[programUrl]': '//www.vrt.be' + path.replace('.relevant/', '/'),
'i': 'video',
'size': '150',
}
api_url = self._VRTNU_SEARCH_URL + '?' + urlencode(params)
else:
api_url = path
self._kodi.log_notice('URL get: ' + unquote(api_url), 'Verbose')
api_json = json.loads(urlopen(api_url).read())
episodes = api_json.get('results', [{}])
if episodes:
episode = episodes[0]
else:
episode = dict()
display_options = episode.get('displayOptions', dict())
# NOTE: Hard-code showing seasons because it is unreliable (i.e; Thuis or Down the Road have it disabled)
display_options['showSeason'] = True
# Look for seasons items if not yet done
season_key = None
# path = requests.utils.unquote(path)
path = unquote(path)
if all_seasons is True:
episode_items, sort, ascending, content = self._map_to_episode_items(episodes, season_key=None)
elif 'facets[seasonTitle]' in path:
season_key = path.split('facets[seasonTitle]=')[1]
elif display_options.get('showSeason') is True:
episode_items, sort, ascending, content = self._get_season_items(api_url, api_json)
# No season items, generate episode items
if not episode_items:
episode_items, sort, ascending, content = self._map_to_episode_items(episodes, season_key=season_key)
return episode_items, sort, ascending, content
def _map_to_episode_items(self, episodes, titletype=None, season_key=None, filtered=False):
from datetime import datetime
import dateutil.parser
import dateutil.tz
now = datetime.now(dateutil.tz.tzlocal())
sort = 'episode'
ascending = True
if statichelper.is_filtered(filtered):
favorite_names = self._favorites.names()
episode_items = []
for episode in episodes:
# VRT API workaround: seasonTitle facet behaves as a partial match regex,
# so we have to filter out the episodes from seasons that don't exactly match.
if season_key and episode.get('seasonTitle') != season_key:
continue
if statichelper.is_filtered(filtered) and episode.get('programName') not in favorite_names:
continue
# Support search highlights
highlight = episode.get('highlight')
if highlight:
for key in highlight:
episode[key] = statichelper.convert_html_to_kodilabel(highlight.get(key)[0])
display_options = episode.get('displayOptions', dict())
# NOTE: Hard-code showing seasons because it is unreliable (i.e; Thuis or Down the Road have it disabled)
display_options['showSeason'] = True
if titletype is None:
titletype = episode.get('programType')
metadata = metadatacreator.MetadataCreator()
metadata.tvshowtitle = episode.get('program')
if episode.get('broadcastDate') != -1:
metadata.datetime = datetime.fromtimestamp(episode.get('broadcastDate', 0) / 1000, dateutil.tz.UTC)
metadata.duration = (episode.get('duration', 0) * 60) # Minutes to seconds
metadata.plot = statichelper.convert_html_to_kodilabel(episode.get('description'))
metadata.brands = episode.get('programBrands') or episode.get('brands')
metadata.geolocked = episode.get('allowedRegion') == 'BE'
if display_options.get('showShortDescription'):
short_description = statichelper.convert_html_to_kodilabel(episode.get('shortDescription'))
metadata.plotoutline = short_description
metadata.subtitle = short_description
else:
metadata.plotoutline = episode.get('subtitle')
metadata.subtitle = episode.get('subtitle')
metadata.season = episode.get('seasonTitle')
metadata.episode = episode.get('episodeNumber')
metadata.mediatype = episode.get('type', 'episode')
metadata.permalink = statichelper.shorten_link(episode.get('permalink')) or episode.get('externalPermalink')
if episode.get('assetOnTime'):
metadata.ontime = dateutil.parser.parse(episode.get('assetOnTime'))
if episode.get('assetOffTime'):
metadata.offtime = dateutil.parser.parse(episode.get('assetOffTime'))
# Add additional metadata to plot
plot_meta = ''
if metadata.geolocked:
# Show Geo-locked
plot_meta += self._kodi.localize(30201)
# Only display when a video disappears if it is within the next 3 months
if metadata.offtime is not None and (metadata.offtime - now).days < 93:
# Show date when episode is removed
plot_meta += self._kodi.localize(30202).format(date=self._kodi.localize_dateshort(metadata.offtime))
# Show the remaining days/hours the episode is still available
if (metadata.offtime - now).days > 0:
plot_meta += self._kodi.localize(30203).format(days=(metadata.offtime - now).days)
else:
plot_meta += self._kodi.localize(30204).format(hours=int((metadata.offtime - now).seconds / 3600))
if plot_meta:
metadata.plot = '%s\n%s' % (plot_meta, metadata.plot)
if self._showpermalink and metadata.permalink:
metadata.plot = '%s\n\n[COLOR yellow]%s[/COLOR]' % (metadata.plot, metadata.permalink)
program_path = statichelper.unique_path(episode.get('programUrl'))
if self._favorites.is_activated():
program = episode.get('program').encode('utf-8')
if self._favorites.is_favorite(program_path):
params = dict(action='unfollow', program=program, path=program_path)
context_menu = [(self._kodi.localize(30412), 'RunPlugin(plugin://plugin.video.vrt.nu?%s)' % urlencode(params))]
else:
params = dict(action='follow', program=program, path=program_path)
context_menu = [(self._kodi.localize(30411), 'RunPlugin(plugin://plugin.video.vrt.nu?%s)' % urlencode(params))]
else:
context_menu = []
thumb = statichelper.add_https_method(episode.get('videoThumbnailUrl', 'DefaultAddonVideo.png'))
fanart = statichelper.add_https_method(episode.get('programImageUrl', thumb))
video_url = statichelper.add_https_method(episode.get('url'))
label, sort, ascending = self._make_label(episode, titletype, options=display_options)
metadata.title = label
episode_items.append(TitleItem(
title=label,
url_dict=dict(action=actions.PLAY, video_url=video_url, video_id=episode.get('videoId'), publication_id=episode.get('publicationId')),
is_playable=True,
art_dict=dict(thumb=thumb, icon='DefaultAddonVideo.png', fanart=fanart),
video_dict=metadata.get_video_dict(),
context_menu=context_menu,
))
return episode_items, sort, ascending, 'episodes'
def _map_to_season_items(self, api_url, seasons, episodes):
import random
season_items = []
sort = 'label'
ascending = True
episode = random.choice(episodes)
program_type = episode.get('programType')
fanart = statichelper.add_https_method(episode.get('programImageUrl', 'DefaultSets.png'))
metadata = metadatacreator.MetadataCreator()
metadata.tvshowtitle = episode.get('program')
metadata.plot = statichelper.convert_html_to_kodilabel(episode.get('programDescription'))
metadata.plotoutline = statichelper.convert_html_to_kodilabel(episode.get('programDescription'))
metadata.brands = episode.get('programBrands') or episode.get('brands')
metadata.geolocked = episode.get('allowedRegion') == 'BE'
metadata.season = episode.get('seasonTitle')
# Add additional metadata to plot
plot_meta = ''
if metadata.geolocked:
# Show Geo-locked
plot_meta += self._kodi.localize(30201) + '\n'
metadata.plot = '%s[B]%s[/B]\n%s' % (plot_meta, episode.get('program'), metadata.plot)
# Reverse sort seasons if program_type is 'reeksaflopend' or 'daily'
if program_type in ('daily', 'reeksaflopend'):
ascending = False
# Add an "* All seasons" list item
if self._kodi.get_global_setting('videolibrary.showallitems') is True:
season_items.append(TitleItem(
title=self._kodi.localize(30096),
url_dict=dict(action=actions.LISTING_ALL_EPISODES, video_url=api_url),
is_playable=False,
art_dict=dict(thumb=fanart, icon='DefaultSets.png', fanart=fanart),
video_dict=metadata.get_video_dict(),
))
# NOTE: Sort the episodes ourselves, because Kodi does not allow to set to 'ascending'
seasons = sorted(seasons, key=lambda k: k['key'], reverse=not ascending)
for season in seasons:
season_key = season['key']
try:
# If more than 150 episodes exist, we may end up with an empty season (Winteruur)
episode = random.choice([e for e in episodes if e.get('seasonName') == season_key])
except IndexError:
episode = episodes[0]
fanart = statichelper.add_https_method(episode.get('programImageUrl', 'DefaultSets.png'))
thumbnail = statichelper.add_https_method(episode.get('videoThumbnailUrl', fanart))
label = '%s %s' % (self._kodi.localize(30094), season_key)
params = {'facets[seasonTitle]': season_key}
path = api_url + '&' + urlencode(params)
season_items.append(TitleItem(
title=label,
url_dict=dict(action=actions.LISTING_EPISODES, video_url=path),
is_playable=False,
art_dict=dict(thumb=thumbnail, icon='DefaultSets.png', fanart=fanart),
video_dict=metadata.get_video_dict(),
))
return season_items, sort, ascending, 'seasons'
def search(self, search_string, page=0):
import json
page = statichelper.realpage(page)
params = {
'from': ((page - 1) * 50) + 1,
'i': 'video',
'size': 50,
'q': search_string,
'highlight': 'true',
}
api_url = 'https://search.vrt.be/search?' + urlencode(params)
self._kodi.log_notice('URL get: ' + unquote(api_url), 'Verbose')
api_json = json.loads(urlopen(api_url).read())
episodes = api_json.get('results', [{}])
episode_items, sort, ascending, content = self._map_to_episode_items(episodes, titletype='recent')
return episode_items, sort, ascending, content
def get_live_screenshot(self, channel):
url = '%s/%s.jpg' % (self._VRTNU_SCREENSHOT_URL, channel)
self.__delete_cached_thumbnail(url)
return url
def __delete_cached_thumbnail(self, url):
crc = self.__get_crc32(url)
ext = url.split('.')[-1]
path = 'special://thumbnails/%s/%s.%s' % (crc[0], crc, ext)
self._kodi.delete_file(path)
@staticmethod
def __get_crc32(string):
string = string.lower()
string_bytes = bytearray(string.encode())
crc = 0xffffffff
for b in string_bytes:
crc = crc ^ (b << 24)
for _ in range(8):
if crc & 0x80000000:
crc = (crc << 1) ^ 0x04C11DB7
else:
crc = crc << 1
crc = crc & 0xFFFFFFFF
return '%08x' % crc
def _make_label(self, result, titletype, options=None):
if options is None:
options = dict()
if options.get('showEpisodeTitle'):
label = statichelper.convert_html_to_kodilabel(result.get('title') or result.get('shortDescription'))
elif options.get('showShortDescription'):
label = statichelper.convert_html_to_kodilabel(result.get('shortDescription') or result.get('title'))
else:
label = statichelper.convert_html_to_kodilabel(result.get('title') or result.get('shortDescription'))
sort = 'unsorted'
ascending = True
if titletype == 'recent':
ascending = False
sort = 'dateadded'
label = '[B]%s[/B] - %s' % (result.get('program'), label)
elif titletype in ('reeksaflopend', 'reeksoplopend'):
if titletype == 'reeksaflopend':
ascending = False
# NOTE: This is disable on purpose as 'showSeason' is not reliable
if options.get('showSeason') is False and options.get('showEpisodeNumber') and result.get('seasonName') and result.get('episodeNumber'):
try:
sort = 'dateadded'
label = 'S%02dE%02d: %s' % (int(result.get('seasonName')), int(result.get('episodeNumber')), label)
except Exception:
# Season may not always be a perfect number
sort = 'episode'
elif options.get('showEpisodeNumber') and result.get('episodeNumber') and ascending:
# NOTE: Sort the episodes ourselves, because Kodi does not allow to set to 'descending'
# sort = 'episode'
sort = 'label'
label = '%s %s: %s' % (self._kodi.localize(30095), result.get('episodeNumber'), label)
elif options.get('showBroadcastDate') and result.get('formattedBroadcastShortDate'):
sort = 'dateadded'
label = '%s - %s' % (result.get('formattedBroadcastShortDate'), label)
else:
sort = 'dateadded'
elif titletype == 'daily':
ascending = False
sort = 'dateadded'
label = '%s - %s' % (result.get('formattedBroadcastShortDate'), label)
elif titletype == 'oneoff':
sort = 'label'
return label, sort, ascending