From: kaliko Date: Sat, 7 Dec 2013 10:07:35 +0000 (+0100) Subject: Add lastfm similartracks request X-Git-Tag: mpd-sima/0.12.0pr2~17 X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=9a37ebf816f88bbf94f45afa4dcb8a40b8b5c369;p=mpd-sima.git Add lastfm similartracks request --- diff --git a/sima/lib/simafm.py b/sima/lib/simafm.py index 7918403..12ca0d5 100644 --- a/sima/lib/simafm.py +++ b/sima/lib/simafm.py @@ -276,6 +276,20 @@ class SimaFM(): for track in elem.getiterator(tag='track'): yield str(track.findtext('name')), int(track.attrib.get('rank')) + def get_similartracks(self, track=None, artist=None): + """ + """ + # Construct URL + url = SimaFM.root_url + SimaFM.request.get('track') + self._url = url % (urllib.parse.quote(artist.encode('UTF-8'), safe=''), + urllib.parse.quote(track.encode('UTF-8'), safe='')) + self._fetch() + elem = self.current_element + for trk in elem.getiterator(tag='track'): + yield (str(trk.findtext('artist/name')), + str(trk.findtext('name')), + 100 * float(trk.findtext('match'))) + def get_mbid(self, artist=None): """ """ @@ -291,8 +305,8 @@ class SimaFM(): def run(): test = SimaFM() - for a, m in test.get_similar(artist='Tool'): - pass + for t, a, m in test.get_similartracks(artist='Nirvana', track='Smells Like Teen Spirit'): + print(a, t, m) return if __name__ == '__main__':