From: kaliko Date: Thu, 13 Feb 2014 13:02:38 +0000 (+0100) Subject: Fix data structures inconsistency in last.fm WS X-Git-Tag: mpd-sima/0.12.0pr4~25 X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=26407984575aaf451eddfe3395a23779ab2c6640;p=mpd-sima.git Fix data structures inconsistency in last.fm WS --- diff --git a/sima/lib/simafm.py b/sima/lib/simafm.py index 08fdbc3..3457016 100644 --- a/sima/lib/simafm.py +++ b/sima/lib/simafm.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- - # Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Jack Kaliko # # This program is free software: you can redistribute it and/or modify @@ -112,7 +111,8 @@ class SimaFM(): if artist.mbid: payload.update(mbid='{0}'.format(artist.mbid)) else: - payload.update(artist=artist.name) + payload.update(artist=artist.name, + autocorrect=1) payload.update(results=100) if method == 'track': payload.update(track=track) @@ -124,6 +124,15 @@ class SimaFM(): payload = self._forge_payload(artist) # Construct URL self._fetch(payload) + # Artist might be found be return no 'artist' list… + # cf. "Mulatu Astatqe" vs. "Mulatu Astatqé" with autocorrect=0 + # json format is broken IMHO, xml is more consistent IIRC + # Here what we got: + # >>> {"similarartists":{"#text":"\n","artist":"Mulatu Astatqe"}} + # autocorrect=1 should fix it, checking anyway. + simarts = self.current_element.get('similarartists').get('artist') + if not isinstance(simarts, list): + raise WSError('Artist found but no similarities returned') for art in self.current_element.get('similarartists').get('artist'): yield Artist(name=art.get('name'), mbid=art.get('mbid', None))