1#!/usr/local/bin/perl -w
2
3=pod
4
5=head1 NAME
6
7tv_count - Count (and print) the number of channels and programmes
8in an XMLTV file.
9
10=head1 SYNOPSIS
11
12tv_count FILE
13
14=head1 DESCRIPTION
15
16Read XMLTV listings and print a count of the number of channels and
17number of programmes in the file.
18
19=head1 EXAMPLE
20
21Use C<tv_count listings.xml> to count programmes in the file called
22F<listings.xml>.
23
24=head1 SEE ALSO
25
26L<xmltv(5)>
27
28=head1 AUTHOR
29
30Copyright Geoff Westcott, February 2013.
31
32This code is distributed under the GNU General Public License v2 (GPLv2) .
33
34=cut
35
36
37my $_version 	= '$Id: tv_count,v 1.2 2015/07/12 00:46:37 knowledgejunkie Exp $';
38
39use strict;
40use XML::TreePP;		# http://search.cpan.org/~kawasaki/XML-TreePP-0.41/lib/XML/TreePP.pm
41use Date::Parse;
42use POSIX qw(strftime);
43use Getopt::Std;
44use Data::Dumper;
45
46# Process command line arguments
47my %opts = ();              			# hash to store input args
48getopts("dDhi:o:m:q",\%opts); 		# load the args into %opts
49my $input_file     = ($opts{'i'}) ? $opts{'i'} : $ARGV[0];     # main file
50my $debug          = ($opts{'d'}) ? $opts{'d'} : "";    # print out debugging when set to true (1)
51my $debugmore      = ($opts{'D'}) ? $opts{'D'} : "";    # print out debugging when set to true (1)
52my $quiet_mode     = ($opts{'q'}) ? $opts{'q'} : "";
53
54if ((!%opts && ! -r $ARGV[0]) || $opts{'h'}) {
55    # Print usage message
56    usage();
57}
58
59# Parse the XMLTV file
60my $tpp = XML::TreePP->new();
61$tpp->set( force_array => [ 'channel', 'programme' ] );  # force array ref for some fields
62
63
64my $xmltv = $tpp->parsefile( $input_file );
65if ($debugmore) {  print Dumper($xmltv); }
66
67
68my $ccount = scalar @{ $xmltv->{'tv'}->{'channel'} };
69my $pcount = scalar @{ $xmltv->{'tv'}->{'programme'} };
70if (!$quiet_mode) { print "Count : $ccount channels $pcount programmes \n"; }
71
72
73
74###############################################################################
75###############################################################################
76
77
78sub usage {
79    #
80    # print Usage message
81    #
82    my $filename = (split(/\//,$0))[-1];
83		print STDERR << "EOF";
84
85Count (and print) the number of channels and programmes in an XMLTV file
86
87Usage: $filename [-dDh] -i input_xmltv_file
88
89-h        : this (help) message
90-d        : print debugging messages
91-D        : print even more debugging messages
92-q        : quiet mode (no STDOUT messages)
93-i file   : input XMLTV file (or filename as first arg to script)
94
95example: $filename -i xmltv.xml
96
97EOF
98exit;
99}
100