]> git.donarmstrong.com Git - infobot.git/blob - blootbot/src/Modules/Weather.pl
parsing
[infobot.git] / blootbot / 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>([^<]*?)\s*<.*?</TR>||is;
64         my $place = $1;
65         chomp $place;
66
67         $content =~ s|.*?<TR>(?:\s*<[^>]+>)*\s*([^<]+)\s<.*?</TR>||is;
68         my $id = $1;
69         chomp $id;
70
71         $content =~ s|.*?conditions at.*?</TD>||is;
72
73         #$content =~ s|.*?<OPTION SELECTED>\s+([^<]+)\s<OPTION>.*?</TR>||s; # local time
74         $content =~ s|.*?<BR>\s+([^<]+?)\s*</FORM>.*?</TR>||s; # UTC
75         my $time = $1;
76         $time =~ s/-//g;
77         $time =~ s/\s+/ /g;
78
79         $content =~ s|\s(.*?)<TD COLSPAN=2>||s;
80         my $features = $1;
81
82         while ($features =~ s|.*?<TD ALIGN[^>]*>(?:\s*<[^>]+>)*\s+([^<]+?)\s+<.*?<TD>(?:\s*<[^>]+>)*\s+([^<]+?)\s<.*?/TD>||s) {
83             my ($f,$v) = ($1, $2);
84             chomp $f; chomp $v;
85             $feat{$f} = $v;
86         }
87
88         $content =~ s|.*?>(\d+\S+\s+\(\S+\)).*?</TD>||s;  # max temp;
89         $max_temp = $1;
90         $content =~ s|.*?>(\d+\S+\s+\(\S+\)).*?</TD>||s;
91         $min_temp = $1;
92
93         if ($time) {
94             $result = "$place; $id; last updated: $time";
95             foreach (sort keys %feat) {
96                 next if $_ eq 'ob';
97                 $result .= "; $_: $feat{$_}";
98             }
99             my $t = time();
100             $cache{$station} = join $;, $t, $result;
101         } else {
102             $result = "I can't find that station code (see http://weather.noaa.gov/weather/curcond.html for locations and codes)";
103         }
104         return $result;
105     }
106 }
107
108 if (0) {
109     if (-t STDIN) {
110         my $result = Weather::NOAA::get($default);
111         $result =~ s/; /\n/g;
112         print "\n$result\n\n";
113     }
114 }
115
116 1;
117
118 __END__
119
120 =head1 NAME
121
122 NOAA.pl - Get the weather from a NOAA server
123
124 =head1 PREREQUISITES
125
126         LWP::UserAgent
127
128 =head1 PARAMETERS
129
130 weather
131
132 =head1 PUBLIC INTERFACE
133
134         weather [for] <station>
135
136 =head1 DESCRIPTION
137
138 Contacts C<weather.noaa.gov> and gets the weather report for a given
139 station.
140
141 =head1 AUTHORS
142
143 Kevin Lenzo