]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/Weather.pl
ws
[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));
25         return;
26 }
27
28 sub queryText {
29     my ($station) = shift;
30     my $result;
31
32     $station = uc($station);
33     $station =~ s/for //i;
34
35     if ($no_weather) {
36         return 0;
37     } else {
38
39         if (exists $cache{$station}) {
40             my ($time, $response) = split $; , $cache{$station};
41             if ((time() - $time) < $cache_time) {
42                 return $response;
43             }
44         }
45
46         my $ua = new LWP::UserAgent;
47         $ua->proxy('http', $::param{'httpProxy'}) if (&::IsParam("httpProxy"));
48
49         $ua->timeout(10);
50         my $request = new HTTP::Request('GET', "http://weather.noaa.gov/weather/current/$station.html");
51         my $response = $ua->request($request);
52
53         if (!$response->is_success) {
54             if ($response->code == 404) {
55                 return "I can't find station code \"$station\""
56                     . " (see http://www.nws.noaa.gov/oso/site.shtml"
57                     . " for ICAO locations codes).";
58             } else {
59                 return "Something failed in connecting to the NOAA web"
60                     . " server. Try again later.";
61             }
62         }
63
64         $content = $response->content;
65         $content =~ s|.*?<BODY[^>]*>||is;
66         #$content =~ s|.*?current weather conditions.*?<BR>([^<]*?)\s*<.*?</TR>||is;
67         $content =~ s|.*?current weather conditions[^<]*(<[^>]+>\s*)+||is;
68         $content =~ s|([^<]*?)\s*<.*?</TR>||is;
69         my $place = $1;
70         chomp $place;
71
72         $content =~ s|.*?<TR>(?:\s*<[^>]+>)*\s*([^<]+)\s<.*?</TR>||is;
73         my $id = $1;
74         chomp $id;
75
76         $content =~ s|.*?conditions at.*?</TD>||is;
77
78         #$content =~ s|.*?<OPTION SELECTED>\s+([^<]+)\s<OPTION>.*?</TR>||s; # local time
79         $content =~ s|.*?<BR>\s+([^<]+?)\s*</FORM>.*?</TR>||s; # UTC
80         my $time = $1;
81         $time =~ s/-//g;
82         $time =~ s/\s+/ /g;
83
84         $content =~ s|\s(.*?)<TD COLSPAN=2>||s;
85         my $features = $1;
86
87         while ($features =~ s|.*?<TD ALIGN[^>]*>(?:\s*<[^>]+>)*\s+([^<]+?)\s+<.*?<TD>(?:\s*<[^>]+>)*\s+([^<]+?)\s<.*?/TD>||s) {
88             my ($f,$v) = ($1, $2);
89             chomp $f; chomp $v;
90             $feat{$f} = $v;
91         }
92
93         $content =~ s|.*?>(\d+\S+\s+\(\S+\)).*?</TD>||s;  # max temp;
94         $max_temp = $1;
95         $content =~ s|.*?>(\d+\S+\s+\(\S+\)).*?</TD>||s;
96         $min_temp = $1;
97
98         if ($time) {
99             $result = "$place; $id; last updated: $time";
100             foreach (sort keys %feat) {
101                 next if $_ eq 'ob';
102                 $result .= "; $_: $feat{$_}";
103             }
104             my $t = time();
105             $cache{$station} = join $;, $t, $result;
106         } else {
107             $result = "I can't find that station code (see http://weather.noaa.gov/weather/curcond.html for locations and codes)";
108         }
109         return $result;
110     }
111 }
112
113 if (0) {
114     if (-t STDIN) {
115         my $result = Weather::NOAA::get($default);
116         $result =~ s/; /\n/g;
117         print "\n$result\n\n";
118     }
119 }
120
121 1;
122
123 __END__
124
125 =head1 NAME
126
127 NOAA.pl - Get the weather from a NOAA server
128
129 =head1 PREREQUISITES
130
131         LWP::UserAgent
132
133 =head1 PARAMETERS
134
135 weather
136
137 =head1 PUBLIC INTERFACE
138
139         weather [for] <station>
140
141 =head1 DESCRIPTION
142
143 Contacts C<weather.noaa.gov> and gets the weather report for a given
144 station.
145
146 =head1 AUTHORS
147
148 Kevin Lenzo