#!/usr/bin/perl -w eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}' if 0; # not running under some shell =pod =head1 NAME tv_grab_au - Grab TV listings for Australia (oztivo.net) =head1 SYNOPSIS tv_grab_au --help tv_grab_au --configure [--config-file FILE] [--gui OPTION] tv_grab_au [--config-file FILE] [--days N] [--offset N] [--output FILE] [--quiet] [--debug] tv_grab_au --list-channels [--config-file FILE] [--output FILE] [--quiet] [--debug] =head1 DESCRIPTION Output TV and listings in XMLTV format for many stations available in Australia. First you must run B to choose which stations you want to receive. Then running B with no arguments will get a listings for the stations you chose for five days including today. =head1 OPTIONS B<--configure> Prompt for which stations to download and write the configuration file. B<--config-file FILE> Set the name of the configuration file, the default is B<~/.xmltv/tv_grab_au.conf>. This is the file written by B<--configure> and read when grabbing. B<--gui OPTION> Use this option to enable a graphical interface to be used. OPTION may be 'Tk', or left blank for the best available choice. Additional allowed values of OPTION are 'Term' for normal terminal output (default) and 'TermNoProgressBar' to disable the use of Term::ProgressBar. B<--output FILE> When grabbing, write output to FILE rather than standard output. B<--days N> When grabbing, grab N days rather than 5. B<--offset N> Start grabbing at today + N days. N may be negative. B<--quiet> Suppress the progress-bar normally shown on standard error. B<--debug> Provide more information on progress to stderr to help in debugging. B<--list-channels> Output a list of all channels that data is available for. The list is in xmltv-format. B<--version> Show the version of the grabber. B<--help> Print a help message and exit. =head1 ERROR HANDLING If the grabber fails to download data for some channel on a specific day, it will print an errormessage to STDERR and then continue with the other channels and days. The grabber will exit with a status code of 1 to indicate that the data is incomplete. =head1 ENVIRONMENT VARIABLES The environment variable HOME can be set to change where configuration files are stored. All configuration is stored in $HOME/.xmltv/. On Windows, it might be necessary to set HOME to a path without spaces in it. =head1 SUPPORTED CHANNELS For information on supported channels, see http://www.oztivo.net/xmltv/channels.xml.gz =head1 AUTHOR Mike Williams, mike -at- better-access -dot- com. This documentation and parts of the code copied from tv_grab_se_swedb by Mattias Holmlund =head1 BUGS =cut use strict; use XMLTV; use XMLTV::ProgressBar; use XMLTV::Options qw/ParseOptions/; use XMLTV::Configure::Writer; use XML::LibXML; use Date::Manip; use Compress::Zlib; use File::Path; use File::Basename; use LWP::Simple qw($ua get); $ua->agent("xmltv/$XMLTV::VERSION"); use HTTP::Cache::Transparent; # Although we use HTTP::Cache::Transparent, this undocumented --cache # option for debugging is still useful since it will _always_ use a # cached copy of a page, without contacting the server at all. # use XMLTV::Memoize; XMLTV::Memoize::check_argv('get'); sub t; my $default_root_url = 'http://www.oztivo.net/xmltv/channels.xml.gz'; my $default_cachedir = get_default_cachedir(); my( $opt, $conf ) = ParseOptions( { grabber_name => "tv_grab_au", capabilities => [qw/baseline manualconfig tkconfig apiconfig cache/], stage_sub => \&config_stage, listchannels_sub => \&list_channels, load_old_config_sub => \&load_old_config, version => '$Id: tv_grab_au.in,v 1.2 2008/03/14 09:45:00 mikewilliams Exp $', description => "Australia (oztivo.net)", } ); if (not defined( $conf->{username}->[0] )) { print STDERR "No username defined.\n" . "Please run the grabber with --configure.\n"; exit 1; } if (not defined( $conf->{password}->[0] )) { print STDERR "No password defined.\n" . "Please run the grabber with --configure.\n"; exit 1; } if (not defined( $conf->{cachedir} )) { print STDERR "No cachedir defined.\n" . "Please run the grabber with --configure.\n"; exit 1; } if (not defined( $conf->{'root-url'} )) { print STDERR "No root-url defined.\n" . "Please run the grabber with --configure.\n"; exit 1; } init_cachedir( $conf->{cachedir}->[0] ); HTTP::Cache::Transparent::init( { BasePath => $conf->{cachedir}->[0], NoUpdate => 15*60, Verbose => $opt->{debug}, } ); my($xmldecl, $channels) = load_channels( $conf->{'root-url'}->[0] ); my( $odoc, $root ); my $warnings = 0; write_header( $xmldecl ); write_channel_list( $conf->{channel} ); my $now = ParseDate( 'now' ); my $date =$now; $date = DateCalc( $now, "+$opt->{offset} days" ) if( $opt->{offset} ); my $bar = undef; $bar = new XMLTV::ProgressBar( { name => 'downloading listings', count => $opt->{days} * @{$conf->{channel}}, }) if (not $opt->{quiet}) && (not $opt->{debug}); for( my $i=0; $i < $opt->{days}; $i++ ) { t "Date: $date"; foreach my $channel_id (@{$conf->{channel}}) { # We have already warned the user if the channel doesn't exist. if( exists $channels->{$channel_id} ) { t " $channel_id"; my( $channel_name, $url ) = @{$channels->{$channel_id}}; print_data( $url, $channel_id, $date ) or warning( "Failed to download data for $channel_id on " . UnixDate( $date, "%Y-%m-%d" ) . "." ); } $bar->update() if defined( $bar ); } $date = DateCalc( $date, "+1 days" ); } $bar->finish() if defined $bar; write_footer(); # Signal that something went wrong if there were warnings. exit(1) if $warnings; # All data fetched ok. t "Exiting without warnings."; exit(0); sub t { my( $message ) = @_; print STDERR $message . "\n" if $opt->{debug}; } sub warning { my( $message ) = @_; print STDERR $message . "\n"; $warnings++; } sub list_channels { my( $conf, $opt ) = @_; ( $xmldecl, $channels ) = load_channels( $conf->{'root-url'}->[0] ); my $result=""; my $fh = new IO::Scalar \$result; my $oldfh = select( $fh ); write_header( $xmldecl ); write_channel_list( [sort keys %{$channels}] ); write_footer(); select( $oldfh ); $fh->close(); return $result; } sub config_stage { my( $stage, $conf ) = @_; die "Unknown stage $stage" if $stage ne "start"; my $result; my $writer = new XMLTV::Configure::Writer( OUTPUT => \$result, encoding => 'iso-8859-1' ); $writer->start( { grabber => 'tv_grab_au' } ); $writer->write_string( { id => 'username', title => [ [ 'Username', 'en' ] ], description => [ [ 'Your username at oztivo. ', 'en' ] ], default => '', } ); $writer->write_string( { id => 'password', title => [ [ 'Password', 'en' ] ], description => [ [ 'Your password at oztivo. ', 'en' ] ], default => '', } ); $writer->write_string( { id => 'root-url', title => [ [ 'Root URL for grabbing data', 'en' ] ], description => [ [ 'The file at this URL describes which channels are available and ' . 'where data can be found for them. ', 'en' ] ], default => $default_root_url, } ); $writer->write_string( { id => 'cachedir', title => [ [ 'Directory to store the cache in', 'en' ] ], description => [ [ 'tv_grab_au uses a cache with files that it has already '. 'downloaded. Please specify where the cache shall be stored. ', 'en' ] ], default => $default_cachedir, } ); $writer->end( 'select-channels' ); return $result; } # # Load a configuration file in the old format. # sub load_old_config { my( $config_file ) = @_; my @lines = XMLTV::Config_file::read_lines( $config_file ); my $conf = {}; $conf->{cachedir}->[0] = $default_cachedir; $conf->{'root-url'}->[0] = $default_root_url; $conf->{channel} = []; foreach my $line (@lines) { next unless defined $line; my( $command, $param ) = split( /\s+/, $line, 2 ); $param =~ tr/\n\r//d; $param =~ s/\s+$//; if ( $command =~ /^\s*username\s*$/) { $conf->{'username'}->[0], $param; } elsif ( $command =~ /^\s*password\s*$/) { $conf->{'password'}->[0], $param; } elsif ( $command =~ /^\s*root-url\s*$/) { $conf->{'root-url'}->[0] = $param; } elsif ( $command =~ /^\s*channel\s*$/) { push @{$conf->{channel}}, $param; } elsif ( $command eq 'cache-dir' ) { $conf->{'cachedir'}->[0] = $param; } else { die "Unknown command $command in config-file $config_file" } } return $conf; } sub get_default_cachedir { my $winhome = $ENV{HOMEDRIVE} . $ENV{HOMEPATH} if defined( $ENV{HOMEDRIVE} ) and defined( $ENV{HOMEPATH} ); my $home = $ENV{HOME} || $winhome || "."; return "$home/.mythtv/cache"; } sub init_cachedir { my( $path ) = @_; if( not -d $path ) { mkpath( $path ) or die "Failed to create cache-directory $path: $@"; } } sub load_channels { my( $url ) = @_; my %channels; # my $compressed = get( $url ) # or die "Failed to fetch $url"; open urlHandle, "wget -O - --user=$conf->{'username'}->[0] --password=$conf->{'password'}->[0] http://www.oztivo.net/xmltv/channels.xml.gz |" or die $!; my @finLines = ; my $compressed = join("", @finLines); my $xmldata = Compress::Zlib::memGunzip( \$compressed ); my $xml = XML::LibXML->new; my $doc = $xml->parse_string($xmldata); my $xmldecl = "version() . "' " . "encoding='" . $doc->encoding() . "'?>\n"; my $ns = $doc->find( "//channel" ); foreach my $node ($ns->get_nodelist) { my $id = $node->findvalue( '@id' ); my $name = $node->findvalue( 'display-name[1]' ); # my $url = $node->findvalue( 'base-url' ); # had to hard code this: oztivo lists two urls, which # get catted, fails completely to fetch all listings my $url = "http://www.oztivo.net/xmltv/"; my $urlns = $node->find( './base-url' ); foreach my $urlnode ($urlns->get_nodelist) { $node->removeChild( $urlnode ); } $channels{$id} = [ $name, $url, $node->toString(0) ]; } return ($xmldecl, \%channels); } sub print_data { my( $rooturl, $channel_id, $date ) = @_; my $url = $rooturl . $channel_id . "_" . UnixDate( $date, "%Y-%m-%d" ) . ".xml.gz"; # my $compressed = get( $url ) # or return 0; open chanHandle, "wget -O - --user=$conf->{'username'}->[0] --password=$conf->{'password'}->[0] $url |" or die $!; my @chanLines = ; my $compressed = join("", @chanLines); my $xmldata = Compress::Zlib::memGunzip( \$compressed ); my $in = new IO::Scalar \$xmldata; while( my $line = $in->getline() ) { last if $line =~ /getline() ) { last if $line =~ /<\/tv>/; print $line; } return 1; } sub write_header { my( $xmldecl ) = @_; # Use the same xml declaration as the one in # channels.xml print $xmldecl; print '' . "\n"; print "\n"; } sub write_channel_list { my( $channel_list ) = @_; # Write list of channels. t 'Writing list of channels.'; foreach my $channel_id (@{$channel_list}) { if( not exists $channels->{$channel_id} ) { print STDERR "Unknown channel $channel_id." . " See http://oztivo.net/" . " for a list of available channels or run" . " tv_grab_au --configure to reconfigure.\n"; next; } my( $channel_name, $url, $def ) = @{$channels->{$channel_id}}; print " $def\n"; } } sub write_footer { print "\n"; }