Muschamp Rd

Finding audio previews for album tracks in iTunes with PHP

March 3rd, 2013
PHP logo

My mashup codebase has not been updated in years but even without that APIs change their policies and the technique documented below probably no longer works. Sorry if you were counting on this code to solve all your problems, you may have to hire a developer to fix this or try another API.

When I started building my mashups with PHP one idea I had was to fetch audio previews for songs and albums, this was supposedly possible with the Amazon Product Advertising API, but I could never get it to work with the limited data I had in my csv file. Last.fm does not provide audio previews through their API either. iTunes Music Store isn’t perhaps known for their API, nor can you earn any referral income if you’re Canadian but I actually was able to fetch direct links to audio previews from the iTunes Music Store using their Search API.

Beware API Term of Service Agreements

I’m not sure Apple would applaud this, I’ve been doing it for well over a year, but it is only used as part of one or two of my web mashups. I don’t think I violate the TOS, I do cache the returned JSON results but only to prevent repeated redundant calls to Apple’s API. I don’t cache images and certainly not audio previews. I’m not sure I meet their display requirements, frickin’ display requirements.

Apple’s API was relatively open

Considering how much time and effort I spent debugging and re-reading API documentation there may be other methods I’ve written which people would find useful, but no one uses because they don’t want to jump through all the hoops necessary to use my mashup codebase. Apple doesn’t require public and private keys or OAuth or hashing, you can just format a URL and get back data. It is up to you to format the URL correctly and parse the data, this is non-trivial.

It only took three methods

This code is part of my musicCollection.php class but it is actually only three methods, one which makes requests to the other two. It is necessary to make multiple calls to Apple’s Search API in order to get the resulting preview tracks for an entire album. You can rip out the caching if you like. My caching technique isn’t terribly sophisticated, it is contained in caching.php and the code below may need one function from muskLib.php which is just a wrapper for cURL.  All this code is in GitHub.

There is no warranty or guarantee for this code and you are responsible for not breaking Apple’s Terms of Service and/or getting throttled for making excessive number of requests, I recommend you cache what you can.

Album track previews from iTunes working on my random CD mashup

Code Maintenance is Needed

I have not had time to make web mashups, like a lot of things I tried it did not make any money or lead to fame and fortune though I think I did win an award once. I still am not the biggest fan of PHP and I have to caution against relying on APIs to be free forever as numerous times after I got them integrated into my code they changed their policies or even went out of business. Maybe one day I’ll update all this old PHP but my priority is getting a job and I don’t think PHP is the answer so I’m trying Python.

If you have thoughts on APIs, web mashups, PHP, or code maintenance you can leave a comment below.

One Comment

  • Muskie says:

    This code is actually quite useful, someone could make money with it, but not me as I’m Canadian… It appears there may be a bug in my damn Twitter code, it was working perfectly when I posted it to my blog the other day, but now I’m not so sure.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Posts on Muskblog © Andrew "Muskie" McKay.
CFA Institute does not endorse, promote or warrant the accuracy or quality of Muskblog. CFA® and Chartered Financial Analyst® are registered trademarks owned by CFA Institute.