Apple kindly provides RSS feeds of “Top 10” and “Recently Added” items to the iTunes Music Store. The version linked above is for the general public. Partners/affiliates use a separate interface to generate RSS feeds with embedded affiliate IDs. Either way, the feeds they generate display by default as … wait for it … a series of HTML tables including all kinds of information you probably don’t want to display on your site — stuff like price, release date, and copyright holder all seem locked into the feed.
I’m using Magpie to display columns of genre-specific artist images on pages in The Archive of Misheard Lyrics. At first thought I’d have to scrape the feed to get just the data I wanted out of the tables, but then discovered that all of the data elements actually are atomic – they’re just stored in a subarray. Since this isn’t documented and Google turned up nothing useful, thought I’d share the code I came up with for the sake of future searchers.
PHP chunk for parsing iTMS RSS feeds with Magpie.
foreach ($items as $item) { $href = $item['link']; /* The crux of the biscuit -- to fetch album cover data you need to jump into a separate (sub)array -- the itms namespace. */ $itms = $item["itms"]; $artist = $itms["artist"]; $album = $itms["album"]; $title = $itms["title"]; /* Now that we're in the itms subarray and can get the URI to the cover art, we find that Apple does a pretty bizarre thing -- they put multiple coverart img URIs into one long concatenated string. I needed to extract the fourth (100x100) one. */ $coverart = $itms["coverart"]; $cover_pieces = explode("http://", $coverart); $coverart = "http://$cover_pieces[3]"; // The Xs in the string below are due to a weird // quirk in WordPress. Remove them. $ITMSImgString .= " <a xhref=\"$href\"><img xsrc=\"$coverart\"></a> <br /> "; }
Simple stuff in retrospect, but sometimes it takes way too long to get to the simple solution (would it kill you to document this, Apple?)
Thanks so much for posting this… you helped me out of a real bind. Good luck on your lyrics project.
Thanks for this. It was driving me crazy trying to display ITMS cover art. Nice one!