Skip to content

Commit

Permalink
Add a Refresh context menu (#317)
Browse files Browse the repository at this point in the history
The original implementation was very invasive and touched almost every
API to pass a parameter `use_cache` so the specific cache would be
invalidated.

This is easier but as effective, we have a small list of actions with
known caches, and we simply reuse the existing params to refresh the
specific cache and then refresh the container.
  • Loading branch information
dagwieers authored Jun 17, 2019
1 parent 9301064 commit e0e6de5
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 16 deletions.
13 changes: 13 additions & 0 deletions resources/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,16 @@ class actions:
REFRESH_FAVORITES = 'refreshfavorites'
SEARCH = 'search'
UNFOLLOW = 'unfollow'


CACHES = dict(
listingallepisodes=[], # Refresh, but no cache
listingaztvshows=['programs.json'],
listingcategorytvshows=['category.{category}.json'],
listingchannels=['channel.{channel}.json'],
listingepisodes=[], # Refresh, but no cache
listinglive=[], # Refresh, but no cache
listingoffline=['{prefix}offline-{page}.json'],
listingrecent=['{prefix}recent-{page}.json'],
listingtvguide=['schedule.{date}.json'],
)
2 changes: 1 addition & 1 deletion resources/lib/favorites.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def titles(self):

def invalidate_caches(self):
''' Invalidate caches that rely on favorites '''
# NOTE/ Do not invalidate favorites cache as we manage it ourselves
# NOTE: Do not invalidate favorites cache as we manage it ourselves
# self._kodi.invalidate_caches('favorites.json')
self._kodi.invalidate_caches('my-offline-*.json')
self._kodi.invalidate_caches('my-recent-*.json')
32 changes: 30 additions & 2 deletions resources/lib/kodiwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ def has_socks():
class KodiWrapper:
''' A wrapper around all Kodi functionality '''

def __init__(self, handle, url):
def __init__(self, handle, url, params):
''' Initialize the Kodi wrapper '''
self._handle = handle
self._url = url
self._params = params
self._addon = xbmcaddon.Addon()
self._addon_id = self._addon.getAddonInfo('id')
self._max_log_level = log_levels.get(self.get_setting('max_log_level', 'Debug'), 3)
Expand Down Expand Up @@ -513,8 +514,29 @@ def update_cache(self, path, data):
self.log_notice("Cache '%s' has not changed, updating mtime only." % path, 'Debug')
os.utime(path)

def refresh_caches(self, params):
''' Invalidate the needed caches and refresh container '''
from resources.lib import CACHES, statichelper

prefix = ''
use_favorites = statichelper.boolean(params.get('use_favorites'))
if use_favorites:
prefix = 'my-'
# self.invalidate_cache(path='favorites.json')

if 'page' not in params:
params['page'] = 1

action = params.get('action')
if action in CACHES:
for cache in CACHES[action]:
self.invalidate_caches(expr=cache.format(prefix=prefix, **params))
self.container_refresh()
else:
self.log_notice("No caches found for action '%s'" % action)

def invalidate_cache(self, path):
''' Invalidate an existing cache file '''
''' Invalidate a existing cache file '''
self.delete_file(self._cache_path + path)

def invalidate_caches(self, expr=None):
Expand All @@ -526,6 +548,12 @@ def invalidate_caches(self, expr=None):
for f in files:
self.delete_file(self._cache_path + f)

def container_url(self, **params):
''' Return an updated URL for this container '''
new_params = self._params
new_params.update(params)
return self._url + '?' + urlencode(new_params)

def container_refresh(self):
''' Refresh the current container '''
self.log_notice('Execute: Container.Refresh', 'Debug')
Expand Down
19 changes: 13 additions & 6 deletions resources/lib/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from __future__ import absolute_import, division, unicode_literals

from resources.lib import actions, kodiwrapper, tokenresolver
from resources.lib import actions, kodiwrapper

try: # Python 3
from urllib.parse import parse_qsl
Expand All @@ -24,17 +24,16 @@ def router(argv):
params = dict(parse_qsl(params_string))
action = params.get('action')

_kodi = kodiwrapper.KodiWrapper(addon_handle, addon_url)
_tokenresolver = tokenresolver.TokenResolver(_kodi)
_kodi = kodiwrapper.KodiWrapper(addon_handle, addon_url, params)
_kodi.log_access(addon_url, params_string)

# Actions that only require _kodi
if params.get('refresh') == 'true':
_kodi.refresh_caches(params)
return
if action == actions.INVALIDATE_CACHES:
_kodi.invalidate_caches()
return
if action == actions.DELETE_TOKENS:
_tokenresolver.delete_tokens()
return
if action == actions.LISTING_TVGUIDE:
from resources.lib import tvguide
_tvguide = tvguide.TVGuide(_kodi)
Expand All @@ -44,6 +43,14 @@ def router(argv):
_kodi.install_widevine()
return

from resources.lib import tokenresolver
_tokenresolver = tokenresolver.TokenResolver(_kodi)

# Actions requiring _tokenresolver as well
if action == actions.DELETE_TOKENS:
_tokenresolver.delete_tokens()
return

from resources.lib import favorites
_favorites = favorites.Favorites(_kodi, _tokenresolver)

Expand Down
1 change: 1 addition & 0 deletions resources/lib/tvguide.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def show_date_menu(self):
is_playable=False,
art_dict=dict(thumb='DefaultYear.png', icon='DefaultYear.png', fanart='DefaultYear.png'),
video_dict=dict(plot=self._kodi.localize_datelong(day)),
context_menu=[('Refresh', 'RunPlugin(%s)' % self._kodi.container_url(refresh='true'))],
))
return date_items

Expand Down
5 changes: 5 additions & 0 deletions resources/lib/vrtapihelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def _map_to_tvshow_items(self, tvshows, use_favorites=False):
context_menu = [(self._kodi.localize(30411), 'RunPlugin(plugin://plugin.video.vrt.nu?%s)' % urlencode(params))]
else:
context_menu = []
context_menu.append(('Refresh', 'RunPlugin(%s)' % self._kodi.container_url(refresh='true')))
# 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, '')
Expand Down Expand Up @@ -308,6 +309,7 @@ def _map_to_episode_items(self, episodes, titletype=None, season_key=None, use_f
context_menu = [(self._kodi.localize(30411), 'RunPlugin(plugin://plugin.video.vrt.nu?%s)' % urlencode(params))]
else:
context_menu = []
context_menu.append(('Refresh', 'RunPlugin(%s)' % self._kodi.container_url(refresh='true')))

if self._showfanart:
thumb = statichelper.add_https_method(episode.get('videoThumbnailUrl', 'DefaultAddonVideo.png'))
Expand Down Expand Up @@ -526,6 +528,7 @@ def get_channel_items(self, action=actions.PLAY, channels=None):
label = channel.get('label')
plot = '[B]%s[/B]' % channel.get('label')
is_playable = False
context_menu = []
else:
url_dict = dict(action=action)
label = self._kodi.localize(30101).format(**channel)
Expand All @@ -540,6 +543,7 @@ def get_channel_items(self, action=actions.PLAY, channels=None):
url_dict['video_url'] = channel.get('live_stream')
if channel.get('live_stream_id'):
url_dict['video_id'] = channel.get('live_stream_id')
context_menu = [('Refresh', 'RunPlugin(%s)' % self._kodi.container_url(refresh='true'))]

channel_items.append(TitleItem(
title=label,
Expand All @@ -552,6 +556,7 @@ def get_channel_items(self, action=actions.PLAY, channels=None):
studio=channel.get('studio'),
mediatype='video',
),
context_menu=context_menu,
))

return channel_items
Expand Down
2 changes: 1 addition & 1 deletion service.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self):

def onSettingsChanged(self):
''' Handler for changes to settings '''
_kodi = kodiwrapper.KodiWrapper(None, None)
_kodi = kodiwrapper.KodiWrapper(None, None, dict())
_kodi.log_notice('VRT NU Addon: settings changed')
_kodi.container_refresh()

Expand Down
2 changes: 1 addition & 1 deletion test/apihelpertests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class ApiHelperTests(unittest.TestCase):

_kodi = kodiwrapper.KodiWrapper(None, 'plugin://plugin.video.vrt.nu')
_kodi = kodiwrapper.KodiWrapper(None, 'plugin://plugin.video.vrt.nu', dict())
_tokenresolver = tokenresolver.TokenResolver(_kodi)
_favorites = favorites.Favorites(_kodi, _tokenresolver)
_apihelper = vrtapihelper.VRTApiHelper(_kodi, _favorites)
Expand Down
2 changes: 1 addition & 1 deletion test/favoritestests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class TestFavorites(unittest.TestCase):

_kodi = kodiwrapper.KodiWrapper(None, 'plugin://plugin.video.vrt.nu')
_kodi = kodiwrapper.KodiWrapper(None, 'plugin://plugin.video.vrt.nu', dict())
_tokenresolver = tokenresolver.TokenResolver(_kodi)
_favorites = favorites.Favorites(_kodi, _tokenresolver)

Expand Down
2 changes: 1 addition & 1 deletion test/searchtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class TestSearch(unittest.TestCase):

_kodi = kodiwrapper.KodiWrapper(None, 'plugin.video.vrt.nu')
_kodi = kodiwrapper.KodiWrapper(None, 'plugin.video.vrt.nu', dict())
_tokenresolver = tokenresolver.TokenResolver(_kodi)
_favorites = favorites.Favorites(_kodi, _tokenresolver)
_apihelper = vrtapihelper.VRTApiHelper(_kodi, _favorites)
Expand Down
2 changes: 1 addition & 1 deletion test/streamservicetests.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

class StreamServiceTests(unittest.TestCase):

_kodi = kodiwrapper.KodiWrapper(None, 'plugin://plugin.video.vrt.nu')
_kodi = kodiwrapper.KodiWrapper(None, 'plugin://plugin.video.vrt.nu', dict())
_tokenresolver = tokenresolver.TokenResolver(_kodi)
_streamservice = streamservice.StreamService(_kodi, _tokenresolver)

Expand Down
2 changes: 1 addition & 1 deletion test/tvguidetests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class TestTVGuide(unittest.TestCase):

_kodi = kodiwrapper.KodiWrapper(None, 'plugin.video.vrt.nu')
_kodi = kodiwrapper.KodiWrapper(None, 'plugin.video.vrt.nu', dict())
_tvguide = tvguide.TVGuide(_kodi)

def test_tvguide_date_menu(self):
Expand Down
2 changes: 1 addition & 1 deletion test/vrtplayertests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class TestVRTPlayer(unittest.TestCase):

_kodi = kodiwrapper.KodiWrapper(None, 'plugin://plugin.video.vrt.nu')
_kodi = kodiwrapper.KodiWrapper(None, 'plugin://plugin.video.vrt.nu', dict())
_tokenresolver = tokenresolver.TokenResolver(_kodi)
_favorites = favorites.Favorites(_kodi, _tokenresolver)
_apihelper = vrtapihelper.VRTApiHelper(_kodi, _favorites)
Expand Down

0 comments on commit e0e6de5

Please sign in to comment.