Source

django-lastfm / lastfm / views.py

Diff from to

File lastfm/views.py

 # encoding: utf-8
-
 """
 The AJAX code generate by the template tag (see
 :mod:`lastfm.templatetags.lastfm_widget`) doesn’t contact Last.fm directly,
 tracks or top artists).
 
 """
-
 import urllib
 
 from django.conf import settings
 LASTFM_IMG_SIZE = getattr(settings, 'LASTFM_IMG_SIZE', 'large')
 
 
-# TODO: move caching to url-conf
 @cache_page(60 * 5)
 def lastfm_data(request):
     """
             raise IOError('No artists found.')
         elif type(data) == dict:
             data = [data]
-    except IOError:
+    except (IOError, ValueError):
         return HttpResponse(json.dumps([]), mimetype='application/json')
 
     items = []
         try:
             img_data = json.loads(urllib.urlopen(
                     URL + params).read())['images']
-        except IOError:
+        except (IOError, ValueError):
             return ''
+
+        fallback = ''
         if 'image' in img_data:
             img_data = img_data['image']['sizes']['size']
             for img in img_data:
+                fallback = img['#text']
                 if img['name'] == img_size and img['#text']:
                     return img['#text']
+        return fallback
 
 
 class TopArtists(object):