iTunes Podcast URL Extractor


/ Published in: PHP
Save to your folder(s)

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]


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. /*
  4.  * Podcast URL Extractor
  5.  * implemented at http://itunes.so-nik.com
  6.  *
  7.  * All code has been re-written by lasavior.
  8.  * Original code & inspiration from Michael Sitarzewski, zerologic.com
  9.  *
  10.  * Ex: http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStore.woa/wa/viewPodcast?id=269238657
  11.  * Ex: http://itunes.apple.com/us/podcast/hombre-potato/id319810190?uo=4
  12.  *
  13.  * Usage: itunes.php?http://itunes.apple.com/us/podcast/the-morning-stream/id414564832?uo=4
  14.  * Returns: http://myextralife.com/ftp/radio/morningstream.xml
  15.  
  16. CHANGE LOG:::
  17. 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.
  18.  
  19. 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.
  20.  
  21. 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).
  22.  
  23. EDIT 6/28/11: Previous iTunes-U update was flawed and resulted in a lot of false-positives. Fixed in new update.
  24.  
  25. EDIT 8/29/11: Added catch for material not available in the US store.
  26.  
  27. EDIT 9/3/11: Removed POST option, changed to GET only. (if you wish to re-enable it, set $url = $_REQUEST['terms'];)
  28.  
  29. EDIT 10/16/12: Changed the checkFEED function to look only for "http" instead of "http://" as it would fail on https:// links.
  30. 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.
  31.  
  32. EDIT 10/23/12: Lots of little changes. Control structure changes (i used alt syntax improperly), plus adding in new error finding.
  33.  
  34. EIDT 10/25/12: Minor fixes and restructuring to make the code flow better. Also fixed cache displaying error.
  35.  */
  36.  
  37.  
  38. //-----------------------------------------
  39. // a couple of settings
  40. $_SETT = array(
  41. 'hashalgo' => 'md5',
  42. 'contactemail' => '[email protected]',
  43. 'dbtable' => 'feedurls',
  44. 'dbfilename' => 'itnfedb.sqdb'
  45. );
  46.  
  47. //-----------------------------------------
  48. // the base URL
  49. $url = ($_GET['terms'] != null) ? urldecode(urldecode($_GET['terms'])) : urldecode(urldecode($_SERVER['QUERY_STRING']));
  50. $urlhash = hash($_SETT['hashalgo'], $url);
  51.  
  52. //-----------------------------------------
  53. //Functions
  54. function curlfeed($churl) {
  55.  
  56. $ch = curl_init();
  57. CURLOPT_RETURNTRANSFER => TRUE,
  58. CURLOPT_FOLLOWLOCATION => TRUE,
  59. CURLOPT_MAXREDIRS => 10,
  60. CURLOPT_CONNECTTIMEOUT => 5,
  61. CURLOPT_URL => $churl,
  62. CURLOPT_USERAGENT => 'iTunes/9.1.1'
  63. ));
  64. $chresult = curl_exec($ch);
  65. curl_close($ch);
  66.  
  67. return $chresult;
  68. } //END function: curlfeed
  69.  
  70. function subURL($urltosub, $string1 = 'feed-url="', $string2 ='"') {
  71.  
  72. $str1 = strpos($urltosub, $string1) + strlen($string1);
  73. $str2 = strpos($urltosub, $string2, $str1);
  74. $subbedstring = substr($urltosub, $str1, ($str2 - $str1));
  75.  
  76. return $subbedstring;
  77. } //END function: subURL
  78.  
  79. function checkFEED($thefeed, $mainURL, $isCached = NULL) {
  80.  
  81. global $feedURL, $invalidURL, $_SETT;
  82.  
  83. //For your reference, the following is called a HEREDOC
  84. $badURL = <<<BADURL
  85.   <div style="font-size:15px;white-space:normal;max-width:400px">iTunes failed to return the feed-url.
  86.   <br>
  87.   <br>This may be due to different reasons:
  88.   <br>&nbsp;&nbsp;1) the podcast has been deleted,
  89.   <br>&nbsp;&nbsp;2) the podcast has no items,
  90.   <br>&nbsp;&nbsp;3) the podcast may be outside the USA,
  91.   <br>and many other reasons im not aware of. Unfortunately iTunes isnt perfect (hence why your here).
  92.   <br>
  93.   <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>
  94. BADURL;
  95.  
  96. if (substr($thefeed, 0, 4) != "http") {
  97.  
  98. if (is_null($isCached)) {
  99.  
  100. $feedURL = $badURL;
  101. } //END if: isCached
  102. $invalidURL = "The cake is a lie.";
  103. } //END if: http
  104.  
  105. } //END function: checkFEED
  106.  
  107. //-----------------------------------------
  108. //Here we initiate the sqlite database and setup the cached variables
  109. if ($database = sqlite_open($_SETT['dbfilename'], 0666, $sqlerror)) {
  110.  
  111. $check_cache_query = "SELECT url FROM {$_SETT['dbtable']} WHERE uniqueid='$urlhash'";
  112. $cache_file = @sqlite_single_query($database, $check_cache_query, true);
  113. if(sqlite_last_error($database)) {
  114.  
  115. $create_table_query = "CREATE TABLE {$_SETT['dbtable']}(uniqueid TEXT, url TEXT, date TEXT)";
  116. @sqlite_exec($database, $create_table_query);
  117. $cache_file = NULL;
  118. } //END if: sqlite last error
  119. } //END if: sqlite open
  120. else {
  121.  
  122. $failed_to_initialize_sqlite = $sqlerror;
  123. } //END if: sqlite open
  124.  
  125. //-----------------------------------------
  126. //For caching files, this determines if the cached file already exists
  127. if ($cache_file == NULL || isset($failed_to_initialize_sqlite)) {
  128.  
  129. //-----------------------------------------
  130. //Here we identify the podcast url scheme
  131. $idstyles = array(
  132. '?id=',
  133. '&id=',
  134. '/id'
  135. );
  136.  
  137. for ($counter = 0; $counter < count($idstyles); $counter++) {
  138.  
  139. if (strpos($url, $idstyles[$counter])) {
  140.  
  141. $idstyle = $idstyles[$counter];
  142. $validID = "So, how are you holding up? Because I'm a potato!";
  143. break;
  144. } //END if: idstyles
  145. } //END for: counter
  146.  
  147. //-----------------------------------------
  148. //Since iTunes-U uses the same identifier symbols,
  149. //this is where we rule them out until it is supported
  150. //Note: more checking for itunes-u content is done farther below
  151. if (strpos($url, '/itunes-u/')) {
  152.  
  153. unset($validID);
  154. $invalidID = "itunes-u";
  155. } //END if: itunes-u
  156.  
  157. //-----------------------------------------
  158. // extract feed-id, get page from itunes & find feed-url
  159. if (isset($validID)) {
  160.  
  161. for ($loopcount = 1; $loopcount < 3; $loopcount++) {
  162.  
  163. preg_match("/[0-9]+/", $url, $podid, 0, strpos($url, $idstyle)); // here we extract the feed ID
  164. $curled1 = curlfeed("http://itunes.apple.com/podcast/id".$podid[0]);
  165. if (strpos($curled1, "<key>kind</key><string>Goto</string>") > 1) {
  166.  
  167. $newURL = subURL($curled1, "<key>url</key><string>", "</string>");
  168. $curled1 = curlfeed($newURL);
  169. } //END if:goto
  170.  
  171. if (strpos($curled1, 'not currently available in the U.S. store')) {
  172.  
  173. $feedURL = <<<ITUNESFOREIGNSTORE
  174.   <div style="font-size:15px;white-space:normal;max-width:400px">This item is not available in the US store.
  175.   <br>
  176.   <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.
  177.   <br>
  178.   <br>If you have any PHP knowledge you can try translating the public source code to retrieve the URL on your own:
  179.   <br>http://snipplr.com/view/52465</div>
  180. ITUNESFOREIGNSTORE;
  181. $invalidID = 'itunes_foreign';
  182. } //END if: us store
  183. elseif (strpos($curled1, '<span class="track-count">0 Items</span>')) {
  184.  
  185. $emptyPodcastText = <<<EMPTYPODCAST
  186.   <div style="font-size:15px;white-space:normal;max-width:400px"><b>Empty podcast.</b>
  187.   <br>
  188.   <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>
  189. EMPTYPODCAST;
  190. $feedURL = $emptyPodcastText . "\n" . (time() + 60 * 60 * 12);
  191. $invalidID = 'empty_podcast';
  192. } //END if: 0 items
  193. elseif (strpos($curled1, ' <key>message</key><string>Your request is temporarily unable to be processed.</string>') || strlen($curled1) < 20) {
  194.  
  195. if ($loopcount == 2) {
  196.  
  197. $feedURL = <<<CANTPROCESS
  198.   <div style="font-size:15px;white-space:normal;max-width:400px">Temporarily unable to process.
  199.   <br>
  200.   <br>iTunes returned the following error: "Your request is temporarily unable to be processed." Please try again later.</div>
  201. CANTPROCESS;
  202. $invalidID = $doNotCacheResults = TRUE;
  203. } //END if: loopcount
  204. } //END if: unable to process
  205. elseif (strpos($curled1, 'iTunes U')) { //Leave this as the last elseif as it may catch non-iTunesU material
  206.  
  207. $itunesU_title = subURL($curled1, '<title>', '</title>');
  208. $itunesU_title_char = urlencode($itunesU_title);
  209. $itunesU_crumbs = subURL($curled1, 'start breadcrumbs', 'end breadcrumbs');
  210. $itunesU_li = subURL($itunesU_crumbs, '<li>', '</li>');
  211. if (strpos($itunesU_li, 'iTunes U')) {
  212.  
  213. $feedURL = <<<ITUNESU
  214.   <div style="font-size:15px;white-space:normal;max-width:400px">iTunes-U links not supported.
  215.   <br>
  216.   <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:
  217.   <br>"$itunesU_title"
  218.   <br>
  219.   <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>
  220. ITUNESU;
  221. $invalidID = 'itunes-u';
  222. } //END if: itunesu_li
  223. } //END if: iTunes U
  224. if (!isset($invalidID)) {
  225.  
  226. $feedURL = subURL($curled1);
  227. checkFEED($feedURL, $url);
  228. if (isset($invalidURL) && $loopcount == 1) {
  229.  
  230. unset($invalidURL, $podid);
  231. sleep(2);
  232. } // END if: loopcount
  233. else {
  234.  
  235. break;
  236. } //END if: loopcount
  237. } //END if: invalidID
  238. else {
  239.  
  240. break;
  241. } //END if: invalidID
  242. } //END for: loopcount
  243.  
  244. if (!isset($failed_to_initialize_sqlite) && !isset($doNotCacheResults)) {
  245.  
  246. $newfeedURL = sqlite_escape_string($feedURL);
  247. $cache_query_put_content = "INSERT INTO {$_SETT['dbtable']} (uniqueid,url,date) VALUES ('$urlhash', '$newfeedURL', '".date("r")."'";
  248. @sqlite_exec($database, $cache_query_put_content);
  249. } //END if: failed sqlite
  250. } //END if: validID
  251. else {
  252.  
  253. if ($invalidID == "itunes-u") {
  254.  
  255. //Example URL: http://itunes.apple.com/itunes-u/the-civil-war-reconstruction/id341650730
  256. $itu_label = subURL($url, '/itunes-u/', '/');
  257. $itu_label_white = trim(ucwords(str_replace('-', ' ', $itu_label)));
  258. $itu_label_char = str_replace('-', '%20', $itu_label);
  259. $feedURL = <<<FEEDURL
  260.   <div style="font-size:15px;white-space:normal;max-width:400px">iTunes-U links not supported.
  261.   <br>
  262.   <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:
  263.   <br>"$itu_label_white"
  264.   <br>
  265.   <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>
  266. FEEDURL;
  267. } //END if: itunes-u
  268. else {
  269.  
  270. $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.";
  271. $invalidID = "I was doing fine. Noone was trying to murder me, or put me in a potato, or feed me to birds.";
  272. } //END if: itunes-u
  273. } //END if: validID
  274. } //END if: cache_file
  275. else {
  276.  
  277. $feedURL = $cache_file;
  278. if (strpos($feedURL, '<b>Empty podcast.</b>')) {
  279.  
  280. $invalidID = TRUE;
  281. $feedURL_explode = explode("\n", $feedURL);
  282. if (time() > (int)trim(end($feedURL_explode))) {
  283.  
  284. $delete_table_query = "DELETE FROM {$_SETT['dbtable']} WHERE uniqueid='$urlhash'";
  285. @sqlite_exec($database, $delete_table_query);
  286. header("Location: " . $_SERVER["SCRIPT_URI"] . "?" . $_SERVER["QUERY_STRING"]);
  287. } //END if: time
  288. else {
  289.  
  290. $feedURL = implode("\n", explode("\n", $feedURL, -1));
  291. } //END if: time
  292. } //END if: empty podcast
  293. elseif (stripos($feedURL, 'itunes-u')) {
  294.  
  295. $invalidID = 'itunes-u';
  296. } //END elseif: itunes-u
  297. else {
  298.  
  299. checkFEED($feedURL, $url, TRUE);
  300. } //END if: empty podcast
  301. } //END if: cache_file
  302.  
  303. //-----------------------------------------
  304. // html output to browser
  305. $podcastURL = (isset($invalidURL) || isset($invalidID)) ? ($invalidID == 'empty_podcast' ? $emptyPodcastText : $feedURL) : "<a href=\"$feedURL\">$feedURL</a>";
  306.  
  307. echo <<<OUTTEXT
  308. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  309.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  310.  
  311. <html xmlns="http://www.w3.org/1999/xhtml" style="height: 100%;">
  312.   <head>
  313.   <title>Podcast URL</title>
  314.   <meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
  315.   <meta name="robots" content="NOINDEX, NOFOLLOW, NOARCHIVE, NOSNIPPET">
  316.   <style type="text/css" media="screen">@import "iphonenav.css";</style>
  317.   <link rel="apple-touch-icon" href="./icon.png" />
  318.   <link rel="shortcut icon" href="./favicon.ico">
  319.   </head>
  320.  
  321.   <!-- GUI, iTunes Store searching & feed extraction credited to lasavior, so-nik.com -->
  322.   <!-- Original idea & inspiration credited to Michael Sitarzewski, zerologic.com -->
  323.   <!-- For the PHP code to extract the podcast feed yourself, visit http://snipplr.com/view/52465 -->
  324.  
  325.   <body style="height: 100%;">
  326.  
  327.   <h1>Podcast URL:</h1>
  328.   <ul>
  329.   <li style="height: 100%; padding:10px 10px 10px 10px; font-size:20px; word-wrap:break-word">$podcastURL</li>
  330.   <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>&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;<a href="http://podcasturlextractor.blogspot.com/" style="color:LightGray; font-size:12px; text-decoration:none;" target="_blank">News</a>&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;<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>
  331.   </ul>
  332.   </body>
  333. </html>
  334. OUTTEXT;
  335.  
  336. ?>

URL: http://itunes.so-nik.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.