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).
This view retrievs the data from Last.fm and returns a JSON encoded list. The template tag’s AJAX code will retrive this list and generate the chart from it.
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.