Source

django-lastfm / lastfm / views.py

Diff from to

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):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.