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 says:

    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

    Reply
  2. amyo says:

    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.

    Reply
  3. Tom Boucher says:

    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.

    Reply
  4. amyo says:

    Excellent news! Regarding your styles issues, I find w3schools as an invaluable resource for all things html/css related.

    Reply
  5. Saima says:

    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?

    Reply
  6. pete says:

    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?

    Reply
  7. amyo says:

    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!

    Reply
  8. jessamyyn says:

    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

  9. “> 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.

  10. Reply
  11. jessamyn says:

    ok, to heck with psting XML into comment boxes, feel free to email me for the code. my first name at gmail

    Reply

Leave a Reply