New function: getImageLinks(ArtistID/AlbumID). Just queries last fm to grab the artwork/thumbnail links without saving them to the cache. Useful for search results, etc

This commit is contained in:
rembo10
2012-06-25 18:47:21 +05:30
parent 52dd47db4c
commit 77a887c09b
2 changed files with 99 additions and 0 deletions

View File

@@ -194,6 +194,88 @@ class Cache(object):
else:
info_dict = { 'Summary' : db_info['Summary'], 'Content' : db_info['Content'] }
return info_dict
def get_image_links(self, ArtistID=None, AlbumID=None):
'''
Here we're just going to open up the last.fm url, grab the image links and return them
Won't save any image urls, or save the artwork in the cache. Useful for search results, etc.
'''
if ArtistID:
self.id_type = 'artist'
params = { "method": "artist.getInfo",
"api_key": lastfm_apikey,
"mbid": ArtistID,
"format": "json"
}
url = "http://ws.audioscrobbler.com/2.0/?" + urllib.urlencode(params)
logger.debug('Retrieving artist information from: ' + url)
try:
result = urllib2.urlopen(url, timeout=20).read()
except:
logger.warn('Could not open url: ' + url)
return
if result:
try:
data = simplejson.JSONDecoder().decode(result)
except:
logger.warn('Could not parse data from url: ' + url)
return
try:
image_url = data['artist']['image'][-1]['#text']
except KeyError:
logger.debug('No artist image found on url: ' + url)
image_url = None
thumb_url = self._get_thumb_url(data)
if not thumb_url:
logger.debug('No artist thumbnail image found on url: ' + url)
else:
self.id_type = 'album'
params = { "method": "album.getInfo",
"api_key": lastfm_apikey,
"mbid": AlbumID,
"format": "json"
}
url = "http://ws.audioscrobbler.com/2.0/?" + urllib.urlencode(params)
logger.debug('Retrieving album information from: ' + url)
try:
result = urllib2.urlopen(url, timeout=20).read()
except:
logger.warn('Could not open url: ' + url)
return
if result:
try:
data = simplejson.JSONDecoder().decode(result)
except:
logger.warn('Could not parse data from url: ' + url)
return
try:
image_url = data['artist']['image'][-1]['#text']
except KeyError:
logger.debug('No artist image found on url: ' + url)
image_url = None
thumb_url = self._get_thumb_url(data)
if not thumb_url:
logger.debug('No artist thumbnail image found on url: ' + url)
image_dict = {'artwork' : image_url, 'thumbnail' : thumb_url }
return image_dict
def _update_cache(self):
'''
@@ -429,3 +511,10 @@ def getInfo(ArtistID=None, AlbumID=None):
info_dict = c.get_info_from_cache(ArtistID, AlbumID)
return info_dict
def getImageLinks(ArtistID=None, AlbumID=None):
c = Cache()
image_links = c.get_image_links(ArtistID, AlbumID)
return image_links

View File

@@ -616,3 +616,13 @@ class WebInterface(object):
return cache.getThumb(ArtistID, AlbumID)
getThumb.exposed = True
# If you just want to get the last.fm image links for an album, make sure to pass a releaseid and not a releasegroupid
def getImageLinks(self, ArtistID=None, AlbumID=None):
from headphones import cache
image_dict = cache.getImageLinks(ArtistID, AlbumID)
return simplejson.dumps(image_dict)
getImageLinks.exposed = True