itunes_playlist
What I did on my birthday weekend (besides the requisite alcoholic binging) was write a script to list the last 5 songs I’ve listened to on iTunes. (I guess I’m just an irresolute geek.)
In the process, I managed to learn how to use XML::LibXML. So my XPath skills aren’t going to go to waste.
My first attempt involved trying to parse the iTunes Music Library XML file. Unfortunately, this is a rather large file (5.2M with over 4,000 songs) and Apple’s plist format is a pretty bad implementation of XML (you have use some rather arcane XPath to get the value of the key you’re interested in, because the key name and the value are siblings instead of parent-child, which would make more sense), and maybe I wasn’t using the most efficient way to do it (with two XSL transforms), but it was taking forever to pull a playlist out.
So I went with Applescript. (Applescript for extracting playlists by Kimbra Staken) Even this is a little slow, but nowhere near as horribly so as my Perl script. I ended up embedding the Applescript in Perl anyway (using Mac::Applescript) because I wanted to use XML::LibXML.
The Applescript just parses a smart playlist I set up to give back the last five songs I’ve played.
Now, what I wanted as output was an HTML fragment that I could include into my template with Blosxom’s file plugin. I wanted a link to lyrics (via Google), a link to search the iTunes Music Store, and a link to Amazon.com.
For the Google link, all I did was escape the artist name and the song name, concatenate them together with the word “lyrics” and then stick it all into a URI that will feed it to Google’s “I’m Feeling Lucky” mode.
For the iTMS link, I formatted the escaped parameters to adhere to the iTMS search URI syntax
The trickiest part was getting the Amazon links. You have to figure out the ASIN identifier for whatever you’re looking for in order to have a usable URI. Luckily, Amazon.com has exposed their API for public consumption. You have to register as a developer, and then you can send queries to Amazon’s database. Amazon.com will send back an XML file, which you can then parse for the ASIN.
So basically I execute this perl script before I generate static pages with Blosxom and rsync with my webhost.