#!/usr/bin/perl -w

=pod

=head1 NAME

tv_grab_es_laguiatv - Alternative TV grabber for Spain.

=head1 SYNOPSIS

tv_grab_es_laguiatv --help

tv_grab_es_laguiatv [--config-file FILE] --configure [--gui OPTION]

tv_grab_es_laguiatv [--config-file FILE] [--output FILE] [--days N]
           [--offset N] [--quiet]

tv_grab_es_laguiatv --list-channels

tv_grab_es_laguiatv --capabilities

tv_grab_es_laguiatv --version

=head1 DESCRIPTION

Output TV listings for spanish channels from www.laguiatv.com.
Supports analogue and digital (D+) channels.
The grabber relies on parsing HTML so it might stop working at any time.

First run B<tv_grab_es_laguiatv --configure> to choose, which channels you want
to download. Then running B<tv_grab_es_laguiatv> with no arguments will output
listings in XML format to standard output.

B<--configure> Prompt for which channels,
and write the configuration file.

B<--config-file FILE> Set the name of the configuration file, the
default is B<~/.xmltv/tv_grab_es_laguiatv.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 XMLTV::ProgressBar.

B<--output FILE> Write to FILE rather than standard output.

B<--days N> Grab N days.  The default is 3.

B<--offset N> Start N days in the future.  The default is to start
from today.

B<--quiet> Suppress the progress messages normally written to standard
error.

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 SEE ALSO

L<xmltv(5)>.

=head1 AUTHOR

CandU, candu_sf@sourceforge.net, based on tv_grab_es, from Ramon Roca.

=head1 BUGS

=cut

# 


######################################################################
# initializations

use strict;
use XMLTV::Version '$Id: tv_grab_es_laguiatv,v 1.16 2010/11/21 14:49:59 dekarl Exp $ ';
use XMLTV::Capabilities qw/baseline manualconfig cache/;
use XMLTV::Description 'Spain (laguiatv.com)';
use Getopt::Long;
use Date::Manip;
use HTML::TreeBuilder;
use HTML::Entities; # parse entities
use IO::File;

use XMLTV;
use XMLTV::Memoize;
use XMLTV::ProgressBar;
use XMLTV::Ask;
use XMLTV::Config_file;
use XMLTV::DST;
use XMLTV::Get_nice;
use XMLTV::Mode;
use XMLTV::Date;
# Todo: perhaps we should internationalize messages and docs?
use XMLTV::Usage <<END
$0: get Spanish television listings in XMLTV format
To configure: $0 --configure [--config-file FILE]
To grab listings: $0 [--config-file FILE] [--output FILE] [--days N]
        [--offset N] [--quiet]
To list channels: $0 --list-channels
To show capabilities: $0 --capabilities
To show version: $0 --version
END
  ;

# Attributes of the root element in output.
my $HEAD = { 'source-info-url'     => 'http://www.laguiatv.com/programacion.php',
	     'source-data-url'     => "http://www.laguiatv.com/programacion_vertical.php",
	     'generator-info-name' => 'XMLTV',
	     'generator-info-url'  => 'http://xmltv.org/',
	   };
		   
# Whether zero-length programmes should be included in the output.
my $WRITE_ZERO_LENGTH = 0;
my $DO_SLOWER_DESC_GET = 0;

# default language
my $LANG="es";

# Global channel_data
our @ch_all;

# debug print function
sub debug_print
{
#	my ($str) = @_;
#	print $str;
}

# hard-coded list of TDT channels since laguia dont list them separately
my @tdt_channels = (
    "La 10",
    "Antena.neox",
    "Antena.nova",
    "Clan / TVE 50",
    "Factor{icute}a de Ficci{ocute}n Telecinco",
    "Boing",
    "Gol TV",
    "La Siete",
    "Veo7",
    "LaSexta2",
    "LaSexta3",
    "Nitro",
    "Canal 24 horas",
    "CNN+"
);

# hard-coded list of channels to hide from channel list
my @hide_channels = (
    "Clan  TVE 50", # missing / in name
);


######################################################################
# get options

# Get options, including undocumented --cache option.
XMLTV::Memoize::check_argv('XMLTV::Get_nice::get_nice_aux');
my ($opt_days, $opt_offset, $opt_help, $opt_output,
    $opt_configure, $opt_config_file, $opt_gui,
    $opt_quiet, $opt_list_channels);
$opt_days  = 3; # default
$opt_offset = 0; # default
$opt_quiet  = 0; # default
GetOptions('days=i'        => \$opt_days,
	   'offset=i'      => \$opt_offset,
	   'help'          => \$opt_help,
	   'configure'     => \$opt_configure,
	   'config-file=s' => \$opt_config_file,
       'gui:s'         => \$opt_gui,
	   'output=s'      => \$opt_output,
	   'quiet'         => \$opt_quiet,
	   'list-channels' => \$opt_list_channels
	  )
  or usage(0);
die 'number of days must not be negative'
  if (defined $opt_days && $opt_days < 0);
usage(1) if $opt_help;

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

my $mode = XMLTV::Mode::mode('grab', # default
			     $opt_configure => 'configure',
			     $opt_list_channels => 'list-channels',
			    );

# File that stores which channels to download.
my $config_file
  = XMLTV::Config_file::filename($opt_config_file, 'tv_grab_es_laguiatv', $opt_quiet);

my @config_lines; # used only in grab mode
if ($mode eq 'configure') {
    XMLTV::Config_file::check_no_overwrite($config_file);
}
elsif ($mode eq 'grab') {
    @config_lines = XMLTV::Config_file::read_lines($config_file);
}
elsif ($mode eq 'list-channels') {
    # Config file not used.
}
else { die }

# Whatever we are doing, we need the channels data.
my %channels; # sets @ch_all
my @channels;

my %icons;
######################################################################
# write configuration

if ($mode eq 'configure') {
	%channels = get_channels();
    
	open(CONF, ">$config_file") or die "cannot write to $config_file: $!";

	# Ask about getting descs
	my $getdescs = ask_boolean("Do you want to get descriptions (very slow)");
	warn("cannot read input, using default")
	  if not defined $getdescs;

	print CONF "getdescriptions ";
	print CONF "yes\n" if $getdescs;
	print CONF "no\n" if not $getdescs;

    # Ask about each channel.
    my @chs = sort keys %channels;
    my @names = map { $channels{$_} } @chs;
    my @qs = map { "Add channel $_?" } @names;
    my @want = ask_many_boolean(1, @qs);
    foreach (@chs) {
	my $w = shift @want;
	warn("cannot read input, stopping channel questions"), last
	  if not defined $w;
	# No need to print to user - XMLTV::Ask is verbose enough.

	# Print a config line, but comment it out if channel not wanted.
	print CONF '#' if not $w;
	my $name = shift @names;
	print CONF "channel $_ $name\n";
	# TODO don't store display-name in config file.
    }

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

    exit();
}


# Not configuration, we must be writing something, either full
# listings or just channels.
#
die if $mode ne 'grab' and $mode ne 'list-channels';

# Options to be used for XMLTV::Writer.
my %w_args;
if (defined $opt_output) {
    my $fh = new IO::File(">$opt_output");
    die "cannot write to $opt_output: $!" if not defined $fh;
    $w_args{OUTPUT} = $fh;
}
$w_args{encoding} = 'ISO-8859-1';
my $writer = new XMLTV::Writer(%w_args);
$writer->start($HEAD);

if ($mode eq 'list-channels') {
    $writer->write_channel($_) foreach @ch_all;
    $writer->end();
    exit();
}

######################################################################
# We are producing full listings.
die if $mode ne 'grab';

# Read configuration
my $line_num = 1;
foreach (@config_lines) {
    ++ $line_num;
    next if not defined;
    if (/getdescriptions:?\s+(\S+)/)
	{
		if($1 eq "yes")
		{
			$DO_SLOWER_DESC_GET = 1;
		}
    }
	elsif (/^channel:?\s+(\S+)\s+([^\#]+)/)
	{
		my $ch_did = $1;
		my $ch_name = $2;
		$ch_name =~ s/\s*$//;
		push @channels, $ch_did;
		$channels{$ch_did} = $ch_name;
    }
    else {
	warn "$config_file:$line_num: bad line\n";
    }
}

######################################################################
# begin main program

# Assume the listings source uses CET (see BUGS above).
my $now = DateCalc(parse_date('now'), "$opt_offset days");
die "No channels specified, run me with --configure\n"
  if not keys %channels;
my @to_get;

my $iconbar = new XMLTV::ProgressBar({name => 'getting channel info', count => scalar @channels})
  if not $opt_quiet;
# the order in which we fetch the channels matters
foreach my $ch_did (@channels) {
    my $ch_name=$channels{$ch_did};
    my $ch_xid="$ch_did.laguiatv.com";
    my $ch_icon=get_icon($ch_did);
    if(index($ch_icon, "shim.gif") < 0)
    {
		$writer->write_channel({ id => $ch_xid,
					 'display-name' => [ [ $ch_name ] ] ,
					 'icon' => [ { 'src' => $ch_icon } ] });
	}
	else
	{
		$writer->write_channel({ id => $ch_xid,
					 'display-name' => [ [ $ch_name ] ] });
	}
    my $day=UnixDate($now,'%Q');
    for (my $i=0;$i<$opt_days;$i++) {
        push @to_get, [ $day, $ch_xid, $ch_did ];
        #for each day
        $day=nextday($day); die if not defined $day;
    }
	update $iconbar if not $opt_quiet;
}

# This progress bar is for both downloading and parsing.  Maybe
# they could be separate.
#
my $bar = new XMLTV::ProgressBar({name => 'getting listings', count => scalar @to_get})
  if not $opt_quiet;
foreach (@to_get) {
	debug_print("process $_->[0], $_->[1], $_->[2]\n");
	foreach (process_table($_->[0], $_->[1], $_->[2])) {
		$writer->write_programme($_);
	}
	update $bar if not $opt_quiet;
}
$bar->finish() if not $opt_quiet;
$writer->end();

######################################################################
# subroutine definitions

# Use Log::TraceMessages if installed.
BEGIN {
    eval { require Log::TraceMessages };
    if ($@) {
	*t = sub {};
	*d = sub { '' };
    }
    else {
	*t = \&Log::TraceMessages::t;
	*d = \&Log::TraceMessages::d;
	Log::TraceMessages::check_argv();
    }
}

####
# process_table: fetch a URL and process it
#
# arguments:
#    Date::Manip object giving the day to grab
#    xmltv id of channel
#    elpais.es id of channel
#
# returns: list of the programme hashes to write
#
sub process_table {

    my ($date, $ch_xmltv_id, $ch_es_id) = @_;
	my $ch_conv_id = convert_id_to_laguiatvid($ch_es_id);
    my $today = UnixDate($date, '%d/%m/%Y');

    my $url = "http://www.laguiatv.com/programacion.php?vertical=1&fecha=$today&cadena=$ch_conv_id";
	debug_print "Getting $url\n";
    t $url;
    local $SIG{__WARN__} = sub 
	{
		warn "$url: $_[0]";
	};

    # parse the page to a document object
    my $tree = get_nice_tree $url;
    my @program_data = get_program_data($tree);
    my $bump_start_day=0;

    my @r;
    while (@program_data) {
	my $cur = shift @program_data;
	my $next = shift @program_data;
	unshift @program_data,$next if $next;
	
	my $p = make_programme_hash($date, $ch_xmltv_id, $ch_es_id, $cur, $next);
	if (not $p) {
	    require Data::Dumper;
	    my $d = Data::Dumper::Dumper($cur);
	    warn "cannot write programme on $ch_xmltv_id on $date:\n$d\n";
	}
	else {
	    push @r, $p;
	}

	if (!$bump_start_day && bump_start_day($cur,$next)) {
	    $bump_start_day=1;
	    $date = UnixDate(DateCalc($date,"+ 1 day"),'%Q');
	}
    }
    return @r;
}

sub make_programme_hash {
    my ($date, $ch_xmltv_id, $ch_es_id, $cur, $next) = @_;

    my %prog;

    $prog{channel}=$ch_xmltv_id;
    $prog{title}=[ [ $cur->{title}, $LANG ] ];
    $prog{"sub-title"}=[ [ $cur->{subtitle}, $LANG ] ] if defined $cur->{subtitle};
    # $prog{category}=[ [ $cur->{category}, $LANG ] ];

    t "turning local time $cur->{time}, on date $date, into UTC";
    eval { $prog{start}=utc_offset("$date $cur->{time}", '+0100') };
    if ($@) {
	warn "bad time string: $cur->{time}";
	return undef;
    }
    t "...got $prog{start}";
    # FIXME: parse description field further

    $prog{desc}=[ [ $cur->{desc}, $LANG ] ] if defined $cur->{desc};
    $prog{category}=[ [ $cur->{category}, $LANG ] ] if defined $cur->{category};
    return \%prog;
}
sub bump_start_day {
    my ($cur,$next) = @_;
    if (!defined($next)) {
	return undef;
    }
    my $start = UnixDate($cur->{time},'%H:%M');
    my $stop = UnixDate($next->{time},'%H:%M');
    if (Date_Cmp($start,$stop)>0) {
	return 1;
    } else {
	return 0;
    }
}


#
sub get_program_data 
{
    my ($tree) = @_;
    my @data;

	# find schedule table
	my @tables = $tree->find_by_tag_name("_tag"=>"table");

	foreach my $table (@tables)
	{
		my $class = $table->attr('class');
		
		if ($class && $class eq "grid cadena")
		{
			debug_print("Got correct class\n");
			my @trs = $table->find_by_tag_name("_tag"=>"tr");
			my $state = 1;
			my $p_title = "";
			my $p_stime = "";
			my $p_category = "";
			my $p_description = "";

			foreach my $tr(@trs)
			{
				my @ths = $tr->find_by_tag_name("_tag"=>"th");
				my $nths = @ths;
				
				debug_print("Got tr with $nths ths\n");
				if($nths == 1)
				{
					my $class = $ths[0]->attr('scope');
					if($class && $class eq "row")
					{
						# time data
						my @txts = $ths[0]->content_list;
						$p_stime = $txts[0];
						debug_print("Time: ".$p_stime."\n");
					}
					
				}

				my @tds = $tr->find_by_tag_name("_tag"=>"td");
				my $ntds = @tds;
				
				debug_print("Got tr with $ntds tds\n");
				foreach my $td(@tds)
				{
					my $td_class = $td->attr('row');
				
					# title & link data
					debug_print("Got td with parrilla\n");

					my @as = $td->find_by_tag_name("_tag"=>"a");
					my $size = @as;
					if($size > 0)
					{
						my $a = $as[0];
						if ($DO_SLOWER_DESC_GET)
						{
							($p_description, $p_category) = get_prog_info($a->attr('href'));
						}
						else
						{
							$p_category = $a->attr('href');
							$p_category =~ s/^\///;
							$p_category =~ s/\/.*//;
						}
						debug_print "Category: ".$p_category."\n";

						my @atxts = $a->content_list;
						$p_title = $atxts[0];

						if($p_title)
						{
							debug_print("Title: ".$p_title."\n");
						}

						if($p_title && $p_title ne "" && $p_stime && $p_stime ne "")
						{
							my %h = (	time =>		$p_stime,
										title=>		$p_title);

							$h{desc} = $p_description if $p_description ne "";
							$h{category} = $p_category if $p_category ne "";
							push @data, \%h;

							$p_title = "";
							$p_stime = "";
							$p_category = "";
						}
					}
					else
					{
						my @atxts = get_txt_elems($td);
						$p_title = $atxts[0];
						
						if($p_title)
						{
							debug_print("Title: ".$p_title."\n");
						}

						if($p_title && $p_title ne "" && $p_stime && $p_stime ne "")
						{
							my %h = (	time =>		$p_stime,
										title=>		$p_title);

							$h{desc} = $p_description if $p_description ne "";
							$h{category} = $p_category if $p_category ne "";

							push @data, \%h;

							$p_title = "";
							$p_stime = "";
							$p_category = "";
						}
					}
				}
			}
		}
	}
    return @data;
}

sub get_icon 
{
	my ($ch_did) = @_;
	
	my $ch_xid = convert_id_to_laguiatvid($ch_did);
	
    my $url = "http://www.laguiatv.com/programacion.php?vertical=1&cadena=$ch_xid";
	debug_print "Getting $url\n";
    t $url;
    local $SIG{__WARN__} = sub 
	{
		warn "$url: $_[0]";
	};

    # parse the page to a document object
    my $tree = get_nice_tree $url;
    my @data;

	# find schedule table
	my @tables = $tree->find_by_tag_name("_tag"=>"table");

	foreach my $table (@tables)
	{
		my $class = $table->attr('class');
		
		if ($class && $class eq "grid cadena")
		{
			debug_print("Got correct class\n");
			my @trs = $table->find_by_tag_name("_tag"=>"tr");
			my $state = 1;
			my $p_title = "";
			my $p_stime = "";
			my $p_category = "";
			my $p_description = "";

			foreach my $tr(@trs)
			{
				my @ths = $tr->find_by_tag_name("_tag"=>"th");
				my $nths = @ths;
				
				debug_print("Got tr with $nths ths\n");
				if($nths == 1)
				{
					my $class = $ths[0]->attr('scope');
					if($class && $class eq "colgroup")
					{
						my @imgs = $ths[0]->find_by_tag_name("_tag"=>"img");
						my $nimgs = @imgs;
				
						debug_print("Got ths with $nimgs imgs\n");
						if($nimgs == 1)
						{
							my $icon = 'http://www.laguiatv.com/' . $imgs[0]->attr('src');
							debug_print("Got icon $icon\n");
							return $icon;
						}
					}
					
				}
			}
		}
	}
    return "";
}


sub get_prog_info
{
	my ($url) = @_;
	my $desc = "";
	my $cat = "";

	$url = "http://www.laguiatv.com/".$url;
	debug_print "Get proginfo $url\n";	

    my $tree = get_nice_tree $url;

	my @divs = $tree->find_by_tag_name("_tag"=>"div");

	foreach my $div(@divs)
	{
		my $class = $div->attr('class');
		if ($class && $class eq "intro-datasheet")
		{
			# debug_print "got losdatos\n";	
			my @subdivs = $div->find_by_tag_name("_tag"=>"div");
					
			foreach my $subdiv(@subdivs)
			{
				# debug_print "subdiv\n";	
				$class = $subdiv->attr('class');
				if ($class && $class eq "text")
				{
					my @ps = $div->find_by_tag_name("_tag"=>"p");
					
					foreach my $p(@ps)
					{
						$class = $p->attr('class');
				
						if ($class && $class eq "desc")
						{
							debug_print "got desc\n";	

							$desc = $desc . $p->as_text;
						}
					}
				}
			}
		}
	}

	debug_print "desc: $desc\n";	

	return ($desc,$cat);
}

sub get_txt_elems {
    my ($tree) = @_;

    my @txt_elem;
    my @txt_cont = $tree->look_down(
                        sub { ($_[0]->descendants() eq 0  ) },       
			sub { defined($_[0]->attr ("_content") ) } );
	foreach my $txt (@txt_cont) {
        	my @children=$txt->content_list;
		if (defined($children[0])) {
                  for (my $tmp=$children[0]) {
			s/^\s+//;s/\s+$//;
			push @txt_elem, $_;
                      }
                }
	}
    return @txt_elem;
}

# get channel listing
sub get_channels 
{
    my $bar = new XMLTV::ProgressBar({name => 'finding channels', count => 1})
	if not $opt_quiet;
    my %channels;
    my $url="http://www.laguiatv.com/programacion.php";
    t $url;

	my $channel_id;
	my $channel_name;
	my $elem;

    my $tree = get_nice_tree $url;

    # Add hard-coded TDT channels (bad chris)
    foreach $channel_name (@tdt_channels)
    {
        $channel_id = convert_name_to_id($channel_name);
        debug_print "Channel $channel_name, id $channel_id\n";
        $channels{$channel_id}=$channel_name;
    }

# <input type="checkbox" name="nacionales1" value="TVE 1"
	# find the channels that are in check boxes
    my @options = $tree->find_by_tag_name("_tag"=>"input");

    foreach $elem (@options) 
	{
		my $ename = $elem->attr('name');
		my $val = $elem->attr('value');
		
		if ($ename && $val)
		{
			if ($ename =~ m/^nacionales/)
			{
				$channel_id = convert_name_to_id($val);
				$channel_name = $val;

				debug_print "Channel $channel_name, id $channel_id\n";
				$channels{$channel_id}=$channel_name;
			}
		}
	}

	# find the channels that are in drop down lists
	@options = $tree->find_by_tag_name("_tag"=>"option");

	foreach $elem (@options) 
	{
		my $val = $elem->attr('value');
		my $parent = $elem->parent();
		my $pname = $parent->attr('name');
		
		if ($pname && $val)
		{
			if ($pname =~ m/^locales/ ||
				$pname =~ m/^digitales/ ||
				$pname =~ m/^autonomicas/)
			{
				$channel_id = convert_name_to_id($val);
				$channel_name = $val;

				debug_print "Channel $channel_name, id $channel_id\n";
				$channels{$channel_id}=$channel_name;
			}
		}
	}

    # remove channels that should not be listed
    foreach $channel_name (@hide_channels)
    {
        $channel_id = convert_name_to_id($channel_name);
        delete $channels{$channel_id};
    }

    die "no channels could be found" if not keys %channels;
    update $bar if not $opt_quiet;
    $bar->finish() if not $opt_quiet;
    return %channels;
}

sub convert_name_to_id
{
    my ($str) = @_;


	$str =~ s/([^A-Za-z0-9])/sprintf("-%02X", ord("$1"))/seg;

	$str = "C" . $str;
	return $str;
}

sub convert_id_to_laguiatvid
{
    my ($str) = @_;

	# convert -20 to + (to replace spaces)
	$str =~ s/-20/+/g;

	# convert - to % for URL encoded chars
	$str =~ s/\-/%/g;

	# strip the C off the front
	$str = substr($str, 1);

	return $str;
}

# Bump a DDMMYYYY date by one.
sub nextday {
    my $d = shift;
    my $p = parse_date($d);
    my $n = DateCalc($p, '+ 1 day');
    return UnixDate($n, '%Q');
}
