#!/usr/pkg/bin/perl

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell

=pod

=head1 NAME

tv_grab_ar - Grab TV listings for Argentina.

=head1 SYNOPSIS

tv_grab_ar --help

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

tv_grab_ar [--config-file FILE] [--output FILE] [--days N]
           [--offset N] [--zone N] [--quiet]

tv_grab_ar --list-channels

tv_grab_ar --capabilities

tv_grab_ar --version

=head1 DESCRIPTION

Output TV listings for several channels available in Argentina.
Now supports terrestrial analog TV listings, which is the most common TV
viewed in Argentina.

The TV listings come from http://www.buscadorcablevision.com.ar/
The grabber relies on parsing HTML so it might stop working at any time.

First run B<tv_grab_ar --configure> to choose, which channels you want
to download. Then running B<tv_grab_ar> 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_ar.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<--zone N>  Specify a different location ID than that specified in the
configuration file.

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

The original author was Christian A. Rodriguez, car at cespi dot unlp dot edu dot ar,
basing tv_grab_ar on tv_grab_es by Ranin Roca.

Significant updates and patches have been provided by:
  - Mariano S. Cosentino, 4xmltv at marianok dot com dot ar
  - Karl Dietz, dekarl at spaetfruehstuecken dot org
  - Geoff Westcott, honir999 at gmail dot com
  - Nick Morrott, knowledgejunkie at gmail dot com

=head1 BUGS

This grabber extracts all information from the cablevision website. Any
changes to the website may cause this grabber to stop working.

Retrieving the description information adds a considerable amount of time
to the run and makes the file quite large.

It might be a good idea to cache program descriptions so there is no need
to refetch them.

=cut

# TODO
#
# ParseOptions
#
# Perhaps we should internationalize messages and docs?
#
# Add channel logo support (download from $urlRoot/logos/lg_99.png where 99 is
# the channel id)
#
# Add Support for Argentinean DST
#
# Add properties in the corresponding XMLTV fields without relying on the
# tv_extractinfo_ar (partialy done)
#
# Might be a good idea to add a cache file the keeps the program description so
# there is no need to fetch it.

######################################################################

use warnings;
use strict;
use utf8;
use open IO => ':encoding(utf8)';

use XMLTV::Version '$Id: tv_grab_ar,v 1.22 2016/03/16 04:12:51 knowledgejunkie Exp $ ';
use XMLTV::Capabilities qw/baseline manualconfig cache/;
use XMLTV::Description 'Argentina';
use Getopt::Long;
use Date::Manip;
use HTML::TreeBuilder;
use HTML::Entities; # parse entities
use IO::File;

use Encode qw(decode encode);

use HTTP::Cookies;
use LWP::UserAgent;
my $lwp = &initialise_ua();

use JSON::PP;

use XMLTV;
use XMLTV::Memoize;
use XMLTV::ProgressBar;
use XMLTV::Ask;
use XMLTV::Config_file;
use XMLTV::DST;
# So we are not affected by winter/summer timezone
$XMLTV::DST::Mode='none';

use XMLTV::Get_nice;
# We don't need random delays
$XMLTV::Get_nice::Delay = 0;

use XMLTV::Mode;
use XMLTV::Date;

BEGIN {
    if (int(Date::Manip::DateManipVersion) >= 6) {
        Date::Manip::Date_Init("SetDate=now,UTC");
    } else {
        Date::Manip::Date_Init("TZ=UTC");
    }
}

sub select_location();

use XMLTV::Usage <<END
$0: get Argentinian 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] [--getdetails] [--zone N]
To list channels: $0 --list-channels
To show capabilities: $0 --capabilities
To show version: $0 --version
END
;

# Root URL for province/location and channel/programming data
my $urlRoot = "http://www.buscador.cablevisionfibertel.com.ar/";

# Attributes of the root element in output.
my $HEAD = { 'source-info-url'     => $urlRoot,
             'source-data-url'     => $urlRoot,
             'generator-info-name' => 'XMLTV',
             'generator-info-url'  => 'http://xmltv.org/',
};

# Default output encoding
my $OUTPUT_ENCODING = "utf-8";

# Default language
my $LANG="es";

# Selected location
our %location;

# Global channel_data
my %channels;
my @channels;
our @ch_all;

# Global ProgID/Description Hash
my %Descriptions = ();

######################################################################

# 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_zone, $opt_list_channels, $opt_debug);

$opt_days   = 3; # default
$opt_offset = 0; # default
$opt_quiet  = 0; # default
$opt_zone   = 0; # default
$opt_debug  = 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,
           'zone=i'        => \$opt_zone,
           'list-channels' => \$opt_list_channels,
           'debug'         => \$opt_debug,
          )
  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_ar', $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_lines = XMLTV::Config_file::read_lines($config_file);
}
else {
    die;
}

if (($opt_zone != 0) && ($mode ne 'configure')) {
    warn "Zone override by user, using zone $opt_zone\n";
    %location = (
        id   => $opt_zone,
        name => 'USER SELECTED LOCATION'
    );
}

######################################################################

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

    %location = select_location();
    print CONF "location $location{id} $location{name}\n";

    # we need the channels data.
    %channels = get_channels(); # sets @ch_all

    # 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} = $OUTPUT_ENCODING;
my $writer = new XMLTV::Writer(%w_args);
$writer->start($HEAD);

######################################################################

# Read configuration
my $line_num = 1;
foreach (@config_lines) {
    ++ $line_num;
    next if not defined;

    if (/^location:?\s+(\S+)\s+([^\#]+)/) {
        %location=( id => $1, name=>$2 );
    }
    else {
        die "No location specified, run me with --configure\n"
          if not keys %location;

        # if (/^channel:?\s+(\S+)\s+(\S+)\s+([^\#]+)/) {
        if (/^channel:?\s+(\S+)\s+([^\#]+)/) {
            my $ch_did = $1;
            #    my $ch_sourceid = $2;
            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";
        }
    }
}

######################################################################

if ($mode eq 'list-channels') {
    # we need the channels data.
    %channels = get_channels(); # sets @ch_all
    foreach (@ch_all) {
        # channel writer only accepts id, display-name, url, icon
        delete $_->{'channel-id'};
        delete $_->{'channel-num'};
        $writer->write_channel($_);
    }
    $writer->end();
    exit();
}


######################################################################

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

######################################################################

# Begin the main program

# Assume the listings source uses ARG TIME (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;

die "No location specified, run me with --configure\n"
  if not keys %location;

die "No channels selected, run me with --configure\n"
  if (scalar @channels == 0);

my @to_get;

# print the <channel> info
foreach my $ch_did (@channels) {
    my $index=0;
    my $ch_name=$channels{$ch_did};
    my $ch_xid="$ch_did.cablevision";

    #       my $tot_ch=@ch_all;
    #       while (($tot_ch > $index) and (${$ch_all[$index]}{'id'} ne $ch_xid)) {
    #           $index=$index+1;
    #       }
    #       next if ($tot_ch <= $index);
    #
    #       my $ch_num=${ch_all[$index]}{'channel-num'};
    #       $writer->write_channel({ id => $ch_xid,
    #                                'display-name' => [ [ $ch_name ],
    #                                [ $ch_num ] ] });
    #       my $day=UnixDate($now,'%Q');
    #       for (my $i=0;$i<$opt_days;$i++) {
    #           push @to_get, [ $day, $ch_did, $ch_num ];
    #           #for each day
    #           $day=nextday($day); die if not defined $day;
    #       }

    $writer->write_channel({ 'id' => $ch_xid,
                             'display-name' => [ [ encode($OUTPUT_ENCODING, $ch_name) ] ] });
}

# progress bar
my $bar = new XMLTV::ProgressBar('getting listings', $opt_days)
  if not $opt_quiet;

# fetch and print the <programme> info
for (my $i = $opt_offset; $i < $opt_offset + $opt_days; $i++) {

    # the ajax fetches 6 stations at a time, so we'll emulate that
    my $numtoprocess = 6;
    for ( my $j=0; $j < scalar @channels; $j+=$numtoprocess ) {

        # array slice returns 'undef' for values oustide the array
        my @slice = @channels[$j..$j+$numtoprocess-1];
        my $schedule = process_table( $i, \@slice );   # $i is the day number (0...) to fetch
        foreach my $c ( keys %{$schedule} ) {
            foreach my $t ( keys %{$schedule->{$c}} ) {
                $writer->write_programme( $schedule->{$c}->{$t} );
            }
        }
    }

    update $bar if not $opt_quiet;
}

$bar->finish() if not $opt_quiet;
$writer->end();

######################################################################

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

######################################################################

# Subroutine definitions

# Select Location
sub select_location() {

    my $bar = new XMLTV::ProgressBar('getting list of Locations', 1)  if not $opt_quiet;

    my ($page, $data, $choice);

    # Get the available provinces
    #   http://buscador.cablevisionfibertel.com.ar/ProvinceSelector.aspx
    #
    $page = fetch_url($urlRoot.'ProvinceSelector.aspx');
    die $page if $page =~ /^Status:/;

    $data = get_json($page); $page = undef;
    my @provinces;
    foreach my $province (@{ $data->{'rows'} }) {
        push @provinces, $province->{'Provincia'};
    }

    $choice = ask_choice("Select your Province:", $provinces[0], @provinces);
    my $selectedprovince = $choice;

    # Get the available localities for the selected province
    #   http://buscador.cablevisionfibertel.com.ar/LocalitySelector.aspx?province=BUENOS%20AIRES
    #
    $page = fetch_url($urlRoot.'LocalitySelector.aspx?province='.$selectedprovince);
    die $page if $page =~ /^Status:/;

    $data = get_json($page); $page = undef;
    my %localities;
    foreach my $locality (@{ $data->{'rows'} }) {
        $localities{ $locality->{'Localidad'} } = $locality->{'Id'};
    }
    my @names;
    foreach (sort keys %localities) {
        push @names, $_;
    }

    $choice = ask_choice("Select your Locality:", $names[0], @names);
    my $selectedlocality = $choice;

    return ( id=> $localities{$choice}, name=>$choice );
}

# get channel listing
sub get_channels {
    my $bar = new XMLTV::ProgressBar('getting list of channels', 1)  if not $opt_quiet;
    my %channels;

    # Get the available channels for the selected locality
    #   http://buscador.cablevisionfibertel.com.ar/index.aspx?int=1&cl=945&pr=1

    my $page = fetch_url($urlRoot.'index.aspx?int=1&cl='.$location{'id'}.'&pr=1');
    die $page if $page =~ /^Status:/;
    store_page($page) if $opt_debug;

    my $tree = get_tree($page); $page = undef;

    my $selectlist = $tree->look_down( '_tag' => 'select', 'id' => 'ChannelChoice' );
    die 'Cannot find channels list' if !$selectlist;

    my @selectoptions = $selectlist->look_down( '_tag' => 'option' );

    # we also need to map the web channel id to the tv channel id
    #    <input type="hidden" name="ctl00$ContGral$idsChanels" id="ContGral_idsChanels"
    #         value="772,192,477,96,175,35,36,6,292,40,229,18,223,84,153,978,136,226,76,2292,77,45,33,15,37,22,19,366,11,164,17,178,9,57" />
    #    <input type="hidden" name="ctl00$ContGral$sintoniaChanels" id="ContGral_sintoniaChanels"
    #         value="7,8,9,10,11,12,13,14,15,16,17,18,19,20,20,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38" />

    my @webIds = split(/,/, $tree->look_down( '_tag' => 'input', 'id' => 'idsChanels' )->attr('value') );
    my @tvIds = split(/,/, $tree->look_down( '_tag' => 'input', 'id' => 'sintoniaChanels' )->attr('value') );
    my %webIds;
    for (my $i=0; $i<scalar @webIds; $i++) {
        $webIds{$webIds[$i]} = $tvIds[$i];
    }

    foreach my $option (@selectoptions) {
        my $channel_id = $option->attr('value');
        next if $channel_id eq '-1' || $channel_id eq '0' || $channel_id eq '';

        my $channel_num = $webIds{$channel_id};
        my $channel_name = $option->as_text();
        $channels{$channel_num} = $channel_name;
        push @ch_all, {
            'display-name' => [[ $channel_name, $LANG ]],
            'channel-num'  => $channel_num,
            'channel-id'   => $channel_id,
            'id'           => "$channel_num.cablevision",
        };
    }

    $tree->delete;
    die "no channels could be found" if not keys %channels;

    #use Data::Dumper; print Dumper(\%channels);exit;
    #use Data::Dumper; print Dumper(@ch_all);exit;

    update $bar if not $opt_quiet;
    $bar->finish() if not $opt_quiet;
    return %channels;
}

# process_table: fetch a URL and process it
#
# arguments:
#    Date::Manip object giving the day to grab
#    xmltv id of channel
#    id of channel from cablevision
#
# returns: list of the programme hashes to write
sub process_table {
    my ($day, $chanstoprocess) = @_;

    # Get the programme schedule for the selected channels for the param day
    my %r;

    # First we need the container for the Location
    #   http://www.buscador.cablevisionfibertel.com.ar/index.aspx?int=1&cl=847&pr=1&dia=4

    my $url = $urlRoot.'index.aspx?int=1&cl='.$location{'id'}.'&pr=1&dia='.$day;
    my $page = fetch_url($url);
    die $page if $page =~ /^Status:/;
    store_page($page) if $opt_debug;

    my $tree = get_tree($page); $page = undef;

    # Now we need to emulate the AJAX call for the grid
    #
    #   "/TVGridWS/TvGridWS.asmx/ReloadGrid"
    #    "{"daySel":"0","clasDigId":"136","signalsIdsReceived":"343,3,192,709,477,62|2,3,4,5,6,7","digHd":1}"
    #
    #         function pageLoad() {
    #            blackin();
    #            var elementAux1 = document.getElementById('ContGral_digClasId');
    #            var elementAux2 = document.getElementById('ContGral_daySelected');
    #
    #            TVGridWS.ReloadGrid(elementAux2.value, elementAux1.value, idsSignalText1 + "|" + sintoniaSignal, (digHd.value != "true") ? 1 : 2, onSuccessFirst);
    #        }
    #
    #    <input type="hidden" name="ctl00$ContGral$digClasId" id="ContGral_digClasId" value="136" />
    #    <input type="hidden" name="ctl00$ContGral$digHd" id="ContGral_digHd" value="false" />
    #    <input type="hidden" name="ctl00$ContGral$daySelected" id="ContGral_daySelected" value="0" />

    $url = $urlRoot.'TVGridWS/TvGridWS.asmx/ReloadGrid';
    my $clasDigId = $tree->look_down( '_tag' => 'input', 'id' => 'digClasId' )->attr('value');
    my $daySel = $tree->look_down( '_tag' => 'input', 'id' => 'daySelected' )->attr('value');
    my $digHd = $tree->look_down( '_tag' => 'input', 'id' => 'digHd' )->attr('value') ne 'true' ? 1 : 2;
    my $idsSignalText1 = $tree->look_down( '_tag' => 'input', 'id' => 'idsChanels' )->attr('value');
    my $sintoniaSignal = $tree->look_down( '_tag' => 'input', 'id' => 'sintoniaChanels' )->attr('value');

    #   <input type="hidden" name="ctl00$ContGral$idsChanels" id="ContGral_idsChanels"
    #         value="772,192,477,96,175,35,36,6,292,40,229,18,223,84,153,978,136,226,76,2292,77,45,33,15,37,22,19,366,11,164,17,178,9,57" />
    #   <input type="hidden" name="ctl00$ContGral$sintoniaChanels" id="ContGral_sintoniaChanels"
    #         value="7,8,9,10,11,12,13,14,15,16,17,18,19,20,20,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38" />

    my @webIds = split(/,/, $idsSignalText1 );
    my @tvIds = split(/,/, $sintoniaSignal );
    my ( %webIdsHash, %tvIdsHash );
    for (my $i=0; $i<scalar @webIds; $i++) {
        $webIdsHash{$webIds[$i]} = $tvIds[$i];
        $tvIdsHash{$tvIds[$i]} = $webIds[$i];
    }
    my ($idsSignalText1_1, $sintoniaSignal_1) = ('','');
    foreach (@{$chanstoprocess}) {
        next if !defined $_;  # see the @channels array slice above
        $sintoniaSignal_1 .= ($sintoniaSignal_1 ne '' ? ',' : '') . $_;
        $idsSignalText1_1 .= ($idsSignalText1_1 ne '' ? ',' : '') . $tvIdsHash{$_};
    }

    # 2014-10-19 site now only returns a 4 hour window
    for (my $i = 1; $i <= 7; $i++) {

        my $vars = [ "daySel"=>$daySel, "clasDigId"=>$clasDigId, "signalsIdsReceived"=>$idsSignalText1_1.'|'.$sintoniaSignal_1, "digHd"=>$digHd, "hourSel"=>$i, "sitio"=>"" ];
        #use Data::Dumper; print STDERR Dumper($vars);
        $page = fetch_url($url, 'post', $vars);
        die $page if $page =~ /^Status:/;
        store_page($page) if $opt_debug;

        use XML::Parser;
        my $xml = XML::Parser->new(Style => 'Tree')->parse($page);
        my $html = $xml->[1]->[2];
        #print Dumper($html);
        my $res_tree = HTML::TreeBuilder->new_from_content($html);
        #print Dumper($res_tree);
        undef $xml; undef $html;

        my @progs = $res_tree->look_down( '_tag' => 'div', 'class' => 'programa' );
        die 'no programmes found' if !scalar @progs;
        #print Dumper(@progs);
        #print STDERR "\nprogs found ".scalar @progs."\n";

        foreach my $cur (@progs) {
            my $p = make_programme_hash($cur, \%webIdsHash, \%tvIdsHash);
            next if (defined $p && $p eq 'false');
            if (not $p) {
                    require Data::Dumper;
                    my $d = Data::Dumper::Dumper($cur);
                    warn "cannot write programme on day $day:\n$d\n";
            }

            else {
                $r{ $p->{'channel'} }{ $p->{'start'} } = $p;        # prevent duplicates
            }
        }
    }

    return \%r;
}

# Hashes the program for future saving in XMLTV format
sub make_programme_hash {
    my ( $prog, $webIds, $tvIds ) = @_;

    #  <div id="accordion38201" class="programa" style="width:204px;left:2244px">
    #    <span class="title" onmouseover="pintarCanal(343);" onmouseout="despintarCanal();">Noticias y consumidores</span>
    #    <div id="programaInterno" class="programaInterno" style="display: none;">
    #      <span class="category">Noticias</span>
    #      <span class="dataProgram">Argentina  |  Con Susana Andrada, Myriam Bunin</span>
    #      <div class="diaGrilla">Hoy martes 10 de diciembre 11:00:00Hs</div>
    #      <img class="chapaParentalProgram" src="images/parental/PG.png" alt="Se recomienda orientación parental" title="Se recomienda orientación parental">
    #      <div class="duracionGrilla">Duración: 60min</div>
    #      <a rel="#overlay_ficha" href="FichaContent.aspx?id=38201&amp;idSig=343" class="masinfoGrid"></a>
    #    </div>
    #  </div>

    # <div id="accordion27254" class="programa" style="width:102px;left:1836px">
    #   <span class="title" onmouseover="pintarCanal(178);" onmouseout="despintarCanal();">Boys Toys<br>
    #      <span class="titleSeason">Temporada: 3</span>
    #      <span class="titleChapter">Episodio: 311</span>
    #   </span>
    #   <div id="programaInterno" class="programaInterno" style="display: none;">
    #      <span class="category">Variedades</span>
    #      <span class="dataProgram">2013 </span>
    #      <span class="sinopsisShort">Los juguetes más innovadores del siglo XXI. Recorremos el mundo para traerte los mejores aparatos técnicos, productos súper exclusivos y vehículos ultra poderosos. Prepárate para experimentar lo último en juguetes tecnológicos.</span>
    #      <div class="diaGrilla">Hoy viernes 13 de diciembre 09:00:00Hs</div>
    #      <img class="chapaParentalProgram" src="images/parental/NR.png" alt="No clasificada" title="No clasificada">
    #      <div class="duracionGrilla">Duración: 30min</div>
    #      <a rel="#overlay_ficha" href="FichaContent.aspx?id=27254&amp;idSig=178" class="masinfoGrid"></a>
    #   </div>
    # </div>

    # As at 2014-10-19:
    #  <div id="accordion33892" class="programa" style="width: 204px; left: 2652px; height: 50px; display: block; ">
    #    <div id="programaInterno" class="programaInterno" style="display: none; "></div>
    #    <div class="title" onmouseover="pintarCanal(292);" onmouseout="despintarCanal();" style="">
    #      <span>Visión 7 - Mediodía</span><br>
    #    </div>
    #  </div>
    #
    # Programme details needs a separate ajax call ! (sheesh these JS programmers crack me up; just because you *can* do something via ajax doesn't mean
    #                                                                   to say you *should* - technology gone mad!)
    #
    # <img class="imgAcordeon" src="https://buscador.cablevisionfibertel.com.ar/imagesFilms/charlie_and_the_chocolate_factory1.jpg_h.jpg" />
    #   <div class="contenedorDatosAcordeon">
    #     <span class="category">Noticias</span>
    #     <span class="dataProgram">Argentina  Con Agustina Díaz, Roberto Gómez Ragozza</span>
    #     <span class="sinopsisShort">El noticiero de la TV Pública refleja la actualidad desde una visión federal y latinoamericana, que defiende el interés
    #                   comunitario. Visión Siete es sinónimo de noticias para todos.</span>
    #     <div class="diaGrilla">lunes 20 de octubre | 13:00:00Hs | Duración: 60min</div>
    #     <a alt="Mas info" title="Mas info" rel="#overlay_ficha" href="FichaContent.aspx?id=33892&amp;idSig=292" class="masinfoGrid"></a>
    #     <a alt="Agendar en Gmail" title="Agendar en Gmail" href="https://www.google.com/calendar/render?action=TEMPLATE&amp;text=Ver Visión 7 -
    #                   Mediodía&amp;dates=20141020T160000Z/20141020T170000Z&amp;details=El noticiero de la TV Pública refleja la actualidad desde una visión federal
    #                   y latinoamericana, que defiende el interés comunitario. Visión Siete es sinónimo de noticias para todos.&amp;sf=true&amp;output=xml"
    #                   target="_blank" class="agendar"></a>
    #     <a alt="Compartir en Facebook" title="Compartir en Facebook" target="_blank" class="facebook"
    #                   href="http://www.facebook.com/sharer.php?s=100&amp;p[url]=http%3A//clientes.cablevisionfibertel.com.ar%2fBuscador"></a>
    #     <a alt="Compartir en Twitter" title="Compartir en Twitter" target="_blank"
    #                   href="https://twitter.com/share?url=https://clientes.cablevisionfibertel.com.ar/Buscador&amp;text=&amp;count=none" class="twitter"
    #                   data-lang="es"></a>
    #   </div>
    #

    my $cur;
    my %temp;
    $temp{'title'} = $prog->look_down( '_tag' => 'div', 'class' => 'title' );
    if (!defined $temp{'title'}) {          # empty div... try span
        $temp{'title'} = $prog->look_down( '_tag' => 'span', 'class' => 'title' );
        if (!defined $temp{'title'}) {          # empty span
            return 'false';
        }
    }

    my $url = $urlRoot.'TVGridWS/TvGridWS.asmx/GetProgramDataAcordeon';
    my $idSignal = $prog->parent->attr('idsignal');
    my $idEvent = $prog->attr('id');
    $idEvent =~ s/accordion//;

    my $vars = [ "idEvent"=>$idEvent, "idSignal"=>$idSignal, "sitio"=>"" ];
    #use Data::Dumper; print STDERR Dumper($vars);
    my $page = fetch_url($url, 'post', $vars);
    die $page if $page =~ /^Status:/;
    store_page($page) if $opt_debug;

    use XML::Parser;
    my $xml = XML::Parser->new(Style => 'Tree')->parse($page);
    my $html = $xml->[1]->[2];
    #print Dumper($html);
    my $res_tree = HTML::TreeBuilder->new_from_content($html);
    #print Dumper($res_tree);
    undef $xml; undef $html;

    $temp{'ch'} = $temp{'title'}->attr('onmouseover');
    $temp{'series'} = $temp{'title'}->look_down( '_tag' => 'span', 'class' => 'titleSeason' );
    $temp{'episode'} = $temp{'title'}->look_down( '_tag' => 'span', 'class' => 'titleChapter' );

    $temp{'start'} = $res_tree->look_down( '_tag' => 'div', 'class' => 'diaGrilla' );
    $temp{'duration'} = $res_tree->look_down( '_tag' => 'div', 'class' => 'duracionGrilla' );
    $temp{'data'} = $res_tree->look_down( '_tag' => 'span', 'class' => 'dataProgram' );
    $temp{'desc'} = $res_tree->look_down( '_tag' => 'span', 'class' => 'sinopsisShort' );
    $temp{'category'} = $res_tree->look_down( '_tag' => 'span', 'class' => 'category' );
    $temp{'rating'} = $res_tree->look_down( '_tag' => 'img', 'class' => 'chapaParentalProgram' );

    $cur->{'title'} = $temp{'title'}->as_text;
    if ($temp{'duration'}) {        # pre 2014-10-19 format just in case!
        $cur->{'start'} = $temp{'start'}->as_text;
        $cur->{'duration'} = $temp{'duration'}->as_text;
    }
    else {
        my @s = split(/\|/, $temp{'start'}->as_text);  # e.g. "lunes 20 de octubre | 13:00:00Hs | Duración: 60min"
        if (scalar @s) {
            $cur->{'start'} = $s[0] . (defined $s[1] ? ' '.$s[1] : '');
            $cur->{'start'} =~ s/\|//;
            $cur->{'duration'} = (defined $s[2] ? $s[2] : '');
        }  # else barf
    }
    $cur->{'desc'} = $temp{'desc'}->as_text if $temp{'desc'};
    $cur->{'category'} = $temp{'category'}->as_text if $temp{'category'};

    if (defined $temp{'series'}) {
        my $seriestext = $temp{'series'}->as_text;
        $seriestext =~ s/(Temporada:|T:)\s//;
        if ($seriestext =~ /^(\d+)$/ ) {
            $cur->{'series'} = $1  if $1 ne '';
        }
        elsif ($seriestext ne '') {
            $cur->{'sub-title'} = $seriestext;
        }
        $temp{'series'}->detach();
    }

    if (defined $temp{'episode'}) {
        my $episodetext = $temp{'episode'}->as_text;
        $episodetext =~ s/(Episodio:|E:)\s//;
        if ($episodetext =~ /^(\d+)/ ) {
            $cur->{'episode'} = $1  if $1 ne '';
            if ( $episodetext =~ /^(\d+) - (.*)$/ ) {
                $cur->{'sub-title'} = (defined $cur->{'sub-title'} ? $cur->{'sub-title'}.' : ' : '') . $2;
            }
        }
        elsif ($episodetext ne '') {
            $cur->{'sub-title'} = (defined $cur->{'sub-title'} ? $cur->{'sub-title'}.' : ' : '') . $episodetext;
        }
        $temp{'episode'}->detach();
    }
    $cur->{'title'} = $temp{'title'}->as_text;

    #print "$temp{'ch'} $cur->{'start'} $cur->{'title'} \n";

    if ( defined $temp{'data'} ) {
        my $data = $temp{'data'}->as_text;
        if ( $data =~ /^(.*?)\s?((19|20)\d\d)(\s\|\s)?(.*?)$/ ) {
            $cur->{'country'} = $1 if $1 ne '';
            $cur->{'date'} = $2 if $2 ne '';
            if ($5 ne '' && $5 ne ' ') {
                my $credits = $5;
                $credits =~ s/Con\s/,/ ;
                my @credits = split(',',$credits);
                s{^\s+|\s+$}{}g foreach @credits;   # strip leading & trailing spaces
                foreach (@credits) {
                    push @{$cur->{'credits'}}, encode($OUTPUT_ENCODING, $_) if $_ ne '';
                }
                undef $credits;
            }
        }
    }

    if ( defined $temp{'rating'} ) {
        my $rating = $temp{'rating'}->attr('src');
        if ( $rating =~ /.*\/(.*?)\.png/ ) {        # src="images/parental/NR.png"
            $cur->{'rating'} = $1;
        }
    }

    if (defined $cur->{'series'} || defined $cur->{'episode'}) {
        $cur->{'episode-num'} = ( defined $cur->{'series'} && $cur->{'series'} > 0 ? $cur->{'series'} -1 : '' ) . '.' .
                ( defined $cur->{'episode'} && $cur->{'episode'} > 0 ? $cur->{'episode'} -1 : '' ) . '.';
    }

    $temp{'ch'} =~ /pintarCanal\((\d*)\)/;
    my $ch_xmltv_id = $webIds->{$1}.'.cablevision';

    use Date::Language;
    my $lang = Date::Language->new('Spanish');
    # Crappy Date::Language won't parse things like 'Hoy martes 10 de diciembre 2013 11:00' !!
    $cur->{'start'} =~ s/(Hoy|Manana)//;
    $cur->{'start'} =~ s/:00Hs//;
    $cur->{'start'} =~ s/de//;
    $cur->{'starttime'} = $lang->str2time( $cur->{'start'} );
    # durations always seem to be in minutes (e.g. "Duración: 150min")
    my $duration;
    if ($cur->{'duration'} =~ /Duración:\s(\d*)min/ ) {
        $duration = $1;
        $cur->{'stoptime'} = $cur->{'starttime'} + ($duration * 60);
    }

    my %prog;
    $prog{channel}=$ch_xmltv_id;
    $prog{title}=[ [ encode($OUTPUT_ENCODING, $cur->{'title'}), $LANG ] ];
    $prog{'sub-title'}=[ [ encode($OUTPUT_ENCODING, $cur->{'sub-title'}), $LANG ] ] if defined $cur->{'sub-title'};
    $prog{'episode-num'} = [[ $cur->{'episode-num'}, 'xmltv_ns' ]] if defined $cur->{'episode-num'};
    $prog{start} = POSIX::strftime("%Y%m%d%H%M%S", gmtime( $cur->{'starttime'} )) . ' -0300';
    $prog{stop} = POSIX::strftime("%Y%m%d%H%M%S", gmtime( $cur->{'stoptime'} )) . ' -0300'  if defined $cur->{'stoptime'};
    $prog{desc}=[ [ encode($OUTPUT_ENCODING, $cur->{'desc'}), $LANG ] ] if defined $cur->{'desc'} && $cur->{'desc'} ne '';
    $prog{credits}->{'actor'} = $cur->{'credits'}  if defined $cur->{'credits'} && (scalar @{$cur->{'credits'}} > 0);
    $prog{category}=[ [ encode($OUTPUT_ENCODING, $cur->{category}), $LANG ] ] if defined $cur->{category};
    $prog{date}=$cur->{date} if defined $cur->{date};
    $prog{country}=[ [ encode($OUTPUT_ENCODING, $cur->{country}), $LANG ] ] if defined $cur->{country};
    $prog{rating}=[ [ $cur->{rating}, $LANG ] ] if defined $cur->{rating};
    return \%prog;
}

# Initialise LWP::UserAgent
sub initialise_ua {
    #my $ua = LWP::UserAgent->new(keep_alive => 1);
    my $ua = LWP::UserAgent->new;

    # Cookies
    my $cookies = HTTP::Cookies->new;
    $ua->cookie_jar($cookies);

    # Define user agent type
    #$ua->agent('Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0');
    # Define timouts
    $ua->timeout(240);
    # Use proxy if set in http_proxy etc.
    $ua->env_proxy;

    return $ua;
}

# Fetch a page
sub fetch_url ( $$$ ) {
    my $url = shift;

    print STDERR "Retrieving URL: " . $url . "\n" if $opt_debug;

    my $method = shift;
    my $varhash = shift;
    my $res;
    if (defined $method && lc($method) eq 'post') {
        $res = $lwp->post($url, $varhash);
    }
    else {
        $res = $lwp->get($url);
    }
    if (!$res->is_success) {
        # error - format as a valid http status line
        return "Status: ".$res->status_line;
    }

    return decode("UTF-8", $res->content);
}

# Parse a JSON response
sub get_json ( $ ) {
    my $page = shift;
    my $data = JSON::PP->new()->decode($page);
    $page = undef;

    return $data;
}

# Parse HTML into a tree
sub get_tree ( $ ) {
    my $page = shift;
    my $tree = HTML::TreeBuilder->new();
    $tree->utf8_mode(0);
    $tree->parse($page) or die "Cannot parse content\n";
    $tree->eof;

    return $tree;
}

# Store a page (used for debugging)
sub store_page ( $ ) {
    my $data = shift;
    my $fn   = 'grab'.time();
    my $fhok = open my $fh, '>', $fn or warning("Cannot open file $fn");
    print $fh $data;
    close $fh;

    return;
}
