My Recent Tunes

A number of people have asked me how my Recent Tunes list 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 at. 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 50×50 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.

Advertisements
My Recent Tunes

10 thoughts on “My Recent Tunes

  1. Could you send me the script you used?

    I’m just starting to dabble in php and haven’t really the time to try and learn enough to follow this guide. Something like this would look really good (just like yours) on my site methinks.

    Ta,
    Caius.

    Like

  2. Oh, heh, I see, that was just Internet Explorer being Internet Explorer :P

    And my CSS being my CSS, as well. I’m sure it’s all fixable with some work, but I’m no pro at CSS.

    Like

  3. You releasing the php code anytime soon? =p

    Just got a week holiday coming up and it’d be nice to get something like this implemented in the time I have. I don’t mind about it being sloppy php, I’ll be able to tidy it up hopefully, if not then as long as it works, eh ;)

    Like

  4. You releasing the php code anytime soon?

    I think I’ll post it tomorrow. I have to separate it from my site design a bit, but it shouldn’t be too hard to get working.

    Like

  5. Yes, I used AudioScrobbler for some time, however they don’t (that I know of) provide the album name of the current track, which is necessary for looking up the artwork to go with the song.

    Like

Comments are closed.