#!/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("; } else { print "Failed executing command $cmd, error $?\n) # read output from the background process { print $_; flush STDOUT; } print "
"; while("; } else { print "Failed executing command $cmd, error $?\n) # read output from the background process { print $_; flush STDOUT; } print "
";
system ("ps -efwl | grep '$downloader' | grep -v grep");
print "\n";
}
###############################################################################
sub showDownloaded
{
chdir $iplayerDir;
print "";
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 ""; while("; } else { print "Failed executing command $cmd, error $?\n) # read output from the background process { chomp; rewriteAvailableLineToDownloadLink($_); flush STDOUT; } print "
"; while("; } else { print "Failed executing command $cmd, error $?\n) # read output from the background process { chomp; rewriteAvailableLineToDownloadLink($_); flush STDOUT; } print "