Fix error when requesting a My List that has not been created yet. (#73)

This commit is contained in:
Michaël Arnauts 2021-02-11 18:25:51 +01:00 committed by GitHub
parent d8a9ab4e10
commit a80100247e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 23 deletions

View File

@ -181,12 +181,13 @@ class Catalog:
def show_mylist(self): def show_mylist(self):
""" Show all the programs of all channels """ """ Show all the programs of all channels """
try: try:
mylist, _ = self._auth.get_dataset('myList') mylist, _ = self._auth.get_dataset('myList', 'myList')
except Exception as ex: except Exception as ex:
kodiutils.notification(message=str(ex)) kodiutils.notification(message=str(ex))
raise raise
items = [] items = []
if mylist:
for item in mylist: for item in mylist:
program = self._api.get_program_by_uuid(item.get('id')) program = self._api.get_program_by_uuid(item.get('id'))
if program: if program:
@ -205,20 +206,23 @@ class Catalog:
kodiutils.end_of_directory() kodiutils.end_of_directory()
return 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]: 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 # 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')) epoch = datetime(1970, 1, 1, tzinfo=dateutil.tz.gettz('UTC'))
now = datetime.now(tz=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({ mylist.append({
'id': uuid, 'id': uuid,
'timestamp': timestamp, 'timestamp': timestamp,
}) })
self._auth.put_dataset('myList', mylist, sync_info) self._auth.put_dataset('myList', 'myList', mylist, sync_info)
kodiutils.end_of_directory() kodiutils.end_of_directory()
@ -228,8 +232,12 @@ class Catalog:
kodiutils.end_of_directory() kodiutils.end_of_directory()
return 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] 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() kodiutils.end_of_directory()

View File

@ -105,7 +105,7 @@ class AuthApi:
idp_client = CognitoIdp(AuthApi.COGNITO_POOL_ID, AuthApi.COGNITO_CLIENT_ID) idp_client = CognitoIdp(AuthApi.COGNITO_POOL_ID, AuthApi.COGNITO_CLIENT_ID)
return idp_client.renew_token(refresh_token) 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. """ """ Fetch the value from the specified dataset. """
identity_client = CognitoIdentity(AuthApi.COGNITO_POOL_ID, AuthApi.COGNITO_IDENTITY_POOL_ID) identity_client = CognitoIdentity(AuthApi.COGNITO_POOL_ID, AuthApi.COGNITO_IDENTITY_POOL_ID)
id_token = self.get_token() id_token = self.get_token()
@ -113,7 +113,7 @@ class AuthApi:
credentials = identity_client.get_credentials_for_identity(id_token, identity_id) credentials = identity_client.get_credentials_for_identity(id_token, identity_id)
sync_client = CognitoSync(AuthApi.COGNITO_IDENTITY_POOL_ID, identity_id, credentials) 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 = { sync_info = {
'identity_id': identity_id, 'identity_id': identity_id,
@ -125,7 +125,7 @@ class AuthApi:
return data, sync_info return data, sync_info
@staticmethod @staticmethod
def put_dataset(dataset, value, sync_info): def put_dataset(dataset, key, value, sync_info):
""" Store the value from the specified dataset. """ """ 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 = 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'))

View File

@ -112,10 +112,11 @@ class CognitoSync:
'Authorization': authorization_header 'Authorization': authorization_header
}) })
def list_records(self, dataset): def list_records(self, dataset, key):
""" Return the values of this dataset. """ Return the values of this dataset.
:param str dataset: The name of the dataset to request. :param str dataset: The name of the dataset to request.
:param str key: The name of the key to request.
:return The requested dataset :return The requested dataset
:rtype: dict :rtype: dict
""" """
@ -142,13 +143,20 @@ class CognitoSync:
reply.raise_for_status() reply.raise_for_status()
result = json.loads(reply.text) result = json.loads(reply.text)
_LOGGER.debug("Got results: %s", result.get('Records'))
# Return the records # Return the records
record = next(record for record in result.get('Records', []) if record.get('Key') == dataset) try:
record = next(record for record in result.get('Records', []) if record.get('Key') == key)
value = json.loads(record.get('Value')) 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. """ Return the values of this dataset.
:param str dataset: The name of the dataset to request. :param str dataset: The name of the dataset to request.
@ -171,7 +179,7 @@ class CognitoSync:
"SyncSessionToken": session_token, "SyncSessionToken": session_token,
"RecordPatches": [ "RecordPatches": [
{ {
"Key": dataset, "Key": key,
"Op": "replace", "Op": "replace",
"SyncCount": sync_count, "SyncCount": sync_count,
"Value": json.dumps(value), "Value": json.dumps(value),

View File

@ -24,7 +24,7 @@ class TestMyList(unittest.TestCase):
id_token = auth.get_token() id_token = auth.get_token()
self.assertTrue(id_token) self.assertTrue(id_token)
dataset, _ = auth.get_dataset('myList') dataset, _ = auth.get_dataset('myList', 'myList')
self.assertTrue(dataset) self.assertTrue(dataset)
# Test disabled since it would cause locks due to all the CI tests changing this at the same time. # 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} # {'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__': if __name__ == '__main__':