Fun with the Last.fm API, python and Wordle
Monday, May 11th, 2009I’ve been looking for a reason to hack around with python. Recently, my friend @smbrown published a post on his The Monkey Represents Sharing blog entitled Wordle visualization of my Tweetstream where he used a python script to archive his Twitter stream which he then used to create a Wordle, or a visual representation of the frequency of terms used in a collection of words. Words that you use most often are relatively larger than words used less often. Tag Clouds such as these have been around for quite some time, but what sets Wordle apart is its ability to arrange words both vertically and horizontally and even nest small words inside the actually letters of larger words. This got me thinking… “I wonder if I could do the same with data I have scrobbled to my last.fm account”
I started by reading up on the Last.FM API and getting myself an API key so that I could access the data held on Last.FM’s servers. I targeted the user.getTopArtists API call because I thought that would make for an interesting Wordle. Looking over the script used to archive your tweetstream I noticed a python library called Beautiful Soup was used to parse the XML returned from Twitter’s servers. I used Beautiful Soup to parse the top 50 artists from my Last.FM account and their respective playcounts returned from the API call. I looped over that data and exported it to a text file which I then Wordled. The results are below:
Click the image to enlarge
Here is the python code:
Notes
I wasn’t sure how Wordle would handle phrases. At first, I noticed that Artist Names like The Flaming Lips were broken up when Wordled. I tried wrapping the artist’s names in quotes but Wordle removed the quotes. I also tried joining the individual words of the phrases with + (plus) signs. This approach kept the words together, but Wordle rendered the plus signs. I eventually found that non-breaking spaces (%nbsp;) would work.
Here is another Wordle I created by making an API call for user.getWeeklyArtistChart which shows who I have listened to so far this week:
Click the image to enlarge
Wordles
Here is a sample of all of my Wordles to date#.

Thanks to my brother Jim, I have acquired a new (to me)
This year has been marked by a great return of interest in some long time and long lost hobbies of mine: Mountain Biking and Backpacking/Hiking. Having spent a majority of the last ten years of my life glued to a computer screen, I started to become repulsed by all things technology (exaggeration) and found myself craving the great outdoors like I did during my carefree college days. Working from home and living in an area loaded with state parks and recreation areas allowed me to get out into the woods at least a few times a week. 