By Collin Allen

My Recent Tunes

March 15, 2005

A number of people have asked me how the Recent Tunes section of this site works. It’s a little complicated, but here’s how.

First, you need to get track info from iTunes to your website in a fairly timely basis to keep the information accurate. Freshly Squeezed Software, makers of the popular Mac newsreader PulpFiction, has a nice freeware application called Recent Tunes which does exactly that. It can upload a list of recent tracks as well as the current track, all formatted according to a template you design. I don’t use the recent tracks list, and you’ll see why very shortly.

With the current track info on the website, it’s time to actually do something with that data. You could stop here and simply include the HTML file in your design with a small PHP command, and the current tune info would show up, but with no artwork or links. The next step is to create a timed or invoke-able script that will open up that current track HTML file, split apart the various chunks of information (artist, album, title, etc.), and go from there. Now, I should mention at this point that the current track HTML file that Recent Tunes uploads does not have to be a fully formatted HTML file, by which I mean all the standard tags can be left out – it will make your life easier while writing the script that splits up the data.

In Recent Tunes, I set my template to show the album, artist, title, and rating, each separated with a simple few asterisks. This string of asterisks is what your code will look for when splitting up song info. Do note that if one of your current tracks has five asterisks in one of those four pieces of information, your script will break unless that possibility is accounted for. PHP has a wonderful and amusing function called explode(), which separates data however many times is necessary, given a string to split on. In this example, you would do list($artist, $album, $title, $rating) = explode("*****", $trackinfo); After that, you could do some error checking to make sure none of those fields are empty. If one or more is empty, it might not be worth your while to even bother continuing with the script, so have it exit cleanly now.

Now that you have the song info from your computer, to your website, read in with PHP, and split apart into usable bits, you are ready to start looking up artwork, links, and more. Amazon.com has a very nice web service interface that provides just about every bit of info they have on a product, including artwork. This is where the album images will come from. The only problem presented, aside from the task of writing the code that will be communicating with Amazon’s infrastructure, is that their artwork isn’t a consistent size. They offer “small”, “medium”, and “large”, but the sizes in each category vary slightly. If we’re going to go to the trouble of doing all this, it should look good. The obvious answer is to make the album artwork the size you want.

Since the “small” artwork is already low in detail, it would look even worse if resized – any manipulations you make will lessen the quality each time. The best way to approach this would be to use the biggest available image and scale it to the size you want in one shot. Enter GD, an open source image library capable of handling common image formats. PHP must be compiled to use GD, but it’s quite common. I’ve found that most web hosts provide it. It is not built into PHP that comes with Mac OS X, however it is available as an installer package from Marc Liyanage’s site. I always use his releases for local development and never have problems with them.

Once you retrieve the artwork from Amazon by matching artists and album titles, use GD to scale the image to 50x50 pixels and save it (just write it to a file with a unique name, say, an MD5 hash of the song info with “.jpg” tacked on). Amazon’s API also provides the product URL too, so hang onto that as well. The iTunes Music Store addresses are relatively easy to create. Using information I dug up on NSLog();, I was able to create a small function that returns an iTunes link. Once you’ve collected all this information and media, you’re ready to save it all to a database. MySQL runs WordPress, so it was only fitting that I use the same database only a different table. Do a MySQL “INSERT” command and log the song title, artist, album, rating, time (if you want), cover artwork path, Amazon product URL, and iTunes Music Store URL.

A good chunk of code to add here is one that will read the ID of the tenth newest row, and remove all rows and images that were before it. I found a good way to do it was read out the rows first, unlink the images using the paths set in the image path column, then MySQL “DELETE” that row, and continue until there are no more rows older than that tenth entry. In this way, you will keep your list of songs down and not gradually run yourself out of disk space by keeping ancient artwork files around.

Finally, when a visitor comes by, read out the last 5 or so rows (no more than your limit set in the previous paragraph) from the table and format them using some CSS. And last but not least – error checking everywhere. If artwork is not found or broken, or if the scaled image file doesn’t exist, or GD chokes on a corrupted Amazon image, simply don’t add that tune to the database and skip to the next one. It’s just a song.

I would release the code I wrote now, but it’s a little sloppy. I hacked it together in two days time, so I can’t guarantee anything! Let me know if you see any problems. If it continues to work well, I’ll surely hand it out.

Known Bugs: Albums with single quotes in any of the info cause the iTunes link to break. They are stored using the HTML entities ON for quotes, yet that seems to stop iTunes from understanding them. Not yet sure why that is.

Mastodon