root/grabbers/oztivo @ 101

Revision 101, 3.2 kB (checked in by max, 7 years ago)

OzTivo? converts apostrophes.

Line 
1#!/usr/bin/perl -w
2
3# OzTivo grabber
4
5my $version = '0.7';
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# 0.6   : Decodes HTML characters
20# 0.6.1 : Bugfix: Don't decode HTML characters (invalid XMLTV),
21#         Bugfix: translate SBS NEWS XMLTVID properly
22# 0.7   : Decodes apostrophes
23
24use strict;
25
26use LWP::UserAgent;
27use Cwd;
28use Getopt::Long;
29use HTML::Entities;
30
31my $config_file = cwd() . "/oztivo.pw";
32my $output_file = cwd() . "/output.xmltv";
33my $channels_file;
34my $channels;
35my @clist;
36my $ver;
37my $ready;
38my $desc;
39
40print "OzTivo Grabber v$version\n";
41
42$| = 1;
43
44GetOptions( 
45            'channels_file=s'   => \$channels_file,
46            'output=s'          => \$output_file,
47            'version'           => \$ver,
48            'ready'             => \$ready,
49            'desc'              => \$desc
50          );
51
52exit 0 if ($ver);
53
54print "Reading configuration file $config_file.\n";
55
56open(CONF, $config_file)
57    or die "Unable to read config file $config_file: $!";
58my $line = <CONF>;
59close CONF;
60
61unless ($line =~ /^(.*):(.*)$/)
62{
63    die "Unable to parse config file!\n" .
64        "Should be in format: username:password\n";
65}
66my ($user, $pw) = ($1, $2);
67
68exit 0 if ($ready);
69
70unless ($channels_file)
71{
72    die "No --channels_file specified.\n";
73}
74
75unless( -r $channels_file)
76{
77    die "Unable to read channels file $channels_file: $!";
78}
79local (@ARGV, $/) = ($channels_file);
80eval <>;
81die "\nError in channels file!\nDetails:\n$@" if ($@);
82
83# Create a list of channel names from longest to shortest
84@clist = sort { length $b <=> length $a } keys %$channels;
85print "Channels: @clist.\n";
86
87my $ua = LWP::UserAgent->new();
88$ua->agent("Shepherd OzTivo Grabber/$version");
89$ua->default_header('Accept-Encoding' => 'gzip');
90
91my $fn = "http://$user:YOURPASSWORD\@minnie.tuhs.org/tivo-bin/xmlguide.pl";
92print "Retrieving $fn...\n";
93
94$fn =~ s/YOURPASSWORD/$pw/;
95
96my $response = $ua->get($fn);
97unless ($response->is_success())
98{
99    print "Download failed.\n" . $response->status_line() . "\nExiting.\n";
100    exit;
101}
102my $data = $response->content();
103print "Downloaded " . int((do {use bytes; length($data)}) / 1024) . "KB.\n";
104
105if ($response->header('Content-Encoding')
106        and
107    $response->header('Content-Encoding') eq 'gzip')
108{
109    print "Unzipping.\n";
110    $data = Compress::Zlib::memGunzip($data);
111    #$data = Compress::Zlib::memGunzip($response->content());
112}
113
114# print "Decoding HTML.\n";
115# HTML::Entities::decode($data);
116
117print "Converting apostrophes.\n";
118$data =~ s/\&#39;/'/g;
119
120print "Transforming XMLTVIDs.\n";
121$data =~ s/channel="(.*)"/'channel="'.subme($1).'"'/ge;
122
123print "Writing output.\n";
124open (OUT, ">$output_file");
125print OUT $data;
126close OUT;
127
128print "Done.\n";
129
130sub subme
131{
132    my $station = shift;
133
134    $station = "SBS NEWS" if ($station eq "SBS-NEWS");
135
136    foreach (@clist)
137    {
138        return $channels->{$_} if ($station =~ /$_/i);
139    }
140    print "Warning: station \"$station\" unknown.\n";
141    return $station;
142}
Note: See TracBrowser for help on using the browser.