X-Git-Url: http://dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=Geo%2FTAF%2Fexample%2Ffetch_weather.pl;fp=Geo%2FTAF%2Fexample%2Ffetch_weather.pl;h=8d01a225b73be7a557dbf7fee935ed522dd70b00;hb=b4ab8d50203bcb77dc2fa09d3339ad6ec07304a8;hp=0000000000000000000000000000000000000000;hpb=a6627ef7afbee6ab015d9f8b75e96cd59809bb59;p=spider.git diff --git a/Geo/TAF/example/fetch_weather.pl b/Geo/TAF/example/fetch_weather.pl new file mode 100755 index 00000000..8d01a225 --- /dev/null +++ b/Geo/TAF/example/fetch_weather.pl @@ -0,0 +1,101 @@ +#!/usr/bin/perl -w + +# $Id$ + +# this has been taken from Geo::METAR and modified +# +# Brief Description +# ================= +# +# fetch_temp.pl is a program that demonstrates how to get the current +# temperature from a nearby (or not) airport using Geo::METAR and the +# LWP modules. +# +# Given an airport site code on the command line, fetch_temp.pl +# fetches the current temperature and displays it on the +# command-line. For fun, here are some example airports: +# +# LA : KLAX +# Dallas : KDFW +# Detroit: KDTW +# Chicago: KMDW +# +# and of course: EGSH (Norwich) +# +# + +# Get the site code. +my ($debug, $raw); +my @sort; +while ($ARGV[0] =~ /^-/ && @ARGV > 1) { + my @f = split //, shift @ARGV; + shift @f; + foreach $f (@f) { + push @sort, 'taf' if $f eq 't' && ! grep $_ eq 'taf', @sort; + push @sort, 'staf' if $f eq 's' && ! grep $_ eq 'staf', @sort; + push @sort, 'metar' if $f eq 'm' && ! grep $_ eq 'metar', @sort; + $debug++ if $f eq 'x'; + $raw++ if $f eq 'r'; + } +} +push @sort, 'metar' unless @sort; + +my $site_code = uc shift @ARGV; + +die "Usage: $0 [-mts] \n" unless $site_code; + +# Get the modules we need. + +use Geo::TAF; +use LWP::UserAgent; +use strict; + +my $sort; + +foreach $sort (@sort) { + + my $ua = new LWP::UserAgent; + + my $req = new HTTP::Request GET => + "http://weather.noaa.gov/cgi-bin/mget$sort.pl?cccc=$site_code"; + + my $response = $ua->request($req); + + if ($response->is_success) { + + # Yep, get the data and find the METAR. + + my $m = new Geo::TAF; + my $data; + $data = $response->as_string; # grap response + $data =~ s/\n//go; # remove newlines + $data =~ m/($site_code\s\d+Z.*?)taf($metar); + } else { + $m->metar($metar); + } + print $m->raw, "\n" if $raw; + print join "\n", $m->as_chunk_strings, "\n" if $debug; + print $m->as_string, "\n"; + + } else { + + print $response->as_string, "\n"; + + } + print "\n"; +} + +exit 0; + +