From a80100247ec022bc3cf5e549842053b112d7e1fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Arnauts?= Date: Thu, 11 Feb 2021 18:25:51 +0100 Subject: [PATCH] Fix error when requesting a My List that has not been created yet. (#73) --- resources/lib/modules/catalog.py | 30 ++++++++++++------- resources/lib/viervijfzes/auth.py | 8 ++--- resources/lib/viervijfzes/aws/cognito_sync.py | 20 +++++++++---- tests/test_mylist.py | 4 +-- 4 files changed, 39 insertions(+), 23 deletions(-) diff --git a/resources/lib/modules/catalog.py b/resources/lib/modules/catalog.py index f1c2da0..91fd0b5 100644 --- a/resources/lib/modules/catalog.py +++ b/resources/lib/modules/catalog.py @@ -181,17 +181,18 @@ class Catalog: def show_mylist(self): """ Show all the programs of all channels """ try: - mylist, _ = self._auth.get_dataset('myList') + mylist, _ = self._auth.get_dataset('myList', 'myList') except Exception as ex: kodiutils.notification(message=str(ex)) raise items = [] - for item in mylist: - program = self._api.get_program_by_uuid(item.get('id')) - if program: - program.my_list = True - items.append(program) + 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) listing = [Menu.generate_titleitem(item) for item in items] @@ -205,20 +206,23 @@ class Catalog: kodiutils.end_of_directory() return - mylist, sync_info = self._auth.get_dataset('myList') + 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 = str(int((now - epoch).total_seconds())) + '000' + timestamp = int((now - epoch).total_seconds()) * 1000 mylist.append({ 'id': uuid, 'timestamp': timestamp, }) - self._auth.put_dataset('myList', mylist, sync_info) + self._auth.put_dataset('myList', 'myList', mylist, sync_info) kodiutils.end_of_directory() @@ -228,8 +232,12 @@ class Catalog: kodiutils.end_of_directory() return - mylist, sync_info = self._auth.get_dataset('myList') + 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', new_mylist, sync_info) + self._auth.put_dataset('myList', 'myList', new_mylist, sync_info) kodiutils.end_of_directory() diff --git a/resources/lib/viervijfzes/auth.py b/resources/lib/viervijfzes/auth.py index 70a92ed..04dfa46 100644 --- a/resources/lib/viervijfzes/auth.py +++ b/resources/lib/viervijfzes/auth.py @@ -105,7 +105,7 @@ class AuthApi: idp_client = CognitoIdp(AuthApi.COGNITO_POOL_ID, AuthApi.COGNITO_CLIENT_ID) return idp_client.renew_token(refresh_token) - def get_dataset(self, dataset): + def get_dataset(self, dataset, key): """ Fetch the value from the specified dataset. """ identity_client = CognitoIdentity(AuthApi.COGNITO_POOL_ID, AuthApi.COGNITO_IDENTITY_POOL_ID) id_token = self.get_token() @@ -113,7 +113,7 @@ class AuthApi: credentials = identity_client.get_credentials_for_identity(id_token, identity_id) sync_client = CognitoSync(AuthApi.COGNITO_IDENTITY_POOL_ID, identity_id, credentials) - data, session_token, sync_count = sync_client.list_records(dataset) + data, session_token, sync_count = sync_client.list_records(dataset, key) sync_info = { 'identity_id': identity_id, @@ -125,7 +125,7 @@ class AuthApi: return data, sync_info @staticmethod - def put_dataset(dataset, value, sync_info): + def put_dataset(dataset, key, value, sync_info): """ Store the value from the specified dataset. """ sync_client = CognitoSync(AuthApi.COGNITO_IDENTITY_POOL_ID, sync_info.get('identity_id'), sync_info.get('credentials')) - sync_client.update_records(dataset, value, sync_info.get('session_token'), sync_info.get('sync_count')) + sync_client.update_records(dataset, key, value, sync_info.get('session_token'), sync_info.get('sync_count')) diff --git a/resources/lib/viervijfzes/aws/cognito_sync.py b/resources/lib/viervijfzes/aws/cognito_sync.py index fadd6fd..1d2ed6f 100644 --- a/resources/lib/viervijfzes/aws/cognito_sync.py +++ b/resources/lib/viervijfzes/aws/cognito_sync.py @@ -112,10 +112,11 @@ class CognitoSync: 'Authorization': authorization_header }) - def list_records(self, dataset): + def list_records(self, dataset, key): """ Return the values of this dataset. :param str dataset: The name of the dataset to request. + :param str key: The name of the key to request. :return The requested dataset :rtype: dict """ @@ -142,13 +143,20 @@ class CognitoSync: reply.raise_for_status() result = json.loads(reply.text) + _LOGGER.debug("Got results: %s", result.get('Records')) + # Return the records - record = next(record for record in result.get('Records', []) if record.get('Key') == dataset) - value = json.loads(record.get('Value')) + try: + record = next(record for record in result.get('Records', []) if record.get('Key') == key) + value = json.loads(record.get('Value')) + sync_count = record.get('SyncCount') + except StopIteration: + value = None + sync_count = 0 - return value, result.get('SyncSessionToken'), record.get('SyncCount') + return value, result.get('SyncSessionToken'), sync_count - def update_records(self, dataset, value, session_token, sync_count): + def update_records(self, dataset, key, value, session_token, sync_count): """ Return the values of this dataset. :param str dataset: The name of the dataset to request. @@ -171,7 +179,7 @@ class CognitoSync: "SyncSessionToken": session_token, "RecordPatches": [ { - "Key": dataset, + "Key": key, "Op": "replace", "SyncCount": sync_count, "Value": json.dumps(value), diff --git a/tests/test_mylist.py b/tests/test_mylist.py index d5f891e..1a5c9cb 100644 --- a/tests/test_mylist.py +++ b/tests/test_mylist.py @@ -24,7 +24,7 @@ class TestMyList(unittest.TestCase): id_token = auth.get_token() self.assertTrue(id_token) - dataset, _ = auth.get_dataset('myList') + dataset, _ = auth.get_dataset('myList', 'myList') self.assertTrue(dataset) # Test disabled since it would cause locks due to all the CI tests changing this at the same time. @@ -38,7 +38,7 @@ class TestMyList(unittest.TestCase): # {'id': 'da584be3-dea6-49c7-bfbd-c480d8096937', 'timestamp': timestamp} # ] # - # auth.put_dataset('myList', new_dataset, sync_info) + # auth.put_dataset('myList', 'myList', new_dataset, sync_info) if __name__ == '__main__':