root/grabbers/jrobbo @ 37

Revision 37, 5.5 kB (checked in by lincoln, 7 years ago)

add jrobbo grabber

  • Property svn:executable set to *
Line 
1#!/usr/bin/perl -w
2
3# details of jrobbo's tv guide data is posted at
4#    http://wiki.dvbowners.com/index.php?title=JRobbo's_EPG_Guides
5# the actual guides are at http://www.jrobbo.com/dwxmltv/<name>.zip
6
7use strict;
8
9my $progname = "jrobbo";
10my $version = "0.01";
11
12use LWP::UserAgent;
13use Getopt::Long;
14use Cwd;
15$| = 1;
16
17#
18# table mapping 'region_id' to guidenames
19#
20my @guidename;
21# VIC
22$guidename[93] = "Geelong",             $guidename[94] = "Melbourne",           $guidename[95] = "MilduraSunraysia";
23$guidename[90] = "EasternVictoria",     $guidename[98] = "WesternVictoria";
24# NSW
25$guidename[73] = "Sydney",              $guidename[66] = "CentralCoastNSW",     $guidename[67] = "Griffith";
26$guidename[63] = "BrokenHill",          $guidename[69] = "NorthernNSW",         $guidename[71] = "SouthernNSW";
27$guidename[106] = "RemoteCentralNSW",   $guidename[184] = "Newcastle";
28# QLD
29$guidename[75] = "Brisbane",            $guidename[78] = "GoldCoast",           $guidename[79] = "RegionalQLD";
30$guidename[114] = "RemoteCentralQLD";
31# WA
32$guidename[101] = "Perth",              $guidename[102] = "RegionalWA";
33# SA
34$guidename[81] = "Adelaide",            $guidename[82] = "Renmark",             $guidename[83] = "Riverland";
35$guidename[85] = "SouthEastSA",         $guidename[86] = "SpencerGulf",         $guidename[107] = "RemoteCentralSA";
36$guidename[74] = "Darwin",              $guidename[108] = "RemoteCentralNT";    # NT
37$guidename[73] = "ACT";         # ACT
38$guidename[88] = "Hobart";      # TAS
39
40# default settings
41my $opt = { };
42$opt->{outputfile} =            cwd() . "/output.xmltv";
43$opt->{downloadfile} =          cwd() . "/last_jrobbo_download.zip";
44$opt->{region} =                94;
45
46GetOptions(
47        'region=i'      => \$opt->{region},
48        'days=i'        => \$opt->{days},               # ignored
49        'offset=i'      => \$opt->{offset},             # ignored
50        'timezone=s'    => \$opt->{timezone},           # ignored
51        'channels_file=s' => \$opt->{channels_file},    # ignored
52        'output=s'      => \$opt->{outputfile},
53        'downloadfile=s' => \$opt->{downloadfile},
54        'nowarper'      => \$opt->{nowarper},
55        'help'          => \$opt->{help},
56        'version'       => \$opt->{version},
57        'v'             => \$opt->{version},
58        'ready'         => \$opt->{version},
59        'desc'          => \$opt->{desc});
60
61printf "%s %s\n",$progname,$version;
62exit(0) if ($opt->{version});
63if ($opt->{desc}) {
64        printf "%s grabs tvguide data from jrobbo's website.  see http://wiki.dvbowners.com/index.php?title=JRobbo's_EPG_Guides for details\n",$progname;
65        exit(0);
66}
67if ($opt->{help}) {
68        printf "\noptions as follows:\n";
69        printf "   --region=<i>           region as per the table below (default $opt->{region})\n";
70        printf "   --output=<file>        file to send output to (default $opt->{outputfile})\n";
71        printf "   --downloadfile=<file>  file to send output to (default $opt->{downloadfile})\n";
72        printf "   --nowarper              don't fetch via webwarper\n";
73        printf "\n";
74        printf "  regions are as follows:\n";
75        my $count = 0;
76        foreach my $i (1..1000) {
77                if (defined $guidename[$i]) {
78                        $count++;
79                        printf "\t%d=%-15s%s",$i,$guidename[$i],(($count % 4) == 0) ? "\n" : "";
80                }
81        }
82        printf "\n";
83        exit(0);
84}
85
86die "Invalid region $opt->region specified; see --help for list of valid regions.\n" if (!defined $guidename[($opt->{region})]);
87my $url = sprintf "http://www.jrobbo.com/dwxmltv/xmltv%s.zip",$guidename[($opt->{region})];
88
89#
90# go!
91#
92
93printf "Fetching %s %s...\n",$url,($opt->{nowarper} ? "direct" : "via webwarper");
94$url =~ s#^http://#http://webwarper.net/ww/# unless $opt->{nowarper};
95
96my $agent = (
97        'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)',
98        'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4',
99        'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.6) Gecko/20050512 Firefox',
100        'Opera/9.00 (Windows NT 5.1; U; en)',
101        'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/412 (KHTML, like Gecko) Safari/412'
102        )[int(rand(5))];
103my $ua = LWP::UserAgent->new('timeout' => 30, 'keep_alive' => 1, 'agent' => $agent);
104$ua->env_proxy;
105$ua->cookie_jar({});
106
107my $request = HTTP::Request->new(GET => $url);
108my $randomaddr = sprintf "203.%d.%d.%d",rand(255),rand(255),(rand(254)+1);
109$request->header('Via' => '1.0 proxy:81 (Squid/2.3.STABLE3)');
110$request->header('X-Forwarded-For' => $randomaddr);
111my $response;
112
113for (1..3) {
114        $response = $ua->request($request);
115        last if ($response->is_success);
116        printf "Failed to fetch $url!\n";
117        sleep 10;
118}
119die "Failed to fetch $url after 3 retries!\n" if (!($response->is_success));
120
121open(F,">$opt->{downloadfile}") || die "can't open $opt->{downloadfile} for writing: $!\n";
122print F $response->content;
123close(F);
124
125my $inputpipe = "/usr/bin/unzip -p $opt->{downloadfile} |";
126open(INFILE,"$inputpipe") || die "can't open $inputpipe for reading: $!\n";
127open(OUTFILE,">$opt->{outputfile}") || die "can't open $opt->{outputfile} for writing: $!\n";
128
129while(<INFILE>) {
130        chop;
131        my $input_line = $_;
132
133        if ($input_line =~ /^(.*)(channel|id)="([a-z0-9A-Z ]+)"(.*)$/) {
134                # substitute channel as per our standards in channels.conf ..
135                my ($before_match, $id_part, $chan, $after_match) = ($1,$2,$3,$4);
136                my $newchan = $chan;
137                if ($chan =~ /^ABC /)                   { $newchan = "ABC" }
138                elsif ($chan =~ /^Channel Seven/)       { $newchan = "Seven" }
139                elsif ($chan =~ /^Prime/)               { $newchan = "Seven" }
140                elsif ($chan =~ /^Channel Nine/)        { $newchan = "Nine" }
141                elsif ($chan =~ /^WIN/)                 { $newchan = "Nine" }
142                elsif ($chan =~ /^Channel Ten/)         { $newchan = "TEN" }
143                elsif ($chan =~ /^Network TEN/)         { $newchan = "TEN" }
144                elsif ($chan =~ /^Southern Cross/)      { $newchan = "TEN" }
145                elsif ($chan =~ /^SBS News/)            { $newchan = "SBS News" }
146                elsif ($chan =~ /^SBS /)                { $newchan = "SBS"; }
147                $input_line = $before_match.$id_part."='".$newchan."'".$after_match;
148        }
149
150        print OUTFILE $input_line."\n";
151}
152
153close(INFILE);
154close(OUTFILE);
155
156printf "All done, output in $opt->{outputfile}.\n";
157exit(0);
Note: See TracBrowser for help on using the browser.