Setup Automatic podcast download

From LinuxMCE
Revision as of 03:34, 25 August 2009 by Ray N (Talk | contribs)

(diff) ←Older revision | view current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

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.

Steps to follow

In this example I will set up automatic downloads of two podcasts. Security Now and This week in Tech. At 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 cron 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

Step 4 - Add your own podcasts

When I created the downlaod for security now and changed it to twit not many changes were required. You might be lucky and in the same boat. Or you may have to do an couple of changes depending on how the server produces the xml file for the RSS feed.

Step 4a - Find out the URL to get the xml file for the feed

Most feeds will give you the url. You need to find this out and note it down

Step 4b - Copy a current php file to a new one for your new podcast

Logged into the console goto the podcasts directory created eailier and type:

cd /home/public/data/audio/podcasts/
cp get_twit.php get_<<NAME_OF_PODCAST>>.php

(Make sure you have the .php extension)

Step 4c - Create a directory for the files

mkdir <<NAME_OF_PODCAST>>

Step 4d - Edit the PHP file

Edit the new file. I did this by typing:

nano <<NAME_OF_PODCAST>>.php

Do changes to the file as follows: Line 6 -> Change the message to user e.g.

	print "Starting to get the <<NAME_OF_PODCAST>> POD Casts\n";

Line 8 -> Change to point to the directory you created in 4c

	$fp = "/home/public/data/audio/podcasts/<<NAME_OF_PODCAST>>/";

Line 10 -> Change to point to the url you have found

	$request_url = "http://leoville.tv/podcasts/twit.xml";

(Change leiville.tv to the new url)

Step 4e - Edit the PHP file

Test the file by typing:

php get_<<NAME_OF_PODCAST>>.php

Check the directory you created. If the files have downloaded then you are lucky and no further php changes are needed. Otherwise you need to change lines 13 and 16 to new values. This is because the xml file for the podcast has a diffrent format.

Step 4f - Add to script

Finally add a line to get_pods.sh to download the new podcast.

php /home/public/data/audio/podcasts/get_twit.php

Step 5 - Remove the demo podcasts

You may not want security now and twit podcasts to be downloaded. You can stop these downloading by removing the releavant lines in the get_pods.sh file. You may also want to delete their podcast directroies to remove all the old files. (/home/public/data/audio/podcasts/security_now/ and /home/public/data/audio/podcasts/twit/)