Skip to content

Commit e274f6a

Browse files
committed
make get_id_list and get_object_list a bit more resilient
works for all api versions now
1 parent 26056d8 commit e274f6a

1 file changed

Lines changed: 65 additions & 21 deletions

File tree

src/ampache.py

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,52 @@ def get_id_list(self, data, attribute: str):
241241
id_list.append(data['id'])
242242
else:
243243
try:
244-
for data_object in data[attribute]:
245-
id_list.append(data_object['id'])
246-
except TypeError:
247-
for data_object in data:
248-
id_list.append(data_object[0])
249-
except KeyError:
250-
id_list.append(data['id'])
244+
if data[0][0][attribute]:
245+
try:
246+
if data[0][0][attribute]['id']:
247+
id_list.append(data[0][0][attribute]['id'])
248+
except (KeyError, TypeError):
249+
for data_object in data[0][0][attribute]:
250+
try:
251+
id_list.append(data_object[0]['id'])
252+
except (KeyError, TypeError):
253+
id_list.append(data_object['id'])
254+
except (KeyError, TypeError):
255+
try:
256+
if data[0][attribute]:
257+
try:
258+
if data[0][attribute]['id']:
259+
id_list.append(data[0][attribute]['id'])
260+
except (KeyError, TypeError):
261+
for data_object in data[0][attribute]:
262+
try:
263+
id_list.append(data_object[0]['id'])
264+
except (KeyError, TypeError):
265+
id_list.append(data_object['id'])
266+
except (KeyError, TypeError):
267+
try:
268+
if data[attribute]:
269+
try:
270+
if data[attribute]['id']:
271+
id_list.append(data[attribute]['id'])
272+
except (KeyError, TypeError):
273+
for data_object in data[attribute]:
274+
try:
275+
id_list.append(data_object[0]['id'])
276+
except (KeyError, TypeError):
277+
id_list.append(data_object['id'])
278+
except (KeyError, TypeError):
279+
try:
280+
try:
281+
id_list.append(data[0]['id'])
282+
except (KeyError, TypeError):
283+
id_list.append(data['id'])
284+
except (KeyError, TypeError):
285+
id_list.append(data)
286+
251287
return id_list
252288

253-
@staticmethod
254-
def get_object_list(data, field: str, data_format: str = 'xml'):
289+
def get_object_list(self, data, field: str):
255290
""" get_id_list
256291
257292
return a list of objects from the data matching your field stirng
@@ -262,15 +297,25 @@ def get_object_list(data, field: str, data_format: str = 'xml'):
262297
* data_format = (string) 'xml','json'
263298
"""
264299
id_list = list()
265-
if data_format == 'xml':
300+
if self.AMPACHE_API == 'xml':
266301
return data.findall(field)
267302
else:
303+
if not data:
304+
return id_list
268305
try:
269-
for data_object in data[field]:
270-
id_list.append(data_object['id'])
271-
except TypeError:
272-
for data_object in data:
273-
id_list.append(data_object[0])
306+
for data_object in data[0][0][field]:
307+
id_list.append(data_object)
308+
except KeyError:
309+
try:
310+
for data_object in data[0][field]:
311+
id_list.append(data_object)
312+
except KeyError:
313+
try:
314+
for data_object in data[field]:
315+
id_list.append(data_object)
316+
except (KeyError, TypeError):
317+
id_list.append(data)
318+
274319
return id_list
275320

276321
@staticmethod
@@ -401,7 +446,7 @@ def fetch_url(self, full_url: str, api_format: str, method: str):
401446
"""
402447

403448
def handshake(self, ampache_url: str, ampache_api: str, ampache_user: str = False,
404-
timestamp: int = 0, version: str = '6.5.1'):
449+
timestamp: int = 0, version: str = '6.6.0'):
405450
""" handshake
406451
MINIMUM_API_VERSION=380001
407452
@@ -456,7 +501,7 @@ def handshake(self, ampache_url: str, ampache_api: str, ampache_user: str = Fals
456501
self.AMPACHE_SESSION = token
457502
return token
458503

459-
def ping(self, ampache_url: str, ampache_api: str = False, version: str = '6.5.1'):
504+
def ping(self, ampache_url: str, ampache_api: str = False, version: str = '6.6.0'):
460505
""" ping
461506
MINIMUM_API_VERSION=380001
462507
@@ -2547,7 +2592,7 @@ def last_shouts(self, username, limit=0):
25472592
return False
25482593
return self.return_data(ampache_response)
25492594

2550-
def player(self, filter_str, object_type='song', state='play', time=0, client='python3-ampache'):
2595+
def player(self, filter_str, object_type='song', state='play', play_time=0, client='python3-ampache'):
25512596
""" player
25522597
MINIMUM_API_VERSION=6.4.0
25532598
@@ -2556,7 +2601,7 @@ def player(self, filter_str, object_type='song', state='play', time=0, client='p
25562601
filter_str = (integer) $object_id
25572602
object_type = (string) $object_type ('song', 'podcast_episode', 'video'), DEFAULT 'song'//optional
25582603
state = (string) 'play', 'stop', DEFAULT 'play' //optional
2559-
time = (integer) current song time in whole seconds, DEFAULT 0 //optional
2604+
play_time = (integer) current song time in whole seconds, DEFAULT 0 //optional
25602605
client = (string) $agent, DEFAULT 'python3-ampache' //optional
25612606
"""
25622607
action = self.player.__name__
@@ -2566,7 +2611,7 @@ def player(self, filter_str, object_type='song', state='play', time=0, client='p
25662611
'filter': filter_str,
25672612
'type': object_type,
25682613
'state': state,
2569-
'time': time,
2614+
'time': play_time,
25702615
'client': client}
25712616
data = urllib.parse.urlencode(data)
25722617
full_url = ampache_url + '?' + data
@@ -3796,7 +3841,6 @@ def advanced_search(self, rules,
37963841
return False
37973842
return self.return_data(ampache_response)
37983843

3799-
38003844
def tags(self, filter_str: str = False,
38013845
exact: int = False, offset=0, limit=0):
38023846
""" tags

0 commit comments

Comments
 (0)