itunes.php
Sep 12, 2003 programmingandInterfacesShare/Save
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
[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:
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….
Enjoy!
[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!]
September 12th, 2003 at 3:08 pm
Can’t you just make me an installer package, you insensitive clod?
September 12th, 2003 at 3:11 pm
itunes and php, nice one!! Now everyone can see your tunes now only if we could hear them as well.
Your next assignment is to create a dynamically driven site that uploads all your music to my harddrive, cause I’m the need for some good tunes. Either that or something that gets you outta the house more.
Peace my brother
September 12th, 2003 at 3:12 pm
Evan, We’ll see. An installer package assumes that you’re running OS X on your server and probably that you have administrative access to that machine. For you and me, that may be fine, but what about for everyone else?
Also, Kung-Tunes prefs still have to be worked out regardless, and I’m not about to start distributing Kung-Tunes along with my 300-line PHP script.
You raise a good point though – how savvy do I expect people to be? I guess we’ll see..
September 12th, 2003 at 3:17 pm
Andre, all the album covers have links to Amazon, where you can get links to 30-second clips. Surely that’s what you meant, right?
In terms of getting out of the house, I’ve been waiting around for Verizon for the past two days, and I’ve had a sprained ankle, which is just shy of fully healed. So staying at home has been a necessity. And now that I have no TV, what else to do but listen to music, play the keyboard, and program? Oh, and turn into the world’s biggest nerd.
September 12th, 2003 at 3:41 pm
Worlds biggest nerd? I think not.
[Arthur's Note: Link removed at site owner's request - 2/15/04]
September 13th, 2003 at 12:22 pm
iTunes.php
I implemented Arthur’s iTunes.php, last night. What a wonderful piece of PHP. I think that every single company in the world should send offerings to Arthur, so he can pick which job has the best proportion of stress to money. Yay for better Kung-Tunes…
September 28th, 2003 at 5:55 pm
February 17th, 2005 at 3:48 pm
Boo. Parse error: parse error, unexpected T_OBJECT_OPERATOR in /home/amy/public_html/itunes/searchagent.php on line 94
February 19th, 2005 at 1:05 am
I figuired it out, Line 94 is for an image url to be displayed for a song that is not found. change:
ths->
to:
$this->
Typo?
February 19th, 2005 at 12:51 pm
Ooh, good find. My apologies. Apparently, I’m still using a slightly older edit of the code on my own site, so that part works fine for me.
Let me know if you start using it. I tried to send you an e-mail, but it bounced back (even when I took out the extra dot…)