/usr/bin/tv_grab_fi_sv 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 | #!/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_fi_sv - Grab TV listings for Finland in Swedish.
=head1 SYNOPSIS
tv_grab_fi_sv --help
tv_grab_fi_sv --version
tv_grab_fi_sv --capabilities
tv_grab_fi_sv --description
tv_grab_fi_sv [--config-file FILE]
[--days N] [--offset N]
[--output FILE] [--quiet] [--debug]
tv_grab_fi_sv --configure [--config-file FILE]
tv_grab_fi_sv --configure-api [--stage NAME]
[--config-file FILE] [--output FILE]
tv_grab_fi_sv --list-channels [--config-file FILE]
[--output FILE] [--quiet] [--debug]
=head1 DESCRIPTION
Retrieves and displays TV listings for the Finnish YLE channels plus some of
the most popular commercial channels. The data comes from www.yle.fi and the
Swedish listings are retrieved rather than the Finnish. Just like tv_grab_fi,
this grabber relies on parsing HTML so it could very well stop working at any
time. You have been warned.
=head1 OPTIONS
B<--help> Print a help message and exit.
B<--version> Show the versions of the XMLTV libraries, the grabber and of
key modules used for processing listings.
B<--capabilities> Show which capabilities the grabber supports. For more
information, see L<http://xmltv.org/wiki/xmltvcapabilities.html>
B<--description> Show a brief description of the grabber.
B<--config-file FILE> Specify the name of the configuration file to use.
If not specified, a default of B<~/.xmltv/tv_grab_fi_sv.conf> is used. This
is the file written by B<--configure> and read when grabbing.
B<--output FILE> When grabbing, write output to FILE rather than to standard
output.
B<--days N> When grabbing, grab N days of data instead of all available.
Supported values are 1-14.
B<--offset N> Start grabbing at today + N days. Supported values are 0-13.
=head1 SEE ALSO
L<xmltv(5)>.
=head1 AUTHOR
Per Lundberg, (perlun at gmail dot com). Inspired/based on other grabbers,
like tv_grab_uk_rt, tv_grab_se_swedb and tv_grab_fi.
=cut
my $_version = '$Id: tv_grab_fi_sv,v 1.17 2015/06/20 11:28:15 bilbo_uk Exp $';
use strict;
use DateTime;
use IO::Scalar;
#use LWP::UserAgent;
#use XML::LibXML;
use HTML::TreeBuilder;
use XMLTV::Ask qw/say/;
use XMLTV::Configure::Writer;
use XMLTV::Options qw/ParseOptions/;
use XMLTV::Get_nice 0.005066;
use XMLTV::Memoize;
use Encode;
sub t;
# Constants.
# my $DATA_SITE_ROOT = 'http://svenska.yle.fi/programguide/';
my $DATA_SITE_ROOT = 'http://ohjelmaopas.yle.fi/';
my $GRABBER_NAME = 'tv_grab_fi_sv';
(my $GRABBER_VERSION) = $_version =~ qr/$GRABBER_NAME,v ([\d\.]*) /;
my $XML_ENCODING = 'iso-8859-1';
my $LANGUAGE_CODE = 'sv';
# This is not the timezone for the machine on which the grabber is
# being run, but rather the timezone in which all the grabbed data is
# being specified.
my $TIMEZONE = 'Europe/Helsinki';
# Attributes of the root element in output.
my $xmltv_attributes =
{
'source-info-url' => 'http://www.yle.fi/',
'source-data-url' => "$DATA_SITE_ROOT/",
'generator-info-name' => "XMLTV/$XMLTV::VERSION, $GRABBER_NAME $GRABBER_VERSION",
'generator-info-url' => 'http://www.xmltv.org',
};
# Set up LWP::UserAgent
# my $ua = LWP::UserAgent->new;
# $ua->agent("xmltv/$XMLTV::VERSION");
# Get options, including undocumented --cache option.
XMLTV::Memoize::check_argv('XMLTV::Get_nice::get_nice_aux');
# The list of channels available from the Yle Program Guide. Their
# names are deliberately specified in a manner which would be natural
# for people watching e.g. TV channels from Sweden (so that "TV1"
# would in their mindset not necessarily refer to Yle's TV1 channel -
# thus, the reason behind the "Yle" prefixing here).
#
# The key in this hash is the name of the channel as given on the Yle
# program guide web page.
my $channels =
{
'tv1.yle.fi' => {
'id' => 'tv1.yle.fi',
'group' => 3,
'display-name' => [[ 'YLE TV1', $LANGUAGE_CODE ]]
},
'tv2.yle.fi' => {
'id' => 'tv2.yle.fi',
'group' => 3,
'display-name' => [[ 'YLE TV2', $LANGUAGE_CODE ]]
},
'fem.yle.fi' => {
'id' => 'fem.yle.fi',
'group' => 3,
'display-name' => [[ 'YLE Fem', $LANGUAGE_CODE ]]
},
'teema.yle.fi' => {
'id' => 'teema.yle.fi',
'group' => 3,
'display-name' => [[ 'YLE Teema', $LANGUAGE_CODE ]]
},
'elava-arkisto.yle.fi' => {
'id' => 'elava-arkisto.yle.fi',
'group' => 3,
'display-name' => [[ 'Elava Arkisto', $LANGUAGE_CODE ]]
},
'areena.yle.fi' => {
'id' => 'areena.yle.fi',
'group' => 3,
'display-name' => [[ 'Areena', $LANGUAGE_CODE ]]
},
'mtv3.yle.fi' => {
'id' => 'mtv3.yle.fi',
'group' => 3,
'display-name' => [[ 'MTV3', $LANGUAGE_CODE ]]
},
'nelonen.yle.fi' => {
'id' => 'nelonen.yle.fi',
'group' => 3,
'display-name' => [[ 'Nelonen', $LANGUAGE_CODE ]]
},
'subtv.yle.fi' => {
'id' => 'subtv.yle.fi',
'group' => 3,
'display-name' => [[ 'Subtv', $LANGUAGE_CODE ]]
},
'liv.yle.fi' => {
'id' => 'liv.yle.fi',
'group' => 3,
'display-name' => [[ 'LIV', $LANGUAGE_CODE ]]
},
'jim.yle.fi' => {
'id' => 'jim.yle.fi',
'group' => 3,
'display-name' => [[ 'JIM', $LANGUAGE_CODE ]]
},
'ava.yle.fi' => {
'id' => 'ava.yle.fi',
'group' => 3,
'display-name' => [[ 'AVA', $LANGUAGE_CODE ]]
},
};
# Map between channel names (as presented by the YLE data) and channel
# IDs, as create by us.
my $channel_name_map = # deprecated
{
'TV1' => 'tv1.yle.fi',
'TV2' => 'tv2.yle.fi',
'MTV3' => 'mtv3.yle.fi',
'Nelonen' => 'nelonen.yle.fi',
'YLE Teema' => 'teema.yle.fi',
'FST5' => 'fst5.yle.fi',
'Subtv' => 'subtv.yle.fi',
'JIM' => 'jim.yle.fi',
# See note above about this one.
'Urheilukanava' => 'nelonen.sport.yle.fi'
};
my $channel_id_map =
{
'yle-tv1' => 'tv1.yle.fi',
'yle-tv2' => 'tv2.yle.fi',
'yle-fem' => 'fem.yle.fi',
'yle-teema' => 'teema.yle.fi',
'yle-areena' => 'areena.yle.fi',
'yle-elava-arkisto' => 'elava-arkisto.yle.fi',
'MTV3' => 'mtv3.yle.fi',
'Nelonen' => 'nelonen.yle.fi',
'SUB' => 'subtv.yle.fi',
'Liv' => 'liv.yle.fi',
'Jim' => 'jim.yle.fi',
'AVA' => 'ava.yle.fi',
'tv-finland' => 'tvfinland.yle.fi',
};
my @ARGUMENTS = @ARGV;
# Parse the standard XMLTV grabber options, using the XMLTV module.
my ($opt, $conf) = ParseOptions(
{
grabber_name => "tv_grab_fi_sv",
capabilities => [qw/baseline manualconfig apiconfig/],
stage_sub => \&config_stage,
listchannels_sub => \&list_channels,
version => $_version,
description => "Finland (Swedish)",
});
t("Command line arguments: " . join(' ', @ARGUMENTS));
# When we get here, we know that we are invoked in such a way that the
# channel data should be grabbed.
# Configure the output and write the XMLTV data - header, channels,
# listings, and footer
my $writer;
setup_xmltv_writer();
write_xmltv_header();
write_channel_list(@{ $conf->{channel} });
write_listings_data(@{ $conf->{channel} });
write_xmltv_footer();
# For the moment, we always claim that we've exited successfully...
exit 0;
sub t
{
my $message = shift;
print STDERR $message . "\n" if $opt->{debug};
}
sub config_stage
{
my($stage, $conf) = shift;
die "Unknown stage $stage" if $stage ne "start";
# This grabber doesn't need any configuration (except for
# possibly channel, selection), so this subroutine doesn't need
# to do very much at all.
my $result;
my $writer = new XMLTV::Configure::Writer(OUTPUT => \$result,
encoding => $XML_ENCODING);
$writer->start({ grabber => 'tv_grab_fi_sv' });
$writer->end('select-channels');
return $result;
}
# Returns a string containing an xml-document with <channel>-elements
# for all available channels.
sub list_channels
{
my ($conf, $opt) = shift;
my $result = '';
my $fh = new IO::Scalar \$result;
my $oldfh = select($fh);
# Create an XMLTV::Writer object. The important part here is that
# the output should go to $fh (in other words, to the $result
# string), NOT to stdout...
my %writer_args =
(
encoding => $XML_ENCODING,
OUTPUT => $fh
);
my $writer = new XMLTV::Writer(%writer_args);
$writer->start($xmltv_attributes);
# Loop over all channels and write them to this XMLTV::Writer.
foreach my $channel_id (keys %{ $channels })
{
# We must remove our proprietary hash key here, otherwise
# the XMLTV module will bark at us...
my $channel = $channels->{$channel_id};
delete($channel->{group});
$writer->write_channel($channel);
}
$writer->end;
select($oldfh);
$fh->close();
return $result;
}
# Determine options for XMLTV::Writer, and instantiate it.
sub setup_xmltv_writer
{
# output options
my %g_args = ();
if (defined $opt->{output})
{
t("\nOpening XML output file '$opt->{output}'\n");
my $fh = new IO::File ">$opt->{output}";
die "Error: Cannot write to '$opt->{output}', exiting" if (!$fh);
%g_args = (OUTPUT => $fh);
}
# Determine how many days of listings are required and
# range-check, applying default values if necessary. If --days or
# --offset is specified we must ensure that the values for days,
# offset and cutoff are passed to XMLTV::Writer.
my %d_args = ();
if (defined $opt->{days} || defined $opt->{offset})
{
if (defined $opt->{days})
{
if ($opt->{days} < 1 || $opt->{days} > 14)
{
if (!$opt->{quiet})
{
say("Specified --days option is not possible (1-14). " .
"Retrieving all available listings.");
}
$opt->{days} = 14
}
}
else
{
# No --days parameter were given. Use the default.
$opt->{days} = 14;
}
if (defined $opt->{offset})
{
if ($opt->{offset} < 0 || $opt->{offset} > 13)
{
if (!$opt->{quiet})
{
say("Specified --offset option is not possible (0-13). "
. "Retrieving all available listings.");
}
$opt->{offset} = 0;
}
}
else
{
$opt->{offset} = 0;
}
}
t("Setting up XMLTV::Writer using \"" . $XML_ENCODING . "\" for output");
$writer = new XMLTV::Writer(%g_args, %d_args, encoding => $XML_ENCODING);
}
# Writes the XMLTV header.
sub write_xmltv_header
{
t("Writing XMLTV header");
$writer->start($xmltv_attributes);
}
# Writes the channel list (of all configured channels).
sub write_channel_list
{
my (@channels) = @_;
t("Started writing <channel> elements");
foreach my $channel_id (@channels)
{
# We must remove our proprietary hash key here, otherwise the
# XMLTV module will bark at us...
my $channel = $channels->{$channel_id};
my $group = $channel->{group};
delete($channel->{group});
$writer->write_channel($channel);
# Put it back, since we'll need it later.
$channel->{group} = $group;
}
t("Finished writing <channel> elements");
}
# Download listings data for all the configured channels
sub write_listings_data
{
my (@channels) = @_;
my $programmes = {};
say(scalar @channels ." configured channels") if !$opt->{quiet};
# 2014-04-13 channel groups are deprecated
my $channel_groups = {};
# create hash of wanted channels
my %wanted;
@wanted{ @channels } = ();
foreach my $channel (@channels)
{
# Get the channel group for this channel and check if the group has already
# been fetched.
my $channel_group = $channels->{$channel}->{group};
if (defined($channel_groups->{$channel_group}))
{
# Group has been fetched already - ignoring it.
next;
}
# Mark the group as fetched.
$channel_groups->{$channel_group} = 1;
my $today = DateTime->today( time_zone => $TIMEZONE );
if (!$opt->{quiet})
{
# say("Downloading data for channel group $channel_group");
}
# Get start & stop times for the grab
my $t = $today->clone->add( days => $opt->{offset} );
my $grab_start = $t->epoch() + $t->offset();
$t = $t->add( days => $opt->{days} );
my $grab_end = $t->epoch() + $t->offset();
undef $t;
# schedules run from 06:00-06:00 so to pass tv_validate_file we need to get the day before also
for (my $i = $opt->{offset} - 1; $i < $opt->{offset} + $opt->{days}; $i++)
{
# Create the URL for the schedules for this
# channel/month/day combination.
my $date = $today->clone->add( days => $i );
# broken: my $url = sprintf("%s?g=%s&d=%s", $DATA_SITE_ROOT, $channel_group, $date->strftime( '%Y%m%d' ));
my $url = sprintf('%stv/guide?t=%s', $DATA_SITE_ROOT, $date->strftime( '%Y-%m-%d' )); # e.g. http://ohjelmaopas.yle.fi/tv/guide?t=2014-04-14
say("Downloading $url") if $opt->{debug};
# Get the HTML from the created URL.
# my $response = $ua->get($url);
# my $file_contents = $response->decoded_content;
# ^^ change to use Get_nice
#
$XMLTV::Get_nice::IncludeUnknownTags = 1;
my $tree = get_nice_tree($url,'','UTF-8');
#$tree->dump; exit;
next if !$tree;
my $t_guide = $tree->look_down('_tag' => 'section', 'class' => qr/guide/);
next if !$t_guide;
my $t_chanlists = $t_guide->look_down('_tag' => 'ul', 'class' => qr/channel-lists/);
next if !$t_chanlists;
my @t_channels = $t_chanlists->look_down('_tag' => 'li', sub {$_[0]->parent() == $t_chanlists} );
say 'Found '.scalar @t_channels.' channels' if $opt->{debug};
next if scalar @t_channels == 0;
for (my $j = 0; $j < scalar @t_channels; $j++) { # can't use foreach because of clumpidx processing
my $t_channel = $t_channels[$j];
my $chan_id = $t_channel->look_down('_tag' => 'h1')->attr('id');
my $c_chan_name = $t_channel->look_down('_tag' => 'h1')->as_text();
# Check if this program belongs to one of the
# configured channels. If it doesn't, ignore it.
say "Found $chan_id" if $opt->{debug};
my $c_channel_id = $channel_id_map->{$chan_id};
say "UNKNOWN CHANNEL ID $chan_id" if $opt->{debug} && !$c_channel_id;
next unless( $c_channel_id && exists( $wanted{ $c_channel_id } ) );
say "Processing $c_channel_id" if $opt->{debug};
my $t_schedule = $t_channel->look_down('_tag' => 'ul', 'class' => qr/selected/);
my @t_progs = $t_schedule->look_down('_tag' => 'li', 'class' => qr/program-entry/);
foreach my $t_prog (@t_progs) {
my ($t_prog_label, $t_prog_desc, $t_prog_link, $t_prog_title, $t_prog_film);
my ($p_dtstart, $p_dtend, $p_title, $p_subtitle, $p_desc, $p_url, $p_category);
$t_prog_label = $t_prog->look_down('_tag' => 'div', 'class' => qr/program-label/);
$t_prog_desc = $t_prog->look_down('_tag' => 'div', 'class' => qr/program-desc/);
# If programme is outside of requested timeframe then drop it
my $p_start = $t_prog_label->look_down('_tag' => 'time', 'class' => qr/dtstart/)->attr('datetime');
$p_start = epoch_isotime($p_start);
next if $p_start < $grab_start || $p_start >= $grab_end;
$p_dtstart = xmltv_isotime( $t_prog_label->look_down('_tag' => 'time', 'class' => qr/dtstart/)->attr('datetime') );
$p_dtend = xmltv_isotime( $t_prog_label->look_down('_tag' => 'time', 'class' => qr/dtend/)->attr('datetime') );
# it seems the stop times aren't to be trusted
# - on Sub tv they seem to be the actual stop times of the programme (i.e. start + duration), i.e. excludes trailers/ads before next prog (so causing gaps)
# - on Areena and Ava they're often 1 or 2 minutes *after* the start of the next prog (so causing overlaps).
undef $p_dtend;
$t_prog_title = $t_prog_label->look_down('_tag' => 'div', 'class' => qr/program-title/);
$p_title = $t_prog_title->look_down('_tag' => 'a', 'class' => qr/link-grey/)->as_text();
# the <div class="program-title"> may contain <span class="movie">Film</span> if prog is a film
if ( $t_prog_film = $t_prog_title->look_down('_tag' => 'span', 'class' => qr/movie/) ) {
$p_category = $t_prog_film->as_text();
}
$p_desc = $t_prog_desc->look_down('_tag' => 'p')->as_text(); # desc is in first <p>
$t_prog_link = $t_prog_desc->look_down('_tag' => 'a', 'class' => qr/double-angle/, sub { return $_[0]->as_text() =~ /Programinformation/ });
$p_url = $t_prog_link->attr('href') if $t_prog_link;
# occasionally it seems there's a 'null' programme (usually with overlapping time)
# I think we should drop these
next if $p_title eq '';
# Create the data structure for the program.
my $program =
{
'channel' => $c_channel_id,
'title' => [[ encode('iso-8859-1', $p_title), $LANGUAGE_CODE ]],
'start' => $p_dtstart
};
$program->{'stop'} = $p_dtend if (defined $p_dtend && $p_dtend ne '');
$program->{'desc'} = [[ encode('iso-8859-1', tidy( $p_desc) ), $LANGUAGE_CODE ]] if (defined $p_desc && $p_desc ne '');
$program->{'sub-title'} = [[ encode('iso-8859-1', $p_subtitle), $LANGUAGE_CODE ]] if (defined $p_subtitle && $p_subtitle ne '');
$program->{'category'} = [[ encode('iso-8859-1', $p_category) ]] if (defined $p_category && $p_category ne '');
$program->{'url'} = [ $p_url ] if (defined $p_url && $p_url ne '');
# store the programme avoiding duplicates
# also check for deuplicate start times and set clumpidx
if ( defined $programmes->{$c_channel_id}->{$p_dtstart} ) {
# duplicate prog or contemporary?
my $dup = 0;
foreach my $p ( @{ $programmes->{$c_channel_id}->{$p_dtstart} } ) {
$dup = 1 if ( $p->{'title'}[0][0] eq $program->{'title'}[0][0] ); # duplicate
}
next if $dup; # ignore duplicates
if (!$dup) {
# contemporary programme so set clumpidx
my $numclumps = scalar @{ $programmes->{$c_channel_id}->{$p_dtstart} } + 1;
# set (or adjust) clumpidx of existing programmes
my $i = 0;
foreach my $p ( @{ $programmes->{$c_channel_id}->{$p_dtstart} } ) {
$p->{'clumpidx'} = "$i/$numclumps";
$i++;
}
# set clumpidx for new programme
$program->{'clumpidx'} = "$i/$numclumps";
}
}
# store the programme
push @{ $programmes->{$c_channel_id}->{$p_dtstart} }, $program;
}
}
}
}
# All data has been gathered. We can now write the
# program element to the output.
#
foreach ( keys %{$programmes} ) {
my $ch_progs = $programmes->{$_};
foreach ( sort keys %{$ch_progs} ) {
my $dt_progs = $ch_progs->{$_};
foreach (@{ $dt_progs }) {
$writer->write_programme($_);
}
}
}
}
# Writes the XMLTV footer.
sub write_xmltv_footer
{
t("\nWriting XMLTV footer\n");
$writer->end;
}
# Remove bad chars from an element
sub tidy( $ ) {
return $_[0] if !defined $_[0];
$_[0] =~ s/(\s)\xA0/$1/og; # replace 'space- ' with 'space'
$_[0] =~ s/\xA0/ /og; # replace any remaining with space
$_[0] =~ s/\xAD//og; # delete soft hyphens
return $_[0];
}
# Trim function to remove whitespace from the start and end of the
# string.
sub trim ($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
# Left trim function to remove leading whitespace.
sub ltrim ($)
{
my $string = shift;
$string =~ s/^\s+//;
return $string;
}
# Strips HTML tags from a string.
sub strip_tags ($)
{
my $string = shift;
$string =~ s/<(?:[^>'"]*|(['"]).*?\1)*>//gs;
return $string;
}
# Converts a DateTime + time of the form "09.45" to something suitable
# for XMLTV, i.e. 201010270945 +0300
sub xmltv_time ($$)
{
my $date = shift;
my $time = shift;
my ($hour, $minute) = ($time =~ m|^(\d+)\.(\d+)$|);
my $dt=$date->clone();
if ($hour > 23)
{
$dt->add(days => 1);
$hour -= 24;
}
$dt->set_hour($hour);
$dt->set_minute($minute);
return $dt->strftime('%Y%m%d%H%M%S %z');
}
# Converts a date + time of the form "09.45" to something suitable for
# XMLTV, i.e. 201010270945, and handle special semantics for an "end
# time" (related to date rollovers).
sub xmltv_end_time ($$)
{
my $date = shift;
my $start_time = shift;
my $end_time = shift;
if ($end_time lt $start_time)
{
# This program is spanning across a date boundary. We need to increase the
# date part so that the end time gets produced correctly.
# Clone to ensure that this adjustment is only for this timestamp!
$date=$date->clone->add(days => 1);
}
return xmltv_time($date, $end_time);
}
# Converts a DateTime + time of the form "2014-04-12T09:00:00.000+03:00" to something suitable
# for XMLTV, i.e. 20140412090000 +0300
sub xmltv_isotime ($)
{
my $time = shift;
# let's not overthink this... just use a regexp!
$time =~ s/[:-]//g;
$time =~ /^(\d{8})T(\d{6}).*(\+\d{4})$/;
return $1.$2.' '.$3;
}
# Converts a DateTime + time of the form "2014-04-12T09:00:00.000+03:00" to epoch time (local or utc)
sub epoch_isotime ($;$)
{
my $time = shift;
my $withoffset = shift; # set to 1 to generate local epoch (else will get utc)
my ($y, $m, $d, $h, $i, $s, $ms, $t, $th, $tm) = $time =~ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{3})([\+-])(\d{2}):(\d{2})$/;
$y -= 1900;
$m -= 1;
use POSIX;
my $epoch = mktime($s, $i, $h, $d, $m, $y);
if (defined $withoffset && $withoffset) {
my $offset = ($th * 3600) + ($tm * 60);
$epoch += $offset if $t eq '+';
$epoch -= $offset if $t eq '-';
}
return $epoch;
}
|