|
package NWS;
use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
use LWP::Simple;
require Exporter;
@ISA = qw(Exporter AutoLoader);
@EXPORT = qw( );
$VERSION = '0.01';
sub new {
my ($class, $state) = (shift, shift);
my (%zone_forecasts, %county_forecasts, %city_forecasts);
my $text = get("http://iwin.nws.noaa.gov/iwin/$state/zone.html");
return undef unless $text;
%zone_forecasts = ();
while ($text =~ m{ # extract zone forecasts
(${state}Z\d+)[^\r\n]*\r\n # capture zone
(?:UPDATED\r\n)? # optional "UPDATED" line
([^\r\n]*)\r\n # list of counties in zone
(?:INCLUDING\ THE\ CIT(?:Y|IES)\ OF\.\.\.([^\r\n]*)\r\n)? # major city in zone
(?:NATIONAL\ WEATHER\ SERVICE[^\r\n]*\r\n)? # optional service
([^\r\n]*)\r\n # time of forecast
\s*\r\n # blank line before body
(.*?) # body of forecast
^\$\$\=*\r\n # end of forecast mark
}gsmx) {
my $zone_Id = $1;
# dont bother with subsequent forecasts for a zone: usually the first is newest
next if (exists $zone_forecasts{$zone_Id});
my @counties = split(/-/, $2);
my @cities = $3 ? split(/-/, $3) : ();
my ($time, $body) = ($4, $5);
my $forecast = [$zone_Id, $time, $body];
$zone_forecasts{$zone_Id} = $forecast;
foreach (@counties) { $county_forecasts{$_} = $forecast }
foreach (@cities) { $city_forecasts{$_} = $forecast }
}
my $self = { state => $state,
zone_forecasts => \%zone_forecasts,
county_forecasts => \%county_forecasts,
city_forecasts => \%city_forecasts };
return bless $self, $class;
}
sub zones { keys %{shift()->{zone_forecasts} } }
sub counties { keys %{shift()->{county_forecasts} } }
sub cities { keys %{shift()->{city_forecasts} } }
sub get_zone {
my ($self, $zone_id) = (shift, shift);
my($zone, $time, $body) = @{$self->{zone_forecasts}->{$zone_id}};
return wantarray ? ($zone, $time, $body) : $body;
}
sub get_county {
my ($self, $county) = (shift, shift);
my($zone, $time, $body) =@{$self->{county_forecasts}->{$county}};
return wantarray ? ($zone, $time, $body) : $body;
}
sub get_city {
my ($self, $city) = (shift, shift);
my($zone, $time, $body) = @{$self->{city_forecasts}->{$city}};
return wantarray ? ($zone, $time, $body) : $body;
}
1;
|
|