/ 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]
Expand |
Embed | Plain Text
<?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. */ //----------------------------------------- // a couple of settings 'hashalgo' => 'md5', 'contactemail' => '-INSERT-EMAIL-HERE-', 'dbtable' => 'feedurls', 'dbfilename' => 'itnfedb.sqdb' ); //----------------------------------------- // the base URL $url = ($_POST['terms'] != null) ? $_POST['terms'] : $_SERVER['QUERY_STRING']; $urlhash = hash($_SETT['hashalgo'], $url); //----------------------------------------- //Functions function curlfeed($churl) { $ch = curl_init(); CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_FOLLOWLOCATION => TRUE, CURLOPT_MAXREDIRS => 10, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_URL => $churl, CURLOPT_USERAGENT => 'iTunes/9.1.1' )); $chresult = curl_exec($ch); curl_close($ch); return $chresult; } function subURL($urltosub, $string1 = 'feed-url="', $string2 ='"') { return $subbedstring; } function checkFEED($thefeed, $mainURL) { //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>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; $invalidURL = "The cake is a lie."; endif; } //----------------------------------------- //Here we initiate the sqlite database and setup the cached variables if ($database = sqlite_open($_SETT['dbfilename'], 0666, $sqlerror)): $check_cache_query = "SELECT url FROM {$_SETT['dbtable']} WHERE uniqueid='$urlhash'"; $cache_file = @sqlite_single_query($database, $check_cache_query, true); if(sqlite_last_error($database)): $create_table_query = "CREATE TABLE {$_SETT['dbtable']}(uniqueid TEXT, url TEXT, date TEXT)"; @sqlite_exec($database, $create_table_query); $cache_file = NULL; endif; else: $failed_to_initialize_sqlite = $sqlerror; endif; //----------------------------------------- //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; endif; endfor; //----------------------------------------- //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 further below $invalidID = "itunes-u"; endif; //----------------------------------------- // extract feed-id, get page from itunes & find feed-url $curled1 = curlfeed("http://itunes.apple.com/podcast/id".$podid[0]); $newURL = subURL($curled1, "<key>url</key><string>", "</string>"); $curled1 = curlfeed($newURL); endif; $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_check = 1; $invalidID = 'itunes-u'; endif; $feedURL = <<<ITUNESFOREIGNSTORE <div style="font-size:15px;white-space:normal;max-width:400px">This item is not available in the US store. <br> <br>Apple currently restricts access to some content based on geographic locations. As I reside in the United States, I unfortunately cannot help you with your podcast link. <br> <br>If you have any PHP knowledge you can use the public source code to retrieve the URL yourself: <br>http://snipplr.com/view/52465</div> ITUNESFOREIGNSTORE; $invalidID_check = 1; $invalidID = 'itunes_foreign'; endif; if ($invalidID_check != 1): $feedURL = subURL($curled1); checkFEED($feedURL, $url); endif; $newfeedURL = sqlite_escape_string($feedURL); $cache_query_put_content = "INSERT INTO {$_SETT['dbtable']} (uniqueid,url,date) VALUES ('$urlhash', '$newfeedURL', '".date("r")."')"; @sqlite_exec($database, $cache_query_put_content); endif; 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; 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."; endif; endif; else: $feedURL = $cache_file; if (!stripos($feedURL, 'itunes-u')): checkFEED($feedURL, $url); else: $invalidID = 'itunes-u'; endif; endif; //----------------------------------------- // 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:ife@so-nik.com" style="color:LightGray; font-size:12px; text-decoration:none;">ife@so-nik.com</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; ?>
Comments
Subscribe to comments
You need to login to post a comment.

thanks for the help