X-Git-Url: http://dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=Geo%2FTAF%2Fexample%2Fcgi_weather.pl;fp=Geo%2FTAF%2Fexample%2Fcgi_weather.pl;h=3091c5359f590eb879ad82167d8c25166d920c8e;hb=b4ab8d50203bcb77dc2fa09d3339ad6ec07304a8;hp=0000000000000000000000000000000000000000;hpb=a6627ef7afbee6ab015d9f8b75e96cd59809bb59;p=spider.git 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); +} +