#!/usr/bin/perl -w
# Script to grab XMLTV data from IceTV server
# Version 1, 10 Apr 2005, Joshua King, jking_ok@yahoo.com.au
#   (initial creation)
# Version 2, 06 May 2005, IceTV, support@icetv.com.au
#   (initial release by IceTV)
# Version 3, 29 Jan 2007, IceTV, support@icetv.com.au
#   (added --quiet, --description, and --capabilities)
# Version 4, 14 Sep 2007, Patrick Nichols, (go look it up using google).au
#   (added the preferredmethod capability to let MythTV grab all guide data at once)
# Version 5, 22 Nov 2009, David Peterson, support@icetv.com.au
#   Converted to XMLTV libraries for use with Myth's graphical screen configuration
# Version 5.01, 8 Dec 2009, David Peterson, support@icetv.com.au
#   Re-added channel list at the end of the config phase
#
# Requires an active IceTV subscription.  See http://www.icetv.com.au for information.
# Interface-compatible with other grabbers (ie, tv_grab_au*)

# Define version of the grabber
use vars qw($VERSION);
$VERSION = 5;

use strict;
use Date::Manip;
use Getopt::Long;

# Requires LWP/libwww-perl to be installed
use LWP::UserAgent;

use XMLTV::Version '$Id: tv_grab_au_ice 4 2009/11/22 dpeterson Exp $';
use XMLTV::Capabilities qw/baseline manualconfig cache preferredmethod/;
use XMLTV::Description 'Australia (IceTV)';
use XMLTV::PreferredMethod qw/allatonce/;

use Memoize;
use XMLTV;
use XMLTV::Memoize;
use XMLTV::Ask;
use XMLTV::Config_file;
use XMLTV::ProgressBar;
use XMLTV::DST;
use XMLTV::Get_nice;
use XMLTV::Mode;

use XMLTV::Usage <<END
$0: get Australian television listings from IceTV in XMLTV format
To configure: $0 --configure [--config-file FILE]
To grab listings: $0 [--config-file FILE] [--output FILE] [--days N]
        [--offset N] [--quiet] [--fast]
To list available channels: $0 [--output FILE] [--quiet] --list-channels
To show capabilities: $0 --capabilities
To show version: $0 --version
END
;



# Documentation of tool below

=pod

=head1 NAME

tv_grab_au_ice - Download EPG information from IceTV in XMLTV format

=head1 SYNOPSIS

tv_grab_au_ice --version

tv_grab_au_ice --description

tv_grab_au_ice --capabilities

tv_grab_au_ice --preferredmethod

tv_grab_au_ice --configure [--config-file <config file>]

tv_grab_au_ice [--config-file <config file>] [--output <guide file> [--fast]] [--days <# days>] [--offset <# days>] [--quiet]

=head1 DESCRIPTION

tv_grab_au_ice is an XMLTV downloader for registered users of the IceTV service.
B<IceTV requires a paid subscription.>

The IceTV server provides data in a format that extends the XMLTV DTD. It should
be compatible with most programs that support XMLTV guide data.

=head1 COMMAND LINE PARAMETERS

=over

=item --version

Lists the version information.

=item --description

Displays the program description.

=item --capabilities

Displays the program capabilities, one per line.

=item --preferredmethod

Display the preferred grabbing method (for mythfilldatabase)

=item --configure

Tells tv_grab_au_ice that you want to configure it (change your options) rather than collect guide data.
This will cause tv_grab_au to ask you for the following information:

=over

=item Username

Your IceTV guide subscription User ID.

=item Password

Your IceTV guide subscription Password.

=back

The information will then be sent to the IceTV server to check that the User ID and Password
are correct.

=item --config-file <config file>

The file in which to store your configuration (when used with --configure) or to read your configuration from (in normal mode).
If this is not given, a file called F<tv_grab_au_ice.conf> (or similar) is created or expected to be in the current directory.

=item --output <guide file>

The file in which to store the downloaded guide data.
If this is not given, the guide data will be printed to the terminal. This is then intended to be captured by your PVR's guide collection program.

=item --fast

This asks tv_grab_au_ice to download as little information as possible.
For this to work, the guide data must be sent to a file using the --output option.
The modification date of the file will be sent to IceTV's server and only guide data that has been added or changed since that date will be downloaded.

=item --days <# days>

This asks tv_grab_au_ice to download at most # days of data from the server.
This is dependent upon the data being available on the server.
Without this option all available data will be downloaded.

=item --offset <# days>

This asks tv_grab_au_ice to shift the start and end dates to download by the number of days.
This is useful to only get the newest day of the week, but with the support of IceTV's servers, the --fast option is probably more appropriate.

=item --quiet

This asks tv_grab_au_ice to suppress all progress information and only print error messages to stderr.

=head1 REQUIREMENTS

This tool requires Perl. The latest version of Perl is highly recommended, since testing has only been carried out on recent versions.

Some additional modules must also be installed. Some will come with your Perl distribution, others can be acquired using CPAN. L<http://search.cpan.org>

=over

=item *

libwww-perl package

=item *

Getopt::Long module

=back

=head1 SEE ALSO

=over

=item IceTV web site

L<http://www.icetv.com.au>

=item IceTV online discussion forum

L<http://www.icetv.com.au/iceforum/>

=item XMLTV web site

L<http://www.membled.com/work/apps/xmltv>

=item Perl CPAN search engine

L<http://search.cpan.org>

=back

=head1 AUTHOR/CONTACT INFORMATION

Original script by Joshua King.  Updates by IceTV.

Please refer all questions to IceTV support (L<mailto:support@icetv.com.au>) or the IceTV discussion forums (L<http://www.icetv.com.au/iceforum/>).

=cut

# Some constants
use vars qw($BASE_URL $ua);
$BASE_URL = 'http://iceguide.icetv.com.au/cgi-bin/epg/iceguide.cgi';

# Utility functions
sub echo_on {
 	system "stty", "echo"
}

sub echo_off {
	system "stty", "-echo"
}

sub start_ua {
	# Create a user agent object
	$ua = LWP::UserAgent->new;

	# Set user agent
	$ua->agent("$0/$VERSION");

	# Allow proxies to be defined by the user
	$ua->env_proxy;

	return $ua;
}

sub datestr {
	my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = @_;
	my $output = '';
	$output .= sprintf("%02d", $year % 100);
	$output .= sprintf("%02d", $mon + 1);
	$output .= sprintf("%02d", $mday);

	return $output;
}

sub datetimestr {
	my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = @_;
	my $output = '';
	$output .= sprintf("%04d", $year + 1900);
	$output .= sprintf("%02d", $mon + 1);
	$output .= sprintf("%02d", $mday);
	$output .= sprintf("%02d", $hour);
	$output .= sprintf("%02d", $min);
	$output .= sprintf("%02d", $sec);

	return $output;
}

my ($configure, $output, $days, $offset, $fast, $quiet, $capabilities, $help, $config_file_option, $gui, $list_channels);
my ($username, $password);

# Get command line parameters
$configure = 0; # Whether to configure
$output = ''; # Where to put guide data
$days = 0; # Number of days to download
$offset = 0; # Number of days to offset date by
$fast = 0; # Whether to speed up (download changes only)
$quiet = 0; # Whether to only print error-messages to stderr
$capabilities = 0; # Whether to display the capabilities
$help = 0;
GetOptions('configure' => \$configure,
	   'config-file=s' => \$config_file_option,
	   'output=s' => \$output,
	   'days=i' => \$days,
	   'offset=i' => \$offset,
	   'fast' => \$fast,
	   'quiet' => \$quiet,
	   'help' => \$help,
	   'gui:s'            => \$gui,
	   'list-channels'    => \$list_channels
    ) or usage(0);

my $mode = XMLTV::Mode::mode('grab',
                             $list_channels => 'list-channels',
                             $configure => 'configure');

XMLTV::Ask::init($gui);

my $config_file;
$config_file = XMLTV::Config_file::filename($config_file_option, 'tv_grab_au_ice', $quiet);

if ($mode eq 'configure') {

    # Configuring the grabber
    if ($output ne '' || $days != 0 || $offset != 0 || $fast != 0) {
	say "Invalid options with --configure";
	exit 1;
    }

    XMLTV::Config_file::check_no_overwrite($config_file);

    open(CONF, ">$config_file") or die "cannot write to $config_file: $!";

    $username = ask("IceTV User ID:");
    $password = ask("IceTV Password:");
    print CONF "username: $username\n";
    print CONF "password: $password\n";

    # Create a test request
    my $url = $BASE_URL . '?op=show_user_channels';
    my $req = HTTP::Request->new(GET => $url);

    # Set authentication parameters
    $req->authorization_basic($username, $password);

    say "Connecting to IceTV to test login information...\n";

    # Set up user agent
    my $ua = start_ua();

    # Get a response
    my $res = $ua->request($req);

    unless ($res->is_success) {
	if($res->code == 401)
	{
	    say "Request failed. Invalid User ID or Password.\n";
	} else {
	    say "Request failed. Error " . $res->code . " " . $res->message . "\n"; 
	}
	exit 1;
    }

    if($res->content =~ /^\s*$/) {
	say "No channels are configured for your User ID. Please login to the IceTV web site at www.icetv.com.au to make your selection.";
    }

    # Show channel list
    say("Below is your channel list. Please write these down. The ID is your Myth xmltvid.");
    say($res->content);
    ask("Press Enter once you have copied down your channel list:");

    close CONF or warn "cannot close $config_file: $!";
    say("Finished configuration.");

    exit();

}

if ($mode eq 'list-channels') {

    # Channels are always returned at the top of each XML file, so if --list-channels is given, grab the list as normal
    $days = 1;

    # Also removes all <program> tabs before output

}

# Running the grabber

# Get configuration from file
my $line_num = 0;
my $stripTitles;
foreach (XMLTV::Config_file::read_lines($config_file)) {
    ++ $line_num;
    next if not defined;
    if (/^username:?\s+(\S+)/){
	$username = $1;
    } elsif (/^password:?\s+(\S+)/){
	$password = $1;
    } elsif (/^stripTitles:?\s+'(.+)'/){
	$stripTitles = $1;
    } else {
        warn ("$config_file:$line_num: bad line\n");
    }
}

# Create a request
my $url = $BASE_URL . "?op=xmlguide";
if ($days > 0 || $offset != 0) {
    my $start = time + $offset * 60 * 60 * 24;
    my @oftime = localtime($start);
    $url .= ('&start_date=' . datestr(@oftime));
    if ($days > 0) {
	my $end = $start + ($days - 1) * 60 * 60 * 24 + 1;
	@oftime = localtime($end);
	$url .= ('&end_date=' . datestr(@oftime));
    }
}
# Get changed/new entries only (faster?)
if ($fast == 1 && $output ne '' && -e $output) {
    my $s = stat($output);
    my @modtime = gmtime($s->mtime);
    $url .= ('&last_mod=' . datetimestr(@modtime));
}
my $req = HTTP::Request->new(GET => $url);

# Set authentication parameters
$req->authorization_basic($username, $password);

say "Requesting programmes..." if($quiet != 1);

# Set up connection
my $ua = start_ua();

# Get a response
my $res = $ua->request($req);

unless ($res->is_success) {
    if($res->code == 401)
    {
	say "Request failed. Invalid User ID or Password.\n";
    }
    else
    {
	say "Request failed. Error " . $res->code . " " . $res->message . "\n"; 
    }
    exit 1;
}

my $xmlout = $res->content;

if ($mode eq 'list-channels') {
    # Just show channels, so remove any program tags
    $xmlout =~ s|<programme.*</programme>||s;
}


if ($stripTitles && $stripTitles ne '') {
    #Strip unwanted prefixes
    warn ("Stripping the following characters from show Titles:\n$stripTitles\n");
    $xmlout =~ s/$stripTitles//g;
}

if ($output ne '') {

    # Print XML to file
    open GUIDEDATA, ">$output" or die "Cannot open guide data file for writing!\n";
    print GUIDEDATA $xmlout;
    close GUIDEDATA;
} else {
    # Print XML to stdout
    print $xmlout;
}

say "Done.\n" if($quiet != 1);

exit 0;
