This file is indexed.

/usr/bin/tv_grab_na_icons 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
#!/usr/bin/perl -w

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell
#
# tv_grab_na_icon
#
# This script goes to the zap2it web site and downloads icon links or images
# to the share/icons directory.
#
# (C)2001 - Robert Eden, free to use under the GNU License.
#
#  Robert Eden - reden@cpan.org
#  	
#     See cvs logs entries for module history
#

=pod

=head1 NAME

tv_grab_na_icons - Grab channel icon images or links from zap2it.com

=head1 SYNOPSIS

tv_grab_na_icons [--debug] [--links] [--share dir]

tv_grab_na_icons --help

tv_grab_na_icons --version

=head1 DESCRIPTIONS

This script goes to the zap2it web site and downloads icon
links or images to the share/icons directory.

It was designed to allow icons to be added by tv_grab_na_dd,
but there is no reason it can't be used for other purposes.

To minimize the load on zap2it.com, downloaded icons are recommended.
Links are available for those applications that need it.

Also to reduce the load on zap2it.com, this script should be run
sparingly. There is really no need to run it frequently, new networks
or icons don't show up that often.  To emphasize that point, there is
no --configure mode, the questions need to be answered each run.

=head1 OPTIONS

=over

=item --links

Store imge URLs in *.url files instead of downloading images.

=item --share I<SHAREDIR>

Icons are stored in I<SHAREDIR>/icons.  The share directory is set at install time,
but there may be times when it needs to be specified. (for example: no write access to the default share
directory)

=item --debug

Turns on debugging mode. The grabber will be more verbose, and saves some extra debugging data with the icons.

=item --version

Show the version of the grabber.

=item --help

Print a help message and exit.

=back

=head1 SEE ALSO

L<xmltv>.

=head1 AUTHOR

Robert Eden

=cut 

use strict;
use open qw(:utf8);
use Getopt::Long;
use Data::Dumper;
use File::Basename;
use WWW::Mechanize 1.02;
use HTML::TreeBuilder;
use XML::Twig 3.28;

use XMLTV;
use XMLTV::Ask;
use XMLTV::Version '$Id: tv_grab_na_icons.in,v 1.17 2010/04/27 03:50:46 rmeden Exp $ ';
use XMLTV::Usage <<END

$0 - Grab channel icon images or links from zap2it.com

tv_grab_na_icons [--debug] [--links] [--share dir]

tv_grab_na_icons --help

tv_grab_na_icons --version

END
;

select STDERR; $|=1;
select STDOUT; $|=1;

my $opt_help=0;
my $opt_debug=0;
my $opt_links=0;
my $SHARE_DIR='/usr/share/xmltv'; # by grab/na_icons/tv_grab_na_icons.PL
my $fileno=0;
my $file=undef;

GetOptions(
	   'help'          => \$opt_help,
	   'debug'         => \$opt_debug,
	   'links'         => \$opt_links,
	   'share=s'       => \$SHARE_DIR,
	  )
  or usage(0);

usage(1) if $opt_help;

die "ERROR:Share directory '$SHARE_DIR' not found\n" unless -d $SHARE_DIR;
die "ERROR:Share directory '$SHARE_DIR' not writable\n" unless -w $SHARE_DIR;
mkdir "$SHARE_DIR/icons" unless -d "$SHARE_DIR/icons";
die "ERROR:directory '$SHARE_DIR/icons' not writable\n" unless -w "$SHARE_DIR/icons";
#
# create our agent
#
my $a = WWW::Mechanize->new( env_proxy => 1   );
$a->agent(sprintf("%s/$XMLTV::VERSION",basename($0)||'xmltv'));

print STDERR "Getting inital page\n" if $opt_debug;
$a->get('http://www.zap2it.com/index');
check_page($a);

#
# select zip
#
while (1)
{
    die "ERROR:Can't find zipcode form\n" unless find_form($a,"zipcode");

    my $zip=ask("\nPostal Code:");
    print STDERR "Submitting zip code $zip\n" if $opt_debug;
    $a->field("zipcode",$zip);
    $a->submit;
    check_page($a);
#
# bug in zap2it.com, zip not being picked up from first page, try next page
#
    if (grep(/Enter ZIP for local listings/,$a->content)) {
        die "ERROR:Can't find zipcode form\n" unless find_form($a,"zipcode");
        print STDERR "Submitting zip code $zip (again)\n" if $opt_debug;
        $a->field("zipcode",$zip);
        $a->submit;
    check_page($a);
   }
    last if grep(/No Provider Selected/,$a->content);
    last if grep(/Choose Your Provider/,$a->content);
    print "  Invalid Postal Code, try again\n";
}
# for some reason, we only get a little data without this header
$a->add_header( 'Accept-Language' => 'en-us,en;q=0.5');

#
# select lineup
#
{
    my %lineups=();
    my $def_lineup=undef;
    foreach $_ ($a->links) {
        next unless ($_->url =~ /lineupId=(.+)/ );
        my $name=$_->text;
           $name =~ s/\xa0//g; # drop bad characters

        print "Lineup $name\n" if $opt_debug;
        $lineups{$name}=$_;
        $def_lineup=$name unless defined $def_lineup;
    }
    
#print "Link: ", Dumper($_)  foreach $a->links();

    die "ERROR:Can't find provider links\n" unless defined $def_lineup;

    my $name=$def_lineup;
    $name=ask_choice("\nLineup?",$name,sort keys %lineups);

    printf STDERR "getting lineup %s\n",$lineups{$name}->url if $opt_debug;
#print Dumper($lineups{$name});
    $a->follow_link(url => $lineups{$name}->url);
    check_page($a);
} # select lineup
    
#
# select all channels
#
#    print "Display All Channels\n" if $opt_debug;
#    $a->follow_link(text_regex => qr/ALL CHANNELS/i) || warn "*WARNING* Can't find all channels link, hope we got them all!\n";
#    check_page($a);
#
#
# Convert HTML to XML
#
# we can use TWIG to do this because we need to use utf8_mode to avoid a warning
#
my $tree = HTML::TreeBuilder->new; # empty tree
   $tree->utf8_mode(1);
   $tree->parse($a->content);

my $xml = $tree->as_XML;

#
# Now parse the XML
#
my $image;
my %icons;
my $twig=XML::Twig->new(   
         twig_roots    => { html => 1},
		 twig_handlers => 
		      {
		         td  => sub {
                                  my $img_el;
                                  if (defined  $_->att('class')
                                            && $_->att('class') =~ /zc-st/ ) {
                                      my $name=$_ -> parent_trimmed_text;
                                      $name=~s/^([\.\d]+) //; # trim leading channel number
                                      
                                      if ( $img_el=$_ -> first_descendant('img')) {
                                          $icons{$name}=$img_el->att('src');
                                          print "Got image $name->$icons{$name}\n" if $opt_debug;
                                      }
                                      else {
                                          print "Got $name but no image\n" if $opt_debug;
                                      }
                                  }
                                  $_->twig->purge;
                                  return 0;
		         }
		      });
$twig->parse_html($xml);

#
# check for problems
#
unless (keys %icons)
{
        open  FILE,">na_icon_error.html" || die "ERROR:Can't open na_icon_error.html\n";
        print FILE $a->content;
        close FILE;
        die "ERROR:No icons were found.  Please check 'na_icon_error.html'\n";
}

#
# print results
#
my $base=$a->base;
foreach (sort keys %icons)
{
    $image=URI->new_abs($icons{$_},$base);
    if ($opt_links)
    {
        $file="$SHARE_DIR/icons/$_.url";
        open(FILE,">$file") || die "ERROR:Can't write to $file\n";
            print FILE $image."\n";
            close FILE;
        printf "Stored %10s in %20s\n",$_,$file;
    }
    else
    {
        my $type=(fileparse($image,'\..*'))[2];
        $file="$SHARE_DIR/icons/$_$type";
        printf "Getting %10s as %20s: %s\n",$_,$file,$a->mirror($image,$file)->message;
    }
}
    
exit;

#print Dumper($a);
#print "Link: ", Dumper($_)  foreach $a->links();
#print "Form: ", $_->dump  foreach $a->forms();
#print $a->current_form->dump;

#
# check status, write out html file
#
sub check_page {
    my $res=shift || die "ERROR:No Mechanize specified\n";
    $fileno++;
    if ($opt_debug)
    {
        $file="na_icon_${fileno}";
        open  FILE,">$file.html" || die "ERROR:Can't open $file.html\n";
        print FILE $res->content;
        close FILE;

        open  FILE,">$file.txt" || die "ERROR:Can't open $file.txt\n";
        print FILE Dumper($res);
        close FILE;
    }
    die "ERROR:page error ",$res->status_line unless $res->success;
} # check_page

#
# subroutine to search for form w/o knowing it's name
#
sub find_form
{
    my $mech=shift || die "ERROR:find_form: mechanize object not specified";
    my $name=shift || die "ERROR:find_form: field name not specified";
    my @forms=$mech->forms;
    my $fn=0;
    foreach (0..$#forms)
    {
        $fn=$_ if $forms[$_]->find_input($name);
    }
    $mech->form_number($fn+1) if $fn;
    return $fn;
} #find_form