root/grabbers/oztivo @ 62

Revision 62, 2.8 kB (checked in by max, 7 years ago)

OzTivo? enhancement: support gzip compression, identify user agent, turn
off output buffer.

Line 
1#!/usr/bin/perl -w
2
3# OzTivo grabber
4
5my $version = '0.5';
6
7# Requires configuration!
8# 1. Register at http://www.tvguide.org.au/
9# 2. Create file "oztivo.conf" containing the line "username:password"
10#    (no quotation marks), e.g. tomdaniels:qwerty09
11#
12# Changelog:
13# 0.1   : Yucky little initial version
14# 0.2   : --ready option
15# 0.3.  : Don't log password
16# 0.4   : Changed password filename
17# 0.5   : Identify user agent; support gzip compression; turn off
18#         output buffering
19
20use strict;
21
22use LWP::UserAgent;
23use Cwd;
24use Getopt::Long;
25
26my $config_file = cwd() . "/oztivo.pw";
27my $output_file = cwd() . "/oztivo.xmltv";
28my $channels_file;
29my $channels;
30my @clist;
31my $ver;
32my $ready;
33my $desc;
34
35print "OzTivo Grabber v$version\n";
36
37$| = 1;
38
39GetOptions( 
40            'channels_file=s'   => \$channels_file,
41            'output=s'          => \$output_file,
42            'version'           => \$ver,
43            'ready'             => \$ready,
44            'desc'              => \$desc
45          );
46
47exit 0 if ($ver);
48
49print "Reading configuration file $config_file.\n";
50
51open(CONF, $config_file)
52    or die "Unable to read config file $config_file: $!";
53my $line = <CONF>;
54close CONF;
55
56unless ($line =~ /^(.*):(.*)$/)
57{
58    die "Unable to parse config file!\n" .
59        "Should be in format: username:password\n";
60}
61my ($user, $pw) = ($1, $2);
62
63exit 0 if ($ready);
64
65unless ($channels_file)
66{
67    die "No --channels_file specified.\n";
68}
69
70unless( -r $channels_file)
71{
72    die "Unable to read channels file $channels_file: $!";
73}
74local (@ARGV, $/) = ($channels_file);
75eval <>;
76die "\nError in channels file!\nDetails:\n$@" if ($@);
77
78# Create a list of channel names from longest to shortest
79@clist = sort { length $b <=> length $a } keys %$channels;
80print "Channels: @clist.\n";
81
82my $ua = LWP::UserAgent->new();
83$ua->agent("Shepherd OzTivo Grabber/$version");
84$ua->default_header('Accept-Encoding' => 'gzip');
85
86my $fn = "http://$user:YOURPASSWORD\@minnie.tuhs.org/tivo-bin/xmlguide.pl";
87print "Retrieving $fn...\n";
88
89$fn =~ s/YOURPASSWORD/$pw/;
90
91my $response = $ua->get($fn);
92unless ($response->is_success())
93{
94    print "Download failed.\n" . $response->status_line() . "\nExiting.\n";
95    exit;
96}
97my $data = $response->content();
98print "Downloaded " . int((do {use bytes; length($data)}) / 1024) . "KB.\n";
99
100if ($response->header('Content-Encoding')
101        and
102    $response->header('Content-Encoding') eq 'gzip')
103{
104    print "Unzipping.\n";
105    $data = Compress::Zlib::memGunzip($data);
106    #$data = Compress::Zlib::memGunzip($response->content());
107}
108
109print "Transforming XMLTVIDs.\n";
110
111$data =~ s/channel="(.*)"/'channel="'.subme($1).'"'/ge;
112
113print "Writing output.\n";
114
115open (OUT, ">$output_file");
116print OUT $data;
117close OUT;
118
119print "Done.\n";
120
121sub subme
122{
123    my $station = shift;
124
125    foreach (@clist)
126    {
127        return $channels->{$_} if ($station =~ /$_/i);
128    }
129    print "Warning: station \"$station\" unknown.\n";
130    return $station;
131}
Note: See TracBrowser for help on using the browser.