iTunes "Recently Played" list on your website
The other evening I set about putting my iTunes "Recently Played" list on a web page. Not only that, but updating it regularly throughout the day. I got it working and you can see the songs that are on my turntable right now. It updates every hour and is automatically uploaded to my website, as if by magic.

The non-technically minded need read no further.
After investigating and discarding some AppleScript and Automator options, I ended up plumping for an XSLT solution. The iTunes application saves a snapshot of its library, playlists and smartlists in a file called "iTunes Music Library.xml". Translating this file into HTML should be a simple job but Apple's XML style is pretty poor; it's a list of key-property pairs, which is pretty nasty to transform.
XSLT is a open standard that describes the transformation of XML into other text-based forms using an XSL stylesheet. It has a pretty steep learning curve, so it's not for the faint hearted.
A smart-list called 'Recently Added' is sought in the XML file using the XPath query:
The list contains an array of integers which represent the IDs of the tracks in the list. The songs that are represented by these IDs are found in the XML with the XPath
where $trackid is the ID of the track to be fetched. The iTunes XML to HTML XSL stylesheet can be found here. It runs in a couple of seconds on my Powerbook G4 on a 4000 song library.
XSLT is easily done with free software tools such as Xalan or Saxon. I have always found "xsltproc" that comes with libXSLT to be small and fast. If you have "xsltproc" working, then a simple shell script can do the job:
The script can be set to run periodically by installing it into a "crontab".
The XSL stylsheet can be easily modified to work on any of the other Smart Lists e.g. "Top 50 Most Played", "Recently Added" or any Smart-Lists you have created yourself.

The non-technically minded need read no further.
Using XSLT to transform the iTunes XML library file
After investigating and discarding some AppleScript and Automator options, I ended up plumping for an XSLT solution. The iTunes application saves a snapshot of its library, playlists and smartlists in a file called "iTunes Music Library.xml". Translating this file into HTML should be a simple job but Apple's XML style is pretty poor; it's a list of key-property pairs, which is pretty nasty to transform.
XSLT is a open standard that describes the transformation of XML into other text-based forms using an XSL stylesheet. It has a pretty steep learning curve, so it's not for the faint hearted.
How does the transformation work?
A smart-list called 'Recently Added' is sought in the XML file using the XPath query:
/plist/dict/array/dict[string[1]='Recently Played']
The list contains an array of integers which represent the IDs of the tracks in the list. The songs that are represented by these IDs are found in the XML with the XPath
/plist/dict/dict/dict[integer[1]=$trackid]
where $trackid is the ID of the track to be fetched. The iTunes XML to HTML XSL stylesheet can be found here. It runs in a couple of seconds on my Powerbook G4 on a 4000 song library.
How do I do the XSLT?
XSLT is easily done with free software tools such as Xalan or Saxon. I have always found "xsltproc" that comes with libXSLT to be small and fast. If you have "xsltproc" working, then a simple shell script can do the job:
#!/bin/bash
xsltproc songs.xsl itunes.xml >the-ape-turntable.xhtml
scp the-ape-turntable.xhtml username@domainname:directory/
The script can be set to run periodically by installing it into a "crontab".
Other applications of this technique
The XSL stylsheet can be easily modified to work on any of the other Smart Lists e.g. "Top 50 Most Played", "Recently Added" or any Smart-Lists you have created yourself.


3 Comments:
Very Impressive. Would it be possible to link the songs to iTunes aswell
P.S Since when have you been an XTC fan
I really like their Apple Venus Part 1 album. I've had it for years. I've tried their other stuff but I can't really get away with it. Andy Partridge produced a Lilac Time album - I think that's why I bought an XTC album in the first place.
A good idea about links - I'll think about that one....
Hi,
I've also done a bit of poking around the iTunes XML file using XSLT. In fact I posted something similar to your script a while back (albeit with zero formatting).
You might find that you get a fair bit of a speedup using xsl:key lookups. I tried your script on my library and it ran in about 22 seconds, versus 5 seconds for my xsl:key version.
Also note that the XSLT can be fairly trivially wrapped in a shell script using a here document. This means users don't have to know that there's XSLT under the hood. See my wikipod script for an example.
Post a Comment
Subscribe to Post Comments [Atom]
<< Home