/usr/bin/tv_grab_fr is in xmltv-util 0.5.67-0.1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 | #!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
if 0; # not running under some shell
eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
if 0; # not running under some shell
=head1 NAME
tv_grab_fr - Grab TV listings for France.
=head1 SYNOPSIS
To configure:
tv_grab_fr --configure [--config-file FILE] [--gui OPTION]
To grab listings:
tv_grab_fr [--config-file FILE] [--output FILE] [--days N]
[--offset N] [--quiet] [--debug]
To list available channels:
tv_grab_fr --list-channels
To show capabilities:
tv_grab_fr --capabilities
To show version:
tv_grab_fr --version
Help:
tv_grab_fr --help
=head1 DESCRIPTION
Output TV listings for many channels available in France (Orange,
Free, cable/ADSL/satellite, Canal+ Sat). The data comes from
telestar.fr. The default is to grab 7 days, but there are usually
14 days of listings available from the current day onwards.
Grabbing speed varies, but is typically 1-2 minutes/day/channel.
B<--configure> Choose which bouquets/channels to grab listings data for.
B<--list-channels> List all available channels.
B<--config-file FILE> Use FILE as config file instead of the default config
file. This allows for different config files for different applications.
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 Term::ProgressBar.
B<--output FILE> Write to FILE rather than standard output.
B<--days N> Grab N days (default 7, maximum 14) starting from today.
B<--offset N> Start grabbing N days from today, rather than starting
today.
B<--quiet> Suppress the progress messages normally written to standard
error.
B<--debug> Provide additional debugging messages during processing.
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 current version of the script was rewritten by Nick Morrott,
knowledgejunkie at gmail dot com, to support the new telestar.fr site.
The original author was Sylvain Fabre, centraladmin at lahiette dot com,
with patches from:
- Francois Gouget, fgouget at free dot fr,
- Geoff Westcott, honir999 at gmail dot com,
- Karl Dietz, dekarl at spaetfruehstuecken dot org,
- Niel Markwick, nielm at bigfoot dot com,
- Zubrick, zubrick at number6 dot ch,
- and many more!
=cut
# TODO
# ====
#
# - convert to use ParseOptions
#
# - perhaps we should internationalize messages and docs?
#
# - try to detect the language based on the country so we can make use of the
# set the VO, VF and original title markers
#
# - investigate how to better handle France 5 and Arte as they share a single
# channel for over-the-air broadcasts, but each have their own channel on
# cable, satellite and ADSL. See the thread at:
# http://sourceforge.net/mailarchive/message.php?msg_id=15181920
#
# - Add caching via HTTP::Cache::Transparent
use XMLTV::Usage <<END
$0: get French television listings in XMLTV format
To configure: tv_grab_fr --configure [--config-file FILE]
To grab listings: tv_grab_fr [--config-file FILE] [--output FILE] [--days N]
[--offset N] [--quiet] [--debug]
To list available channels: tv_grab_fr --list-channels
To show capabilities: tv_grab_fr --capabilities
To show version: tv_grab_fr --version
Help: tv_grab_fr --help
END
;
use warnings;
use strict;
use XMLTV::Version '$Id: tv_grab_fr,v 1.107 2015/08/15 06:09:48 knowledgejunkie Exp $ ';
use XMLTV::Capabilities qw/baseline manualconfig cache/;
use XMLTV::Description 'France';
use XMLTV;
use XMLTV::Ask;
use XMLTV::ProgressBar;
use XMLTV::Mode;
use XMLTV::Config_file;
use XMLTV::DST;
use XMLTV::Get_nice qw(get_nice);
$XMLTV::Get_nice::ua->parse_head(0);
$XMLTV::Get_nice::FailOnError = 0;
use XMLTV::Memoize;
XMLTV::Memoize::check_argv('XMLTV::Get_nice::get_nice_aux');
use DateTime;
use DateTime::Duration;
use Encode qw(decode_utf8 encode_utf8);
use Getopt::Long;
use HTML::Entities;
use HTML::TreeBuilder;
use IO::File;
#***************************************************************************
# Main declarations
#***************************************************************************
my $ROOT_URL = 'http://www.telestar.fr';
my $GRID_FOR_CHANNEL = "$ROOT_URL/programme-tv/";
my $GRID_BY_CHANNEL_PER_DAY = "$ROOT_URL/programme-tv/grille-chaine/";
my $ENCODING = 'utf-8';
my $LANG = "fr";
my $MAX_RETRY = 5;
my %GridType = ( "Grandes Chaines" => "grandes-chaines-et-tnt",
"Orange" => "orange",
"Free" => "free",
"Bouygues" => "bouygues",
"SFR" => "sfr",
"Numéricable" => "numericable",
"Canal+" => "canal",
"Câble, ADSL, Satellite" => "cable-adsl-satellite",
"Canal+ et Canalsat" => "canal-et-canalsat",
"Belgique" => "belgique",
"Hors bouquet" => "hors-bouquet",
);
#***************************************************************************
# Global variables allocation according to options
#***************************************************************************
my ( $opt_config_file, $opt_days, $opt_offset, $opt_output,
$opt_gui, $opt_quiet, $opt_list_channels, $opt_configure,
$opt_help, $opt_debug, );
# Default to non-quiet, non-debug mode
$opt_quiet = 0;
$opt_debug = 0;
# Although the website is able to provide up to fourteen days of listings,
# parsing pages is slow so we set the default grab to 7 days. You can
# specify up to 14 days via the --days option.
my $default_opt_days = 7;
# Default to STDOUT
$opt_output = '-';
GetOptions( 'days=i' => \$opt_days,
'help' => \$opt_help,
'output=s' => \$opt_output,
'offset=i' => \$opt_offset,
'quiet' => \$opt_quiet,
'configure' => \$opt_configure,
'gui:s' => \$opt_gui,
'debug' => \$opt_debug,
'config-file=s' => \$opt_config_file,
'list-channels' => \$opt_list_channels,
)
or usage(0);
#***************************************************************************
# Options processing, warnings, checks and default parameters
#***************************************************************************
die 'Number of days must not be negative' if (defined $opt_days && $opt_days < 0);
die 'Number of days must be less than 15' if (defined $opt_days && $opt_days > 14);
die 'Offset must not be negative' if (defined $opt_offset && $opt_offset < 0);
die 'Offset must be less than 14' if (defined $opt_offset && $opt_offset > 13);
die 'Offset+days must be less than 15' if (defined $opt_offset && defined $opt_days && ($opt_offset + $opt_days > 14));
die 'Error: You cannot specify --quiet with --debug, exiting' if ($opt_quiet && $opt_debug);
usage(1) if $opt_help;
XMLTV::Ask::init($opt_gui);
# The days/offset options can be used, but we default them if not set.
$opt_offset = 0 if not defined $opt_offset;
$opt_days = $default_opt_days if not defined $opt_days;
if ( (($opt_offset + $opt_days) > $default_opt_days) or ($opt_offset > $default_opt_days) ) {
$opt_days = $default_opt_days - $opt_offset;
if ($opt_days < 0) {
$opt_offset = 0;
$opt_days = $default_opt_days;
}
say <<END
The website does not handle more than $default_opt_days days.
So, the grabber is now configured with --offset $opt_offset and --days $opt_days
END
;
}
#***************************************************************************
# Last init before doing real work
#***************************************************************************
# Detect if we are in configure mode
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_fr', $opt_quiet);
#***************************************************************************
# Subroutines
#***************************************************************************
sub get_dates_to_grab;
sub get_listings_page_start_date;
sub get_data_for_available_days;
sub get_data_for_filtered_days;
sub process_channel_row;
sub process_program;
sub get_channels;
sub debug_print;
sub trim;
sub tidy_html;
sub parse_name_list;
sub get_tree;
sub get_country_code;
sub get_date_today;
sub get_date_today_with_offset;
#***************************************************************************
# Configure mode
#***************************************************************************
if ($mode eq 'configure') {
XMLTV::Config_file::check_no_overwrite($config_file);
open(CONF, ">$config_file") or die "Cannot write to $config_file: $!";
# Choose which bouquets to configure channels for
my @gts = sort keys %GridType;
my @gtnames = map { $GridType{$_} } @gts;
my @gtqs = map { "Get channels type : $_?" } @gts;
my @gtwant = ask_many_boolean(1, @gtqs);
# Get channels for each requested bouquet
my $bar = new XMLTV::ProgressBar('getting channel lists',
scalar grep { $_ } @gtwant)
if not $opt_quiet;
my %channels_for;
foreach my $i (0 .. $#gts) {
my ($gt, $gtw, $gtname) = ($gts[$i], $gtwant[$i], $gtnames[$i]);
next if not $gtw;
my %channels = get_channels( $gtname );
print STDERR 'WARNING: No channels could be found'."\n" if not %channels;
$channels_for{$gt} = \%channels;
update $bar if not $opt_quiet;
}
$bar->finish() if not $opt_quiet;
my %asked;
foreach (@gts) {
my $gtw = shift @gtwant;
my $gtname = shift @gtnames;
if ($gtw) {
my %channels = %{$channels_for{$_}};
say "Channels for $_";
# Ask about each channel (unless already asked).
my @chs = grep { not $asked{$_}++ } sort keys %channels;
my @names = map { $channels{$_}{name} } @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;
# Print a config line, but comment it out if channel not wanted.
print CONF '#' if not $w;
print CONF "channel $channels{$_}{name}\n";
}
}
}
close CONF or warn "cannot close $config_file: $!";
say("Finished configuration.");
exit();
}
#***************************************************************************
# Check mode
#***************************************************************************
die if $mode ne 'grab' and $mode ne 'list-channels';
#***************************************************************************
# Prepare the XMLTV writer object
#***************************************************************************
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} = "$ENCODING";
$w_args{days} = "$opt_days";
$w_args{offset} = "$opt_offset";
$w_args{cutoff} = "000000";
my $writer = new XMLTV::Writer(%w_args);
$writer->start
({ 'source-info-url' => "$ROOT_URL/",
'source-data-url' => "$ROOT_URL/",
'generator-info-name' => 'XMLTV',
'generator-info-url' => 'http://xmltv.org/',
});
#***************************************************************************
# List channels only case
#***************************************************************************
if ($mode eq 'list-channels') {
# Get a list of all available channels, for all bouquets
#
# Unlike channel configuration in --configure, we do not ask the user
# for any input to select bouquets, we list everything
my @gts = sort keys %GridType;
my @gtnames = map { $GridType{$_} } @gts;
my %seen;
foreach (@gts) {
my $gtname = shift @gtnames;
my %channels = get_channels( $gtname );
print STDERR 'WARNING: No channels could be found'."\n" if (scalar(keys(%channels)) == 0);
foreach my $ch_did (sort(keys %channels)) {
my $ch_xid = $ch_did.".telestar.fr";
$writer->write_channel({ id => $ch_xid,
'display-name' => [[encode_utf8( $channels{$ch_did}{name} )]],
})
unless $seen{$ch_xid}++;
}
}
$writer->end();
exit();
}
#***************************************************************************
# Only grabbing beyond here...
#***************************************************************************
die if $mode ne 'grab';
#***************************************************************************
# Read configuration file
#***************************************************************************
my @config_lines;
@config_lines = XMLTV::Config_file::read_lines($config_file);
my (%channels, $chname);
my $line_num = 1;
foreach (@config_lines) {
++ $line_num;
next if not defined;
# We store the channel name as the identifier in the config file
if (/^channel (\w+)$/) {
$chname = $1;
$channels{$line_num} = { 'name'=>$chname };
} else {
warn "$config_file:$line_num: bad line $_\n";
}
}
#***************************************************************************
# Process requested days
#***************************************************************************
warn "No working channels configured, so no listings\n" if not %channels;
my $script_duration = time();
my @to_get;
my $listings_page_start_date = get_listings_page_start_date();
# The telestar.fr website provides up to 14 days of listings for each
# channel. The first 7 days from today are on one page, the second
# 7 days on another.
debug_print("\nCreating list of pages to grab based on configured channels...");
foreach my $idx (sort { $a <=> $b } keys %channels) {
my $chname = $channels{$idx}{name};
my $xmltvid = $chname.".telestar.fr";
my $url;
my $dayoff;
$writer->write_channel({ id => $xmltvid,
'display-name' => [[encode_utf8( $chname )]],
});
debug_print("\n <channel>: xmltvid=$xmltvid, name=$chname");
debug_print(" Creating list of pages for " . $chname);
my @urls;
if ($opt_offset < 7) {
$url = $GRID_BY_CHANNEL_PER_DAY . $chname;
push @urls, $url;
debug_print( " adding: url=$url" );
}
if ($opt_offset + $opt_days >= 7) {
$dayoff = get_date_today_with_offset(7)->strftime("%d-%m-%Y");
$url = $GRID_BY_CHANNEL_PER_DAY . "$chname/$dayoff";
push @urls, $url;
debug_print( " adding: url=$url" );
}
push @to_get, [\@urls, $chname];
}
debug_print("\nGenerating list of dates...\n");
my $dates_to_grab = get_dates_to_grab();
my $bar;
if (not $opt_quiet and not $opt_debug) {
$bar = new XMLTV::ProgressBar('getting listings', scalar @to_get);
}
debug_print("\nProcessing list of pages to grab...\n");
CHANNEL:
foreach (@to_get) {
# Each item in the @to_get listref is a listref for each channel
# (a list of URLS to get, and the channel name).
my ($urls, $chname) = @$_;
my @data_for_available_days;
foreach my $url (@$urls) {
push @data_for_available_days, get_data_for_available_days($url, $chname);
}
my $data_for_filtered_days = get_data_for_filtered_days(\@data_for_available_days);
debug_print(" Have " . scalar @{$data_for_filtered_days} . " days of data\n");
debug_print(" Have " . scalar @{$dates_to_grab} . " dates to grab for\n");
if (scalar @{$data_for_filtered_days} != scalar @{$dates_to_grab}) {
if (not $opt_quiet) {
print STDERR "ERROR: Not enough data available for requested number of days\n";
print STDERR " Have " . scalar @{$data_for_filtered_days} . " days of data\n";
print STDERR " Need " . scalar @{$dates_to_grab} . " days of data\n";
print STDERR " Skipping channel\n";
}
next CHANNEL;
}
# Store individual programmes in a list and write each channel in full.
# No stop times are given in the listings (only inaccurate durations), so
# we can use the start time of a following programme as the stop time of
# the previous programme. (May fail if channel does not have listings
# for full 24hrs).
my @programmes = ();
foreach my $i (0 .. (scalar @{$data_for_filtered_days} - 1)) {
debug_print(" Getting listings for day " . ($i+1));
push @programmes, @{ process_channel_row( $chname, $data_for_filtered_days->[$i], $dates_to_grab->[$i] ) };
}
# Update programme stop times
@programmes = @{ update_programme_stop_times(\@programmes) };
# Write the channel's programme elements to output
foreach my $prog (@programmes) {
$writer->write_programme($prog);
}
if (not $opt_quiet and not $opt_debug) {
update $bar;
}
}
$writer->end();
if (not $opt_quiet and not $opt_debug) {
$bar->finish();
}
# Print the script's execution time
$script_duration = time() - $script_duration;
print STDERR "Grabber process finished in " . $script_duration . " seconds.\n" if not $opt_quiet;
#***************************************************************************
# Subroutines
#***************************************************************************
sub get_dates_to_grab {
my @dates_to_grab = ();
# First date to grab listings for
my $grab_start_date = get_date_today_with_offset($opt_offset);
push @dates_to_grab, $grab_start_date;
# Remaining dates to grab listings for
for (my $offset = 1; $offset < $opt_days; $offset++) {
push @dates_to_grab, $grab_start_date + DateTime::Duration->new( days => $offset );
}
debug_print("Will grab listings for following dates:");
if ($opt_debug) {
foreach (@dates_to_grab) {
# print STDERR " " . $_->strftime("%a, %d %b %Y %H:%M:%S %z") . "\n";
print STDERR " " . $_->strftime("%a, %d %b %Y") . "\n";
}
}
debug_print("\n");
return \@dates_to_grab;
}
# Each week of listings for a channel is provided on a single page.
#
# * if we are grabbing with no starting offset (today) or an offset less than
# 7 days into the future, we'll start with the default page (starts today).
#
# * if we are grabbing with a starting offset of at least 7 days, we'll
# start with next week's page.
#
sub get_listings_page_start_date {
my $start_date;
if ($opt_offset < 7) {
$start_date = get_date_today();
debug_print("get_listings_page_start_date(): " . $start_date->strftime("%d-%m-%Y"));
}
else {
$start_date = get_date_today_with_offset(7);
debug_print("get_listings_page_start_date(): " . $start_date->strftime("%d-%m-%Y"));
}
return $start_date;
}
sub get_data_for_available_days {
my ($url, $chname) = @_;
debug_print("get_available_days(): url=$url");
# Get the page's tree
my $t = get_tree($url);
if (not defined $t) {
debug_print("*** Error: Could not get tree for '" . $url . "' ***");
return;
}
# Return all available day elements ( <div class="channel"...> )
my @nodes = $t->look_down('_tag', 'div', 'class', 'channel');
debug_print("get_available_days(): Found " . scalar @nodes . " days of data\n");
return @nodes;
}
sub get_data_for_filtered_days {
my $available_days = shift;
my @filtered_days;
AVAILABLE:
foreach my $i (0 .. scalar @$available_days) {
next AVAILABLE if ($i < $opt_offset);
last AVAILABLE if ($i == $opt_offset + $opt_days);
push @filtered_days, $available_days->[$i];
}
debug_print("filter_days(): Will get listings for " . scalar @filtered_days . " days\n");
undef $available_days;
return \@filtered_days;
}
sub process_channel_row {
my ($chname, $row, $dt) = @_;
my $dateindex = $dt->strftime("%Y%m%d");
debug_print(" ###############################################################");
debug_print(" process_row: processing listings for: chname=$chname, dateindex=$dateindex");
debug_print(" ###############################################################\n");
my @programmes = ();
PROGRAMME:
foreach my $programme ($row->look_down('_tag', 'div', 'class', qr/program /) ) {
# skip empty program cells
if ($programme->attr('class') =~ /no-program/) {
debug_print(" Skipping 'no-program' entry\n");
next PROGRAMME;
}
# extract the programme data
push @programmes, process_program($chname, $programme, $dateindex);
debug_print("\n");
}
return \@programmes;
}
sub process_program {
my ($chname, $programme, $dateindex) = @_;
my $title_text;
my $prog_page;
my $title = $programme->look_down('_tag', 'p', 'class', 'title');
if ($title) {
if ($title->as_text() =~ /\w+/) {
$title_text = trim($title->as_text());
debug_print(" Found programme title '" . $title_text . "'");
#FIXME for all prog types
my $link = $title->look_down('_tag', 'a', 'class', 'lien-fiche');
if ($link and $link->attr('href') =~ /programme-tv/) {
$prog_page = $ROOT_URL . $link->attr('href');
debug_print(" Programme subpage found '" . $prog_page . "'");
}
}
else {
debug_print(" No programme title text found, skipping programme");
return undef; # REQUIRED
}
}
else {
debug_print(" No programme title found, skipping programme");
return undef; # REQUIRED
}
my $start_time;
my $duration_mins;
my $start = $programme->look_down('_tag', 'p', 'class', 'time');
if ($start) {
if ($start->as_text() =~ /(\d\d)h(\d\d)/) {
my ($hh, $mm) = ($1, $2);
$start_time = $dateindex.$hh.$mm."00";
debug_print(" Found programme start '" . $hh."h".$mm . "'");
}
else {
debug_print(" Start time not parsed, skipping programme'");
return undef; # REQUIRED
}
# Programme durations are given, but rarely agree with the difference
# between this programme's start time and the next
$duration_mins = $start->look_down('_tag', 'span');
if ($duration_mins) {
if ($duration_mins->as_text() =~ /\((\d+) min\)/) {
$duration_mins = $1;
debug_print(" Found programme duration '" . $duration_mins ." mins'");
}
else {
debug_print(" No programme duration found");
}
}
}
else {
debug_print(" No start time found, skipping programme'");
return undef; # REQUIRED
}
debug_print(" Creating programme hash for '" . $title_text . " / " . $start_time);
my %prog = (channel => $chname.".telestar.fr",
title => [ [ encode_utf8( trim($title_text) ) ] ], # lang unknown
start => utc_offset($start_time, "+0100"),
);
# Store some temp data for later processing. A leading underscore in
# a key name means the data is not written by XMLTV::Writer
if ($duration_mins and $duration_mins > 0) {
$prog{'_duration_mins'} = $duration_mins;
}
if ($prog_page) {
$prog{'_prog_page'} = $prog_page;
}
my $episodenumber = $programme->look_down('_tag', 'p', 'class', 'title-episode');
if ($episodenumber) {
if ($episodenumber->as_text() =~ /Saison (\d+) Episode (\d+)/) {
my ($season_num, $episode_num) = ($1, $2);
# Season/episode number is zero-indexed. (Totals are one-indexed.)
# Sometimes, a series or episode number of 0 is seen, so we ignore it
if ($season_num == 0) {
$season_num = "";
}
else {
$season_num--;
}
if ($episode_num == 0) {
$episode_num = "";
}
else {
$episode_num--;
}
$episodenumber = $episodenumber->as_text();
$prog{'episode-num'} = [ [ $season_num . "." . $episode_num . ".", "xmltv_ns" ] ];
debug_print(" Found programme episodenumber '" . $episodenumber . "'");
}
# Likely the programme's sub-title if not an episode number
elsif ($episodenumber->as_text() =~ /\w+/) {
$episodenumber = $episodenumber->as_text();
$prog{'sub-title'} = [ [ encode_utf8( $episodenumber ) ] ];
debug_print(" Found programme sub-title '" . $episodenumber . "'");
}
}
else {
debug_print(" No episodenumber found");
}
my $category = $programme->look_down('_tag', 'p', 'class', 'category');
if ($category and $category->as_text() =~ /\w+/) {
$category = trim($category->as_text());
$prog{category} = [ [ encode_utf8( $category ), $LANG ] ];
debug_print(" Found programme genre '" . $category . "'");
}
else {
debug_print(" No category found");
}
my $synopsis = $programme->look_down('_tag', 'p', 'class', 'synopsis');
if ($synopsis and $synopsis->as_text() =~ /\w+/) {
$synopsis = trim($synopsis->as_text());
$prog{desc} = [ [ encode_utf8( $synopsis ), $LANG ] ];
debug_print(" Found programme synopsis '" . $synopsis . "'");
}
else {
debug_print(" No synopsis found");
}
my $rating = $programme->look_down('_tag', 'span', 'class', 'pastille csa');
if ($rating and trim($rating->as_text()) =~ /^(-(?:10|12|16|18))$/) {
$rating = $1;;
$prog{rating} = [ [ $rating, "CSA" ] ];
debug_print(" Found programme rating '" . $rating . "'");
}
else {
debug_print(" No rating found");
}
my $thumbnail = $programme->look_down('_tag', 'img', 'class', 'thumbnail');
if ($thumbnail) {
my $url = $thumbnail->attr('src');
push @{$prog{icon}}, {src => $url};
debug_print(" Found programme icon: '" . $url . "'");
}
if ($prog_page) {
process_programme_page(\%prog);
}
return \%prog;
}
# Process a page of detailed programme information for stored url and
# update the given programme hash with any extra information extracted
# from the page
#
sub process_programme_page {
my $prog = shift;
my $prog_page = $prog->{'_prog_page'};
debug_print(" process_programme_page(): url=$prog_page");
# Get the page's tree
my $t = get_tree($prog_page);
if (not defined $t) {
debug_print(" *** Error: Could not get tree for '" . $prog_page . "' ***");
return $prog;
}
my $prog_info = $t->look_down('_tag', 'div', 'class', qr/program-informations/);
if ($prog_info) {
my $episode_name = $prog_info->look_down('_tag', 'h2', 'class', 'underlined red');
if ($episode_name) {
if ($episode_name->as_text() =~ /Saison \d+ Episode \d+ : (\w.*)$/) {
$episode_name = trim($1);
$prog->{'sub-title'} = [ [ encode_utf8( $episode_name ), $LANG ] ];
debug_print(" Found programme sub-title '" . $episode_name . "'");
}
elsif ($episode_name->as_text() =~ /\w+/) {
$episode_name = trim($episode_name->as_text());
$prog->{'sub-title'} = [ [ encode_utf8( $episode_name ), $LANG ] ];
debug_print(" Found programme sub-title '" . $episode_name . "'");
}
}
my $date_created = $prog_info->look_down('_tag', 'span', 'itemprop', 'dateCreated');
if ($date_created) {
if ($date_created->as_text() =~ /^\d{4}$/) {
$date_created = trim($date_created->as_text());
$prog->{'date'} = $date_created;
debug_print(" Found programme year '" . $date_created . "'");
}
}
my $genre = $prog_info->look_down('_tag', 'span', 'itemprop', 'genre');
if ($genre) {
my $subgenre;
($genre, $subgenre) = split(/,|\s-\s/, $genre->as_text() );
if (defined $genre && $genre =~ /\w+/) {
$genre = trim($genre);
debug_print(" Found programme genre '" . $genre . "'");
if (defined $subgenre && $subgenre =~ /\w+/) {
$subgenre = trim($subgenre);
debug_print(" Found programme sub-genre '" . $subgenre . "'");
$prog->{category} = [ [ encode_utf8( $genre ), $LANG ],
[ encode_utf8( $subgenre ), $LANG ] ];
}
else {
$prog->{category} = [ [ encode_utf8( $genre ), $LANG ] ];
}
}
}
}
my $synopsis = $t->look_down('_tag', 'p', 'class', qr/synopsis/);
if ($synopsis) {
$synopsis = trim($synopsis->as_text());
$prog->{desc} = [ [ encode_utf8( $synopsis ), $LANG ] ];
debug_print(" Found programme synopsis '" . $synopsis . "'");
}
# Casting information on the default programme information page is
# typically limited to series.
#
# A separate "Casting" page is available for many programmes which may
# include casting information for the given programme, but typically
# includes all casting information for the whole series of programmes so is
# not currently used.
my $casting = $t->look_down('_tag', 'div', 'class', qr/block-casting/);
if ($casting) {
my @directors = $casting->look_down('_tag', 'a', 'itemprop', 'producer');
foreach my $director (@directors) {
$director = trim($director->as_text());
push @{$prog->{credits}{director}}, encode_utf8( $director );
debug_print(" Found programme director '" . $director . "'");
}
my @actors = $casting->look_down('_tag', 'a', 'itemprop', 'actor');
foreach my $actor (@actors) {
$actor = trim($actor->as_text());
push @{$prog->{credits}{actor}}, encode_utf8( $actor );
debug_print(" Found programme actor '" . $actor . "'");
}
my @writers = $casting->look_down('_tag', 'a', 'itemprop', 'author');
foreach my $writer (@writers) {
$writer = trim($writer->as_text());
push @{$prog->{credits}{writer}}, encode_utf8( $writer );
debug_print(" Found programme writer '" . $writer . "'");
}
}
return $prog;
}
# For each programme in the given programme listref, set the stop time
# of a given programme to the start time of the following programme. For
# the last programme in the list, set the stop time to start time plus
# its duration
sub update_programme_stop_times {
my $programmes = shift;
# Stop at antepenultimate programme
foreach my $i (0 .. (scalar @{$programmes} -2)) {
my $prog_current = $programmes->[$i];
my $prog_next = $programmes->[$i+1];
$prog_current->{stop} = $prog_next->{start};
}
# Handle final programmes separately: add duration to start time
my $prog_last = $programmes->[-1];
my ($y, $m, $d, $hh, $mm, $ss) = $prog_last->{start} =~ /^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/;
my $prog_last_start = DateTime->new(
year => $y, month => $m, day => $d,
hour => $hh, minute => $mm, second => $ss,
time_zone => 'Europe/Paris', );
my $prog_last_duration = DateTime::Duration->new( minutes => $prog_last->{'_duration_mins'});
my $prog_last_stop = $prog_last_start + $prog_last_duration;
$prog_last->{stop} = utc_offset($prog_last_stop->strftime("%Y%m%d%H%M%S"), "+0100");
# Return updates listref of programmes
return $programmes;
}
# Return a hash of available channels for a given bouquet.
#
sub get_channels {
my $bouquet = shift;
my %bouquet_channels;
return undef if not defined $bouquet;
# Get the current page for the given bouquet
# http://www.telestar.fr/programme-tv/grille/09-08-2015/grandes-chaines-et-tnt
my $today = get_date_ddmmyyyy();
my $url = $GRID_FOR_CHANNEL.'grille/'.$today.'/'.$bouquet;
my $t = get_tree($url);
debug_print("get_channels(): url = '" . $url . "'");
if (!defined $t) {
print STDERR "Unable to retrieve channels for $bouquet \n";
return %bouquet_channels;
}
foreach my $cellTree ( $t->look_down( "_tag", "div", "class", "channel" ) ) {
my $tag = $cellTree->look_down( "_tag", "a" );
if (defined $tag) {
my $progurl = $tag->attr('href');
if ( $progurl =~ /^\/programme-tv\/grille-chaine\/(\w+)/ ) {
# There are now no parsable channel names and icons
# are given as a single site-wide imagemap instead of individual URLs:
# http://css1.telestar.fr/extension/telestar/design/telestar/images/chaines/chaines.jpg
my $chname = $1;
debug_print(" Found channel: name = '" . $chname . "'");
$bouquet_channels{$chname} = {'name' => $chname };
}
}
}
$t->delete(); undef $t;
return %bouquet_channels;
}
# Prints a debug string when the --debug option is used
#
sub debug_print {
if ($opt_debug) {
my ($msg) = shift;
print STDERR $msg . "\n";
}
}
# Remove leading/trailing whitespace
#
sub trim {
for (my $s = shift) {
s/^\s*//;
s/\s*$//;
return $s;
}
}
# Replaces specific HTML entities with text replacements, and then
# decodes any remaining entities in the string
#
sub tidy_html {
for (my $s = shift) {
# handle specific entities
s/ / /g;
s/\x8c/Œ/g;
s/Œ/OE/g;
s/\x9c/œ/g;
s/œ/oe/g;
s/“|”|&\#8219;|&\#8220;/\"/g;
s/‘|’|&\#8216;|&\#8217;|&\#8218;/\'/g;
s/&\#8212;/--/g;
s/\x85/.../g;
s/&\#8230;/.../g;
s/&\#8230;/.../g;
s/&\#821[0123];/-/g;
s/\xe2\x80\x99/\'/g;
s/\x92/\'/g; # turn windows-1252 right single quotation mark into apostrophe
s/\x80/€/g; # turn windows-1252 euro sign into real euro sign
s/(&\#[0-9]{4,};)//g;
# decode remaining entities
decode_entities($s);
return $s;
}
}
# Returns a TreeBuilder instance for a given URL. The URL is retrieved
# via get_nice(), decoded into a Perl string, processed to remove HTML
# entities and then parsed into a HTML::TreeBuilder object
#
sub get_tree {
my $url = shift;
my $content;
my $nbretry = 0;
while ( (not defined($content = get_nice($url))) || (length($content) == 0) ) {
++$nbretry;
return undef if $nbretry > $MAX_RETRY;
debug_print("*** Retrying URL: '" . $url
. "' (attempt " . $nbretry . " of " . $MAX_RETRY . ") ***");
}
$content = decode_utf8($content);
$content = tidy_html($content);
my $t = new HTML::TreeBuilder;
$t->parse($content) or die "Cannot parse content of Tree\n";
$t->eof;
return $t;
}
# Return a DateTime object representing 'now'
#
sub get_date_today {
return DateTime->now( time_zone => 'Europe/Paris' );
}
# Return a DateTime object represent 'now' + $offset days
#
sub get_date_today_with_offset {
my $offset = DateTime::Duration->new( days => shift );
return get_date_today() + $offset;
}
|