| 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 | |
|---|
| 7 | use strict; |
|---|
| 8 | |
|---|
| 9 | my $progname = "jrobbo"; |
|---|
| 10 | my $version = "0.02"; |
|---|
| 11 | |
|---|
| 12 | use LWP::UserAgent; |
|---|
| 13 | use Getopt::Long; |
|---|
| 14 | use Cwd; |
|---|
| 15 | $| = 1; |
|---|
| 16 | |
|---|
| 17 | # |
|---|
| 18 | # table mapping 'region_id' to guidenames |
|---|
| 19 | # |
|---|
| 20 | my @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 |
|---|
| 41 | my $opt = { }; |
|---|
| 42 | my $channels; |
|---|
| 43 | $opt->{channels_file} = ""; |
|---|
| 44 | $opt->{outputfile} = cwd() . "/output.xmltv"; |
|---|
| 45 | $opt->{downloadfile} = cwd() . "/last_jrobbo_download.zip"; |
|---|
| 46 | $opt->{region} = 94; |
|---|
| 47 | |
|---|
| 48 | GetOptions( |
|---|
| 49 | 'region=i' => \$opt->{region}, |
|---|
| 50 | 'days=i' => \$opt->{days}, # ignored |
|---|
| 51 | 'offset=i' => \$opt->{offset}, # ignored |
|---|
| 52 | 'timezone=s' => \$opt->{timezone}, # ignored |
|---|
| 53 | 'channels_file=s' => \$opt->{channels_file}, # ignored |
|---|
| 54 | 'output=s' => \$opt->{outputfile}, |
|---|
| 55 | 'downloadfile=s' => \$opt->{downloadfile}, |
|---|
| 56 | 'nowarper' => \$opt->{nowarper}, |
|---|
| 57 | 'help' => \$opt->{help}, |
|---|
| 58 | 'version' => \$opt->{version}, |
|---|
| 59 | 'v' => \$opt->{version}, |
|---|
| 60 | 'ready' => \$opt->{version}, |
|---|
| 61 | 'desc' => \$opt->{desc}); |
|---|
| 62 | |
|---|
| 63 | printf "%s %s\n",$progname,$version; |
|---|
| 64 | exit(0) if ($opt->{version}); |
|---|
| 65 | if ($opt->{desc}) { |
|---|
| 66 | 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; |
|---|
| 67 | exit(0); |
|---|
| 68 | } |
|---|
| 69 | if ($opt->{help}) { |
|---|
| 70 | printf "\noptions as follows:\n"; |
|---|
| 71 | printf " --channels_file=<file> channels file (mandatory)\n"; |
|---|
| 72 | printf " --region=<i> region as per the table below (default $opt->{region})\n"; |
|---|
| 73 | printf " --output=<file> file to send output to (default $opt->{outputfile})\n"; |
|---|
| 74 | printf " --downloadfile=<file> file to send output to (default $opt->{downloadfile})\n"; |
|---|
| 75 | printf " --nowarper don't fetch via webwarper\n"; |
|---|
| 76 | printf "\n"; |
|---|
| 77 | printf " regions are as follows:\n"; |
|---|
| 78 | my $count = 0; |
|---|
| 79 | foreach my $i (1..1000) { |
|---|
| 80 | if (defined $guidename[$i]) { |
|---|
| 81 | $count++; |
|---|
| 82 | printf "\t%d=%-15s%s",$i,$guidename[$i],(($count % 4) == 0) ? "\n" : ""; |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | printf "\n"; |
|---|
| 86 | exit(0); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | die "Invalid region $opt->region specified; see --help for list of valid regions.\n" if (!defined $guidename[($opt->{region})]); |
|---|
| 90 | die "no channel file specified, see --help for instructions\n", if ($opt->{channels_file} eq ""); |
|---|
| 91 | |
|---|
| 92 | my $url = sprintf "http://www.jrobbo.com/dwxmltv/xmltv%s.zip",$guidename[($opt->{region})]; |
|---|
| 93 | |
|---|
| 94 | # |
|---|
| 95 | # go! |
|---|
| 96 | # |
|---|
| 97 | |
|---|
| 98 | # read channels file |
|---|
| 99 | if (-r $opt->{channels_file}) { |
|---|
| 100 | local (@ARGV, $/) = ($opt->{channels_file}); |
|---|
| 101 | no warnings 'all'; eval <>; die "$@" if $@; |
|---|
| 102 | } else { |
|---|
| 103 | die "channels file $opt->{channels_file} could not be read: $!\n"; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | printf "Fetching %s %s...\n",$url,($opt->{nowarper} ? "direct" : "via webwarper"); |
|---|
| 107 | $url =~ s#^http://#http://webwarper.net/ww/# unless $opt->{nowarper}; |
|---|
| 108 | |
|---|
| 109 | my $agent = ( |
|---|
| 110 | 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)', |
|---|
| 111 | 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4', |
|---|
| 112 | 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.6) Gecko/20050512 Firefox', |
|---|
| 113 | 'Opera/9.00 (Windows NT 5.1; U; en)', |
|---|
| 114 | 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/412 (KHTML, like Gecko) Safari/412' |
|---|
| 115 | )[int(rand(5))]; |
|---|
| 116 | my $ua = LWP::UserAgent->new('timeout' => 30, 'keep_alive' => 1, 'agent' => $agent); |
|---|
| 117 | $ua->env_proxy; |
|---|
| 118 | $ua->cookie_jar({}); |
|---|
| 119 | |
|---|
| 120 | my $request = HTTP::Request->new(GET => $url); |
|---|
| 121 | my $randomaddr = sprintf "203.%d.%d.%d",rand(255),rand(255),(rand(254)+1); |
|---|
| 122 | $request->header('Via' => '1.0 proxy:81 (Squid/2.3.STABLE3)'); |
|---|
| 123 | $request->header('X-Forwarded-For' => $randomaddr); |
|---|
| 124 | my $response; |
|---|
| 125 | |
|---|
| 126 | for (1..3) { |
|---|
| 127 | $response = $ua->request($request); |
|---|
| 128 | last if ($response->is_success); |
|---|
| 129 | printf "Failed to fetch $url!\n"; |
|---|
| 130 | sleep 10; |
|---|
| 131 | } |
|---|
| 132 | die "Failed to fetch $url after 3 retries!\n" if (!($response->is_success)); |
|---|
| 133 | |
|---|
| 134 | open(F,">$opt->{downloadfile}") || die "can't open $opt->{downloadfile} for writing: $!\n"; |
|---|
| 135 | print F $response->content; |
|---|
| 136 | close(F); |
|---|
| 137 | |
|---|
| 138 | my $inputpipe = "/usr/bin/unzip -p $opt->{downloadfile} |"; |
|---|
| 139 | open(INFILE,"$inputpipe") || die "can't open $inputpipe for reading: $!\n"; |
|---|
| 140 | open(OUTFILE,">$opt->{outputfile}") || die "can't open $opt->{outputfile} for writing: $!\n"; |
|---|
| 141 | |
|---|
| 142 | while(<INFILE>) { |
|---|
| 143 | chop; |
|---|
| 144 | my $input_line = $_; |
|---|
| 145 | |
|---|
| 146 | if ($input_line =~ /^(.*)(channel|id)="([a-z0-9A-Z ]+)"(.*)$/) { |
|---|
| 147 | # substitute channel as per our standards in channels.conf .. |
|---|
| 148 | my ($before_match, $id_part, $chan, $after_match) = ($1,$2,$3,$4); |
|---|
| 149 | my $newchan = $chan; |
|---|
| 150 | if ($chan =~ /^ABC /) { $newchan = $channels->{"ABC"}; } |
|---|
| 151 | elsif ($chan =~ /^Channel Seven/) { $newchan = $channels->{"Seven"}; } |
|---|
| 152 | elsif ($chan =~ /^Prime/) { $newchan = $channels->{"Seven"}; } |
|---|
| 153 | elsif ($chan =~ /^Channel Nine/) { $newchan = $channels->{"Nine"}; } |
|---|
| 154 | elsif ($chan =~ /^WIN/) { $newchan = $channels->{"Nine"}; } |
|---|
| 155 | elsif ($chan =~ /^Channel Ten/) { $newchan = $channels->{"TEN"}; } |
|---|
| 156 | elsif ($chan =~ /^Network TEN/) { $newchan = $channels->{"TEN"}; } |
|---|
| 157 | elsif ($chan =~ /^Southern Cross/) { $newchan = $channels->{"TEN"}; } |
|---|
| 158 | elsif ($chan =~ /^SBS News/) { $newchan = $channels->{"SBS News"}; } |
|---|
| 159 | elsif ($chan =~ /^SBS /) { $newchan = $channels->{"SBS"}; } |
|---|
| 160 | $input_line = $before_match.$id_part."='".$newchan."'".$after_match; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | print OUTFILE $input_line."\n"; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | close(INFILE); |
|---|
| 167 | close(OUTFILE); |
|---|
| 168 | |
|---|
| 169 | printf "All done, output in $opt->{outputfile}.\n"; |
|---|
| 170 | exit(0); |
|---|