itunes.php LIVES!

By tying together Amazon Web Services, Kung-Tunes, and a simple mySQL table using PHP, I have managed to speed up album cover lookups on the sidebar there, but more importantly, I've got album covers for all recent tracks now! I wrote a previous entry about grouping recent tracks by album - this one is about how to suck the album covers off of Amazon's servers...

[Update 9/15 2:08 PM: I've fixed a bunch of bugs, including bad character encoding in song titles, database problems with long album titles, and issues with including both frontend pages within the same webpage. Thanks to Etan for catching a bunch of them. ]

The basic algorithm of it goes like this:

Kung-Tunes supplies an artist and album name. searchagent.php does a keyword search through Amazon, which sends back an XML page rife with information. The scripts parse out the URL for the thumbnail image along with the unique ASIN (Amazon Standard Identification Number) for that album. They then save the information into a MySQL table. The next time the page is loaded for that album, the script checks the table for it, saving valuable load time. This info is passed to one of the front end scripts for formatting.

searchagent.php stores the Lookup object and the SearchAgent object. Lookup handles SQL lookups, and, if it doesn't find anything, calls SearchAgent to look it up on Amazon.

amazon.php is the frontend for the "iTunes is currently playing" section. It basically passes information from Kung-Tunes to the Lookup object, gets back the info, and formats it for viewing.

itunes.php is the frontend for the recent tracks page. It groups the tracks by album, and then passes that info on to the Lookup object. It takes what it gets back and puts in a pretty table.

You can download all three here.

On the SQL side, you need to create a table by the name of amazon with the proper fields. This SQL query should probably do it:

sql CREATE TABLE `amazon` ( `Name` varchar(100) NOT NULL default '', `ASIN` varchar(11) default NULL, `Date` date NOT NULL default '0000-00-00', `imgURL` varchar(100) default NULL, `URL` varchar(100) default NULL, UNIQUE KEY `Name` (`Name`) );

That Date field isn't currently used in the code, but it's there to deal with Amazon's caching policies, which I haven't quite figured out yet.

On the Kung-Tunes end, the scripts assume ^t@@ #p^p#p @@ #a^a#a @@ #y^y#y @@ for currently-playing track and for recent tracks list. Encode HTML entities is off, as they seem to muck with the Amazon searching.

To use my scripts, you need an Amazon Developer Token obtainable at Amazon's Web Services site. (Since I'm making the source available, I've taken mine out of the code.) You also need Kung-Tunes, a great piece of GPL software that runs on OS X, uploading iTunes track data to the web server of your choice. And of course you need OS X - not on the server, just on your own machine. And finally, the willingess to futz around with my code to make nice with your server configuration.

This code is covered under the Creative Commons Attribution-NonCommercial-ShareAlike License. I wrote a previous entry about why I chose this license and how I think it works in terms of PHP scripts. If you don't agree with something in the license or my interpretation, please don't use the scripts. Make a comment here or there, I'll look into it, and we'll work something out....


[Update 9/28/03: I noticed a couple of downloads of the code in my server logs recently, and my curiosity is just killing me. If you do end up downloading and using it, please make a comment or send a trackback. You're certainly not required to, but it would make me smile. Thanks!]