From 77a887c09b896cba662bfc3c654883a5dc750767 Mon Sep 17 00:00:00 2001 From: rembo10 Date: Mon, 25 Jun 2012 18:47:21 +0530 Subject: [PATCH] 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 --- headphones/cache.py | 89 ++++++++++++++++++++++++++++++++++++++++++ headphones/webserve.py | 10 +++++ 2 files changed, 99 insertions(+) diff --git a/headphones/cache.py b/headphones/cache.py index fa3e1083..32ca9d62 100644 --- a/headphones/cache.py +++ b/headphones/cache.py @@ -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 diff --git a/headphones/webserve.py b/headphones/webserve.py index 4fabb3fd..e2589ba1 100644 --- a/headphones/webserve.py +++ b/headphones/webserve.py @@ -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