This file is indexed.

/usr/bin/tv_grab_se_tvzon is in xmltv-util 0.5.69-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
#!/usr/bin/perl -w

=pod

=head1 NAME

tv_grab_se_tvzon - Grab TV listings for Sweden.

=head1 SYNOPSIS

tv_grab_se_tvzon --help

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

tv_grab_se_tvzon [--config-file FILE] 
           [--days N] [--offset N]
           [--output FILE] [--quiet] [--debug]

tv_grab_se_tvzon --list-channels [--config-file FILE]
           [--output FILE] [--quiet] [--debug]
                 
                
=head1 DESCRIPTION

Output TV and listings in XMLTV format for many stations
available in Sweden. 

First you must run B<tv_grab_se_tvzon --configure> to choose which stations
you want to receive.

Then running B<tv_grab_se_tvzon> with no arguments will get a listings for
the stations you chose for five days including today.

=head1 OPTIONS

B<--configure> Prompt for which stations to download and write the
configuration file.

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

B<--output FILE> When grabbing, write output to FILE rather than
standard output.

B<--days N> When grabbing, grab N days rather than 5.

B<--offset N> Start grabbing at today + N days.  N may be negative.

B<--quiet> Suppress the progress-bar normally shown on standard error.

B<--debug> Provide more information on progress to stderr to help in
debugging.

B<--list-channels>    Output a list of all channels that data is available
                      for. The list is in xmltv-format.

B<--version> Show the version of the grabber.

B<--help> Print a help message and exit.

=head1 ERROR HANDLING

If the grabber fails to download data for some channel on a specific day, 
it will print an errormessage to STDERR and then continue with the other
channels and days. The grabber will exit with a status code of 1 to indicate 
that the data is incomplete. 

=head1 ENVIRONMENT VARIABLES

The environment variable HOME can be set to change where configuration
files are stored. All configuration is stored in $HOME/.xmltv/. On Windows,
it might be necessary to set HOME to a path without spaces in it.

=head1 SUPPORTED CHANNELS

For information on supported channels, see http://xmltv.xmltv.se/

=head1 AUTHOR

Mattias Holmlund, mattias -at- holmlund -dot- se. This documentation
and parts of the code copied from tv_grab_uk by
Ed Avis, ed -at- membled -dot- com.

=head1 BUGS

=cut

use strict;

use XMLTV;
use XMLTV::ProgressBar;
use XMLTV::Options qw/ParseOptions/;
use XMLTV::Configure::Writer;

use XML::LibXML;
use Date::Manip;
use Compress::Zlib;
use File::Path;
use File::Basename;
use IO::Scalar;
use LWP;

my $ua;
$ua = LWP::UserAgent->new();
$ua->agent("xmltv/$XMLTV::VERSION");
$ua->env_proxy();

use HTTP::Cache::Transparent;

# Although we use HTTP::Cache::Transparent, this undocumented --cache
# option for debugging is still useful since it will _always_ use a
# cached copy of a page, without contacting the server at all.
#
use XMLTV::Memoize; XMLTV::Memoize::check_argv('getuncompressed');

sub t;

my $default_root_url = 'http://xmltv.xmltv.se/channels.xml.gz';
my $default_cachedir = get_default_cachedir();

my( $opt, $conf ) = ParseOptions( { 
    grabber_name => "tv_grab_se_tvzon",
    capabilities => [qw/baseline manualconfig tkconfig apiconfig cache/],
    stage_sub => \&config_stage,
    listchannels_sub => \&list_channels,
    load_old_config_sub => \&load_old_config,
    version => '$Id: tv_grab_se_swedb.in,v 1.8 2010/10/01 17:49:30 dekarl Exp $',
    description => "Sweden (TVZon)",

} );

if (not defined( $conf->{cachedir} )) {
    print STDERR "No cachedir defined in configfile " . 
                 $opt->{'config-file'} . "\n" .
                 "Please run the grabber with --configure.\n";
    exit 1;
}

if (not defined( $conf->{'root-url'} )) {
    print STDERR "No root-url defined in configfile " .
                 $opt->{'config-file'} . "\n" .
                 "Please run the grabber with --configure.\n";
    exit 1;
}

if (not defined( $conf->{'channel'} )) {
    print STDERR "No channels selected in configfile " .
                 $opt->{'config-file'} . "\n" .
                 "Please run the grabber with --configure.\n";
    exit 1;
}

init_cachedir( $conf->{cachedir}->[0] );
HTTP::Cache::Transparent::init( { 
    BasePath => $conf->{cachedir}->[0],
    NoUpdate => 15*60,
    Verbose => $opt->{debug},
    } );

binmode (STDOUT);

my($xmldecl, $channels) = load_channels( $conf->{'root-url'}->[0] );

my( $odoc, $root );
my $warnings = 0;

write_header( $xmldecl );

write_channel_list( $conf->{channel} );

my $now = ParseDate( 'now' );
my $date =$now;
$date = DateCalc( $now, "+$opt->{offset} days" ) 
    if( $opt->{offset} );

my $bar = undef;
$bar = new XMLTV::ProgressBar( {
    name => 'downloading listings',
    count => $opt->{days} * @{$conf->{channel}},
    }) if (not $opt->{quiet}) && (not $opt->{debug});

for( my $i=0; $i < $opt->{days}; $i++ )
{
    t "Date: $date";
    foreach my $channel_id (@{$conf->{channel}})
    {
        # We have already warned the user if the channel doesn't exist.
        if( exists $channels->{$channel_id} )
        {
            t "  $channel_id";
            my( $channel_name, $url ) = @{$channels->{$channel_id}};
            print_data( $url, $channel_id, $date )
                or warning( "Failed to download data for $channel_id on " . 
                            UnixDate( $date, "%Y-%m-%d" ) . "." );
        }
        $bar->update() if defined( $bar );
    }
    $date = DateCalc( $date, "+1 days" );
}

$bar->finish() if defined $bar;

write_footer();

# Signal that something went wrong if there were warnings.
exit(1) if $warnings;

# All data fetched ok.
t "Exiting without warnings.";
exit(0);

sub t
{
    my( $message ) = @_;
    print STDERR $message . "\n" if $opt->{debug};
}

sub warning
{
    my( $message ) = @_;
    print STDERR $message . "\n";
    $warnings++;
}

sub list_channels
{
    my( $conf, $opt ) = @_;

    ( $xmldecl, $channels ) = load_channels( $conf->{'root-url'}->[0] );
    
    my $result="";
    my $fh = new IO::Scalar \$result;
    my $oldfh = select( $fh );
    write_header( $xmldecl );
    write_channel_list( [sort keys %{$channels}] );
    write_footer();
    select( $oldfh );
    $fh->close();

    return $result;
}

sub config_stage
{
    my( $stage, $conf ) = @_;

    die "Unknown stage $stage" if $stage ne "start";

    my $result;
    my $writer = new XMLTV::Configure::Writer( OUTPUT => \$result,
                                               encoding => 'iso-8859-1' );
    $writer->start( { grabber => 'tv_grab_se_tvzon' } );
    $writer->write_string( {
        id => 'root-url', 
        title => [ [ 'Root URL for grabbing data', 'en' ] ],
        description => [ 
         [ 'The file at this URL describes which channels are available and ' .
           'where data can be found for them. ', 'en' ] ],
        default => $default_root_url,
     } );
    $writer->write_string( {
        id => 'cachedir', 
        title => [ [ 'Directory to store the cache in', 'en' ] ],
        description => [ 
         [ 'tv_grab_se_tvzon uses a cache with files that it has already '. 
           'downloaded. Please specify where the cache shall be stored. ', 
           'en' ] ],
        default => $default_cachedir,
     } );

    $writer->end( 'select-channels' );

    return $result;
}

#
# Load a configuration file in the old format.
#

sub load_old_config
{
    my( $config_file ) = @_;

    my @lines = XMLTV::Config_file::read_lines( $config_file );
    
    my $conf = {};
    $conf->{cachedir}->[0] = $default_cachedir;
    $conf->{'root-url'}->[0] = $default_root_url;
    $conf->{channel} = [];

    foreach my $line (@lines)
    {
        next unless defined $line;

        my( $command, $param ) = split( /\s+/, $line, 2 );
        $param =~ tr/\n\r//d;
        $param =~ s/\s+$//;

        if ( $command =~ /^\s*root-url\s*$/) {
            $conf->{'root-url'}->[0] = $param;
        } elsif  ( $command =~ /^\s*channel\s*$/) {
            push @{$conf->{channel}}, $param;
        } elsif ( $command eq 'cache-dir' ) {
            $conf->{'cachedir'}->[0] = $param;
        } else {
            die "Unknown command $command in config-file $config_file"
        }
    }

    return $conf;
}

sub get_default_cachedir
{
    my $winhome = $ENV{HOMEDRIVE} . $ENV{HOMEPATH} 
    if defined( $ENV{HOMEDRIVE} ) 
        and defined( $ENV{HOMEPATH} ); 
    
    my $home = $ENV{HOME} || $winhome || ".";
    return "$home/.xmltv/cache";
}

sub init_cachedir
{
    my( $path ) = @_;
    if( not -d $path )
    {
        mkpath( $path ) or die "Failed to create cache-directory $path: $@";
    }
}

sub load_channels
{
    my( $url ) = @_;
    
    my %channels;

    my $xmldata = getuncompressed( $url );

    defined( $xmldata ) or die "Failed to fetch $url";

    my $xml = XML::LibXML->new;
    
    my $doc = $xml->parse_string($xmldata);

    my $xmldecl = "<?xml version='" . $doc->version() . "' " . 
        "encoding='" . $doc->encoding() . "'?>\n";

    my $ns = $doc->find( "//channel" );

    foreach my $node ($ns->get_nodelist)
    {
        my $id = $node->findvalue( '@id' );
        my $name = $node->findvalue( 'display-name[1]' );
        my $url = $node->findvalue( 'base-url' );
        my $urlns = $node->find( './base-url' );
        foreach my $urlnode ($urlns->get_nodelist)
        {
            $node->removeChild( $urlnode );
        }
        $channels{$id} = [ $name, $url, $node->toString(0, 1) ];
    }

    return ($xmldecl, \%channels);
}

sub print_data
{
    my( $rooturl, $channel_id, $date ) = @_;
    
    my $url = $rooturl . $channel_id . "_" . UnixDate( $date, "%Y-%m-%d" ) . 
        ".xml.gz";

    my $xmldata = getuncompressed( $url );

    defined $xmldata or return 0;

    my $in = new IO::Scalar \$xmldata;
    while( my $line = $in->getline() )
    {
        last if $line =~ /<tv/;
    }

    while( my $line = $in->getline() )
    {
        last if $line =~ /<\/tv>/;
        print $line;
    }

    return 1;
}

sub write_header
{
    my( $xmldecl ) = @_;

    # Use the same xml declaration as the one in
    # channels.xml
    print $xmldecl;
    print '<!DOCTYPE tv SYSTEM "xmltv.dtd">' . "\n";
    print "<tv>\n";
}

sub write_channel_list
{
    my( $channel_list ) = @_;

    # Write list of channels.
    t 'Writing list of channels.';

    foreach my $channel_id (@{$channel_list})
    {
        if( not exists $channels->{$channel_id} )
        {
            print STDERR "Unknown channel $channel_id." .
                " See http://xmltv.xmltv.se/" . 
                " for a list of available channels or run" . 
                " tv_grab_se_tvzon --configure to reconfigure.\n";
            next;
        }
        
        my( $channel_name, $url, $def ) = @{$channels->{$channel_id}};
        print "  $def\n";
    }
}

sub write_footer
{
    print "</tv>\n";
}

sub getuncompressed {
    my( $url ) = @_;

    my $response = $ua->get($url);

    return undef
        unless $response->is_success;

    my $compressed = $response->content
        or return undef;

    # Since LWP 5.827, the result from get() is already
    # uncompressed.

    my $uncompressed;

    eval {
	$uncompressed = Compress::Zlib::memGunzip( \$compressed );
    };

    $uncompressed = $compressed if not defined $uncompressed;

    return $uncompressed;
}

### Setup indentation in Emacs
## Local Variables:
## perl-indent-level: 4
## perl-continued-statement-offset: 4
## perl-continued-brace-offset: 0
## perl-brace-offset: -4
## perl-brace-imaginary-offset: 0
## perl-label-offset: -2
## cperl-indent-level: 4
## cperl-brace-offset: 0
## cperl-continued-brace-offset: 0
## cperl-label-offset: -2
## cperl-extra-newline-before-brace: t
## cperl-merge-trailing-else: nil
## cperl-continued-statement-offset: 2
## End: