From c3a91d23a8055ed39ca336f6dabaaa0c278c2a93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Arnauts?= Date: Mon, 26 Oct 2020 22:01:40 +0100 Subject: [PATCH] Use cached list of all catalog ids when cleaning up the library --- resources/lib/kodiutils.py | 2 +- resources/lib/modules/library.py | 24 +++++-------- resources/lib/streamz/api.py | 58 +++++++++++++++++++++----------- tests/test_api.py | 8 +++++ 4 files changed, 56 insertions(+), 36 deletions(-) diff --git a/resources/lib/kodiutils.py b/resources/lib/kodiutils.py index b82ea33..39e92a0 100644 --- a/resources/lib/kodiutils.py +++ b/resources/lib/kodiutils.py @@ -599,7 +599,7 @@ def get_cache(key, ttl=None): def set_cache(key, data): """ Store an item in the cache :type key: list[str] - :type data: str|None + :type data: any """ fullpath = get_cache_path() + '/' if not xbmcvfs.exists(fullpath): diff --git a/resources/lib/modules/library.py b/resources/lib/modules/library.py index df7ed75..c609403 100644 --- a/resources/lib/modules/library.py +++ b/resources/lib/modules/library.py @@ -114,15 +114,11 @@ def check_library_movie(self, movie): return if kodiutils.get_setting_int('library_movies') == LIBRARY_FULL_CATALOG: - try: - result = self._api.get_movie(movie, cache=CACHE_AUTO) - except UnavailableException: - result = None - kodiutils.library_return_status(result is not None) - + id_list = self._api.get_catalog_ids() else: - mylist_ids = self._api.get_swimlane_ids('my-list') - kodiutils.library_return_status(movie in mylist_ids) + id_list = self._api.get_mylist_ids() + + kodiutils.library_return_status(movie in id_list) def check_library_tvshow(self, program): """ Check if the given program is still available. """ @@ -134,15 +130,11 @@ def check_library_tvshow(self, program): return if kodiutils.get_setting_int('library_tvshows') == LIBRARY_FULL_CATALOG: - try: - result = self._api.get_program(program, cache=CACHE_AUTO) - except UnavailableException: - result = None - kodiutils.library_return_status(result is not None) - + id_list = self._api.get_catalog_ids() else: - mylist_ids = self._api.get_swimlane_ids('my-list') - kodiutils.library_return_status(program in mylist_ids) + id_list = self._api.get_mylist_ids() + + kodiutils.library_return_status(program in id_list) @staticmethod def mylist_added(video_type, content_id): diff --git a/resources/lib/streamz/api.py b/resources/lib/streamz/api.py index 69ff320..0abdad1 100644 --- a/resources/lib/streamz/api.py +++ b/resources/lib/streamz/api.py @@ -104,25 +104,6 @@ def get_swimlane(self, swimlane, content_filter=None, cache=CACHE_ONLY): return items - def get_swimlane_ids(self, swimlane): - """ Returns the IDs of the contents of My List """ - # Try to fetch from cache - result = kodiutils.get_cache(['swimlane', swimlane], 300) # 5 minutes ttl - - if not result: - # Fetch from API - response = util.http_get(API_ENDPOINT + '/%s/main/swimlane/%s' % (self._mode(), swimlane), - token=self._tokens.jwt_token, - profile=self._tokens.profile) - - # Result can be empty - result = json.loads(response.text) if response.text else [] - - kodiutils.set_cache(['swimlane', swimlane], result) - - items = [item.get('target', {}).get('id') for item in result.get('teasers', [])] - return items - def add_mylist(self, video_type, content_id): """ Add an item to My List. """ util.http_put(API_ENDPOINT + '/%s/userData/myList/%s/%s' % (self._mode(), video_type, content_id), @@ -370,6 +351,45 @@ def get_episode(self, episode_id): next_episode=next_episode, ) + def get_mylist_ids(self): + """ Returns the IDs of the contents of My List """ + # Try to fetch from cache + items = kodiutils.get_cache(['mylist_id'], 300) # 5 minutes ttl + if items: + return items + + # Fetch from API + response = util.http_get(API_ENDPOINT + '/%s/main/swimlane/%s' % (self._mode(), 'my-list'), + token=self._tokens.jwt_token, + profile=self._tokens.profile) + + # Result can be empty + result = json.loads(response.text) if response.text else [] + + items = [item.get('target', {}).get('id') for item in result.get('teasers', [])] + + kodiutils.set_cache(['mylist_id'], items) + return items + + def get_catalog_ids(self): + """ Returns the IDs of the contents of the Catalog """ + # Try to fetch from cache + items = kodiutils.get_cache(['catalog_id'], 300) # 5 minutes ttl + if items: + return items + + # Fetch from API + response = util.http_get(API_ENDPOINT + '/%s/catalog' % self._mode(), + params={'pageSize': 2000, 'filter': None}, + token=self._tokens.jwt_token, + profile=self._tokens.profile) + info = json.loads(response.text) + + items = [item.get('target', {}).get('id') for item in info.get('pagedTeasers', {}).get('content', [])] + + kodiutils.set_cache(['catalog_id'], items) + return items + def do_search(self, search): """ Do a search in the full catalog. :type search: str diff --git a/tests/test_api.py b/tests/test_api.py index 33563ad..35c0203 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -56,6 +56,14 @@ def test_search(self): results = self.api.do_search('huis') self.assertIsInstance(results, list) + def test_mylist_ids(self): + mylist = self.api.get_mylist_ids() + self.assertIsInstance(mylist, list) + + def test_catalog_ids(self): + mylist = self.api.get_catalog_ids() + self.assertIsInstance(mylist, list) + def test_errors(self): with self.assertRaises(UnavailableException): self.api.get_movie('0')