Fix error when requesting a My List that has not been created yet. (#73)
This commit is contained in:
parent
d8a9ab4e10
commit
a80100247e
@ -181,12 +181,13 @@ 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 = []
|
||||
if mylist:
|
||||
for item in mylist:
|
||||
program = self._api.get_program_by_uuid(item.get('id'))
|
||||
if program:
|
||||
@ -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()
|
||||
|
@ -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'))
|
||||
|
@ -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)
|
||||
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),
|
||||
|
@ -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__':
|
||||
|
Loading…
Reference in New Issue
Block a user