Skip to content

Commit

Permalink
always use programUrl (instead of programName)
Browse files Browse the repository at this point in the history
  • Loading branch information
mediaminister committed Jun 10, 2019
1 parent 7104fd9 commit eedb8aa
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 132 deletions.
55 changes: 24 additions & 31 deletions resources/lib/favorites.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
''' Implementation of Favorites class '''

from __future__ import absolute_import, division, unicode_literals
from resources.lib import statichelper

try: # Python 3
from urllib.request import build_opener, install_opener, ProxyHandler, Request, urlopen
Expand Down Expand Up @@ -54,12 +55,12 @@ def get_favorites(self, ttl=None):
self._kodi.update_cache('favorites.json', api_json)
self._favorites = api_json

def set_favorite(self, program, path, value=True):
def set_favorite(self, title, program, value=True):
''' Set a program as favorite, and update local copy '''
import json

self.get_favorites(ttl=60 * 60)
if value is self.is_favorite(path):
if value is self.is_favorite(program):
# Already followed/unfollowed, nothing to do
return True

Expand All @@ -74,63 +75,55 @@ def set_favorite(self, program, path, value=True):
'content-type': 'application/json',
'Referer': 'https://www.vrt.be/vrtnu',
}
payload = dict(isFavorite=value, programUrl=path, title=program)
payload = dict(isFavorite=value, programUrl=statichelper.program_to_short_program_url(program), title=title)
data = json.dumps(payload).encode('utf-8')
self._kodi.log_notice('URL post: https://video-user-data.vrt.be/favorites/%s' % self.uuid(path), 'Verbose')
req = Request('https://video-user-data.vrt.be/favorites/%s' % self.uuid(path), data=data, headers=headers)
self._kodi.log_notice('URL post: https://video-user-data.vrt.be/favorites/%s' % self.uuid(program), 'Verbose')
req = Request('https://video-user-data.vrt.be/favorites/%s' % self.uuid(program), data=data, headers=headers)
result = urlopen(req)
if result.getcode() != 200:
self._kodi.show_notification(message="Failed to (un)follow program '%s' at VRT NU" % path)
self._kodi.log_error("Failed to (un)follow program '%s' at VRT NU" % path)
self._kodi.show_notification(message="Failed to (un)follow program '%s' at VRT NU" % program)
self._kodi.log_error("Failed to (un)follow program '%s' at VRT NU" % program)
return False
# NOTE: Updates to favorites take a longer time to take effect, so we keep our own cache and use it
self._favorites[self.uuid(path)] = dict(value=payload)
self._favorites[self.uuid(program)] = dict(value=payload)
self._kodi.update_cache('favorites.json', self._favorites)
self.invalidate_caches()
return True

def is_favorite(self, path):
def is_favorite(self, program):
''' Is a program a favorite ? '''
value = False
favorite = self._favorites.get(self.uuid(path))
favorite = self._favorites.get(self.uuid(program))
if favorite is not None:
value = favorite.get('value', dict(isFavorite=False)).get('isFavorite', False)
return value

def follow(self, program, path):
def follow(self, title, program):
''' Follow your favorite program '''
ok = self.set_favorite(program, path, True)
ok = self.set_favorite(title, program, True)
if ok:
self._kodi.show_notification(message='Follow ' + program)
self._kodi.show_notification(message='Follow ' + title)
self._kodi.container_refresh()

def unfollow(self, program, path):
def unfollow(self, title, program):
''' Unfollow your favorite program '''
ok = self.set_favorite(program, path, False)
ok = self.set_favorite(title, program, False)
if ok:
self._kodi.show_notification(message='Unfollow ' + program)
self._kodi.show_notification(message='Unfollow ' + title)
self._kodi.container_refresh()

def uuid(self, path):
''' Return a favorite uuid, used for lookups in favorites dict '''
return path.replace('/', '').replace('-', '')

def name(self, path):
''' Return the favorite name '''
try:
return path.replace('.relevant/', '/').split('/')[-2]
except IndexError:
# FIXME: Investigate when this fails !
return path

def names(self):
''' Return all favorite names '''
return [self.name(p.get('value').get('programUrl')) for p in self._favorites.values() if p.get('value').get('isFavorite')]
def uuid(self, program):
''' Convert a program url component (e.g. de-campus-cup) to a favorite uuid (e.g. vrtnuazdecampuscup), used for lookups in favorites dict '''
return 'vrtnuaz' + program.replace('-', '')

def titles(self):
''' Return all favorite titles '''
return [p.get('value').get('title') for p in self._favorites.values() if p.get('value').get('isFavorite')]

def programs(self):
''' Return all favorite programs '''
return [statichelper.short_program_url_to_program(p.get('value').get('programUrl')) for p in self._favorites.values() if p.get('value').get('isFavorite')]

def invalidate_caches(self):
''' Invalidate caches that rely on favorites '''
# NOTE/ Do not invalidate favorites cache as we manage it ourselves
Expand Down
8 changes: 4 additions & 4 deletions resources/lib/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ def router(argv):

# Actions requiring _favorites as well
if action == actions.FOLLOW:
_favorites.follow(program=params.get('program'), path=params.get('path'))
_favorites.follow(title=params.get('title'), program=params.get('program'))
return
if action == actions.UNFOLLOW:
_favorites.unfollow(program=params.get('program'), path=params.get('path'))
_favorites.unfollow(title=params.get('title'), program=params.get('program'))
return
if action == actions.REFRESH_FAVORITES:
_favorites.get_favorites(ttl=0)
Expand Down Expand Up @@ -109,11 +109,11 @@ def router(argv):
return
if action == actions.LISTING_EPISODES:
_favorites.get_favorites(ttl=60 * 60)
_vrtplayer.show_episodes(path=params.get('video_url'))
_vrtplayer.show_episodes(program=params.get('program'), season=params.get('season'))
return
if action == actions.LISTING_ALL_EPISODES:
_favorites.get_favorites(ttl=60 * 60)
_vrtplayer.show_all_episodes(path=params.get('video_url'))
_vrtplayer.show_all_episodes(program=params.get('program'))
return
if action == actions.SEARCH:
_favorites.get_favorites(ttl=60 * 60)
Expand Down
43 changes: 38 additions & 5 deletions resources/lib/statichelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,44 @@ def convert_html_to_kodilabel(text):
return unescape(text).strip()


def unique_path(path):
''' Create a unique path to be used in VRT favorites '''
if path.startswith('//www.vrt.be/vrtnu'):
return path.replace('//www.vrt.be/vrtnu/', '/vrtnu/').replace('.relevant/', '/')
return path
def program_to_short_program_url(program):
''' Convert a program url component (e.g. de-campus-cup) to a short programUrl (e.g. /vrtnu/a-z/de-campus-cup/) '''
short_program_url = None
if program:
short_program_url = '/vrtnu/a-z/' + program + '/'
return short_program_url


def program_to_long_program_url(program):
''' Convert a program url component (e.g. de-campus-cup) to a long programUrl (e.g. //www.vrt.be/vrtnu/a-z/de-campus-cup/) '''
long_program_url = None
if program:
long_program_url = '//www.vrt.be/vrtnu/a-z/' + program + '/'
return long_program_url


def target_url_to_program(target_url):
''' Convert a targetUrl (e.g. //www.vrt.be/vrtnu/a-z/de-campus-cup.relevant/) to a program url component (e.g. de-campus-cup) '''
program = None
if target_url.startswith('//www.vrt.be') and target_url.endswith('.relevant/'):
program = target_url.replace('//www.vrt.be/vrtnu/a-z/', '').replace('.relevant/', '')
return program


def short_program_url_to_program(short_program_url):
''' Convert a short programUrl (e.g. /vrtnu/a-z/de-campus-cup/) to a program url component (e.g. de-campus-cup) '''
program = None
if short_program_url.startswith('/vrtnu/a-z/'):
program = short_program_url.replace('/vrtnu/a-z/', '').rstrip('/')
return program


def long_program_url_to_program(long_program_url):
''' Convert a long programUrl (e.g. //www.vrt.be/vrtnu/a-z/de-campus-cup/) to a program url component (e.g. de-campus-cup) '''
program = None
if long_program_url.startswith('//www.vrt.be/vrtnu/a-z/'):
program = long_program_url.replace('//www.vrt.be/vrtnu/a-z/', '').rstrip('/')
return program


def shorten_link(url):
Expand Down
Loading

0 comments on commit eedb8aa

Please sign in to comment.