--- artwork.py~ 2009-09-22 03:02:16.000000000 +0600 +++ artwork.py 2011-08-26 16:17:53.810656715 +0600 @@ -538,20 +538,35 @@ return False self.downloading_image = True + page = 1 rhapsody_uri = "http://feeds.rhapsody.com" - url = "%s/%s/%s/data.xml" % (rhapsody_uri, artist, album) - url = url.replace(" ", "").replace("'", "").replace("&","") - request = urllib2.Request(url) + lastfm_uri = "http://ws.audioscrobbler.com/2.0/" + key = "" # here should be Last.fm API key + url = "%s/?method=album.search&album=%s&api_key=%s&page=%s" % (lastfm_uri, album.replace(" ", "+").replace("&",""), key, "%s") + request = urllib2.Request(url % page) opener = urllib2.build_opener() try: body = opener.open(request).read() - xml = ElementTree.fromstring(body) - imgs = xml.getiterator("img") + xml = ElementTree.fromstring(body.replace("opensearch:", "opensearch_")) # this is the result of my XML ignorance except: self.downloading_image = False return False - imglist = [im.attrib['src'] for im in imgs if im.attrib['src']] + imglist = [im.find("image[@size='extralarge']").text for im in xml.findall("results/albummatches/album") if im.find("artist").text.lower() == artist.lower()] + + # Because of Last.fm returns results in short bits (30 items) we should go all around the pages + count = int( xml.find("results/opensearch_totalResults").text ) + from math import ceil + while ceil(count/30.0) > page : + page += 1 + request = urllib2.Request(url % page) + opener = urllib2.build_opener() + try: + body = opener.open(request).read() + xml = ElementTree.fromstring(body) + imglist += [im.find("image[@size='extralarge']").text for im in xml.findall("results/albummatches/album") if im.find("artist").text.lower() == artist.lower()] + except: + pass # Couldn't find any images if not imglist: self.downloading_image = False