#!perl -w
my $title = "TVListings";
my $version = "v3.0.0 03/12/01";
use strict;
## use diagnostics;
################################################################
##    tvlistings.pl
##
##    TVListings is a small web transcoder utility that pulls up to 7
##    days of UK TV listings from pages on the net (currently
##    ananova.com) into simple HTML pages suitable for
##    further conversion for mobile devices or what-have-you.
##
##    CHANGES
##
##   v3.0.0 03/12/01
##   * Rewrite for new Ananova.com data source
##

use LWP::Simple;
use Getopt::Long;

# Some hot handles (not really needed)...
select((select(STDOUT), $| = 1)[0]);
select((select(STDERR), $| = 1)[0]);

msgout("$title - $version");

sub usage
{
   print STDOUT <<"EOUSAGE";
      -help -- print this!
      -days=7 -- number of days' listings to get (default=1 max=7)
      -pagedir=./pages -- drop docs in this dir
EOUSAGE
}

## The URL components...
my $root = 'http://www.ananova.com/tv_listings/tv_mainlisting.html?';
# my $root = 'http://195.92.245.210/tv_listings/tv_mainlisting.html?';
## Default settings...
my $pagedir = ".";  # <-- drop files in this dir
my $numdays = 7; # <-- just get today by default
## Abstract message output interface...
sub msgout
{
   my $text = "@_";
   print STDOUT "$text\n";
}
## Read command line args...
&readArgs();

my $now = time;
## Loop days...
for (my $daynum = 1; $daynum <= $numdays; $daynum++)
{
   ## Get the date for the day in question...
   my ($date, $mcdate);
   {
      my $epoch = $now + (($daynum - 1) * 60 * 60 * 24);
      my( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime($epoch);
      $mon++;
      $year-=100;
      $wday = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat')[$wday];
      $date = sprintf "%02d/%02d/%02d %s", $mday, $mon, $year, $wday;
      $mcdate = sprintf "%02d%02d%02d_%s", $year, $mon, $mday , $wday;
      msgout("day $daynum of $numdays, date = $date ($mcdate)");
   }

## all channels on one page
## http://www.ananova.com/tv_listings/tv_mainlisting.html?
## day=day1&start=Start&end=End&tvregion=mid_3&or=mid_3&i=1&p=0&h=
## &c=bbc1_mid_3.bbc2_mid_3.central_mid_3.ch4_mid_3.ch5_mid_3
## .radio4.britisheurosport.family_channeldigital.discoverydigital
## .e4.mtv.nickelodeonddigital.paramount.ukplay.sky_channel.vh1
## &S=1&h=1
my @newchans = qw{
bbc1_mid_3
bbc2_mid_3
central_mid_3
ch4_mid_3
ch5_mid_3
radio4
britisheurosport
family_channeldigital
discoverydigital
e4
mtv
nickelodeonddigital
paramount
ukplay
sky_channel
vh1
};

## Form URL...
my $url = $root.'day=day'.$daynum
.'&start=Start&end=End&tvregion=mid_3&or=mid_3'
.'&i=1&p=0&h=&c='
. join('.', @newchans)
.'&S=1&h=1';
## Form output filename...
my $outfile = "$pagedir/$mcdate.html";
msgout("   Getting URL = '$url'.");
msgout("   filename = '$outfile'.");
my $content;
unless (defined ($content = get $url)) {
    msgout("   Error could not get '$url'.");
    next;
}
unless( open OUTFILE, ">$outfile" ) {
    msgout("   Error could not open '$outfile' for write: '$!'.");
    next;
}
# Print a header...
print OUTFILE $content;
close OUTFILE;
# OK!
msgout("   OK.");
} #<-- end of loop
## End of main.

## Read command line args...
sub readArgs
{
   my ($clHelp, $clDays, $clpagedir);
   GetOptions(
      "help|?" => \$clHelp,
      "days=i"  => \$clDays,
      "pagedir=s" => \$clpagedir,
   );
   if( $clHelp ) {
      usage();
      exit 0;
   }
   if( $clDays ) {
      if( $clDays >= 1 && $clDays <= 7 ) {
         $numdays = $clDays;
      } else {
         msgout("Warning: invalid arg for days: '$clDays'");
      }
   }
   if( $clpagedir ){
      msgout("Using dir for created pages '$clpagedir'.");
      $pagedir = $clpagedir;
   }

}

__END__