]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/Weather.pl
metar
[infobot.git] / src / Modules / Weather.pl
1 #!/usr/bin/perl
2
3 package Weather;
4
5 # kevin lenzo (C) 1999 -- get the weather forcast NOAA.
6 # feel free to use, copy, cut up, and modify, but if
7 # you do something cool with it, let me know.
8
9 # 16-SEP-99 lenzo@cs.cmu.edu switched to LWP::UA and
10 #           put in a timeout.
11
12 my $no_weather;
13 my $cache_time = 60 * 40 ; # 40 minute cache time
14 my $default = 'KAGC';
15
16 BEGIN {
17     $no_weather = 0;
18     eval "use LWP::UserAgent";
19     $no_weather++ if ($@);
20 }
21
22 sub Weather {
23         my ($args) = @_;
24         &::performStrictReply(&queryText($args, 'weather'));
25         return;
26 }
27
28 sub Metar {
29         my ($args) = @_;
30         &::performStrictReply(&queryText($args, 'metar'));
31         return;
32 }
33
34 sub queryText {
35     my ($station) = shift;
36     my ($wxmode) = shift;
37     my $result;
38
39     $station = uc($station);
40     $station =~ s/for //i;
41
42     if ($no_weather) {
43         return 0;
44     } else {
45
46         if (exists $cache{$station}) {
47             my ($time, $response) = split $; , $cache{$station};
48             if ((time() - $time) < $cache_time) {
49                 return $response;
50             }
51         }
52
53         my $ua = new LWP::UserAgent;
54         $ua->proxy('http', $::param{'httpProxy'}) if (&::IsParam("httpProxy"));
55
56         $ua->timeout(10);
57         my $request = new HTTP::Request('GET', "http://weather.noaa.gov/weather/current/$station.html");
58         my $response = $ua->request($request);
59
60         if (!$response->is_success) {
61             if ($response->code == 404) {
62                 return "I can't find station code \"$station\""
63                     . " (see http://www.nws.noaa.gov/oso/site.shtml"
64                     . " for ICAO locations codes).";
65             } else {
66                 return "Something failed in connecting to the NOAA web"
67                     . " server. Try again later.";
68             }
69         }
70
71         $content = $response->content;
72         $content =~ s|.*?<BODY[^>]*>||is;
73         #$content =~ s|.*?current weather conditions.*?<BR>([^<]*?)\s*<.*?</TR>||is;
74         $content =~ s|.*?current weather conditions[^<]*(<[^>]+>\s*)+||is;
75         $content =~ s|([^<]*?)\s*<.*?</TR>||is;
76         my $place = $1;
77         chomp $place;
78
79         $content =~ s|.*?<TR>(?:\s*<[^>]+>)*\s*([^<]+)\s<.*?</TR>||is;
80         my $id = $1;
81         chomp $id;
82
83         $content =~ s|.*?conditions at.*?</TD>||is;
84
85         #$content =~ s|.*?<OPTION SELECTED>\s+([^<]+)\s<OPTION>.*?</TR>||s; # local time
86         $content =~ s|.*?<BR>\s+([^<]+?)\s*</FORM>.*?</TR>||s; # UTC
87         my $time = $1;
88         $time =~ s/-//g;
89         $time =~ s/\s+/ /g;
90
91         $content =~ s|\s(.*?)<TD COLSPAN=2>||s;
92         my $features = $1;
93
94         while ($features =~ s|.*?<TD ALIGN[^>]*>(?:\s*<[^>]+>)*\s+([^<]+?)\s+<.*?<TD>(?:\s*<[^>]+>)*\s+([^<]+?)\s<.*?/TD>||s) {
95             my ($f,$v) = ($1, $2);
96             chomp $f; chomp $v;
97             $feat{$f} = $v;
98         }
99
100         $content =~ s|.*?>(\d+\S+\s+\(\S+\)).*?</TD>||s;  # max temp;
101         $max_temp = $1;
102         $content =~ s|.*?>(\d+\S+\s+\(\S+\)).*?</TD>||s;
103         $min_temp = $1;
104
105         if ($time) {
106             if ($wxmode eq 'metar' && defined($feat{'ob'})) {
107                 return ("METAR " . $place . ": " . $feat{'ob'});
108             }
109
110             $result = "$place; $id; last updated: $time";
111             foreach (sort keys %feat) {
112                 next if $_ eq 'ob';
113                 $result .= "; $_: $feat{$_}";
114             }
115             my $t = time();
116             $cache{$station} = join $;, $t, $result;
117         } else {
118             $result = "I can't find that station code (see http://weather.noaa.gov/weather/curcond.html for locations and codes)";
119         }
120         return $result;
121     }
122 }
123
124 if (0) {
125     if (-t STDIN) {
126         my $result = Weather::NOAA::get($default);
127         $result =~ s/; /\n/g;
128         print "\n$result\n\n";
129     }
130 }
131
132 1;
133
134 __END__
135
136 =head1 NAME
137
138 NOAA.pl - Get the weather from a NOAA server
139
140 =head1 PREREQUISITES
141
142         LWP::UserAgent
143
144 =head1 PARAMETERS
145
146 weather
147
148 =head1 PUBLIC INTERFACE
149
150         weather [for] <station>
151
152 =head1 DESCRIPTION
153
154 Contacts C<weather.noaa.gov> and gets the weather report for a given
155 station.
156
157 =head1 AUTHORS
158
159 Kevin Lenzo