/usr/bin/tv_split 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 | #!/usr/bin/perl -w
=pod
=head1 NAME
tv_split - Split XMLTV listings into separate files by date and
channel.
=head1 SYNOPSIS
tv_split --output TEMPLATE [FILE...]
=head1 DESCRIPTION
Read XMLTV listings and split them into some number of output files.
The output file chosen for each programme is given by substitutions on
the filename template supplied. You can split listings by time and by
channel.
The TEMPLATE is a filename but substitutions are applied: first
C<%channel> is replaced with the id of a programmeE<39>s channel, and
then L<Date::Manip> substitutions (which broadly follow L<date(1)>)
are applied based on the start time of each programme. In this way
each programme is written to a particular output file. When an output
file is created it will also contain all the channel elements from the
input.
One or more input files can be given; if more than one then they are
concatenated in the same way as L<tv_cat>. If no input files are
given then standard input is read.
=head1 EXAMPLE
Use C<tv_split --output %channel-%Y%m%d.xml> to separate standard
input into separate files for each day and channel. The files will be
created with names like B<bbc1.bbc.co.uk-20020330.xml>.
=head1 SEE ALSO
L<Date::Manip(3)>.
=head1 AUTHOR
Ed Avis, ed@membled.com.
=cut
use strict;
use XMLTV::Version '$Id: tv_split,v 1.5 2004/01/20 20:57:37 epaepa Exp $ ';
use Data::Dumper;
use Getopt::Long;
use Date::Manip;
# 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();
}
}
use XMLTV;
use XMLTV::Usage <<END
$0: concatenate listings, merging channels
usage: $0 [--help] [--output FILE] [FILE...]
END
;
sub new_writer( $$ );
my ($opt_help, $opt_output);
GetOptions('help' => \$opt_help, 'output=s' => \$opt_output) or usage(0);
usage(1) if $opt_help;
usage(1) if not defined $opt_output;
@ARGV = ('-') if not @ARGV;
# Whether we are splitting by channel - if so, write just one
# <channel> to each output file.
#
my $by_channel = ($opt_output =~ /%channel/);
my ($encoding, $credits, %channels);
sub encoding_cb( $ ) { $encoding = shift }
sub credits_cb( $ ) { $credits = shift }
my %seen_unstripped;
sub channel_cb( $ ) {
my $c = shift;
for ($c->{id}) {
my $old = $_;
if (tr/%//d) {
warn "stripping % characters from channel id '$old' (which is not RFC2838 anyway)";
if (defined $seen_unstripped{$old}
and $seen_unstripped{$old} ne $_) {
die "two channel ids ('$old' and '$seen_unstripped{$old}') not unique after stripping %";
}
$seen_unstripped{$old} = $_;
}
$channels{$_} = $c;
}
}
my %writers; # map filename to XMLTV::Writer objects
my %todo; # map filename to programmes, in case too many open files
my $too_many = 0;
sub programme_cb( $ ) {
my $p = shift;
my $ch = $p->{channel};
my $filename = $opt_output;
for ($filename) {
s/%channel/$ch/g;
$_ = UnixDate($p->{start}, $_)
if tr/%//;
}
if (not defined $writers{$filename} and not $too_many) {
my $w = new_writer($filename, $by_channel ? $ch : undef);
if ($w) {
$writers{$filename} = $w;
}
else {
if ($! =~ /[Tt]oo many open files/) {
warn "too many open files, storing programmes in memory\n";
$too_many = 1;
}
else {
die "cannot write to $filename: $!, aborting";
}
}
}
if (defined $writers{$filename}) {
$writers{$filename}->write_programme($p);
}
else {
# Can't write it now, do it later.
push @{$todo{$filename}}, $p;
}
}
XMLTV::parsefiles_callback(\&encoding_cb, \&credits_cb,
\&channel_cb, \&programme_cb,
@ARGV);
# Now finish up (write </tv>, etc).
END {
# First those which have XML::Writer objects to hand.
foreach my $f (keys %writers) {
my $w = $writers{$f};
my $todo = delete $todo{$f};
if ($todo) {
$w->write_programme($_) foreach @$todo;
}
$w->end();
}
# We've freed up some filehandles so we can write the remaining
# todo programmes, if any.
#
foreach my $f (keys %todo) {
my @ps = @{$todo{$f}};
die if not @ps;
my $w = new_writer($f, $by_channel ? $ps[0]->{channel} : undef);
die "cannot write to $f: $!, aborting" if not $w;
$w->write_programme($_) foreach @ps;
$w->end();
}
}
# Create a new XMLTV::Writer and get it ready to write programmes
# (using the global variables set above). Returns undef if failure.
#
# Parameters: filename, channel id that will go into this file (or
# undef for all channels)
#
sub new_writer( $$ ) {
my ($f, $ch) = @_;
my $fh = new IO::File ">$f" or return undef;
my $w = new XMLTV::Writer(OUTPUT => $fh,
encoding => $encoding)
or return undef;
$w->start($credits);
if (defined $ch) {
# Write this one <channel> if we have it. (If it wasn't in
# the input, do without.)
#
for ($channels{$ch}) {
$w->write_channel($_) if defined;
}
}
else { $w->write_channels(\%channels) }
return $w;
}
|