Integrating last.fm with movable type

If you’re not looking at this in bloglines you may have noticed that I just added a "Favourite artists last week" section to the home page side navigation.  This small little side nav looks pretty simple, but the automation and integration behind it is actually quite massive.

It starts with itunes and a plugin called iscrobbler. iscrobbler uploads the the information about a song everytime I listen to one using itunes (as long as the network is connected). This goes to the backend server on Audioscrobbler which stores all the song data and who played it. Audioscrobbler then has a frontend on the last.fm website which gives you a pretty interface to the data. Last.fm is a social networking website like flickr or del.icio.us in the way that you can tag music, recommend it to others and join groups. It also gives you some cool little charts.

This is all great, but it didn’t really provide a way for you to share that information outside the last.fm network. The reason that a lot of websites like flickr exploded so quickly is because they offer tools to integrate their website with your website. Audioscrobbler has realised this and offered some web services that are a great base for developers to build on. Some of the reports they offer are a weekly chart list, top tracks and a friends list. I chose to use an XML file to list my favourite artists from the week before. As I’d never queried an XML feed in movable type before, I was surprised to discover how easy it was.

  1. Download GetXML from staggernation and follow it’s installation instructions
  2. You may need to install the XML::Simple perl module if it doesn’t exist in your movable type/extlib/xml folder. I downloaded the XMLModules.zip file from Adam Kalsey’s site to get it.
  3. Find the XML feed you want to use on the Audioscrobbler web services page. Make sure you pick the XML feed and change the link to have your username in the address (i.e. replace /user/RJ/ with /user/YOURUSER)
  4. Create your code using the examples given on the staggernation website and then stick it in the movable template page you want use.
  5. Save, rebuild and admire your music (or become the laughing stock to your friends when they see you’ve been listening to Hi-Five)

This is the code I used for my list, feel free to reuse or hack it for your own means:

<ol>
<MTGetXML location="http://ws.audioscrobbler.com/1.0/user/viduka_kewell/weeklyartistchart.xml">
 <MTGetXMLElement name="artist" limit="10">
  <li><a href="<$MTGetXMLValue name="url"$>"><$MTGetXMLValue name="name"$> </a>
 played <$MTGetXMLValue name="playcount"$> times</li>
 </MTGetXMLElement>
</MTGetXML>
</ol>

Edit: Some questions were asked about my "listening to now" list which is done in a different method to that stated above. I use magpie rss to do the list whenever some requests the home page, and this is the code I use to display it, don’t forget to change it to your rss feed:

 

<?php

require_once "magpierss/rss_fetch.inc";
// change to reflect path of rss_fetch.inc

$yummy = fetch_rss("http://ws.audioscrobbler.com/rdf/history/viduka_kewell");

$maxitems = 10;

$yummyitems = array_slice($yummy->items, 0, $maxitems);

foreach ($yummyitems as $yummyitem) {
print ‘<li>’;
print ‘<a href="’;
print $yummyitem[‘link’];
print ‘">’;
print $yummyitem[‘description’];
print ‘</a>’;
print ‘</li>’;
print "n";

}

?>

Comments

comments

10 thoughts on “Integrating last.fm with movable type

  1. Tom Boucher

    well, I got really excited at first because this looked awesome. Unfortunately, I apparently need a lot more than the XML::Simple perl module because it threw all sorts of errors about other modules not found, and I kept finding them and adding them until it needed two different types of the same name and I figured I was fighting a loosing battle. I guess my hosting provider doesn’t load a lot of perl modules

  2. amyo

    Yeah sometimes installing perl modules can be as painful as installing an rpm in linux. I went back and looked at the staggernation site again and noticed that he does list all the required perl modules. The links provided offer easy downloads for all of them except LWP::Simple which is part of the Bundle::LWP. I think installing Bundle::LWP would solve the majority of the requirements but I guess that depends on each web host’s Perl installation.

    I fully empathise with your issues.

  3. Tom Boucher

    Voila! After I got my hosting provider to install XML::Parser it works! Now I just need to find a good resource on how to design the styles better because it’s centering for some reason on my site. Thanks again for posting this and highlighting it over on Last.FM.

  4. Saima

    Hi after months looking I finally found this very neat way of integration. Thanks!

    I’ve tried using it for recenttracks.xml however and whenever I try to put the artist in there I get “XML element ‘artist’ contains multiple values”…any ideas why?

  5. pete

    I’ve tried this in movable type and it only refreshes the music list when i rebuild the index. do you have a job set up to rebuild the index every X # of minutes?

  6. amyo

    I don’t have a cron job setup for my queries, but I do handle the what I’m listening to right now list differently to the what’s played each week total list. The total list is done using the MTGetXML method I mentioned in this entry, but the right now list uses magpie rss to query whenever someone asks for the home page (php has to be enabled of course). I had some trouble adding the code to the comments, so it’s now up in the body of the post. Hope this helps!

  7. jessamyyn

    Hi Saima, I know this is sort of late, but I figured out how to end run the “multiple values” problem. I’m new to all of this myself, but it looks like there is special syntax to use if you have a tag with a tex value and other attributes (as artist has the mbd information) the code I used is this

  8. “> by

    this returns five hyperlinked song titles, and then says “by Stephen Malkmus” or whoever the artist is after each song title. So, within the element you also have to grab out the content. Hope this makes sense, it only sort of does to me.

Leave a Reply