Arena Red » 11 Oct 2003 » Now Playing in iTunes
« SHA and MD5 in Java | Playlist Improvements »
Now Playing in iTunes

In the sidebar on most pages on this site, there is a section called "Now Playing". It shows a song name, artist name, and album name, with the album cover art and a link to the album on Amazon.com. This is actually displaying in near real-time, the song that is playing on my computer in iTunes, or if I'm not currently playing anything, the last song played. This is cobbled together on-the-fly from a few snippets of code I wrote. I'll discuss how it works, how you can grab it, and how I plan to enhance it with a few minor changes.

There are 3 parts of the equation: The AppleScript that pulls data from iTunes and invokes a URL on the web server to pass the information up; The servlet that handles the request and stashes the data on the server; The dynamic page generation mechanisms that put the data on each page seen by a browser.

Part 1: Now Playing AppleScript

The AppleScript source code is here.

This part I found and tweaked. I was looking around to see if someone had done an iTunes export from AppleScript. I found something pretty close to start with, through a Google search and landing on Matt Haughey's site. It says it was written by Jerry Kindall, but I can't find the original on Jerry's site.

The script polls iTunes every few seconds to see if the "most recently played song" ID has changed; only then does it invoke the Unix curl command to invoke a certain URL. What I changed in this script is that I invoke the URL of something I wrote for my server (see below), and as request parameters I pull out not only the artist/title/album names, but also the song's comments field, which I pass as the "asin" parameter in the URL that is invoked. An ASIN is the unique ID number of an item on Amazon. I have gone through my iTunes collection of my CDs that I have ripped, and put the Amazon ASIN in the comments field for each album. One enhancement to this would be for me to grab the ASIN out of the comments field in a more flexible way, such as looking for a string like "asin=value" rather than just assuming that the comment field is dedicated solely to the ASIN value. Fortunately, I currently don't use the comments feature in iTunes, so I can just treat it as a field dedicated for this purpose.

Part 2: Now Playing Servlet

The relevant snippet of Java source code is here. This is just the member function that grabs the parameters and writes to the snippet file. It fits into my own particular servlet framework, which for brevity's sake I don't want to post in its entirety here.

The URL that gets invoked by the AppleScript is handled on the server by a servlet I wrote that is running on the web server. It's very simple: it just takes the parameters (ASIN, artist name, song title, album title), formats them nicely as a snippet of HTML, and writes it to a text file in a known location on the server. The format of the file is exactly what you see in the Now Playing section of the sidebar. It has the album artwork IMG tag, which is specified by the ASIN, plus the song title, artist name, and album title, plus a link button. The artwork URL is based on the ASIN, and is documented by Amazon.

Part 3: Dynamic Page Generation

On my static pages I simply insert the Now Playing HTML snippet file on-the-fly through a server-side include statement. On the servlet-based dynamic pages, like this one, there's a variable substition element that knows how to find the file, so I just put the little Now Playing variable into the sidebar template and when the page is rendered the variable is replaced automagically by the contents of that file.

Future Improvements

I recently saw another web site, Andrew Grumet's Weblog, that presents a similar "recently played" list, and I like the way Andrew presents the information on the web page there better. Instead of showing the "currently" playing song with a medium-sized album cover, it shows the most recent 10 songs with small-sized album covers. The problem with the way I'm currently displaying it is that if I'm not currently listening to anything, what you see is the one last song I listed to. It feels more stale more often. If I change it to display the last "n" songs, that's is a better way to show a representation of what's in my playlist. There are a couple of ways I could tweak things to work this way. One would be to change the servlet to just brute-force update the HTML snippet file, stripping a few lines off the end (the least recently played song in the file) and insert at the front of the file. What would be a little more interesting would be to insert the song info into a database table, and then either update the HTML snippet file by querying for the last 10 rows, or eliminate the snippet file entirely and have the template variable handler do the query. My guess is that it would be more efficient to update the HTML file once rather than query the table on every page hit. Of course, that assumes I get a non-trivial number of page hits. :)

It's also interesting to think about how the information in this database could be pooled among users to create a system for recommending music based simply on what you listen to, without having to even rate things. If your song selections are automatically submitted to a database in this way, not only could you allow people to browse your list of favorite or most-listened-to songs, but the system could look for commonalities between your playlist and those of other users, and recommend things you might want to check out. A recommendation system is nothing new, but this would be one way of making it more automatic, and relieve you of having to maintain yet another rating metric -- iTunes' rating values would be used if desired, or just the raw play frequency would be taken as a rough indicator of your rating.

Top 10 of 1627 Referrers:
[612] http://sundown.greyledge.net/pages/2004/01/now_playing_in_it...
[42] Google: "itunes now playing"
[31] Google: "now playing in itunes"
[15] http://www.fourhman.com/blog/archive/2004_03.html
[15] Google: "itunes now playing"
[14] Google: "itunes currently playing"
[13] Google: "iTunes now playing"
[13] Google: "itunes now playing"
[12] Google: "now playing itunes"
[10] http://forum.mercury.to/index.php?act=ST&f=8&t=8361&hl=tell+...