Skip to content

Instantly share code, notes, and snippets.

@refutationalist
Last active Jun 16, 2016
Embed
What would you like to do?
Grabs and sends you the most recent ep of a podcast.
<?php
/*
* Look for the first podcast episode given an rss feed, then download it.
*
*
* This is pretty unsafe, as it'll grab any url handed to it.
*
* You can set feed by commenting the first line where it's defined and setting your
* own URL, but that still means that it'll download any file the supplied feed hands
* to it.
*
* There are 3 ways to use file:
* 1) Post this on your website, and set FEED below. It'll grab the most recent
* version of the podcast feed you set it to *only.* This is the "safer" way
* to do it.
* 2) If FEED is not set below, it'll parse any feed handed to it with the "feed"
* variable. Example: "http://my.site/curep.php?feed=http://my.podcast.feed/feed"
* 3) On the command line: curep.php -f http://my.podcast.feed -o episode.mp3
* This will grab the most recent ep on the feed and save it to episode.mp3.
* This is by far the safest method.
*
*/
define("FEED", ''); // Put feed between quotes.
if (!class_exists("DOMDocument")) {
echo "no dom. shucks.";
exit(1);
}
$via_web = false;
if (isset($_SERVER["REQUEST_URI"])) {
$via_web = true;
$feed = (FEED == '' ) ? $_REQUEST["feed"] : FEED;
} else {
$opts = getopt("f:o:");
$feed = $opts["f"];
}
$dom = new DOMDocument();
$dom->load($feed);
foreach ($dom->getElementsByTagName("item") as $item) {
$e = $item->getElementsByTagName("enclosure");
if ($e->length > 0) {
$mime = (string) $e->item(0)->getAttribute("type");
$url = (string) $e->item(0)->getAttribute("url");
$fname = basename($url);
break;
}
}
if (!isset($url)) {
echo "no podcasts.";
exit(2);
}
if ($via_web) {
header("Content-Type: $mime");
header("Content-disposition: filename='$fname'");
echo file_get_contents($url);
} else {
file_put_contents($opts["o"], file_get_contents($url));
}
// Sam Mulvey, 2016, public domain if it even matters.
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment