#!/usr/bin/perl -w # this is a cgi-bin wrapper around get_iplayer and provides a mainly point # and click interface for get_iplayer http://linuxcentre.net/getiplayer # it's still pretty crude but works well on the authors's linux home file # server. it's kind of grown from a very short simple script I knocked up # one friday evening. I hacked on a lot of new functionality specifically # because I bought a PS3 and it won't play flv directly. # you'll need to download and unpack the JFW flash video player if you # want to use this script to make the downloaded iplayer flv's be playable # through a web browser - find it at # http://www.longtailvideo.com/players/jw-flv-player/ use strict; use CGI qw(:standard); use URI::Escape; use POSIX ":sys_wait_h"; use FileHandle; my $dbgLevel = 0; my $p_func= 'downloaded'; # minimise delays writing to the browser $| = 1; STDERR->autoflush; # already unbuffered in stdio my $smbLocation = '\\\\myserver\\iplayer'; my $baseURL = 'http://myserver.co.uk/iplayer'; my $cgiWrapper = 'cgi_get_iplayer.pl'; my $iplayerDir = '/home/iplayer'; my $getIplayer = '/srv/www/cgi-bin/get_iplayer --proxy=http://192.168.29.1:3128'; my $urlFlv = '/jfwflv'; # prefix to the url where the JFW FLV player was unpacked my $listerTv = $getIplayer . ' -l'; my $listerRadio = $getIplayer . ' -l --type radio'; my $updater = $getIplayer . ' --nopurge --refresh --tree --type radio,tv'; my $downloader = $getIplayer . ' --nopurge --flvstreamer /usr/local/bin/flvstreamer-1.7_fedora --raw -g'; #my $downloader = $getIplayer . ' --pid'; ############################################################################### sub debugPrint { my ($level, $dbgText) = @_; print "Debug: $dbgText\n
\n" if ($level < $dbgLevel); } ############################################################################### sub runDownload { my ($index, $mode) = @_; my $cmd = "$downloader --modes $mode '$index'"; my $pid = fork(); if (!$pid) { $| = 1; # stdout is now a pipe, make it unbuffered too print "Attempting to execute command $cmd\n
\n"; if (open(FH, "$cmd 2>&1 &|")) { $| = 1; #make the pipe output unbuffered too print "
";
			while()	# read output from the background process
			{
				print $_;
				flush STDOUT;
			}
			print "
"; } else { print "Failed executing command $cmd, error $?\n
\n"; } exit; } else { print "Downloading in background, process ID $pid
\n"; my $kid; do { print "...please wait...
\n"; system("ps -ef | grep '$downloader' | grep -v grep"); #system("ls -la $iplayerDir/*$bbcurl*"); print "\n
\n"; $kid = waitpid(-1,WNOHANG); sleep 10; flush STDOUT; } while $kid == 0; print "
\nback to main page\n"; } } ############################################################################### sub updateListings { my $cmd = "$updater"; print "

Updating list of available programs

\n"; debugPrint(2, "Debug: using command $cmd\n
\n"); if (open(FH, "$cmd 2>&1 &|")) { $| = 1; #make the pipe output unbuffered too print "
";
		while()	# read output from the background process
		{
			print $_;
			flush STDOUT;
		}
		print "
"; } else { print "Failed executing command $cmd, error $?\n
\n"; } } ############################################################################### sub showFlvPlayer { my ($p_file) = @_; print<

Get Flash to see this player.

EOF } ############################################################################### sub showActivity { print "here's the current activity\n
";
	system ("ps -efwl | grep '$downloader' | grep -v grep");
	print "
\n"; } ############################################################################### sub showDownloaded { chdir $iplayerDir; print "

Downloaded Programs Ready To Play

\n
\n
";
	my @files = <*>;
	foreach my $file (@files)
	{
		if ($file =~ /.*\.flv$/)
		{
			print "Play on PS3  ";
		}
		else
		{
			print "             ";
		}

		print "Play On PC ";

		print $file . "\n";
	}
	print "
\n"; } ############################################################################### sub rewriteAvailableLineToDownloadLink { my ($availLine) = @_; if ($availLine =~ /^(\d+):\s+(.*)$/) { print "  " . $2; } else { print "    " . $availLine; } print "\n"; } ############################################################################### sub showRadioListing { print "
\n

Radio Listing

\n"; my $cmd = "$listerRadio"; if (open(FH, "$cmd 2>&1 &|")) { $| = 1; #make the pipe output unbuffered too print "
";
		while()	# read output from the background process
		{
			chomp;
			rewriteAvailableLineToDownloadLink($_);
			flush STDOUT;
		}
		print "
"; } else { print "Failed executing command $cmd, error $?\n
\n"; } } ############################################################################### sub showTvListing { print "

TV Listing

\n"; my $cmd = "$listerTv"; if (open(FH, "$cmd 2>&1 &|")) { $| = 1; #make the pipe output unbuffered too print "
";
		while()	# read output from the background process
		{
			chomp;
			rewriteAvailableLineToDownloadLink($_);
			flush STDOUT;
		}
		print "
"; } else { print "Failed executing command $cmd, error $?\n
\n"; } } ############################################################################### sub showAvailable { print "
\n\n"; #print "Enter URL: "; print "For playing on the playstation, use the default mode of flashvhigh. If downloading for a media player then use iphone.\n
\nIf flashvhigh mode fails because the program wasn't available in that mode then try flashhigh next.\n
\n"; print "\n
\n"; print "Either click button below or enter number to download:  "; print "\n"; showTvListing(); print "
\n
\n"; showRadioListing(); print "
\n"; } ############################################################################### $p_func = param('func') if (defined (param('func'))); print "Content-type: text/html\n\n" . "iplayer downloader wrapper\n" . "\n\n"; debugPrint(2, "p_func is " . $p_func ); $ENV{HOME} = '/var/lib/wwwrun'; if ($p_func ne 'flvPlay') { print "show available programs to download\n
" if ($p_func ne 'available'); print "show programs already downloaded\n
" if ($p_func ne 'downloaded'); print "update list of available programmes\n
" if ($p_func ne 'update'); print "
Note: the directory where the files are is $smbLocation - paste this into windows explorer
\n
\n"; } if ($p_func eq 'available') { showAvailable(); } elsif ($p_func eq 'downloaded') { showDownloaded(); } elsif ($p_func eq 'update') { updateListings(); } elsif ($p_func eq 'download') { chdir $iplayerDir; my $index = ''; my $mode = defined param('mode') ? param('mode') : 'flashvhigh'; if (defined(param('index'))) { $index = param('index'); } elsif (defined(param('altIndex'))) { $index = param('altIndex'); } if ($index ne '') { runDownload($index, $mode); } } elsif ($p_func eq 'flvPlay') { if (defined(param('file') )) { my $p_file = param('file'); showFlvPlayer($p_file); } } else { print "unknown function \"$p_func\"!

back to main page\n"; } #if (($p_func eq "list")) # | ($p_func eq "update")) #{ print "
\n"; # showDownloaded(); # #print "
\n"; #showActivity(); #} print "\n\n";