Skip to content

Commit

Permalink
make get_id_list and get_object_list a bit more resilient
Browse files Browse the repository at this point in the history
works for all api versions now
  • Loading branch information
lachlan-00 committed Jul 22, 2024
1 parent 26056d8 commit e274f6a
Showing 1 changed file with 65 additions and 21 deletions.
86 changes: 65 additions & 21 deletions src/ampache.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,17 +241,52 @@ def get_id_list(self, data, attribute: str):
id_list.append(data['id'])
else:
try:
for data_object in data[attribute]:
id_list.append(data_object['id'])
except TypeError:
for data_object in data:
id_list.append(data_object[0])
except KeyError:
id_list.append(data['id'])
if data[0][0][attribute]:
try:
if data[0][0][attribute]['id']:
id_list.append(data[0][0][attribute]['id'])
except (KeyError, TypeError):
for data_object in data[0][0][attribute]:
try:
id_list.append(data_object[0]['id'])
except (KeyError, TypeError):
id_list.append(data_object['id'])
except (KeyError, TypeError):
try:
if data[0][attribute]:
try:
if data[0][attribute]['id']:
id_list.append(data[0][attribute]['id'])
except (KeyError, TypeError):
for data_object in data[0][attribute]:
try:
id_list.append(data_object[0]['id'])
except (KeyError, TypeError):
id_list.append(data_object['id'])
except (KeyError, TypeError):
try:
if data[attribute]:
try:
if data[attribute]['id']:
id_list.append(data[attribute]['id'])
except (KeyError, TypeError):
for data_object in data[attribute]:
try:
id_list.append(data_object[0]['id'])
except (KeyError, TypeError):
id_list.append(data_object['id'])
except (KeyError, TypeError):
try:
try:
id_list.append(data[0]['id'])
except (KeyError, TypeError):
id_list.append(data['id'])
except (KeyError, TypeError):
id_list.append(data)

return id_list

@staticmethod
def get_object_list(data, field: str, data_format: str = 'xml'):
def get_object_list(self, data, field: str):
""" get_id_list
return a list of objects from the data matching your field stirng
Expand All @@ -262,15 +297,25 @@ def get_object_list(data, field: str, data_format: str = 'xml'):
* data_format = (string) 'xml','json'
"""
id_list = list()
if data_format == 'xml':
if self.AMPACHE_API == 'xml':
return data.findall(field)
else:
if not data:
return id_list
try:
for data_object in data[field]:
id_list.append(data_object['id'])
except TypeError:
for data_object in data:
id_list.append(data_object[0])
for data_object in data[0][0][field]:
id_list.append(data_object)
except KeyError:
try:
for data_object in data[0][field]:
id_list.append(data_object)
except KeyError:
try:
for data_object in data[field]:
id_list.append(data_object)
except (KeyError, TypeError):
id_list.append(data)

return id_list

@staticmethod
Expand Down Expand Up @@ -401,7 +446,7 @@ def fetch_url(self, full_url: str, api_format: str, method: str):
"""

def handshake(self, ampache_url: str, ampache_api: str, ampache_user: str = False,
timestamp: int = 0, version: str = '6.5.1'):
timestamp: int = 0, version: str = '6.6.0'):
""" handshake
MINIMUM_API_VERSION=380001
Expand Down Expand Up @@ -456,7 +501,7 @@ def handshake(self, ampache_url: str, ampache_api: str, ampache_user: str = Fals
self.AMPACHE_SESSION = token
return token

def ping(self, ampache_url: str, ampache_api: str = False, version: str = '6.5.1'):
def ping(self, ampache_url: str, ampache_api: str = False, version: str = '6.6.0'):
""" ping
MINIMUM_API_VERSION=380001
Expand Down Expand Up @@ -2547,7 +2592,7 @@ def last_shouts(self, username, limit=0):
return False
return self.return_data(ampache_response)

def player(self, filter_str, object_type='song', state='play', time=0, client='python3-ampache'):
def player(self, filter_str, object_type='song', state='play', play_time=0, client='python3-ampache'):
""" player
MINIMUM_API_VERSION=6.4.0
Expand All @@ -2556,7 +2601,7 @@ def player(self, filter_str, object_type='song', state='play', time=0, client='p
filter_str = (integer) $object_id
object_type = (string) $object_type ('song', 'podcast_episode', 'video'), DEFAULT 'song'//optional
state = (string) 'play', 'stop', DEFAULT 'play' //optional
time = (integer) current song time in whole seconds, DEFAULT 0 //optional
play_time = (integer) current song time in whole seconds, DEFAULT 0 //optional
client = (string) $agent, DEFAULT 'python3-ampache' //optional
"""
action = self.player.__name__
Expand All @@ -2566,7 +2611,7 @@ def player(self, filter_str, object_type='song', state='play', time=0, client='p
'filter': filter_str,
'type': object_type,
'state': state,
'time': time,
'time': play_time,
'client': client}
data = urllib.parse.urlencode(data)
full_url = ampache_url + '?' + data
Expand Down Expand Up @@ -3796,7 +3841,6 @@ def advanced_search(self, rules,
return False
return self.return_data(ampache_response)


def tags(self, filter_str: str = False,
exact: int = False, offset=0, limit=0):
""" tags
Expand Down

0 comments on commit e274f6a

Please sign in to comment.