Setup Automatic podcast download
NOTE TO READERS - I have written this article and this has worked for me. I am not a LinuxMCE expert. I suggest someone who is looks over this and removes this message if this method looks ok.
I listen to podcasts and I would like my core to automatically download podcasts when they become availible, for me to listen to later. I didn't want a whole RSS reader program, just something to downlaod podcasts, so I set this up using a php and a bash script. I needed to use php because it has features to phrase xml files. This enables it to read the RSS feed for the podcast. I used bash to create a simple script which cron could run.
Contents
Steps to follow
In this example I will set up automatic downloads of two podcases. Security Now and This week in Tech. After the end of the instruction set I show how you can add your own podcasts to this. This method checks if files are already downloaded and only downloads new ones.
Step 1 - Create directory
The first step is to create the relevant directories. I did this on my code. Log in as the linuxmce user cd /home/public/data/audio/ mkdir podcasts cd podcasts mkdir security_now mkdir twit
Step 2 - Create and test PHP catching script
I created two php files in the /home/public/data/audio/podcasts directory. The first to get the Security Now podcast called get_security_now.php:
<?PHP
function GetFileName($p) { return substr($p,strrpos($p,"/")+1); };
print "Starting to get the security now POD Casts\n";
$fp = "/home/public/data/audio/podcasts/security_now/";
$request_url = "http://leoville.tv/podcasts/sn.xml"; $xml = simplexml_load_file($request_url) or die("feed not loading");
foreach($xml->channel->item as $item){
print "Title:" . $item->title . "\n"; // print "Link:" . $item->link . "\n";
$fn = $fp . GetFileName($item->link); if (file_exists($fn)) { print " Already Downloaded\n"; } else { $ds = "wget -P". $fp . " '" . $item->link . "'"; $foo = system($ds,$output); print " Downlaoded\n"; }; // print "FileName:$fn\n"; } print "\n";
// var_dump($xml->channel->item);
print "End of get_pod.php\n\n";
?>
The second to get the TWIT podcast called get_twit.php:
<?PHP
function GetFileName($p) { return substr($p,strrpos($p,"/")+1); };
print "Starting to get the TWIT (This week in Tech) POD Casts\n";
$fp = "/home/public/data/audio/podcasts/twit/";
$request_url = "http://leoville.tv/podcasts/twit.xml"; $xml = simplexml_load_file($request_url) or die("feed not loading");
foreach($xml->channel->item as $item){
print "Title:" . $item->title . "\n"; // print "Link:" . $item->link . "\n";
$fn = $fp . GetFileName($item->link); if (file_exists($fn)) { print " Already Downloaded\n"; } else { $ds = "wget -P". $fp . " '" . $item->link . "'"; $foo = system($ds,$output); print " Downlaoded\n"; }; // print "FileName:$fn\n"; // die("$ds\nTest End\n"); } print "\n";
// var_dump($xml->channel->item);
print "End of get_pod.php\n\n";
?>
You can test these scripts. Type "php /home/public/data/audio/podcasts/get_security_now.php" When this is done goto the security_now directory and check the files have appeared Simularly "php /home/public/data/audio/podcasts/get_twit.php" should put the files into the twit directory.
Step 3 - Make corn check for new items daily
Now you can manually type these commands at the prompt to download new items but it would be nice for this to happen automatically. To do this you can add this as a daily cron task. First create a bash script called get_pods.sh into the /home/public/data/audio/podcasts/ directory:
#!/bin/sh php /home/public/data/audio/podcasts/get_security_now.php php /home/public/data/audio/podcasts/get_twit.php
Once you have created this file type the following command to make it executable:
chmod 777 /home/public/data/audio/podcasts/get_pods.sh
This allows cron to use it. Finally create a link to this in the cron.daily folder to make this run daily:
sudo ln -s /home/public/data/audio/podcasts/get_pods.sh /etc/cron.daily/rjm_get_podcasts