Views

The AJAX code generate by the template tag (see lastfm.templatetags.lastfm_widget) doesn’t contact Last.fm directly, but uses a Django view as proxy. The advantage of this is, that you can exactly control what data your site gets. Another advantage is, that visitors can’t see your Last.fm username by inspecting the HTML source of your site.

In addition to the view itself this module also defines some helper classes that are responsible for handling the different types of charts (e.g. top tracks or top artists).

lastfm.views.lastfm_data(request, *args, **kwargs)

This view retrievs the data from Last.fm and returns a JSON encoded list. The template tag’s AJAX code will retrieve this list and generate the chart from it.

Each list entry is a dict with three elements:
  • title: Contains the song title or artist name and can be displayed as alternative text or link title.
  • url: A url pointing to the track or artist on Last.fm.
  • img_url: A url pointing to the track’s cover or artist image.

For each chart type there is a class that handles its data. They are necessary to unify Last.fm’s different key names to those three explained above. Currently, there is no abstract base class which they must inherit from, but each class is expected to implement the following attributes and methods:

params: A dict that contains all parameters required by the Last.fm API call

get_data(data): Last.fm’s JSON data is quite nested. This method should extract the list with the actual items from the raw data.

get_item_title(item): Return an item’s text for the title attribute in the data dict.

get_default_image(): Return a url to a default image that will be used if a track or artist has no image on its own.

get_img_url(img_size, item): This method needs to be implemented only if an item has no image key (which is the case for e.g. the weekly top artist). It gets the desired image size (small, medium, large, …) and the item. It might do another API call and extract a custom image URL for that item.

class lastfm.views.RecentTracks

This class handles the API call user.getRecentTracks.

class lastfm.views.WeeklyTopArtists

This class handles the API call user.getWeeklyArtistChart.

class lastfm.views.TopArtists

This class handles the API call user.getTopArtists. The period must be defined in the site’s settings module.

Previous topic

Template Tags

This Page