/usr/bin/tv_grab_za is in xmltv-util 0.5.63-2.
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 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 | #!/usr/bin/perl -w
eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
if 0; # not running under some shell
=pod
=head1 NAME
tv_grab_za - Grab TV listings for South Africa.
=head1 SYNOPSIS
tv_grab_za --help
tv_grab_za [--config-file FILE] --configure [--gui OPTION]
tv_grab_za [--config-file FILE] [--output FILE] [--days N]
[--quiet] [--retries N]
=head1 DESCRIPTION
Output TV listings for DSTV channels available in South Africa.
The data comes from www.dstv.com. The grabber relies on
parsing HTML so it might stop working at any time.
First run B<tv_grab_za --configure> to choose, which channels you want
to download. Then running B<tv_grab_za> 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_za.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. Can be 1, 7, 14 or 30. Default is 14
B<--quiet> suppress the progress messages normally written to standard
error.
B<--retries> number of retries before failing channel download.
B<--help> print a help message and exit.
=head1 SEE ALSO
L<xmltv(5)>.
=head1 AUTHORS
Chris Picton <cpicton@users.sf.net>
Neil Garratt <ngarratt@users.sf.net>
Based on tv_grab_fi by Matti Airas.
Latest version always available at http://xmltv.cvs.sourceforge.net/xmltv/xmltv/grab/za/
=head1 BUGS
Does not automatically update itself, when DSTV changes their site
=cut
######################################################################
# initializations
use strict;
use XMLTV::Version '$Id: tv_grab_za,v 1.42 2011/05/12 07:05:51 dekarl Exp $ ';
use XMLTV::Capabilities qw/baseline manualconfig cache/;
use XMLTV::Description 'South Africa';
use Getopt::Long;
use List::Util qw(min);
use List::Util qw(max);
use Date::Manip;
use HTML::TreeBuilder;
use HTML::Entities; # parse entities
use IO::File;
use Digest::MD5 qw(md5 md5_hex);
use POSIX qw(strftime);
#use LWP::Simple qw($ua);
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Request::Common qw(GET);
use HTTP::Cookies;
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 South African television listings in XMLTV format
To configure: $0 --configure [--config-file FILE]
To grab listings: $0 [--config-file FILE] [--output FILE] [--days N]
[--quiet] [--retries]
END
;
# Attributes of the root element in output.
my $HEAD = { 'source-info-url' => 'http://www.dstv.com/',
'source-data-url' => "http://www.dstv.com/dstv-guide/default.asp",
'generator-info-name' => 'XMLTV',
'generator-info-url' => 'http://xmltv.org/',
};
# The timezone in South Africa.
my $TZ="+0200";
# default language
my $LANG="en";
our %dstvchannelmap;
our %mnetchannelmap;
my %mnetchannelfixups = (
'Africa Magic Channel (C-Band)' => 'AfricaMagic',
'Bloomberg Information TV' => 'Bloomberg',
'China Central Television 4' => 'CCTV 4',
'Channel O - Sound Television' => 'Channel O',
'CNBC' => 'CNBC Africa',
'CNN International' => 'CNN',
'Deukom - DW' => 'Deutchse Welle',
'E-Entertainment' => 'E! Entertainment',
'eTV' => 'e-TV',
'Go (K-World Teen)' => 'GO',
'Hallmark Entertainment Network' => 'Hallmark',
'K-TV World' => 'K-All Day',
'M-Net Domestic' => 'M-Net',
'M-Net Series' => 'M-Net Series',
'Parliamentary Service' => 'Parliamentary',
'Reality TV' => 'Zone Reality',
'Rhema Network' => 'Rhema TV',
'Summit' => 'Summit TV',
'SuperSport' => 'SuperSport 1',
'SuperSport 3' => 'SuperSport 3 (Soccer)',
'SuperSport 5' => 'SuperSport 5 (Highlights)',
'SuperSport Zone Mosaic' => 'SuperSport Zone',
'Trinity Broadcasting Network' => 'TBN',
'Turner Classic Movies' => 'TCM',
'TV5 Afrique' => 'TV5',
'VH1' => 'VH-1',
);
my %dstvchannelfixups = (
'CNN International' => 'CNN',
'Sony Entertainment Television' => 'Sony Entertainment',
'SABC 1' => 'SABC1',
'SABC 2' => 'SABC2',
'SABC 3' => 'SABC3',
'Crime & Investigation Network' => 'Crime & Investigation',
'E! Entertainment Television' => 'E! Entertainment',
'SuperSport MaXimo 1' => 'SuperSport Maximo',
'MagicWorld' => 'Magic World',
'Deutsche Welle' => 'Deutchse Welle'
);
#These entries appear on the channel index page, but no schedules for them exist on the site.
my @dstvignorechannels = ('Soweto TV', 'Ignition');
#my %dstvfilehashes = (
# '1494729404' => '0',
# '3139098187' => '1',
# '2091571851' => '2',
# '2860538121' => '3',
# '3348398793' => '4',
# '1813599985' => '5',
# '1153776246' => '6',
# '1367985183' => '7',
# '3033721747' => '8',
# '2699942871' => '9',
# );
my %dstvfilehashes = (
'937c943580ac202fc64a80dbd3be3aab' => '0',
'40154b2e17f12abc83304910e8b2c184' => '1',
'261d6eeefee8ee6f398e8d4bef8b51df' => '2',
'f0e730108d788a4fef7966157d223e12' => '3',
'309cad2597b2273ecda6614169e79a78' => '4',
'675fd8104b6fa3ae317cbdc7cb301400' => '5',
'1d8960a26dce4fd9172a06154d66f692' => '6',
'479765dcd17d683a3fdbcd5740e11c15' => '7',
'6eeba41c618fdba24c8fd554023385a9' => '8',
'f888465466ffa7c7c3cc6c5f12414ad3' => '9',
);
our %dstvtimehashes = ();
my $viewstate = '';
my $eventvalidation = '';
my $ua = initialise_ua();
# Set up cache if needed
XMLTV::Memoize::check_argv('get_url');
######################################################################
# Get options.
my ($opt_days, $opt_offset, $opt_help, $opt_output,
$opt_configure, $opt_config_file, $opt_gui,
$opt_quiet, $opt_list_channels, $opt_opentime,
$opt_opentime_combined, $opt_retries, $opt_mnet_fallback,
$opt_dstv_fallback, $days_exceeded);
#$opt_days = 14; # default
$opt_quiet = 0; # default
GetOptions('days=i' => \$opt_days,
'offset=i' => \$opt_offset,
'help' => \$opt_help,
'configure' => \$opt_configure,
'opentime' => \$opt_opentime,
'opentime-combined' => \$opt_opentime_combined,
'config-file=s' => \$opt_config_file,
'gui:s' => \$opt_gui,
'output=s' => \$opt_output,
'quiet' => \$opt_quiet,
'retries' => \$opt_retries,
'mnet-fallback' => \$opt_mnet_fallback,
'list-channels' => \$opt_list_channels,
)
or usage(0);
# dstv.com only allows us to grab one of a few fixed day ranges and
# they all start from today. For baseline compliance, data outside
# the range specified is stripped. mnet.co.za doesn't have this issue,
# so it's now the default site to use. An extra day is also downloaded
# just to calculate the end time of the last program of the previous day
#
my %allowed_days = (1 => 0, 7 => 1, 14 => 2);
die "--offset cannot be negative" if defined $opt_offset and $opt_offset < 0;
die "--days must be positive" if defined $opt_days and $opt_days <= 0;
if ($opt_offset) {
$opt_days += $opt_offset;
} else { $opt_offset = 0; }
my $use_days;
if ($opt_days) {
$use_days = min grep { $_ >= $opt_days } keys %allowed_days;
if (not defined $use_days) {
$opt_days = $use_days = max keys %allowed_days;
warn "rounding down to $use_days days for download (must be one of ".(keys %allowed_days).")\n";
$days_exceeded = 1;
} elsif (($use_days != $opt_days) && (!$opt_quiet)) {
warn "dstv.com only supports the following days: ".(keys %allowed_days).". $use_days day(s) will be downloaded and extraneous ones skipped\n";
}
# OK, now $use_days has the number of days to grab starting from now,
# if that was specified on the command line. If this is specified in the
# config file it will also set this variable. $opt_days will contain the
# number of days we actually want to keep data for.
}
# Default retries = 3;
$opt_retries = 3 if !$opt_retries;
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_za', $opt_quiet);
init_cookies();
if ($mode eq 'configure') {
mode_configure();
exit();
}
# Whatever we are doing, we need the channels data.
my %channels;
my %options;
read_config(\%channels);
if (not defined $use_days) {
# Not got from command line or config file; default it.
$opt_days = $use_days = 14;
}
#$opt_dstv_fallback = 1 if !(defined $opt_mnet_fallback || defined $opt_dstv_fallback);
#######################################
# 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') {
# Write channels mode.
foreach my $id (keys %channels) {
$writer->write_channel({id => $id, 'display-name' => [ [ $channels{$id}, 'en' ] ]});
}
$writer->end();
exit();
}
######################################################################
# We are producing full listings.
die if $mode ne 'grab';
# Prepare channel maps
#get_mnet_channel_mappings() if defined $opt_mnet_fallback || $opt_dstv_fallback;
#get_dstv_channel_mappings();
#if (keys %dstvchannelmap == 0) {
# die "error: can't open channel map (http://www.dstv.com/DStv_Guide/default.aspx)";
#}
######################################################################
# begin main program
# Print out the channels
die "No channels specified, run me with --configure first\n"
if not keys %channels;
foreach my $chanid (keys %channels) {
my $n=$channels{$chanid};
my $ch_xid="$chanid.dstv.com";
$writer->write_channel({ id => $ch_xid, 'display-name' => [ [ $n , 'en' ] ] });
}
my $bar = new XMLTV::ProgressBar('getting listings', (scalar keys %channels))
if not $opt_quiet;
foreach (keys %channels) {
process_html($_);
update $bar if not $opt_quiet;
}
$bar->finish() if not $opt_quiet;
$writer->end();
if (defined $days_exceeded) {
$! = 9;
die;
}
######################################################################
# 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();
}
}
sub tidy( $ ) {
for (my $tmp = shift) {
if (not defined $tmp) { return };
tr/\t\205/ /d;
s/([^\012\015\040-\176\240-\377]+)//g;
return $_;
}
}
# The URI to get listings for a given channel.
sub dstv_channel_uri( $ ) {
my $ch = shift;
my $mapped = dstv_channel_map($ch);
die "cannot look up '$ch' in map" if not defined $mapped;
die if not defined $use_days;
my $days_param = $allowed_days{$use_days};
return "http://guide.dstv.com/listing/default.aspx?drpChannels=$mapped&drpDays=$days_param";
}
# Returns the option ID on the DSTV site for a given channel name
sub dstv_channel_map ($) {
my $ch = shift;
if (!%dstvchannelmap) {
get_dstv_channel_mappings();
}
return $dstvchannelmap{$ch};
}
sub mnet_channel_map ($) {
my $ch = shift;
if (!%mnetchannelmap) {
get_mnet_channel_mappings();
}
return $mnetchannelmap{$ch};
}
sub post_process($$) {
my $title = shift;
my $desc = shift;
my $r = undef;
my $subtitle = undef;
my $episode_num = undef;
my $year = undef;
my $actors = undef;
my $director = undef;
my $writers = undef; # Unused right now
my $commentators = undef; # Unused right now
my $category = undef;
my $subtitles = undef;
my $dolby = undef;
# Try to get full title from description if title seems cut off
$title =~ s/(^\s+|\s+$)//g;
if ($title =~ /\.\.\.$/ ) {
my $temp = $title;
$temp =~ s/.\.\.\.$//g;
# Try get full title from description;
if ($desc =~ /^'?(${temp}[^\.\?]+[^\'])'?[\.\?]\s+(.+)/i) {
t "REMAPPING TITLE from $title to $1";
$title = $1;
$desc = $2;
$title =~ s/(^\s+|\s+$)//g;
$desc =~ s/(^\s+|\s+$)//g;
t "New desc = $desc";
}
}
if ($desc =~ /^'([^\.]+)'\.\s+(.+)/) {
$subtitle = $1;
$desc = $2;
t "FOUND EPISODE TITLE: $subtitle";
t "Title: $title";
t "New desc = $desc";
$category = "series";
}
if ($subtitle && $subtitle =~ /^S?(\d+)\/E?(\d+) - (.*)$/) {
$episode_num = ($1-1) . "." . ($2-1) . ".0/1";
$subtitle = $3;
t "FOUND EPISODE NUMBER: $episode_num";
$category = "series";
}
if ($desc =~ /^Aka ([^\.]+)\. (.*)/) {
$desc = $2;
my $aka = $1;
t "Aka found: $aka\n";
# TODO - do something with the aka
}
if ($desc =~ /,? (HI|English) Subtitles\.?/) {
$desc =~ s/,? (HI|English) Subtitles\.?//g;
t "REMOVING Subtitles string";
$subtitles = 1;
}
if ($desc =~ /,? DD\.?/) {
$desc =~ s/,? DD\.?//g;
t "REMOVING DD string";
$dolby = 1;
}
if ($title =~ /^Press .i.$/) {
$title = $subtitle;
$subtitle = undef;
}
if ($desc =~ /(.*) \((\d{4})\)\s*([^\.]+)\.?\s*$/) {
$year = $2;
$director = $3;
$desc = $1;
t "desc = $desc\n";
t "Year = $year\n";
t "Director = $director\n";
}
if ($desc =~ /(.*) \((\d{4})\)\s*$/) {
$desc = $1;
$year = $2;
t "desc = $desc\n";
t "Year = $year\n";
}
if (defined $year && $desc =~ /(.*\.)\s+([^\.]+ [A-Z][^\.]+)\.\s*/) {
$desc = $1;
$actors = $2;
if (defined $actors) {
$actors =~ s/^\s+//g;
$actors =~ s/\s+$//g;
my @a = split(/,\s+/, $actors);
$actors = [];
foreach my $a (@a) {
push @$actors, $a;
}
}
$category = "movie";
}
# Trim whitespace from elements
$title =~ s/(^\s+|\s+$)//g;
$desc =~ s/(^\s+|\s+$)//g;
$subtitle =~ s/(^\s+|\s+$)//g if $subtitle;
$desc = "No description available" if ($desc eq "");
$r->{title} = [[$title]];
$r->{'sub-title'} = [[$subtitle]] if $subtitle;
$r->{'episode-num'} = [[$episode_num, "xmltv_ns"]] if $episode_num;
$r->{desc} = [[$desc]];
$r->{category} = [[ $category, 'en' ]] if $category;
$r->{'subtitles'} = [ { type => 'teletext' } ] if $subtitles;
$r->{'audio'}->{"stereo"} = "dolby digital" if $dolby;
# credits
my %c;
$c{director} = [ $director ] if $director;
$c{actor} = $actors if $actors;
$c{writer} = $writers if $writers;
$c{commentator} = $commentators if $commentators;
$c{director} = [ $director ] if $director;
$r->{date} = $year if $year;
$r->{credits} = \%c if %c;
return $r;
}
sub process_dstv_html {
my $chanid = shift;
my $name = $channels{$chanid};
my $now = time();
my $data;
my $tries = 0;
# URI just for error reporting.
my $uri = dstv_channel_uri $name;
local $SIG{__DIE__} = sub { die "$uri:$_[0]\n" };
$data = tidy(get_dstv($name));
if ($data =~ /:error:(.*):/) {
return $data;
}
# Get time mappings
# get_dstv_time_mappings($data);
# parse the page to a document object
my $tree = HTML::TreeBuilder->new();
$tree->utf8_mode(1);
$tree->parse($data) or die "cannot parse content\n";
$tree->eof;
my ($prev_r, $r, $prev_time);
my @array_ot;
# Find the main table, and loop through all the table rows
# Find the date headers on the page
my @date_headers = $tree->look_down(_tag => "td", class => 'srch_date_chnl_head');
my $offset_counter = 0;
foreach my $td (@date_headers) {
$offset_counter++;
next if (($offset_counter <= $opt_offset) || ($offset_counter > $opt_days+1));
my $date = $td->as_text();
$date =~ s/^[^0-9]+//g;
my $tr = $td->parent();
while (($tr = $tr->right())) {
last if !defined $tr;
my $result = $tr;
my @alternating = $result->look_down(
sub {
lc($_[0]->attr('_tag')) eq 'td' and lc($_[0]->attr('class')) eq 'srch_rslt_alternating'
or lc($_[0]->attr('_tag')) eq 'td' and lc($_[0]->attr('class')) eq 'srch_rslt'
}
);
my $temp;
last if !defined $alternating[0];
$temp = $alternating[0];
my $time = $temp->look_down(_tag => 'b');
die 'no <b> thing (for time) found' if not defined $time;
$time = $time->as_text;
$time =~ /^(\d)(\d):(\d)(\d)$/ or die "bad time '$time'";
$temp = $alternating[1]->look_down(_tag => "a", name => qr/Bookmark/)->look_down(_tag => "b");
my $title = $temp->as_text;
my ($rating, $duration);
my $tempstring = $temp->right->as_text();
if (defined $tempstring) {
$rating = $1 if $tempstring =~ /Rating: ?(.+?)\s+/;
$duration = $1 if $tempstring =~ /Duration: ?([0-9:]+)/;
}
t "$title: $rating: $duration\n";
my $desc = $alternating[2]->as_text();
t "---\n$desc\n---\n";
my $start = gen_start_time($date, $time, $now);
my $r = post_process($title, $desc);
$start = $start . " $TZ";
if ($rating) { $rating =~ s/(^\s+|\s+$)//g; }
else { $rating = "Family"; }
$r->{rating} = [[$rating, "DSTV"]];
$r->{start} = $start;
$r->{channel} = "$chanid.dstv.com";
if (defined $prev_r) {
$prev_r->{stop} = $start;
$writer->write_programme($prev_r);
}
$prev_time = $time;
$prev_r = $r;
if ($offset_counter > $opt_days) {
$offset_counter++;
last;
}
}
}
$data = "success";
$tree->delete;
return $data;
}
sub process_mnet_html {
my $chanid = shift;
my $name = $channels{$chanid};
my $now = time();
my $data;
my $tries = 0;
$data = tidy(get_mnet($name));
if ($data =~ /:error:(.*):/) {
return $data;
}
# parse the page to a document object
my $tree = HTML::TreeBuilder->new();
$tree->utf8_mode(1);
$tree->parse($data) or die "cannot parse get_mnet data for $name\n";
$tree->eof;
my ($prev_r, $r, $prev_time);
my @array_ot;
# Find the date headers on the page
my @tags = $tree->look_down(
sub {
# the lcs are to fold case
lc($_[0]->attr('_tag')) eq 'font' and lc($_[0]->attr('class')) eq 'scheduledate'
or lc($_[0]->attr('_tag')) eq 'font' and lc($_[0]->attr('class')) eq 'date'
or lc($_[0]->attr('_tag')) eq 'font' and lc($_[0]->attr('class')) eq 'scheduletime'
or lc($_[0]->attr('_tag')) eq 'font' and lc($_[0]->attr('class')) eq 'time'
or lc($_[0]->attr('_tag')) eq 'font' and lc($_[0]->attr('class')) eq 'scheduletitle'
or lc($_[0]->attr('_tag')) eq 'font' and lc($_[0]->attr('class')) eq 'title'
or lc($_[0]->attr('_tag')) eq 'p'
or lc($_[0]->attr('_tag')) eq 'a'
}
);
my ($date,$time,$title,$rating);
my $days_done = 0;
foreach my $tag (@tags) {
my $tag_text = $tag->as_text;
if ($tag->attr('class') && lc($tag->attr('class')) =~ /date$/ ) {
# Date
$tag_text =~ s/\240/ /g;
$tag_text =~ /(\d+) (.*) (\d{4})/;
$date = "$1 $2";
$days_done++;
next;
}
if ($tag->attr('class') && lc($tag->attr('class')) =~ /time$/) {
# Time
$tag_text =~ /^\s?(\d\d:\d\d)$/;
$time = "$1";
next;
}
if ($tag->attr('class') && lc($tag->attr('class')) =~ /title$/) {
# Title
$tag_text =~ s/[\302\240]//g;
$title = $tag_text;
next;
}
if ($tag->attr('_tag') && $tag->attr('_tag') eq 'a' &&
$tag->attr('onclick') && $tag->attr('onclick') =~ /OpenAgeRestriction/) {
# Rating
$rating = $tag_text;
next;
}
if ($tag->attr('_tag') && $tag->attr('_tag') eq 'p' &&
$date && $time && $title) {
# Description
my $desc = $tag_text;
$desc =~ s/(^\s+|\s+$)//g;
t "---\n$desc\n---\n";
my $start = gen_start_time($date, $time, $now);
my $r = post_process($title, $desc);
$start = $start . " $TZ";
if ($rating) { $rating =~ s/(^\s+|\s+$)//g; }
else { $rating = "Family"; }
$r->{rating} = [[$rating, "DSTV"]];
$r->{start} = $start;
$r->{channel} = "$chanid.dstv.com";
if (defined $prev_r) {
$prev_r->{stop} = $start;
$writer->write_programme($prev_r);
}
$prev_time = $time;
$prev_r = $r;
undef $title;
undef $time;
if ($days_done > ($opt_days-$opt_offset)) { last; }
}
}
$data = "success";
$tree->delete;
return $data;
}
####
# process_table: fetch a URL and process it
#
# arguments:
# Date::Manip object giving the day to grab
# xmltv id of channel
# katso id of channel
#
# returns: list of programme hashes to write
#
sub process_html {
my $id = shift;
my $name = $channels{$id};
t "Getting Channel $id";
my $result;
my @order = ('dstv');
# my @order = ('mnet', 'dstv');
# if ($opt_mnet_fallback) {
# @order = ('dstv', 'mnet');
# }
my %processfuncs = (
'dstv' => \&process_dstv_html,
# 'mnet' => \&process_mnet_html,
);
foreach my $site (@order) {
my $func = $processfuncs{$site};
$result = &{$func}($id);
t "result: $result";
if ($result !~ /^:error:(.*):/) {
return;
}
say("\nSite $site returned no data - attempting next site for $name");
}
say("\nskipping channel '$name'. All sites failed");
}
# get channel listing
sub get_channels {
my $channels = shift;
my @urls = (
# 'Google' , 'http://www.google.com/search?q=cache%3Awww.dstv.com%2Fmain.aspx%3FID%3D136',
# 'DSTV' , 'http://www.dstv.com/dstvsa/content/en/sa/dstv_premium?categorylistsearch=cl_results&category_id=158',
# vmlf - Added new link for channel list of premium bouquet
# There are other DSTV bouquets available, each one corresponds to a different bId
'DSTV' , 'http://www.dstv.com/dstvsa/content/en/sa/products?bId=1',
);
my $local_data;
my $i=0;
my $bar;
for ($i=0; $i < $#urls; $i+=2) {
my $key = $urls[$i];
my $url = $urls[$i+1];
$bar = new XMLTV::ProgressBar("Getting list of channels from $key site", 1) if not $opt_quiet;
t "Getting $key from $url";
$local_data = get_url('GET', $url);
if (!defined $local_data || $local_data =~ /^:error/) {
$bar->finish() if not $opt_quiet;
print STDERR "Unable to get channel listing from $key site\n"
if not $opt_quiet;
next;
}
last;
}
if (!defined $local_data || $local_data =~ /^:error/) {
print STDERR "Unable to get channel listing from any site\n. Please check your connectivity or try again later\n"
if not $opt_quiet;
die;
}
t "Got channel data ".length($local_data)." bytes - about to parse";
my $tree = HTML::TreeBuilder->new();
$tree->utf8_mode(1);
$tree->parse($local_data) or die "cannot parse content of channels page\n";
$tree->eof;
# vmlf - DSTV NEW channel list page url includes all types of channel,
# so we need filter channels that are inside the videoChannels div
# to get the tv channels only
my @list = $tree->look_down(
_tag => 'span',
sub {
$_[0]->look_up(_tag => 'div', id => 'videoChannels') and
$_[0]->look_up(_tag => 'div', id => 'channel_list') and
id => 'header_back'
},
);
foreach my $entry (@list) {
my $temp = $entry->right();
$temp =~ /([\w\s&\+!-]+).*\|[^\d]+(\d+)$/;
my $name = $1;
my $chanid = $2;
$name =~ s/\s+$//g;
if (exists {map { $_ => 1 } @dstvignorechannels}->{$name}) {
t "Ignore bogus channel $name";
} else {
t "Channel $chanid = $name";
$channels->{$chanid} = $name;
}
}
$tree->delete;
die "no channels could be found" if not keys %$channels;
update $bar if not $opt_quiet;
$bar->finish() if not $opt_quiet;
}
# Bump a YYYYMMDD date by one.
sub nextday {
my $d = shift;
my $p = parse_date($d);
my $n = DateCalc($p, '+ 1 day');
return UnixDate($n, '%Q');
}
sub mode_configure {
XMLTV::Config_file::check_no_overwrite($config_file);
get_channels(\%channels);
open(CONF, ">$config_file") or die "cannot write to $config_file: $!";
# Ask about each channel.
my @chs = sort {uc($channels{$a}) cmp uc($channels{$b})} keys %channels;
my @qs = map { "add channel '$channels{$_}'? " } @chs;
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;
my $name = $channels{$_};
print CONF "channel $_ $name\n";
}
my @choices = (1,7,14);
my $days = ask_choice("Number of days to retrieve",$choices[2], @choices);
print CONF "option days $days\n";
my $retries = ask("Number of retries for failed downloads? (3)");
$retries = 3 if $retries eq "";
print CONF "option retries $retries\n";
my $timeout = ask("Timeout for requests? (240)");
$timeout = 240 if $timeout eq "";
print CONF "option timeout $timeout\n";
# say ("This grabber can get the listings from either mnet.co.za, or dstv.com");
# say ("Which site would you like to use as the main site (mnet recommended)");
# @choices = ('dstv','mnet');
# my $fallback_option = ask_choice("Select one of: ",$choices[1], @choices);
# if ($fallback_option eq 'dstv') {
# print CONF "option mnet-fallback 1\n";
# } else {
# print CONF "option dstv-fallback 1\n";
# }
close CONF or warn "cannot close $config_file: $!";
say("Finished configuration. ");
exit();
}
sub update_dstv_eventstate {
# update form state attributes
my $data = shift;
if ($data =~ /id=\"__VIEWSTATE\" value=\"(.*)\"/) {
$viewstate = $1;
t "got viewstate: $viewstate";
} else {
print STDERR "VIEWSTATE not found\n" if not $opt_quiet;
}
if ($data =~ /id=\"__EVENTVALIDATION\" value=\"(.*)\"/) {
$eventvalidation = $1;
t "got eventvalidation: $eventvalidation";
} else {
print STDERR "EVENTVALIDATION not found\n" if not $opt_quiet;
}
}
# Initialize cookies and retrieve current channel ID's
sub get_dstv_channel_mappings {
t "refreshing dstv channel mappings";
my $url = "http://guide.dstv.com/listing/default.aspx";
my $data = get_url("GET", $url);
if ($data =~ /^:error:/) {
print STDERR "Error getting dstv channel state data: $data\n"
if not $opt_quiet;
return;
}
update_dstv_eventstate($data);
my %info = (
'__VIEWSTATE' => $viewstate,
'drpBouquet' => '1',
'drpChannels' => '0',
'drpDays' => '0',
'txtKeyword' => 'Keyword...',
'__EVENTVALIDATION' => $eventvalidation,
'__EVENTTARGET' => 'drpBouquet',
'__EVENTARGUMENT' => '',
'__LASTFOCUS' => '',
);
$data = get_url("POST", $url, $url, undef, \%info);
if ($data =~ /^:error:/) {
print STDERR "Error Getting dstv channel mappings: $data\n"
if not $opt_quiet;
return;
}
update_dstv_eventstate($data);
my $chantree = HTML::TreeBuilder->new();
$chantree->utf8_mode(1);
$chantree->parse($data) or die "cannot parse content of channels page\n";
$chantree->eof;
my $channame;
my $chanid;
my $chanselect = $chantree->look_down(_tag => 'select', name => 'drpChannels');
my @chan_list = $chanselect->look_down(_tag => "option");
foreach my $chanentry (@chan_list) {
$chanid = $chanentry->attr('value');
$channame = $chanentry->as_text;
$dstvchannelmap{$channame} = $chanid;
if ($dstvchannelfixups{$channame}) {
$dstvchannelmap{$dstvchannelfixups{$channame}} = $chanid;
}
t "Found channel $channame; internal reference $chanid";
}
$chantree->delete;
t "Refresh successful";
die "no channels could be found" if not keys %dstvchannelmap;
}
# Initialize cookies and retrieve current channel ID's
sub get_mnet_channel_mappings {
t "refreshing mnet channel mappings";
my $url = 'http://www.mnet.co.za/schedules/default.asp';
my $result = get_url("GET", $url);
if ($result =~ /^:error:/) {
if ($result =~ /^:error:no data:(.+)$/s) {
$result = $1;
} else {
print STDERR "Error Getting mnet channel mappings: $result\n"
if not $opt_quiet;
return;
}
}
my $chantree = HTML::TreeBuilder->new();
$chantree->utf8_mode(1);
$chantree->parse($result) or die "cannot parse content of $url\n";
$chantree->eof;
my $chanselect = $chantree->look_down(_tag => 'select', name => 'channelid');
my @chan_list = $chanselect->look_down(_tag => "option");
foreach my $chanentry (@chan_list) {
my $chantemp = $chanentry->as_text;
my $newchan;
$chantemp =~ s/(^\s+|\s+$)//g;
if ($chanentry->attr('value') =~ /^[\d\(\)]+$/) {
foreach my $fixup (keys %mnetchannelfixups) {
if ($fixup eq $chantemp) {
$newchan = $mnetchannelfixups{$fixup};
t "fixing up $chantemp to $newchan";
}
}
$newchan = $chantemp if not defined $newchan;
$mnetchannelmap{$newchan} = $chanentry->attr('value');
}
}
$chantree->delete;
}
sub get_dstv_time_mappings() {
my $data = shift;
my ($res,$req);
my $tree = HTML::TreeBuilder->new();
$tree->utf8_mode(1);
$tree->parse($data) or die "cannot parse dstv time mappings\n";
$tree->eof;
my @tags = $tree->look_down(
sub {
# the lcs are to fold case
lc($_[0]->attr('_tag')) eq 'img' and lc($_[0]->attr('src')) =~ /^get\.aspx\?guid/
}
);
our %dstvtimehashes = ();
foreach my $tag (@tags) {
my $temptag = $tag->attr('src');
$tag->attr('src') =~ /^get\.aspx\?GUID=(.*)$/;
my $guid = $1;
if (not defined $dstvtimehashes{$guid}) {
# unique guid - get data
my $url = "http://www.dstv.com/DStv_Guide/get.aspx?GUID=$1";
t "getting time mapping for GUID: $1";
$req = GET $url;
$req->header('Accept-Encoding','gzip');
$req->header('Referer','http://www.dstv.com/DStv_Guide/default.aspx');
$res = $ua->request($req);
if ($res->is_success) {
if (($res->headers()->header('Content-Encoding')) &&
($res->headers()->header('Content-Encoding') eq 'gzip')) {
$res->content(Compress::Zlib::memGunzip($res->content));
}
# hash
my $imagehash = md5_hex($res->content);
# compare
if (defined $dstvfilehashes{$imagehash}) {
$dstvtimehashes{$guid} = $dstvfilehashes{$imagehash};
} else {
if (! -f "$guid.gif") {
if (not $opt_quiet) {
print STDERR "Undefined image mapping for GUID=$guid\n";
print STDERR "MD5 = $imagehash\n";
print STDERR "Saving to file $guid.gif\n";
}
if (!open GIF, ">$guid.gif") {
print STDERR "Cannot write file: $!\n"
if not $opt_quiet;
next;
}
print GIF $res->content;
close GIF;
}
}
}
}
}
$tree->delete;
}
# Download listings for a channel name - refresh mappings if necessary
sub get_dstv() {
my $channame = shift;
my $url = "http://guide.dstv.com/listing/default.aspx";
my $mapped = dstv_channel_map($channame);
die "cannot look up '$channame' in map" if not defined $mapped;
die if not defined $use_days;
my $days_param = $allowed_days{$use_days};
my %info = (
'__VIEWSTATE' => $viewstate,
'drpBouquet' => '1',
'drpChannels' => $mapped,
'drpDays' => $days_param,
'txtKeyword' => 'Keyword...',
'btnSubmit.x' => '16',
'btnSubmit.y' => '15',
'__EVENTVALIDATION' => $eventvalidation,
'__EVENTTARGET' => '',
'__EVENTARGUMENT' => '',
'__LASTFOCUS' => '',
);
t "getting channel: $channame (ID = $mapped)";
my $result = get_url("POST", $url, $url, undef, \%info);
if ($result =~ /^:error:/) {
# Always attempt a refresh of channel mappings once
get_dstv_channel_mappings();
$mapped = dstv_channel_map($channame);
die "cannot look up '$channame' in map" if not defined $mapped;
%info = (
'__VIEWSTATE' => $viewstate,
'drpChannels' => $mapped,
'drpDays' => $days_param,
'txtKeyword' => '',
'btnSubmit.x' => '15',
'btnSubmit.y' => '12',
'__EVENTVALIDATION' => $eventvalidation,
);
$result = get_url("POST", $url, $url, undef, \%info);
}
return $result;
}
sub get_mnet() {
my $channame = shift;
my $data;
my $tries = 0;
my $req;
my $res;
my $chanid = mnet_channel_map($channame);
if (not defined $chanid) {
my $msg = "no corresponding mnet channel found for $channame";
print STDERR "\n$msg" if not $opt_quiet;
# This seems to be the convention for returning errors.
return ":error:$msg:";
}
my $start_date = POSIX::strftime("%Y/%m/%d", gmtime(time()+(($opt_offset)*86400)));
my $end_date;
if ($opt_days < max keys %allowed_days) {
$end_date = POSIX::strftime("%Y/%m/%d", gmtime(time()+(($opt_days)*86400)));
} else {
$end_date = POSIX::strftime("%Y/%m/%d", gmtime(time()+(($opt_days-1)*86400)));
}
my %info = (
'startDate' => $start_date,
'EndDate' => $end_date,
'sType' => '5',
'channelid' => $chanid,
'searchstring' => '',
'channel' => $chanid,
'theType' => 'today',
'firstRun' => 'false',
);
t "getting channel: $channame (ID = $chanid)";
$data = get_url("POST", "http://www.mnet.co.za/schedules/default.asp", 'http://www.mnet.co.za/schedules/default.asp', undef, \%info);
return $data;
}
sub init_cookies {
get_nice('http://guide.dstv.com/listing/default.aspx');
my $bar = new XMLTV::ProgressBar('Initialising cookies', 1)
if not $opt_quiet;
update $bar if not $opt_quiet;
$bar->finish() if not $opt_quiet;
}
sub gen_start_time {
my ($date, $time, $now) = @_;
# Date = 'Friday 23 May'
# Time = '14:00';
# str2time sometimes gets the wrong year
# Append the current year to the date
# If we are in Nov or Dec, reading for Jan or Feb, year++
$date =~ s/^(Today|Tomorrow|Tommorrow|Tommorow|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)//g;
my $year = (gmtime($now))[5] + 1900;
my $mon = (gmtime($now))[4] + 1;
if (($mon == 11 || $mon == 12) && ($date =~ /(January|February)/)) {
$year++;
}
my $timestamp = UnixDate("$date $year $time", "%s");
# my $timestamp = str2time("$date $year $time");
if (!defined $timestamp) {
print STDERR "Error: Cannot decode time: $date $year $time\n";
}
my $rv = POSIX::strftime("%Y%m%d%H%M%S", gmtime($timestamp));
return $rv;
}
sub initialise_ua {
my $cookies = HTTP::Cookies->new;
#my $ua = LWP::UserAgent->new(keep_alive => 1);
my $ua = LWP::UserAgent->new;
# Cookies
$ua->cookie_jar($cookies);
# Define user agent type
$ua->agent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US)');
# Define timouts
$ua->timeout(240);
# Use proxy if set in http_proxy etc.
$ua->env_proxy;
return $ua;
}
sub read_config {
my $channels = shift;
my $options = shift;
my @config_lines = XMLTV::Config_file::read_lines($config_file);
# Read configuration.
my $line_num = 1;
foreach (@config_lines) {
++ $line_num;
next if not defined;
s/#.*//g;
next if /^\s+$/;
s/\s+$//g;
if (/^channel/) {
my (undef, $chanid, $name) = split(/\s+/, $_, 3);
$channels->{$chanid} = $name;
}
if (/^option/) {
my (undef, $conf_option, $conf_value) = split(/\s+/, $_, 3);
# $opt_mnet_fallback = 1 if $conf_option eq 'mnet-fallback';
# $opt_dstv_fallback = 1 if $conf_option eq 'dstv-fallback';
$opt_retries = $conf_value if $conf_option eq 'retries';
$ua->timeout($conf_value) if $conf_option eq 'timeout';
if ($conf_option eq 'days') {
if (defined $opt_days) {
# Day stuff was given on the command line. This
# should override whatever's in the config file.
#
} else {
# Set the number of days from the config file. It
# must be one of the numbers allowed by the site.
$opt_days = $use_days = $conf_value;
die "bad number of days $use_days in config file\n"
if not grep { $_ == $use_days } keys %allowed_days;
}
}
}
}
# die 'config file: --mnet-fallback and --dstv-fallback are mutually exclusive'
# if (defined $opt_mnet_fallback && $opt_dstv_fallback);
}
sub get_url($$$$$) {
my $method = shift;
my $url = shift;
my $referrer = shift;
my $agent = shift;
my $varhash = shift;
t "Downloading URL: $url";
my $req = GET "$url";
$req->header('Accept-Encoding','gzip');
$req->header('Referer',$referrer) if defined $referrer;
$req->agent($agent) if defined $agent;
my $tries = 0;
my $data;
my $offset_counter = 0;
while ($tries < $opt_retries && not defined $data) {
$tries++;
my $res;
t "Attempt $tries";
if (lc($method) eq 'post') {
$res = $ua->post($url, $varhash);
} else {
$res = $ua->request(GET "$url");
}
if ($res->is_success) {
if (($res->headers()->header('Content-Encoding')) &&
($res->headers()->header('Content-Encoding') eq 'gzip')) {
$res->content(Compress::Zlib::memGunzip($res->content));
}
if (! $res->content =~ /class="srch_rslt_head1"/) {
t "No listing data found";
$data = ":error:no data:" . $res->content;
} else {
$data = $res->content;
}
} else {
print STDERR "\nserver error: " . $res->status_line
if not $opt_quiet;
t "Failed"
}
}
if (not $data) {$data = ":error:maximum retries:"};
return $data;
}
|