From 860a2d0aded9a2c4ed51ce8d779eb3a3404b68bb Mon Sep 17 00:00:00 2001 From: rocus Date: Fri, 19 Jun 2015 15:31:55 +0200 Subject: [PATCH 1/2] When "album" tag not set and "artist" tag is set, a picture of the artist is loaded by "covers_lastfm". --- sonata/plugins/covers_lastfm.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/sonata/plugins/covers_lastfm.py b/sonata/plugins/covers_lastfm.py index ebb668c0..d94ad721 100644 --- a/sonata/plugins/covers_lastfm.py +++ b/sonata/plugins/covers_lastfm.py @@ -35,14 +35,17 @@ def on_cover_fetch(artist, album, on_save_cb, on_err_cb): opener.addheaders = [("User-Agent", make_user_agent())] # First, find the link to the master release of this album - search_url = "http://ws.audioscrobbler.com/2.0/?%s" % ( - urllib.parse.urlencode({ - "method": "album.getInfo", - "artist": artist, - "album": album, - "api_key": API_KEY, - "format": "json", - })) + album_not_given = album == "" or album == None + if album_not_given: + search_url = "http://ws.audioscrobbler.com/2.0/?%s" % ( urllib.parse.urlencode({ "method": "artist.getInfo", "artist": + artist,\ + "api_key": API_KEY, "format": "json" +})) + else: + search_url = "http://ws.audioscrobbler.com/2.0/?%s" % ( urllib.parse.urlencode({ "method": "album.getInfo", "artist": +artist, "album" : album ,\ + "api_key": API_KEY, "format": "json" +})) logger.debug("Querying %r...", search_url) response = opener.open(search_url) @@ -53,7 +56,11 @@ def on_cover_fetch(artist, album, on_save_cb, on_err_cb): lastfm['message'], lastfm['error']) return - for image in lastfm['album']['image']: + if album_not_given: + i = lastfm['artist'] + else: + i = lastfm['album'] + for image in i['image']: if image['size'] != 'mega': continue From d863ca0093477b7c265d81856174dae0f2d3acd2 Mon Sep 17 00:00:00 2001 From: rocus Date: Thu, 25 Jun 2015 12:58:21 +0200 Subject: [PATCH 2/2] Cleanup of code. --- sonata/plugins/covers_lastfm.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/sonata/plugins/covers_lastfm.py b/sonata/plugins/covers_lastfm.py index d94ad721..68af1867 100644 --- a/sonata/plugins/covers_lastfm.py +++ b/sonata/plugins/covers_lastfm.py @@ -35,18 +35,13 @@ def on_cover_fetch(artist, album, on_save_cb, on_err_cb): opener.addheaders = [("User-Agent", make_user_agent())] # First, find the link to the master release of this album - album_not_given = album == "" or album == None - if album_not_given: - search_url = "http://ws.audioscrobbler.com/2.0/?%s" % ( urllib.parse.urlencode({ "method": "artist.getInfo", "artist": - artist,\ - "api_key": API_KEY, "format": "json" -})) + i= { "artist": artist, "api_key": API_KEY, "format": "json" } + if not album: + i.update({"method": "artist.getInfo" }) else: - search_url = "http://ws.audioscrobbler.com/2.0/?%s" % ( urllib.parse.urlencode({ "method": "album.getInfo", "artist": -artist, "album" : album ,\ - "api_key": API_KEY, "format": "json" -})) - + i.update({"method": "album.getInfo", "album" : album }) + + search_url = "http://ws.audioscrobbler.com/2.0/?%s" % (urllib.parse.urlencode(i)) logger.debug("Querying %r...", search_url) response = opener.open(search_url) lastfm = json.loads(response.read().decode('utf-8')) @@ -56,7 +51,7 @@ def on_cover_fetch(artist, album, on_save_cb, on_err_cb): lastfm['message'], lastfm['error']) return - if album_not_given: + if not album: i = lastfm['artist'] else: i = lastfm['album']