xmltv

 view release on metacpan or  search on metacpan

grab/Grab_XML.pm  view on Meta::CPAN

    my $pkg = shift;
    my $d = shift; $d =~ /^\d{8}$/ or die;
    my $p = parse_date($d);
    my $n = DateCalc($p, '+ 1 day'); die if not defined $n;
    return UnixDate($n, '%Q');
}

=item XMLTV::Grab_XML->country()

Return the name of the country youE<39>re grabbing for, used in usage
messages.  Abstract.

=cut
sub country( $ ) {
    my $pkg = shift;
    die 'abstract class method: override in subclass';
}

=item XMLTV::Grab_XML->usage_msg()

Return a command-line usage message.  This calls C<country()>, so you
probably need to override only that method.

=cut
sub usage_msg( $ ) {
    my $pkg = shift;
    my $country = $pkg->country();
    return <<END
$0: get $country television listings in XMLTV format
usage: $0 [--help] [--output FILE] [--days N] [--offset N] [--quiet]
END
      ;
}

=item XMLTV::Grab_XML->get()

Given a URL, fetch the content at that URL.  The default
implementation calls XMLTV::Get_nice::get_nice() but you might want to
override it if you need to do wacky things with http requests, like
cookies.

Note that while this method fetches a page, C<xml_from_data()> does
any further processing of the result to turn it into XML.

=cut
sub get( $$ ) {
    my $pkg = shift;
    my $url = shift;
    return XMLTV::Get_nice::get_nice($url);
}

=item XMLTV::Grab_XML->go()

The main program.  Parse command line options, fetch and write data.

Most of the options are fairly self-explanatory but this routine also
calls the XMLTV::Memoize module to look for a B<--cache> argument.
The functions memoized are those given by the C<cachables()> method.

=cut
sub go( $ ) {
    my $pkg = shift;
    XMLTV::Memoize::check_argv($pkg->cachables());
    my ($opt_days,
	$opt_help,
	$opt_output,
	$opt_offset,
	$opt_quiet,
	$opt_configure,
	$opt_config_file,
       );
    $opt_offset = 0;		# default
    $opt_quiet = 0;		# default
    GetOptions('days=i'        => \$opt_days,
	       'help'          => \$opt_help,
	       'output=s'      => \$opt_output,
	       'offset=i'      => \$opt_offset,
	       'quiet'         => \$opt_quiet,
	       'configure'     => \$opt_configure,
	       'config-file=s' => \$opt_config_file,
	      )
      or usage(0, $pkg->usage_msg());
    die 'number of days must not be negative'
      if (defined $opt_days && $opt_days < 0);
    usage(1, $pkg->usage_msg()) if $opt_help;
    usage(0, $pkg->usage_msg()) if @ARGV;
    if ($opt_configure) {
	print STDERR "no configuration necessary\n";
	exit();
    }
    for ($opt_config_file) {
	warn "this grabber has no configuration, so ignoring --config-file $_\n"
	  if defined;
    }

    # Need to call parse_local_date() before any resetting of
    # Date::Manip's timezone.
    #
    my $now = DateCalc(parse_local_date('now'), "$opt_offset days");
    die if not defined $now;
    $pkg->date_init();
    my $today = UnixDate($now, '%Q');

    my %urls = $pkg->urls_by_date();
    t 'URLs by date: ' . d \%urls;

    my @to_get;
    my $days_left = $opt_days;
    t '$days_left starts at ' . d $days_left;
    t '$today=' . d $today;
    for (my $day = $today; defined $urls{$day}; $day = $pkg->nextday($day)) {
	t "\$urls{$day}=" . d $urls{$day};
	if (defined $days_left and $days_left-- == 0) {
	    t 'got to last day';
	    last;
	}
	push @to_get, $urls{$day};
    }
    if (defined $days_left and $days_left > 0) {
	warn "couldn't get all of $opt_days days, only "
	  . ($opt_days - $days_left) . "\n";



( run in 2.682 seconds using v1.01-cache-2.11-cpan-364913b4093 )