7a2451db177eb8b45d27543c1eb579cd524681ea
[spider.git] / Geo / TAF / example / fetch_taf.pl
1 #!/usr/bin/perl -w
2
3 # $Id$
4
5 # this has been taken from Geo::METAR
6 #
7 # Brief Description
8 # =================
9 #
10 # fetch_staf.pl is a program that demonstrates how to get the current
11 # normal TAF for an airport.
12 #
13 # Given an airport site code on the command line, fetch_taf.pl
14 # fetches the normal TAF and displays it on the
15 # command-line. For fun, here are some example airports:
16 #
17 # LA     : KLAX
18 # Dallas : KDFW
19 # Detroit: KDTW
20 # Chicago: KMDW
21 #
22 # and of course: EGSH (Norwich)
23 #
24 #
25 # Get the site code.
26
27 my $site_code = uc shift @ARGV;
28
29 die "Usage: $0 <site_code>\n" unless $site_code;
30
31 # Get the modules we need.
32
33 use Geo::TAF;
34 use LWP::UserAgent;
35 use strict;
36
37 my $ua = new LWP::UserAgent;
38
39 my $req = new HTTP::Request GET =>
40   "http://weather.noaa.gov/cgi-bin/mgettaf.pl?cccc=$site_code";
41
42 my $response = $ua->request($req);
43
44 if (!$response->is_success) {
45
46     print $response->error_as_HTML;
47     my $err_msg = $response->error_as_HTML;
48     warn "$err_msg\n\n";
49     die "$!";
50
51 } else {
52
53     # Yep, get the data and find the TAF.
54
55     my $m = new Geo::TAF;
56     my $data;
57     $data = $response->as_string;               # grap response
58     $data =~ s/\n//go;                          # remove newlines
59     $data =~ m/($site_code\s\d+Z.*?)</go;       # find the TAF string
60     my $taf = $1;                             # keep it
61
62     # Sanity check
63
64     if (length($taf)<10) {
65         die "TAF is too short! Something went wrong.";
66     }
67
68     # pass the data to the TAF module.
69     $m->taf($taf);
70
71     print $m->as_string, "\n";
72
73 } # end else
74
75 exit;
76
77 __END__
78
79