/ Published in: PHP
If you've ever tried to subscribe to a podcast outside of iTunes but only have an iTunes URL, you know how frustrating it is. Turns out that you just need to spoof yourself as an iTunes client to see the direct feed URL. This script helps with exactly that. I even wrote a front-end @ http://itunes.so-nik.com so you can use it from a mobile device or desktop web browser.
NOTE: if you came here from Google, please use the "Versions" dropbox on the right to select the current version as you may be viewing an older copy with bugs.
[Change log moved into file]
NOTE: if you came here from Google, please use the "Versions" dropbox on the right to select the current version as you may be viewing an older copy with bugs.
[Change log moved into file]
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /* * Podcast URL Extractor * implemented at http://itunes.so-nik.com * * All code has been re-written by lasavior. * Original code & inspiration from Michael Sitarzewski, zerologic.com * * Ex: http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStore.woa/wa/viewPodcast?id=269238657 * Ex: http://itunes.apple.com/us/podcast/hombre-potato/id319810190?uo=4 * * Usage: itunes.php?http://itunes.apple.com/us/podcast/the-morning-stream/id414564832?uo=4 * Returns: http://myextralife.com/ftp/radio/morningstream.xml CHANGE LOG::: EDIT 5/10/11: Removed flat-file database storing. Reformatted for SQLite (requires PHP 5 and above). Script will create database & table if they dont exist. EDIT 6/2/11: Added safety catch for iTunes-U URL's. (Currently Apple does not offer a way to subscribe to these outside of iTunes [as far as i've seen]). Also touched up a little bit of code here & there. EDIT 6/21/11: Added another safety catch for iTunes-U pages. Now instead of just checking the URL, it will also check the page content (as there are links that do not include the tag 'itunes-u' that was previously checked for). EDIT 6/28/11: Previous iTunes-U update was flawed and resulted in a lot of false-positives. Fixed in new update. EDIT 8/29/11: Added catch for material not available in the US store. EDIT 9/3/11: Removed POST option, changed to GET only. (if you wish to re-enable it, set $url = $_REQUEST['terms'];) EDIT 10/16/12: Changed the checkFEED function to look only for "http" instead of "http://" as it would fail on https:// links. EDIT 10/16/12: Script now checks a URL twice before determining that it doesn't work. Sometimes iTunes just doesn't want to cooperate the first time around. EDIT 10/23/12: Lots of little changes. Control structure changes (i used alt syntax improperly), plus adding in new error finding. EIDT 10/25/12: Minor fixes and restructuring to make the code flow better. Also fixed cache displaying error. */ //----------------------------------------- // a couple of settings 'hashalgo' => 'md5', 'dbtable' => 'feedurls', 'dbfilename' => 'itnfedb.sqdb' ); //----------------------------------------- // the base URL //----------------------------------------- //Functions function curlfeed($churl) { CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_FOLLOWLOCATION => TRUE, CURLOPT_MAXREDIRS => 10, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_URL => $churl, CURLOPT_USERAGENT => 'iTunes/9.1.1' )); return $chresult; } //END function: curlfeed function subURL($urltosub, $string1 = 'feed-url="', $string2 ='"') { return $subbedstring; } //END function: subURL function checkFEED($thefeed, $mainURL, $isCached = NULL) { global $feedURL, $invalidURL, $_SETT; //For your reference, the following is called a HEREDOC $badURL = <<<BADURL <div style="font-size:15px;white-space:normal;max-width:400px">iTunes failed to return the feed-url. <br> <br>This may be due to different reasons: <br> 1) the podcast has been deleted, <br> 2) the podcast has no items, <br> 3) the podcast may be outside the USA, <br>and many other reasons im not aware of. Unfortunately iTunes isnt perfect (hence why your here). <br> <br>Im sorry my script was of no help to you. If you feel this was in error, you can <a href="outfeed.php?$mainURL" style="text-decoration:underline" target="_blank">view the source,</a><br> or you can always <a href="mailto:{$_SETT['contactemail']}?subject=IFE Error&body=URL i tried: $mainURL" style="display:inline;text-decoration:underline">email me</a> the URL and i will see if its fixable.</div> BADURL; $feedURL = $badURL; } //END if: isCached $invalidURL = "The cake is a lie."; } //END if: http } //END function: checkFEED //----------------------------------------- //Here we initiate the sqlite database and setup the cached variables $check_cache_query = "SELECT url FROM {$_SETT['dbtable']} WHERE uniqueid='$urlhash'"; $create_table_query = "CREATE TABLE {$_SETT['dbtable']}(uniqueid TEXT, url TEXT, date TEXT)"; $cache_file = NULL; } //END if: sqlite last error } //END if: sqlite open else { $failed_to_initialize_sqlite = $sqlerror; } //END if: sqlite open //----------------------------------------- //For caching files, this determines if the cached file already exists //----------------------------------------- //Here we identify the podcast url scheme '?id=', '&id=', '/id' ); $idstyle = $idstyles[$counter]; $validID = "So, how are you holding up? Because I'm a potato!"; break; } //END if: idstyles } //END for: counter //----------------------------------------- //Since iTunes-U uses the same identifier symbols, //this is where we rule them out until it is supported //Note: more checking for itunes-u content is done farther below $invalidID = "itunes-u"; } //END if: itunes-u //----------------------------------------- // extract feed-id, get page from itunes & find feed-url for ($loopcount = 1; $loopcount < 3; $loopcount++) { $curled1 = curlfeed("http://itunes.apple.com/podcast/id".$podid[0]); $newURL = subURL($curled1, "<key>url</key><string>", "</string>"); $curled1 = curlfeed($newURL); } //END if:goto $feedURL = <<<ITUNESFOREIGNSTORE <div style="font-size:15px;white-space:normal;max-width:400px">This item is not available in the US store. <br> <br>(This may also be a problem with the podcast not existing in the iTunes store at all. Unfortunately iTunes returns the same error for both so they are indistinguishable from each other.) Apple currently restricts access to some content based on geographic locations. As I reside in the United States, I cannot retrieve your podcast link. <br> <br>If you have any PHP knowledge you can try translating the public source code to retrieve the URL on your own: <br>http://snipplr.com/view/52465</div> ITUNESFOREIGNSTORE; $invalidID = 'itunes_foreign'; } //END if: us store $emptyPodcastText = <<<EMPTYPODCAST <div style="font-size:15px;white-space:normal;max-width:400px"><b>Empty podcast.</b> <br> <br>Your podcast contains no episodes at the time this script last checked. Unfortunately Apple does not provide the feed URL for empty podcasts. Please check back in as little as 12 hours or once the podcast has active items.</div> EMPTYPODCAST; $invalidID = 'empty_podcast'; } //END if: 0 items if ($loopcount == 2) { $feedURL = <<<CANTPROCESS <div style="font-size:15px;white-space:normal;max-width:400px">Temporarily unable to process. <br> <br>iTunes returned the following error: "Your request is temporarily unable to be processed." Please try again later.</div> CANTPROCESS; $invalidID = $doNotCacheResults = TRUE; } //END if: loopcount } //END if: unable to process elseif (strpos($curled1, 'iTunes U')) { //Leave this as the last elseif as it may catch non-iTunesU material $itunesU_title = subURL($curled1, '<title>', '</title>'); $itunesU_crumbs = subURL($curled1, 'start breadcrumbs', 'end breadcrumbs'); $itunesU_li = subURL($itunesU_crumbs, '<li>', '</li>'); $feedURL = <<<ITUNESU <div style="font-size:15px;white-space:normal;max-width:400px">iTunes-U links not supported. <br> <br>Currently Apple does not offer a way to subscribe to iTunes-U material outside of iTunes (that i can find). A temporary solution is to search for a similar title as a podcast in hopes that the content providers also posted it to the iTunes Podcast Directory (do no expect this for password protected content). Try searching for: <br>"$itunesU_title" <br> <br>You can <a href='http://itunes.so-nik.com/index.php?terms=$itunesU_title_char' style="display:inline;text-decoration:underline;color:blue">click here</a> to try that now.</div> ITUNESU; $invalidID = 'itunes-u'; } //END if: itunesu_li } //END if: iTunes U $feedURL = subURL($curled1); checkFEED($feedURL, $url); } // END if: loopcount else { break; } //END if: loopcount } //END if: invalidID else { break; } //END if: invalidID } //END for: loopcount $cache_query_put_content = "INSERT INTO {$_SETT['dbtable']} (uniqueid,url,date) VALUES ('$urlhash', '$newfeedURL', '".date("r")."'"; } //END if: failed sqlite } //END if: validID else { if ($invalidID == "itunes-u") { //Example URL: http://itunes.apple.com/itunes-u/the-civil-war-reconstruction/id341650730 $itu_label = subURL($url, '/itunes-u/', '/'); $feedURL = <<<FEEDURL <div style="font-size:15px;white-space:normal;max-width:400px">iTunes-U links not supported. <br> <br>Currently Apple does not offer a way to subscribe to iTunes-U material outside of iTunes (that i can find). A temporary solution is to search for a similar title as a podcast in hopes that the content providers also posted it to the iTunes Podcast Directory (do no expect this for password protected content). Try searching for: <br>"$itu_label_white" <br> <br>You can <a href='http://itunes.so-nik.com/index.php?terms=$itu_label_char' style="display:inline;text-decoration:underline;color:blue">click here</a> to try that now.</div> FEEDURL; } //END if: itunes-u else { $feedURL = "URL not supported. <br><br>Please contact <a href='mailto:{$_SETT['contactemail']}?subject=Feed-Error&body=Error on URL:$url' style=\"display:inline;text-decoration:underline\">{$_SETT['contactemail']}</a> <br>and notify me of the URL you are trying."; $invalidID = "I was doing fine. Noone was trying to murder me, or put me in a potato, or feed me to birds."; } //END if: itunes-u } //END if: validID } //END if: cache_file else { $feedURL = $cache_file; $invalidID = TRUE; $delete_table_query = "DELETE FROM {$_SETT['dbtable']} WHERE uniqueid='$urlhash'"; exit; } //END if: time else { } //END if: time } //END if: empty podcast $invalidID = 'itunes-u'; } //END elseif: itunes-u else { checkFEED($feedURL, $url, TRUE); } //END if: empty podcast } //END if: cache_file //----------------------------------------- // html output to browser echo <<<OUTTEXT <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" style="height: 100%;"> <head> <title>Podcast URL</title> <meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/> <meta name="robots" content="NOINDEX, NOFOLLOW, NOARCHIVE, NOSNIPPET"> <style type="text/css" media="screen">@import "iphonenav.css";</style> <link rel="apple-touch-icon" href="./icon.png" /> <link rel="shortcut icon" href="./favicon.ico"> </head> <!-- GUI, iTunes Store searching & feed extraction credited to lasavior, so-nik.com --> <!-- Original idea & inspiration credited to Michael Sitarzewski, zerologic.com --> <!-- For the PHP code to extract the podcast feed yourself, visit http://snipplr.com/view/52465 --> <body style="height: 100%;"> <h1>Podcast URL:</h1> <ul> <li style="height: 100%; padding:10px 10px 10px 10px; font-size:20px; word-wrap:break-word">$podcastURL</li> <li style="height: 100%; padding:10px 10px 10px 10px; font-size:10px; border-bottom: 0px solid;"><div style="color:LightGray;font-size:12px;"><a href="mailto:[email protected]" style="color:LightGray; font-size:12px; text-decoration:none;">[email protected]</a> / <a href="http://podcasturlextractor.blogspot.com/" style="color:LightGray; font-size:12px; text-decoration:none;" target="_blank">News</a> / <a href="http://podcasturlextractor.blogspot.com/p/what-it-is-and-why-its-here.html" style="color:LightGray; font-size:12px; text-decoration:none;" target="_blank">About</a></div></li> </ul> </body> </html> OUTTEXT; ?>