Skip to content

Commit

Permalink
Use cached list of all catalog ids when cleaning up the library
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelarnauts committed Oct 26, 2020
1 parent 47878d7 commit c3a91d2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 36 deletions.
2 changes: 1 addition & 1 deletion resources/lib/kodiutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
24 changes: 8 additions & 16 deletions resources/lib/modules/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. """
Expand All @@ -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):
Expand Down
58 changes: 39 additions & 19 deletions resources/lib/streamz/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit c3a91d2

Please sign in to comment.