#!/usr/bin/perl -w

# details of jrobbo's tv guide data is posted at
#    http://wiki.dvbowners.com/index.php?title=JRobbo's_EPG_Guides
# the actual guides are at http://www.jrobbo.com/dwxmltv/<name>.zip

use strict;

my $progname = "jrobbo";
my $version = "0.02";

use LWP::UserAgent;
use Getopt::Long;
use Cwd;
$| = 1;

#
# table mapping 'region_id' to guidenames
#
my @guidename;
# VIC
$guidename[93] = "Geelong",		$guidename[94] = "Melbourne",		$guidename[95] = "MilduraSunraysia";
$guidename[90] = "EasternVictoria", 	$guidename[98] = "WesternVictoria";
# NSW
$guidename[73] = "Sydney",		$guidename[66] = "CentralCoastNSW",	$guidename[67] = "Griffith";
$guidename[63] = "BrokenHill",		$guidename[69] = "NorthernNSW",		$guidename[71] = "SouthernNSW";
$guidename[106] = "RemoteCentralNSW",	$guidename[184] = "Newcastle";
# QLD
$guidename[75] = "Brisbane",		$guidename[78] = "GoldCoast",		$guidename[79] = "RegionalQLD";
$guidename[114] = "RemoteCentralQLD";
# WA
$guidename[101] = "Perth",		$guidename[102] = "RegionalWA";
# SA
$guidename[81] = "Adelaide", 		$guidename[82] = "Renmark",		$guidename[83] = "Riverland";
$guidename[85] = "SouthEastSA",		$guidename[86] = "SpencerGulf",		$guidename[107] = "RemoteCentralSA";
$guidename[74] = "Darwin",		$guidename[108] = "RemoteCentralNT";	# NT
$guidename[73] = "ACT";		# ACT
$guidename[88] = "Hobart";	# TAS

# default settings
my $opt = { };
my $channels;
$opt->{channels_file} =		"";
$opt->{outputfile} =		cwd() . "/output.xmltv";
$opt->{downloadfile} =		cwd() . "/last_jrobbo_download.zip";
$opt->{region} =		94;

GetOptions(
	'region=i'	=> \$opt->{region},
	'days=i'	=> \$opt->{days},		# ignored
	'offset=i'	=> \$opt->{offset},		# ignored
	'timezone=s'	=> \$opt->{timezone},		# ignored
	'channels_file=s' => \$opt->{channels_file},	# ignored
	'output=s'	=> \$opt->{outputfile},
	'downloadfile=s' => \$opt->{downloadfile},
	'nowarper'	=> \$opt->{nowarper},
	'help'		=> \$opt->{help},
	'version'	=> \$opt->{version},
	'v'		=> \$opt->{version},
	'ready'		=> \$opt->{version},
	'desc'		=> \$opt->{desc});

printf "%s %s\n",$progname,$version;
exit(0) if ($opt->{version});
if ($opt->{desc}) {
	printf "%s grabs tvguide data from jrobbo's website.  see http://wiki.dvbowners.com/index.php?title=JRobbo's_EPG_Guides for details\n",$progname;
	exit(0);
}
if ($opt->{help}) {
	printf "\noptions as follows:\n";
	printf "   --channels_file=<file> channels file (mandatory)\n";
	printf "   --region=<i>           region as per the table below (default $opt->{region})\n";
	printf "   --output=<file>        file to send output to (default $opt->{outputfile})\n";
	printf "   --downloadfile=<file>  file to send output to (default $opt->{downloadfile})\n";
	printf "   --nowarper              don't fetch via webwarper\n";
	printf "\n";
	printf "  regions are as follows:\n";
	my $count = 0;
	foreach my $i (1..1000) {
		if (defined $guidename[$i]) {
			$count++;
			printf "\t%d=%-15s%s",$i,$guidename[$i],(($count % 4) == 0) ? "\n" : "";
		}
	}
	printf "\n";
	exit(0);
}

die "Invalid region $opt->region specified; see --help for list of valid regions.\n" if (!defined $guidename[($opt->{region})]);
die "no channel file specified, see --help for instructions\n", if ($opt->{channels_file} eq "");

my $url = sprintf "http://www.jrobbo.com/dwxmltv/xmltv%s.zip",$guidename[($opt->{region})];

#
# go!
#

# read channels file
if (-r $opt->{channels_file}) {
	local (@ARGV, $/) = ($opt->{channels_file});
	no warnings 'all'; eval <>; die "$@" if $@;
} else {
	die "channels file $opt->{channels_file} could not be read: $!\n";
}

printf "Fetching %s %s...\n",$url,($opt->{nowarper} ? "direct" : "via webwarper");
$url =~ s#^http://#http://webwarper.net/ww/# unless $opt->{nowarper};

my $agent = (
	'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)',
	'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4',
	'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.6) Gecko/20050512 Firefox',
	'Opera/9.00 (Windows NT 5.1; U; en)',
	'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/412 (KHTML, like Gecko) Safari/412'
	)[int(rand(5))];
my $ua = LWP::UserAgent->new('timeout' => 30, 'keep_alive' => 1, 'agent' => $agent);
$ua->env_proxy;
$ua->cookie_jar({});

my $request = HTTP::Request->new(GET => $url);
my $randomaddr = sprintf "203.%d.%d.%d",rand(255),rand(255),(rand(254)+1);
$request->header('Via' => '1.0 proxy:81 (Squid/2.3.STABLE3)');
$request->header('X-Forwarded-For' => $randomaddr);
my $response;

for (1..3) {
	$response = $ua->request($request);
	last if ($response->is_success);
	printf "Failed to fetch $url!\n";
	sleep 10;
}
die "Failed to fetch $url after 3 retries!\n" if (!($response->is_success));

open(F,">$opt->{downloadfile}") || die "can't open $opt->{downloadfile} for writing: $!\n";
print F $response->content;
close(F);

my $inputpipe = "/usr/bin/unzip -p $opt->{downloadfile} |";
open(INFILE,"$inputpipe") || die "can't open $inputpipe for reading: $!\n";
open(OUTFILE,">$opt->{outputfile}") || die "can't open $opt->{outputfile} for writing: $!\n";

while(<INFILE>) {
	chop;
	my $input_line = $_;

	if ($input_line =~ /^(.*)(channel|id)="([a-z0-9A-Z ]+)"(.*)$/) {
		# substitute channel as per our standards in channels.conf ..
		my ($before_match, $id_part, $chan, $after_match) = ($1,$2,$3,$4);
		my $newchan = $chan;
		if ($chan =~ /^ABC /)			{ $newchan = $channels->{"ABC"}; }
		elsif ($chan =~ /^Channel Seven/)	{ $newchan = $channels->{"Seven"}; }
		elsif ($chan =~ /^Prime/)		{ $newchan = $channels->{"Seven"}; }
		elsif ($chan =~ /^Channel Nine/)	{ $newchan = $channels->{"Nine"}; }
		elsif ($chan =~ /^WIN/)			{ $newchan = $channels->{"Nine"}; }
		elsif ($chan =~ /^Channel Ten/)		{ $newchan = $channels->{"TEN"}; }
		elsif ($chan =~ /^Network TEN/)		{ $newchan = $channels->{"TEN"}; }
		elsif ($chan =~ /^Southern Cross/)	{ $newchan = $channels->{"TEN"}; }
		elsif ($chan =~ /^SBS News/)		{ $newchan = $channels->{"SBS News"}; }
		elsif ($chan =~ /^SBS /)		{ $newchan = $channels->{"SBS"}; }
		$input_line = $before_match.$id_part."='".$newchan."'".$after_match;
	}

	print OUTFILE $input_line."\n";
}

close(INFILE);
close(OUTFILE);

printf "All done, output in $opt->{outputfile}.\n";
exit(0);

