From: minima Date: Sun, 2 Feb 2003 00:39:52 +0000 (+0000) Subject: added cgi_weather X-Git-Url: http://dxcluster.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=bac20efef674741a002cd7d1a6927277df5e1f7f;p=spider.git added cgi_weather merged all the fetch thingies into one --- diff --git a/Geo/TAF/MANIFEST b/Geo/TAF/MANIFEST index a7b7786c..27ae38e0 100644 --- a/Geo/TAF/MANIFEST +++ b/Geo/TAF/MANIFEST @@ -5,7 +5,6 @@ README TAF.pm example/cmd_chunks.pl example/cmd_taf.pl -example/fetch_metar.pl -example/fetch_taf.pl -example/fetch_staf.pl +example/fetch_weather.pl +example/cgi_weather.pl t/1.t diff --git a/Geo/TAF/example/cgi_weather.pl b/Geo/TAF/example/cgi_weather.pl new file mode 100755 index 00000000..3091c535 --- /dev/null +++ b/Geo/TAF/example/cgi_weather.pl @@ -0,0 +1,157 @@ +#!/usr/bin/perl -w +# +# fetch a metar, taf or short taf from http://weather.noaa.gov +# +# This is designed to be used in a IFRAME and returns HTML. +# It will only query the website once every 30 minutes, the rest +# of the time it will cache the result in an 'easily guessable' +# place in /tmp (consider that as a warning). +# +# Call it from a web page like this:- +# +# +# +# You can set as many of these as you like:- +# metar=1 for a metar (default, if no options) +# staf=1 for a short form (usually more uptodate) TAF +# taf=1 for a full 18 hour TAF +# break=1 insert a "

" between each result +# +# $Id$ +# +# Copyright (c) 2003 Dirk Koopman G1TLH +# +use strict; +use CGI; +use Geo::TAF; +use LWP::UserAgent; + +my $q = new CGI; +my $site_code = uc $q->param('icao'); +my @sort; +push @sort, 'taf' if $q->param('taf'); +push @sort, 'staf' if $q->param('staf'); +push @sort, 'metar' if $q->param('metar') || @sort == 0; +my $dobrk = $q->param('break'); + +error("No ICAO (valid) site code ($site_code) specified") unless $site_code && $site_code =~ /^[A-Z]{4}$/; + +my $base = "/tmp"; +my ($sort, $fn, $started); + +while ($sort = shift @sort) { + $fn = "$base/${sort}_$site_code"; + + my ($mt, $size) = (stat $fn)[9,7]; + $mt ||= 0; + $size ||= 0; + + my $brk = "

" unless @sort; + + if ($mt + 30*60 < time || $size == 0) { + fetch_icao($brk); + } else { + my $s = retrieve(); + send_metar($s, $brk); + } +} + +sub retrieve +{ + open IN, "$fn" or die "cannot open $fn $!\n"; + my $s = ; + close IN; + return $s; +} + +sub fetch_icao +{ + my $brk = shift || ""; + 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) { + error("METAR Fetch $site_code Error", $response->error_as_HTML); + } else { + + # 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); + } + my $s = $m->as_string; + send_metar($s, $brk); + store($s); + } +} + +finish(); + +sub start +{ + return if $started; + print $q->header(-type=>'text/html', -expires=>'+60m'); + print $q->start_html(-title=>"Weather for $site_code", -style=>{'src'=>'/style.css'},); + $started = 1; +} + +sub finish +{ + print $q->end_html; +} + +sub store +{ + my $s = shift; + open OUT, ">$fn" or die "cannot open $fn $!\n"; + print OUT $s; + close OUT; +} + +sub send_metar +{ + my $s = shift; + my $brk = shift || ""; + + start(); + print "
$s
$brk"; +} + +sub error +{ + my $err = shift; + my $more = shift; + print $q->header(-type=>'text/html', -expires=>'+60m'); + print $q->start_html($err); + print $q->h3($err); + print $more if $more; + print $q->end_html; + warn($err); + + exit(0); +} + diff --git a/Geo/TAF/example/fetch_metar.pl b/Geo/TAF/example/fetch_metar.pl deleted file mode 100755 index b88aa299..00000000 --- a/Geo/TAF/example/fetch_metar.pl +++ /dev/null @@ -1,80 +0,0 @@ -#!/usr/bin/perl -w - -# $Id$ - -# this has been taken from Geo::METAR -# -# 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 $site_code = uc shift @ARGV; - -die "Usage: $0 \n" unless $site_code; - -# Get the modules we need. - -use Geo::TAF; -use LWP::UserAgent; -use strict; - -my $ua = new LWP::UserAgent; - -my $req = new HTTP::Request GET => - "http://weather.noaa.gov/cgi-bin/mgetmetar.pl?cccc=$site_code"; - -my $response = $ua->request($req); - -if (!$response->is_success) { - - print $response->error_as_HTML; - my $err_msg = $response->error_as_HTML; - warn "$err_msg\n\n"; - die "$!"; - -} else { - - # 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.*?)metar($metar); - - print $m->as_string, "\n"; - -} # end else - -exit; - -__END__ - - diff --git a/Geo/TAF/example/fetch_staf.pl b/Geo/TAF/example/fetch_staf.pl deleted file mode 100755 index d2482d1d..00000000 --- a/Geo/TAF/example/fetch_staf.pl +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/perl -w - -# $Id$ - -# this has been taken from Geo::METAR -# -# Brief Description -# ================= -# -# fetch_staf.pl is a program that demonstrates how to get the current -# short TAF for an airport. -# -# Given an airport site code on the command line, fetch_staf.pl -# fetches the short TAF 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 $site_code = uc shift @ARGV; - -die "Usage: $0 \n" unless $site_code; - -# Get the modules we need. - -use Geo::TAF; -use LWP::UserAgent; -use strict; - -my $ua = new LWP::UserAgent; - -my $req = new HTTP::Request GET => - "http://weather.noaa.gov/cgi-bin/mgetstaf.pl?cccc=$site_code"; - -my $response = $ua->request($req); - -if (!$response->is_success) { - - print $response->error_as_HTML; - my $err_msg = $response->error_as_HTML; - warn "$err_msg\n\n"; - die "$!"; - -} else { - - # Yep, get the data and find the TAF. - - 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($taf); - - print $m->as_string, "\n"; - -} # end else - -exit; - -__END__ - - diff --git a/Geo/TAF/example/fetch_taf.pl b/Geo/TAF/example/fetch_taf.pl deleted file mode 100755 index 7a2451db..00000000 --- a/Geo/TAF/example/fetch_taf.pl +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/perl -w - -# $Id$ - -# this has been taken from Geo::METAR -# -# Brief Description -# ================= -# -# fetch_staf.pl is a program that demonstrates how to get the current -# normal TAF for an airport. -# -# Given an airport site code on the command line, fetch_taf.pl -# fetches the normal TAF 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 $site_code = uc shift @ARGV; - -die "Usage: $0 \n" unless $site_code; - -# Get the modules we need. - -use Geo::TAF; -use LWP::UserAgent; -use strict; - -my $ua = new LWP::UserAgent; - -my $req = new HTTP::Request GET => - "http://weather.noaa.gov/cgi-bin/mgettaf.pl?cccc=$site_code"; - -my $response = $ua->request($req); - -if (!$response->is_success) { - - print $response->error_as_HTML; - my $err_msg = $response->error_as_HTML; - warn "$err_msg\n\n"; - die "$!"; - -} else { - - # Yep, get the data and find the TAF. - - 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($taf); - - print $m->as_string, "\n"; - -} # end else - -exit; - -__END__ - - diff --git a/Geo/TAF/example/fetch_weather.pl b/Geo/TAF/example/fetch_weather.pl new file mode 100755 index 00000000..178a6910 --- /dev/null +++ b/Geo/TAF/example/fetch_weather.pl @@ -0,0 +1,96 @@ +#!/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 @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; + } +} +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->as_string, "\n"; + + } else { + + print $response->as_string, "\n"; + + } + print "\n"; +} + +exit 0; + +