| 1 | #!/usr/bin/perl -w |
|---|
| 2 | |
|---|
| 3 | # OzTivo grabber |
|---|
| 4 | |
|---|
| 5 | my $version = '1.15'; |
|---|
| 6 | |
|---|
| 7 | # Requires configuration! |
|---|
| 8 | # 1. Register at http://www.tvguide.org.au/ |
|---|
| 9 | # 2. Run "./oztivo --configure" to create "oztivo.pw" file. |
|---|
| 10 | |
|---|
| 11 | use strict; |
|---|
| 12 | |
|---|
| 13 | use Getopt::Long; |
|---|
| 14 | use HTML::Entities; |
|---|
| 15 | use POSIX; |
|---|
| 16 | use Shepherd::Common; |
|---|
| 17 | |
|---|
| 18 | my $progname = 'oztivo'; |
|---|
| 19 | my $nicename = 'OzTivo'; |
|---|
| 20 | my $config_file = "$progname.pw"; |
|---|
| 21 | my $output_file = "output.xmltv"; |
|---|
| 22 | my $channels_file; |
|---|
| 23 | my $channels, my $opt_channels; |
|---|
| 24 | my @clist; |
|---|
| 25 | my $ver; |
|---|
| 26 | my $ready; |
|---|
| 27 | my $configure; |
|---|
| 28 | my $raw_input; |
|---|
| 29 | my $raw_output; |
|---|
| 30 | my $d; |
|---|
| 31 | |
|---|
| 32 | print "$nicename Grabber v$version\n"; |
|---|
| 33 | |
|---|
| 34 | $| = 1; |
|---|
| 35 | |
|---|
| 36 | GetOptions( |
|---|
| 37 | 'channels_file=s' => \$channels_file, |
|---|
| 38 | 'output=s' => \$output_file, |
|---|
| 39 | 'rawoutput=s' => \$raw_output, |
|---|
| 40 | 'rawinput=s' => \$raw_input, |
|---|
| 41 | 'version' => \$ver, |
|---|
| 42 | 'ready' => \$ready, |
|---|
| 43 | 'configure' => \$configure |
|---|
| 44 | ); |
|---|
| 45 | |
|---|
| 46 | exit 0 if ($ver); |
|---|
| 47 | |
|---|
| 48 | configure() if ($configure); |
|---|
| 49 | |
|---|
| 50 | unless (-r $config_file) |
|---|
| 51 | { |
|---|
| 52 | print "Can't find $config_file!\n"; |
|---|
| 53 | please_configure_me(); |
|---|
| 54 | exit 1; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | print "Reading configuration file $config_file.\n"; |
|---|
| 58 | |
|---|
| 59 | unless (open(CONF, $config_file)) |
|---|
| 60 | { |
|---|
| 61 | print "Unable to read config file $config_file: $!\n"; |
|---|
| 62 | please_configure_me(); |
|---|
| 63 | exit 1; |
|---|
| 64 | } |
|---|
| 65 | my $line = <CONF>; |
|---|
| 66 | close CONF; |
|---|
| 67 | |
|---|
| 68 | unless ($line =~ /^(.*):(.*)$/) |
|---|
| 69 | { |
|---|
| 70 | print "Unable to parse config file!\n" . |
|---|
| 71 | "It should be in the format: username:password\n"; |
|---|
| 72 | please_configure_me(); |
|---|
| 73 | exit 1; |
|---|
| 74 | } |
|---|
| 75 | my ($user, $pw) = ($1, $2); |
|---|
| 76 | |
|---|
| 77 | unless ($user and $pw) |
|---|
| 78 | { |
|---|
| 79 | print "Failed to extract a sensible username and password from config file.\n"; |
|---|
| 80 | please_configure_me(); |
|---|
| 81 | exit 1; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | exit 0 if ($ready); |
|---|
| 85 | |
|---|
| 86 | unless ($channels_file) |
|---|
| 87 | { |
|---|
| 88 | die "No --channels_file specified.\n"; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | unless( -r $channels_file) |
|---|
| 92 | { |
|---|
| 93 | die "Unable to read channels file $channels_file: $!"; |
|---|
| 94 | } |
|---|
| 95 | local (@ARGV, $/) = ($channels_file); |
|---|
| 96 | eval <>; |
|---|
| 97 | die "\nError in channels file!\nDetails:\n$@" if ($@); |
|---|
| 98 | |
|---|
| 99 | my $shortchannels; |
|---|
| 100 | while (my ($name, $chanid) = each %$channels) |
|---|
| 101 | { |
|---|
| 102 | # Ignore differences between rural stations like |
|---|
| 103 | # Seven (Rockhampton) and Seven (Cairns) -- this is not a great |
|---|
| 104 | # solution and should be fixed. |
|---|
| 105 | $name =~ s/ *\(.*?\)//g; |
|---|
| 106 | # Ignore differences between rural stations like |
|---|
| 107 | # Prime Tamworth/Taree/Port,Prime Lismore/Coffs Hbr -- this is not a great |
|---|
| 108 | # solution and should be fixed. |
|---|
| 109 | $name = "Prime" if $name =~ /^Prime/; |
|---|
| 110 | $shortchannels->{$name} = $chanid; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | # Create a list of channel names from longest to shortest |
|---|
| 114 | @clist = sort { length $b <=> length $a } keys %$shortchannels; |
|---|
| 115 | |
|---|
| 116 | my $data; |
|---|
| 117 | |
|---|
| 118 | if (!defined $raw_input) { |
|---|
| 119 | my ($success, $status); |
|---|
| 120 | |
|---|
| 121 | # Don't rely on Shepherd::Common::get_url()'s retry, because |
|---|
| 122 | # if we get a 401 (wrong password) there's no point in retrying. |
|---|
| 123 | my $max_tries = 3; |
|---|
| 124 | foreach my $tries (1 .. $max_tries) { |
|---|
| 125 | ($data, $success, $status) = Shepherd::Common::get_url( |
|---|
| 126 | url => "http://$user:$pw\@minnie.tuhs.org/tivo-bin/xmlguide.pl", |
|---|
| 127 | retries => 0); |
|---|
| 128 | |
|---|
| 129 | unless ($success) |
|---|
| 130 | { |
|---|
| 131 | if ($status =~ /401/) |
|---|
| 132 | { |
|---|
| 133 | print "Your OzTivo username and/or password may be incorrect!\n". |
|---|
| 134 | "The username and password you supplied when configuring the oztivo grabber\n" . |
|---|
| 135 | "must match your registration details on the OzTivo.com site.\n". |
|---|
| 136 | "If this error persists, try reconfiguring: ". |
|---|
| 137 | " tv_grab_au --configure $progname\n"; |
|---|
| 138 | $tries = $max_tries; |
|---|
| 139 | } |
|---|
| 140 | print "Download failed: $status\n"; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | my $bytes = do { use bytes; length($data) }; |
|---|
| 144 | my $sleep = 47; |
|---|
| 145 | |
|---|
| 146 | # If we get a special message from the oztivo server, sleep |
|---|
| 147 | # a longer time before retrying. |
|---|
| 148 | if ($bytes < 1000 and defined $data and $data =~ /<message>(.*?)<\/message>/si) |
|---|
| 149 | { |
|---|
| 150 | $success = 0; |
|---|
| 151 | print "OzTivo message: $1\n"; |
|---|
| 152 | # Giveup unless message says to try again later |
|---|
| 153 | # eg. OzTivo message: System load on minnie is too high right now to run your request. Please try again later. |
|---|
| 154 | $tries = $max_tries unless $1 =~ /try\s+again\s+later/si; |
|---|
| 155 | $sleep = 93 + int(rand(120)); |
|---|
| 156 | } |
|---|
| 157 | last if ($success); |
|---|
| 158 | exit 6 if $tries == $max_tries; |
|---|
| 159 | print "Sleeping for $sleep seconds...\n"; |
|---|
| 160 | sleep($sleep); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | if (defined $raw_output) { |
|---|
| 164 | open(F,">$raw_output") || die "could not write raw output to $raw_output: $!\n"; |
|---|
| 165 | print F $data; |
|---|
| 166 | close(F); |
|---|
| 167 | print "Raw output saved in $raw_output.\n"; |
|---|
| 168 | } |
|---|
| 169 | } else { |
|---|
| 170 | open(F,"<$raw_input") || die "could not read raw input from $raw_input: $!\n"; |
|---|
| 171 | while(<F>) { |
|---|
| 172 | $data .= $_; |
|---|
| 173 | } |
|---|
| 174 | close(F); |
|---|
| 175 | print "Raw input read from $raw_input.\n"; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | print "Converting apostrophes.\n"; |
|---|
| 179 | $data =~ s/\'/'/g; |
|---|
| 180 | |
|---|
| 181 | # Adjust ABC2 times if we're in a non-DST zone but Sydney is in DST. |
|---|
| 182 | # Apparently ABC2 times are set off Sydney's clock. |
|---|
| 183 | adjust_abc2(); |
|---|
| 184 | |
|---|
| 185 | print "Transforming XMLTVIDs.\n"; |
|---|
| 186 | &setup_channel_mappings; |
|---|
| 187 | $data =~ s/channel="(.*)"/'channel="'.subme($1).'"'/ge; |
|---|
| 188 | |
|---|
| 189 | print "Writing output.\n"; |
|---|
| 190 | open (OUT, ">$output_file") || die "could not write to $output_file: $!\n"; |
|---|
| 191 | |
|---|
| 192 | # |
|---|
| 193 | # oztivo generates invalid XMLTV with fields out of order and |
|---|
| 194 | # some blank fields. |
|---|
| 195 | # the standard XMLTV.pm perl module gets very unhappy about these |
|---|
| 196 | # write output in a manner which addresses the bad input |
|---|
| 197 | # |
|---|
| 198 | |
|---|
| 199 | my @xmltv_tag_order = qw [ title sub-title desc credits date category language |
|---|
| 200 | orig-language length icon url country episode-num video audio |
|---|
| 201 | previously-shown permiere last-chance new subtitles rating |
|---|
| 202 | star-rating ]; |
|---|
| 203 | my %xmltv_tags = map { $_ => "" } @xmltv_tag_order; |
|---|
| 204 | $xmltv_tags{"programme"}=""; $xmltv_tags{"/programme"}=""; |
|---|
| 205 | my $cur_field = ""; |
|---|
| 206 | |
|---|
| 207 | foreach my $line (split/\n/,$data) { |
|---|
| 208 | |
|---|
| 209 | # oztivo generates blank data for these fields - skip if blank |
|---|
| 210 | next if ($line =~ /<director><\/director>/); |
|---|
| 211 | next if ($line =~ /<desc><\/desc>/); |
|---|
| 212 | next if ($line =~ /^\s*$/); |
|---|
| 213 | |
|---|
| 214 | # more oztivo weirdness. filter out 0/10 star-ratings |
|---|
| 215 | next if ($line =~ /<star-rating><value>0\/10</); |
|---|
| 216 | |
|---|
| 217 | if ($line =~ /\s*<([\/a-zA-Z\-]+)/) { |
|---|
| 218 | my $field = lc($1); |
|---|
| 219 | # do we know about this tag? |
|---|
| 220 | $cur_field = $field if (defined $xmltv_tags{$field}); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | if ($cur_field eq "programme") { |
|---|
| 224 | # if we have a start="(time)" and/or stop="(time)" make sure they |
|---|
| 225 | # have a timezone on them. |
|---|
| 226 | $line = $1."start=\"".$2." +0000\"".$3 if ($line =~ /^(.*)start="([0-9]+)"(.*)/); |
|---|
| 227 | $line = $1."stop=\"".$2." +0000\"".$3 if ($line =~ /^(.*)stop="([0-9]+)"(.*)/); |
|---|
| 228 | |
|---|
| 229 | print OUT $line."\n"; # programme tag |
|---|
| 230 | $cur_field = ""; |
|---|
| 231 | } elsif ($cur_field eq "/programme") { |
|---|
| 232 | print "Program with no title!\n" |
|---|
| 233 | if (($xmltv_tags{title} eq "") or ($xmltv_tags{title} =~ />\s*</)); |
|---|
| 234 | # print all previously seen tags in xmltv_tag_order |
|---|
| 235 | foreach my $xmltag (@xmltv_tag_order) { |
|---|
| 236 | if ($xmltv_tags{$xmltag} ne "") { |
|---|
| 237 | print OUT $xmltv_tags{$xmltag}; |
|---|
| 238 | $xmltv_tags{$xmltag} = ""; |
|---|
| 239 | } |
|---|
| 240 | } |
|---|
| 241 | print OUT $line."\n"; # /programme tag |
|---|
| 242 | $cur_field = ""; |
|---|
| 243 | } else { |
|---|
| 244 | if ($cur_field eq "") { |
|---|
| 245 | print OUT $line."\n"; # ?xml, tv and /tv tags |
|---|
| 246 | } else { |
|---|
| 247 | $xmltv_tags{$cur_field} .= $line."\n"; # xmltv_tags tags |
|---|
| 248 | } |
|---|
| 249 | } |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | close OUT; |
|---|
| 253 | |
|---|
| 254 | print "Done.\n"; |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | sub please_configure_me |
|---|
| 259 | { |
|---|
| 260 | print "If you wish to use $nicename, please run \"" . |
|---|
| 261 | "tv_grab_au --configure $progname\".\n" . |
|---|
| 262 | "Configuration of $progname is required.\n"; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | sub configure |
|---|
| 266 | { |
|---|
| 267 | print "Configuring...\n\n" . |
|---|
| 268 | "Before you can use the $nicename grabber, you must create an\n" . |
|---|
| 269 | "OzTivo account here:\n\n" . |
|---|
| 270 | ' http://minnie.tuhs.org/twiki/bin/view/TWiki/TWikiRegistration' . |
|---|
| 271 | "\n\n" . |
|---|
| 272 | "... and select some channels here:\n\n" . |
|---|
| 273 | ' http://minnie.tuhs.org/tivo-bin/tvguide.pl' . "\n\n" . |
|---|
| 274 | "When you're done, enter your username and password here, to\n" . |
|---|
| 275 | "enable this grabber to retrieve your guide data automatically:\n\n" . |
|---|
| 276 | "Username?\n"; |
|---|
| 277 | my $username = <>; |
|---|
| 278 | chomp $username; |
|---|
| 279 | unless ($username) |
|---|
| 280 | { |
|---|
| 281 | print "No username supplied. Exiting configuration.\n"; |
|---|
| 282 | exit 3; |
|---|
| 283 | } |
|---|
| 284 | print "Password?\n"; |
|---|
| 285 | my $pw = <>; |
|---|
| 286 | chomp $pw; |
|---|
| 287 | unless ($pw) |
|---|
| 288 | { |
|---|
| 289 | print "No password supplied. Exiting configuration.\n"; |
|---|
| 290 | exit 3; |
|---|
| 291 | } |
|---|
| 292 | print "Creating config file $config_file...\n"; |
|---|
| 293 | open (CONF, ">$config_file") |
|---|
| 294 | or die "Unable to create $config_file: $!"; |
|---|
| 295 | print CONF "$username:$pw"; |
|---|
| 296 | close CONF; |
|---|
| 297 | |
|---|
| 298 | print "Done.\n"; |
|---|
| 299 | exit 0; |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | sub subme |
|---|
| 303 | { |
|---|
| 304 | my $station = shift; |
|---|
| 305 | |
|---|
| 306 | $station = $d->{chan_map}->{$station} if (defined $d->{chan_map}->{$station}); |
|---|
| 307 | $station =~ s/SC10/Sthn Cross TEN/g; # clashes with TEN but @clist is sorted longest to shortest |
|---|
| 308 | |
|---|
| 309 | my $num = $1 if ($station =~ /(\d{2,})/); |
|---|
| 310 | foreach my $ch (@clist) |
|---|
| 311 | { |
|---|
| 312 | if ($station =~ /$ch/i |
|---|
| 313 | or |
|---|
| 314 | ($num and $ch =~ /$num/)) |
|---|
| 315 | { |
|---|
| 316 | return $shortchannels->{$ch}; |
|---|
| 317 | } |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | return $opt_channels->{$station} if ((defined $opt_channels) && (defined $opt_channels->{$station})); |
|---|
| 321 | |
|---|
| 322 | if (!defined $d->{ignored_channels}->{$station}) { |
|---|
| 323 | $d->{ignored_channels}->{$station} = 1; |
|---|
| 324 | print "Warning: station \"$station\" unknown.\n"; |
|---|
| 325 | } |
|---|
| 326 | return $station; |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | sub setup_channel_mappings |
|---|
| 330 | { |
|---|
| 331 | # unmaped Foxtel channels: AdultsOnly960,FOXTELBoxOffice,FOXTELGamesworld,MainEvent |
|---|
| 332 | # unmaped SelecTV channels: AntennaGreek,ERTGreek |
|---|
| 333 | my %map = ( |
|---|
| 334 | "SBS-NEWS" => "SBS NEWS", |
|---|
| 335 | '31-Syd' => 'TVS', |
|---|
| 336 | |
|---|
| 337 | "ACC" => "ACC", |
|---|
| 338 | "ADV1" => "NatGeoAdventure", |
|---|
| 339 | "ANIMAL" => "AnimalPlanet", |
|---|
| 340 | "ANT" => "AntennaPacific", |
|---|
| 341 | "ARNA" => "ARENATV", |
|---|
| 342 | "ARNA+2" => "ARENATV2", |
|---|
| 343 | "AUR" => "Aurora", |
|---|
| 344 | "BBC" => "BBCWorld", |
|---|
| 345 | "BIOG" => "Bio", |
|---|
| 346 | "BLM" => "BloombergTelevision", |
|---|
| 347 | "BOOM" => "Boomerang", |
|---|
| 348 | "CART" => "CartoonNetwork", |
|---|
| 349 | "CLAS" => "FOXClassics", |
|---|
| 350 | "CLAS+2" => "FOXClassics2", |
|---|
| 351 | "CMC" => "CountryMusicChannel", |
|---|
| 352 | "CMDY" => "THECOMEDYCHANNEL", |
|---|
| 353 | "CMDY+2" => "THECOMEDYCHANNEL2", |
|---|
| 354 | "CNBC" => "CNBC", |
|---|
| 355 | "CNNI" => "CNN", |
|---|
| 356 | "CRIME" => "CrimeandInvestigation", |
|---|
| 357 | "DISC" => "DiscoveryChannel", |
|---|
| 358 | "DISCRT" => "DiscoveryRealTime", # SelecTV and OzTivo |
|---|
| 359 | "DISN" => "DisneyChannel", |
|---|
| 360 | "E!" => "E!Entertainment", |
|---|
| 361 | "ESPN" => "ESPN", |
|---|
| 362 | "EUROSPORT" => "Eurosportnews", |
|---|
| 363 | "EXPO" => "EXPO", |
|---|
| 364 | "FASH" => "FashionTV", |
|---|
| 365 | "FOOD" => "LifeStyleFOOD", |
|---|
| 366 | "FOX8" => "FOX8", |
|---|
| 367 | "FOX8+2" => "FOX82", |
|---|
| 368 | "FOXN" => "FOXNews", |
|---|
| 369 | "FS1" => "FOXSPORTS1", |
|---|
| 370 | "FS2" => "FOXSPORTS2", |
|---|
| 371 | "FS3" => "FOXSPORTS3", |
|---|
| 372 | "FSN" => "FOXSPORTSNews", |
|---|
| 373 | "FUEL" => "FUELTV", |
|---|
| 374 | "HALL" => "Hallmark", |
|---|
| 375 | "HEALTH" => "DiscoveryHealth", |
|---|
| 376 | "HIST" => "TheHistoryChannel", |
|---|
| 377 | "HOWTO" => "HOWTOChannel", |
|---|
| 378 | "LIFE" => "TheLifeStyleChannel", |
|---|
| 379 | "LIFE+2" => "LifestyleChannel2", |
|---|
| 380 | "MOV1" => "MOVIEONE", |
|---|
| 381 | "MOV1+2" => "MOVIETWO", |
|---|
| 382 | "MOVG" => "MOVIEGREATS", |
|---|
| 383 | "MOVX" => "MOVIEEXTRA", |
|---|
| 384 | "MTV" => "MTV", |
|---|
| 385 | "NGEO" => "NationalGeographic", |
|---|
| 386 | "NICK" => "Nickelodeon", |
|---|
| 387 | "NICKJR" => "NickJr", |
|---|
| 388 | "OVAT" => "Ovation", |
|---|
| 389 | "PHDISN" => "PlayhouseDisney", |
|---|
| 390 | "RAI" => "RAIInternational", |
|---|
| 391 | "SCIENCE" => "DiscoveryScience", |
|---|
| 392 | "SHOW" => "SHOWTIME", |
|---|
| 393 | "SHW2" => "SHOWTIME2", |
|---|
| 394 | "SHWGRTS" => "SHOWTIMEGreats", |
|---|
| 395 | "SKYN" => "SkyNewsAustralia", |
|---|
| 396 | "SKYR" => "SkyRacing", |
|---|
| 397 | "Sci-Fi" => "SCIFI", |
|---|
| 398 | "TCM" => "TCM", |
|---|
| 399 | "TRAVEL" => "DiscoveryTravel", |
|---|
| 400 | "TV1" => "TV1", |
|---|
| 401 | "TV1+2" => "TV12", |
|---|
| 402 | "TVCHILE" => "TVChileSpanish", # SelecTV and OzTivo |
|---|
| 403 | "TVE" => "TVE", # SelecTV and OzTivo |
|---|
| 404 | "TVN" => "TVN", |
|---|
| 405 | "TVSN" => "TVSN", |
|---|
| 406 | "TWC" => "TheWeatherChannel", |
|---|
| 407 | "UKTV" => "UKTV", |
|---|
| 408 | "UKTV+2" => "UKTV2", |
|---|
| 409 | "V" => "ChannelV", |
|---|
| 410 | "V2" => "ChannelV2", |
|---|
| 411 | "VH1" => "VH1", |
|---|
| 412 | "W" => "W", |
|---|
| 413 | "W+2" => "W2", |
|---|
| 414 | "WMOV" => "WORLDMOVIES", |
|---|
| 415 | "max" => "MAX" |
|---|
| 416 | ); |
|---|
| 417 | |
|---|
| 418 | foreach (keys %map) |
|---|
| 419 | { |
|---|
| 420 | $d->{chan_map}->{$_} = $map{$_}; |
|---|
| 421 | } |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | # Wrapper sub to ensure TZ gets reset properly |
|---|
| 425 | sub adjust_abc2 |
|---|
| 426 | { |
|---|
| 427 | adjust_abc2_times(); |
|---|
| 428 | POSIX::tzset(); |
|---|
| 429 | } |
|---|
| 430 | |
|---|
| 431 | sub adjust_abc2_times |
|---|
| 432 | { |
|---|
| 433 | my $local_tz = POSIX::strftime("%z",localtime); |
|---|
| 434 | |
|---|
| 435 | local %ENV; |
|---|
| 436 | $ENV{TZ} = 'Australia/Sydney'; |
|---|
| 437 | my $sydney_tz = POSIX::strftime("%z",localtime); |
|---|
| 438 | |
|---|
| 439 | # Okay, I can't believe how hard this is... compute difference |
|---|
| 440 | # between two TZ strings. |
|---|
| 441 | my $l_min = int($local_tz / 100) * 60 + $local_tz % 100 % 60; |
|---|
| 442 | my $s_min = int($sydney_tz / 100) * 60 + $sydney_tz % 100 % 60; |
|---|
| 443 | # can't use %60 because it always returns a positive number |
|---|
| 444 | my $tz_diff = int(($l_min - $s_min) / 60) * 100 + (($l_min - $s_min) - (int(($l_min - $s_min)/60)*60)); |
|---|
| 445 | |
|---|
| 446 | # Are we in DST? |
|---|
| 447 | my $local_dst = (localtime)[8]; |
|---|
| 448 | my $sydney_dst = 0; |
|---|
| 449 | if (!$local_dst) |
|---|
| 450 | { |
|---|
| 451 | # Is Sydney in DST? |
|---|
| 452 | $sydney_dst = compare_sydney_dst(); |
|---|
| 453 | |
|---|
| 454 | if ($sydney_dst) |
|---|
| 455 | { |
|---|
| 456 | print "DST in Sydney but not here: adding 1hr to ABC2 times" . |
|---|
| 457 | ($sydney_dst != 1 ? ($sydney_dst > 1 ? |
|---|
| 458 | " from $sydney_dst GMT" : |
|---|
| 459 | " until " . ($sydney_dst*-1) . ' GMT') : '') . |
|---|
| 460 | ".\n"; |
|---|
| 461 | } |
|---|
| 462 | } |
|---|
| 463 | $data =~ s/(?!=")(\d+)(?=".* channel="ABC2")/$1.add_offset($1,$tz_diff,$sydney_dst)/ge; |
|---|
| 464 | } |
|---|
| 465 | |
|---|
| 466 | # Return '+0100' for any times that are in a period when Sydney's in DST |
|---|
| 467 | # but we're not. |
|---|
| 468 | sub add_offset |
|---|
| 469 | { |
|---|
| 470 | my ($timestamp, $tz_diff, $sydney_dst) = @_; |
|---|
| 471 | |
|---|
| 472 | if ($sydney_dst == 1 |
|---|
| 473 | or |
|---|
| 474 | ($sydney_dst > 1 and $timestamp >= $sydney_dst) |
|---|
| 475 | or |
|---|
| 476 | ($sydney_dst < 0 and $timestamp <= ($sydney_dst*-1))) |
|---|
| 477 | { |
|---|
| 478 | $tz_diff += 0100; |
|---|
| 479 | } |
|---|
| 480 | return sprintf " %+0.4d", $tz_diff; |
|---|
| 481 | } |
|---|
| 482 | |
|---|
| 483 | # Returns: |
|---|
| 484 | # 0 : Sydney is not in DST in the next 7 days |
|---|
| 485 | # 1 : Sydney is in DST for the next 7 days |
|---|
| 486 | # <n> : Sydney is in DST from <n> GMT, where n is (eg) 20070321030000 |
|---|
| 487 | # <-n> : Sydney is in DST until <n> GMT, where n is (eg) -20070321030000 |
|---|
| 488 | # This func assumes that TZ has been set to Sydney by the caller (adjust_abc2_times) |
|---|
| 489 | sub compare_sydney_dst |
|---|
| 490 | { |
|---|
| 491 | my $sydney_dst = (localtime)[8]; |
|---|
| 492 | |
|---|
| 493 | # Normalize $start to 3AM Sydney time, or 2AM if it's in DST |
|---|
| 494 | my $start = time - (time % (60*60*24)) - (($sydney_dst ? 11 : 10) * 60*60) + (3*60*60); |
|---|
| 495 | |
|---|
| 496 | # Check Sydney's DST status each day for 7 days |
|---|
| 497 | for (my $day = 0; $day < 8; $day++) |
|---|
| 498 | { |
|---|
| 499 | my $t = $start + ($day * 24*60*60); |
|---|
| 500 | my $sydney_dst_thisday = (localtime($t))[8]; |
|---|
| 501 | |
|---|
| 502 | # Any changeover? |
|---|
| 503 | if ($sydney_dst_thisday != $sydney_dst) |
|---|
| 504 | { |
|---|
| 505 | # Convert $t to GMT |
|---|
| 506 | $t -= (($sydney_dst ? 11 : 10) * 60*60); |
|---|
| 507 | my $changeoverday = POSIX::strftime("%Y%m%d%H%M%S", localtime($t)); |
|---|
| 508 | $changeoverday *= -1 if ($sydney_dst); |
|---|
| 509 | return $changeoverday; |
|---|
| 510 | } |
|---|
| 511 | } |
|---|
| 512 | return $sydney_dst; |
|---|
| 513 | } |
|---|