Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
Fix empty My List due to unknown items (#102)
Browse files Browse the repository at this point in the history
* Ignore unavailable items on My List

* Rework My List API
  • Loading branch information
michaelarnauts authored Feb 3, 2022
1 parent 21f877f commit 02b2d4d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 48 deletions.
47 changes: 5 additions & 42 deletions resources/lib/modules/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
from __future__ import absolute_import, division, unicode_literals

import logging
from datetime import datetime

import dateutil.tz

from resources.lib import kodiutils
from resources.lib.kodiutils import TitleItem
Expand Down Expand Up @@ -253,22 +250,10 @@ def show_recommendations_category(self, uuid):
kodiutils.show_listing(listing, 30005, content='tvshows')

def show_mylist(self):
""" Show all the programs of all channels """
try:
mylist, _ = self._auth.get_dataset('myList', 'myList')
except Exception as ex:
kodiutils.notification(message=str(ex))
raise

items = []
if mylist:
for item in mylist:
program = self._api.get_program_by_uuid(item.get('id'))
if program:
program.my_list = True
items.append(program)
""" Show the programs of My List """
mylist = self._api.get_mylist()

listing = [Menu.generate_titleitem(item) for item in items]
listing = [Menu.generate_titleitem(item) for item in mylist]

# Sort items by title
# Used for A-Z listing or when movies and episodes are mixed.
Expand All @@ -280,23 +265,7 @@ def mylist_add(self, uuid):
kodiutils.end_of_directory()
return

mylist, sync_info = self._auth.get_dataset('myList', 'myList')

if not mylist:
mylist = []

if uuid not in [item.get('id') for item in mylist]:
# Python 2.7 doesn't support .timestamp(), and windows doesn't do '%s', so we need to calculate it ourself
epoch = datetime(1970, 1, 1, tzinfo=dateutil.tz.gettz('UTC'))
now = datetime.now(tz=dateutil.tz.gettz('UTC'))
timestamp = int((now - epoch).total_seconds()) * 1000

mylist.append({
'id': uuid,
'timestamp': timestamp,
})

self._auth.put_dataset('myList', 'myList', mylist, sync_info)
self._api.mylist_add(uuid)

kodiutils.end_of_directory()

Expand All @@ -306,12 +275,6 @@ def mylist_del(self, uuid):
kodiutils.end_of_directory()
return

mylist, sync_info = self._auth.get_dataset('myList', 'myList')

if not mylist:
mylist = []

new_mylist = [item for item in mylist if item.get('id') != uuid]
self._auth.put_dataset('myList', 'myList', new_mylist, sync_info)
self._api.mylist_del(uuid)

kodiutils.end_of_directory()
76 changes: 70 additions & 6 deletions resources/lib/viervijfzes/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def get_stream_by_uuid(self, uuid):
:type uuid: str
:rtype str
"""
response = self._get_url(self.API_VIERVIJFZES + '/content/%s' % uuid, authentication=True)
response = self._get_url(self.API_VIERVIJFZES + '/content/%s' % uuid, authentication=self._auth.get_token())
data = json.loads(response)

if not data:
Expand All @@ -366,7 +366,7 @@ def get_stream_by_uuid(self, uuid):
drm_key = data['drmKey']['S']

_LOGGER.debug('Fetching Authentication XML with drm_key %s', drm_key)
response_drm = self._get_url(self.API_GOPLAY + '/video/xml/%s' % drm_key, authentication=True)
response_drm = self._get_url(self.API_GOPLAY + '/video/xml/%s' % drm_key, authentication=self._auth.get_token())
data_drm = json.loads(response_drm)

return ResolvedStream(
Expand Down Expand Up @@ -484,6 +484,33 @@ def get_recommendation_categories(self):

return categories

def get_mylist(self):
""" Get the content of My List
:rtype list[Program]
"""
data = self._get_url(self.API_GOPLAY + '/my-list', authentication='Bearer %s' % self._auth.get_token())
result = json.loads(data)

items = []
for item in result:
try:
program = self.get_program_by_uuid(item.get('programId'))
if program:
program.my_list = True
items.append(program)
except Exception as exc: # pylint: disable=broad-except
_LOGGER.warning(exc)

return items

def mylist_add(self, program_id):
""" Add a program on My List """
self._post_url(self.API_GOPLAY + '/my-list', data={'programId': program_id}, authentication='Bearer %s' % self._auth.get_token())

def mylist_del(self, program_id):
""" Remove a program on My List """
self._delete_url(self.API_GOPLAY + '/my-list-item', params={'programId': program_id}, authentication='Bearer %s' % self._auth.get_token())

@staticmethod
def _extract_programs(html):
""" Extract Programs from HTML code
Expand Down Expand Up @@ -669,16 +696,15 @@ def _parse_episode_data(data, season_uuid=None):
)
return episode

def _get_url(self, url, params=None, authentication=False):
def _get_url(self, url, params=None, authentication=None):
""" Makes a GET request for the specified URL.
:type url: str
:type authentication: str
:rtype str
"""
if authentication:
if not self._auth:
raise Exception('Requested to authenticate, but not auth object passed')
response = self._session.get(url, params=params, headers={
'authorization': self._auth.get_token(),
'authorization': authentication,
})
else:
response = self._session.get(url, params=params)
Expand All @@ -689,6 +715,44 @@ def _get_url(self, url, params=None, authentication=False):

return response.text

def _post_url(self, url, params=None, data=None, authentication=None):
""" Makes a POST request for the specified URL.
:type url: str
:type authentication: str
:rtype str
"""
if authentication:
response = self._session.post(url, params=params, json=data, headers={
'authorization': authentication,
})
else:
response = self._session.post(url, params=params, json=data)

if response.status_code != 200:
_LOGGER.error(response.text)
raise Exception('Could not fetch data')

return response.text

def _delete_url(self, url, params=None, authentication=None):
""" Makes a DELETE request for the specified URL.
:type url: str
:type authentication: str
:rtype str
"""
if authentication:
response = self._session.delete(url, params=params, headers={
'authorization': authentication,
})
else:
response = self._session.delete(url, params=params)

if response.status_code != 200:
_LOGGER.error(response.text)
raise Exception('Could not fetch data')

return response.text

def _handle_cache(self, key, cache_mode, update, ttl=30 * 24 * 60 * 60):
""" Fetch something from the cache, and update if needed """
if cache_mode in [CACHE_AUTO, CACHE_ONLY]:
Expand Down

0 comments on commit 02b2d4d

Please sign in to comment.