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