#!/usr/bin/perl -w

=pod

=head1 NAME

tv_grab_cz - Grab TV listings for the Czech Republic.

=head1 SYNOPSIS

tv_grab_cz --help

tv_grab_cz --configure [--config-file FILE]

tv_grab_cz [--config-file FILE]
                 [--days N] [--offset N] [--channel xmltvid,xmltvid,...]
                 [--output FILE] [--quiet] [--debug]

tv_grab_cz --list-channels [--config-file FILE]
                 [--output FILE] [--quiet] [--debug]


=head1 DESCRIPTION

Output TV and listings in XMLTV format for many stations
available in the Czech Republic. Data is downloaded from WebStep, s.r.o.
without any legal WARRANTY.

First you must run B<tv_grab_cz --configure> to choose which stations
you want to receive.

Then running B<tv_grab_cz> 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_cz.conf>.  This is the file written by
B<--configure> and read when grabbing.

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.

B<--quiet> Only print error-messages on STDERR.

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<--capabilities> Show which capabilities the grabber supports. For more
information, see L<http://wiki.xmltv.org/index.php/XmltvCapabilities>

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 from webstep, it will print an
errormessage to STDERR and then exit with a status code of 1 to indicate
that the data is missing.

=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 CREDITS

Grabber written by Mattias Holmlund, mattias -at- holmlund -dot- se.
This documentation copied from tv_grab_uk by Ed Avis,
ed -at- membled -dot- com. Original grabber by Jiri Kaderavek,
jiri -dot- kaderavek -at- webstep -dot- net with modifications by
Petr Stehlik, pstehlik -at- sophics -dot- cz.

Data from Webstep by Jiri Kaderavek,
jiri -dot- kaderavek -at- webstep -dot- net.

=head1 BUGS

None known.

=cut

use strict;
use XMLTV;
use XMLTV::Configure::Writer;
use XMLTV::Options qw/ParseOptions/;
use LWP::Simple qw/$ua get getprint is_success status_message/;

$ua->agent("xmltv/$XMLTV::VERSION");

# This undocumented --cache option for debugging is 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');

my $webroot = "http://www.webstep.net/tv_grab_cz/tv_grab_cz.php?";

my( $opt, $conf ) = ParseOptions( {
    grabber_name => "tv_grab_cz",
    capabilities => [qw/baseline manualconfig tkconfig apiconfig cache/],
    stage_sub => \&config_stage,
    listchannels_sub => \&list_channels,
    version => "$XMLTV::VERSION",
    description => "The Czech Republic (www.webstep.net)",
} );

# Get the actual data
my $channels="";
foreach my $channel (@{$conf->{channel}})
{
    $channels .= "channels=$channel\&";
}

my $url = $webroot . $channels . "days=" . $opt->{days} .
    "\&offset=" . $opt->{offset} . "\&charset=utf-8&rfc2838=1";

print STDERR "Fetching $url\n" if $opt->{debug};

# XMLTV::Options has redirected stdout to a file for us if the
# --output parameter was specified, so all we have to do is print
# the xmltv data.
my $res = getprint( $url );

if( is_success( $res ) )
{
    exit 0;
}
else
{
    print STDERR "Failed to download data: " . status_message( $res ) . "\n";
    exit 1;
}

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 => 'utf-8' );
    $writer->start( { grabber => 'tv_grab_cz' } );

    $writer->start_selectone( {
	id => 'encoding',
	title => [ [ 'Encoding', 'en' ],
		   [ 'Kódování', 'cz' ] ],
	description => [
          [ 'Which character-encoding shall data be generated in?', 'en' ],
	  [ 'V kterém kódování češtiny by měla být data vygenerována?', 'cz' ] ],
    } );

    $writer->write_option( {
	value=>'utf-8',
	text=> => [ [ 'utf-8', 'en' ],
		    [ 'utf-8', 'cz' ] ]
		    } );

    $writer->write_option( {
	value=>'ascii',
	text=> => [ [ 'ascii', 'en' ],
		    [ 'ascii', 'cz' ] ]
		    } );


    $writer->write_option( {
	value=>'iso-8859-2',
	text=> => [ [ 'iso-8859-2', 'en' ],
		    [ 'iso-8859-2', 'cz' ] ]
		    } );

    $writer->write_option( {
	value=>'windows-1250',
	text=> => [ [ 'windows-1250', 'en' ],
		    [ 'windows-1250', 'cz' ] ]
		    } );

    $writer->end_selectone();

    $writer->end( 'select-channels' );

    return $result;
}

sub list_channels
{
    my( $conf, $opt ) = @_;

    # $conf contains the hashref returned by load_config().
    my $url = $webroot . "list-channels=1&charset=utf-8&rfc2838=1";

    print STDERR "Fetching $url\n" if $opt->{debug};

    my $res = get( $url );

    die "Failed to get $url" if not defined $url;

    return $res;
}

### Setup indentation in Emacs
## Local Variables:
## perl-indent-level: 4
## perl-continued-statement-offset: 4
## perl-continued-brace-offset: 0
## perl-brace-offset: -4
## perl-brace-imaginary-offset: 0
## perl-label-offset: -2
## cperl-indent-level: 4
## cperl-brace-offset: 0
## cperl-continued-brace-offset: 0
## cperl-label-offset: -2
## cperl-extra-newline-before-brace: t
## cperl-merge-trailing-else: nil
## cperl-continued-statement-offset: 2
## indent-tabs-mode: t
## End:
