add back the contents of this directory
[spider.git] / Geo / TAF / example / fetch_weather.pl
1 #!/usr/bin/perl -w
2
3 # $Id$
4
5 # this has been taken from Geo::METAR and modified
6 #
7 # Brief Description
8 # =================
9 #
10 # fetch_temp.pl is a program that demonstrates how to get the current
11 # temperature from a nearby (or not) airport using Geo::METAR and the
12 # LWP modules.
13 #
14 # Given an airport site code on the command line, fetch_temp.pl
15 # fetches the current temperature and displays it on the
16 # command-line. For fun, here are some example airports:
17 #
18 # LA     : KLAX
19 # Dallas : KDFW
20 # Detroit: KDTW
21 # Chicago: KMDW
22 #
23 # and of course: EGSH (Norwich)
24 #
25 #
26
27 # Get the site code.
28 my ($debug, $raw);
29 my @sort;
30 while ($ARGV[0] =~ /^-/ && @ARGV > 1) {
31         my @f = split //, shift @ARGV;
32         shift @f;
33         foreach $f (@f) {
34                 push @sort, 'taf' if $f eq 't' && ! grep $_ eq 'taf', @sort; 
35                 push @sort, 'staf' if $f eq 's' && ! grep $_ eq 'staf', @sort; 
36                 push @sort, 'metar' if $f eq 'm' && ! grep $_ eq 'metar', @sort; 
37                 $debug++ if $f eq 'x';
38                 $raw++ if $f eq 'r';
39         }
40 }
41 push @sort, 'metar' unless @sort;
42
43 my $site_code = uc shift @ARGV;
44
45 die "Usage: $0 [-mts] <site_code>\n" unless $site_code;
46
47 # Get the modules we need.
48
49 use Geo::TAF;
50 use LWP::UserAgent;
51 use strict;
52
53 my $sort;
54
55 foreach $sort (@sort) {
56
57         my $ua = new LWP::UserAgent;
58
59         my $req = new HTTP::Request GET =>
60                 "http://weather.noaa.gov/cgi-bin/mget$sort.pl?cccc=$site_code";
61         
62         my $response = $ua->request($req);
63         
64         if ($response->is_success) {
65                 
66                 # Yep, get the data and find the METAR.
67                 
68                 my $m = new Geo::TAF;
69                 my $data;
70                 $data = $response->as_string;               # grap response
71                 $data =~ s/\n//go;                          # remove newlines
72                 $data =~ m/($site_code\s\d+Z.*?)</go;       # find the METAR string
73                 my $metar = $1;                             # keep it
74                 
75                 # Sanity check
76                 
77                 if (length($metar)<10) {
78                         die "METAR is too short! Something went wrong.";
79                 }
80                 
81                 # pass the data to the METAR module.
82                 if ($sort =~ /taf$/) {
83                         $m->taf($metar);
84                 } else {
85                         $m->metar($metar);
86                 }
87                 print $m->raw, "\n" if $raw;
88                 print join "\n", $m->as_chunk_strings, "\n" if $debug;
89                 print $m->as_string, "\n";
90                 
91         } else {
92                 
93                 print $response->as_string, "\n";
94                 
95         } 
96         print "\n";
97 }
98
99 exit 0;
100
101