Weather-Com - script used at www.fpcsparta.org

Posted on Tue Apr 12 05:19:33 2005 by stargazer
script used at www.fpcsparta.org
#!c:/Perl/bin/Perl.exe # This Script uses Weather::Com v0.3.1 or newer by Thomas Schnucker and is available on www.CPAN.or +g # Author: David J. Pontzer (with plenty of borrowing from Thomas S's examples # Currently in use on http://www.doxanet.net and http://www.fpcsparta.org # I have posted the script as an example / guide to using Weather::Com 0.3.1 # Which has been enhanced to include forecasts and extensive flexible formatting # To use this script place the following line in your html page # <FORM ACTION="/cgi-bin/weather.pl" METHOD="POST" name="Weather" target="_blank" id="Weather"> # and # copy this weather.pl to your cgi-bin directory (or wherever you keep such things) # You may also have to edit the first line of this script to point to your PERL directory # Finally, enter your weather.com partner ID and License below, edit the header texts, and thats i +t # Users enter a city,state, or city,country, or a USA zip code, and click on the "go" button # A new window will pop up and display weather information # Enjoy, djp 3/6/05 use strict; use Weather::Com::Finder; use Data::Dumper; print "Content-type: text/html\n\n"; #Get variables from the form my ($i,$j,$vars,$temp,@pairs,$item,$key,$content); my %fields = (); #hash that contains the inputs from the form read(STDIN,$temp,$ENV{'CONTENT_LENGTH'}); @pairs=split(/&/,$temp); foreach $item(@pairs) { ($key,$content)=split(/=/,$item,2); $content=~tr/+/ /; $content=~s/%(..)/pack("c",hex($1))/ge; $content=~s/\t/ /g; $content=~s/\0//g; #strip nulls $fields{$key}=$content; } $| = 1; ############################## TO BE CONFIGURED BY THE USER ############################## my $PartnerId = ''; # put your weather.com partner ID and Lecense key here my $LicenseKey = ''; #put your license key here my $units = 's'; #set to 'm' for metric # if you need a proxy... maybe with authentication my $Proxy = ''; my $Proxy_User = ''; my $Proxy_Pass = ''; ############################# END OF USER CONFIG AREA ##################################### my $debugmode = 0; my %weatherargs = ( 'partner_id' => $PartnerId, 'license' => $LicenseKey, 'units' => $units, 'debug' => $debugmode, 'proxy' => $Proxy, 'proxy_user' => $Proxy_User, 'proxy_pass' => $Proxy_Pass, ); my $weather_finder = Weather::Com::Finder->new(%weatherargs); my $input = $fields{'name'}; #get the city and state from the form # print header ##################### User's to edit this header area to suit print "<FONT FACE=\"Arial\">"; print "<br><b>", "FPC / Doxanet Weather Service", "</b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbs +p&nbsp&nbsp&nbsp&nbsp"; print "</font> "; print "<input type=\"button\" value=\"Close this window\" onclick=\"self.close()\">"; print "<br><br>"; #check for a blank entry unless ($input) { print "<br>Sorry, No weather data found<br><br>"; print "Please enter the city name and state or country separated by a comma<br><br>"; print "Example: Sparta, NJ<br><br>"; print "Example: Toronto, Canada<br><br>"; print "<input type=\"button\" value=\"Close this window\" onclick=\"self.close()\">"; exit } my $locations = $weather_finder->find($input); # if $ws->get_weather() returned 'undef', we haven't found anything... unless ($locations) { print "No weather found for location '$input'", "<br>"; print "Please enter the city name and state or country separated by a comma<br><br>"; print "Example: Sparta, NJ<br><br>"; print "Example: Toronto, Canada<br><br>"; print "<input type=\"button\" value=\"Close this window\" onclick=\"self.close()\">"; exit } print "<FONT FACE=\"Arial\">"; my $uodist = $locations->[0]->units()->distance(); my $uoprecip = $locations->[0]->units()->precipitation(); my $uopress = $locations->[0]->units()->pressure(); my $uotemp = $locations->[0]->units()->temperature(); my $uospeed = $locations->[0]->units()->speed(); foreach my $location ( @{$locations} ) { # current conditions print "<br><b>Conditions for <font color=\"\#0000A0\">" . $location->name() . ":</FONT></b> <font size=\"1\"> Updated: " . $location->current_conditions()->last_updated()->time_ampm()," on ", $location->current_conditions()->last_updated()->formatted("mm\/dd\/yyyy"), "<br>"; print "<hr>"; print " - current conditions: <b>", $location->current_conditions()->description(), ", ", $location->current_conditions()->temperature(), "</b> deg $uotemp.<br>"; print " - visibilty is about ", $location->current_conditions()->visibility(), " $uodist.<br>"; print " - the current windchill is ", $location->current_conditions()->windchill(), " deg $uotemp.<br>"; print " - the humidity is ", $location->current_conditions()->humidity(), "\%.<br>"; print " - the dewpoint is ", $location->current_conditions()->dewpoint(), " deg $uotemp.<br>"; # all about wind print " - wind speed is ", $location->current_conditions()->wind()->speed(), " $uospeed.<br>"; print " - wind comes from ", $location->current_conditions()->wind()->direction_long(), "."; print " max. gust ", $location->current_conditions()->wind()->maximum_gust(), " $uospeed.<br>"; # all about uv index print " - uv index is ", $location->current_conditions()->uv_index()->index(), "."; print " ... that is ", $location->current_conditions()->uv_index()->description(), ".<br>"; # all about barometric pressure print " - air pressure is ", $location->current_conditions()->pressure()->pressure(), " $uopress."; print " ... tendency ", $location->current_conditions()->pressure()->tendency(), ".<br>"; # print location data print "- ", $location->name(), " is located at: ", $location->latitude(), "deg N latitude, ", $location->longitude(), "deg E longitude<br>"; print " - local time is ", $location->localtime_ampm(), "<br>"; print " - sunrise will be/has been at ", $location->sunrise()->time_ampm(), "<br>"; print " - sunset (am/pm) will be/has been at ", $location->sunset_ampm(), "<br>"; print " - timezone is GMT ", $location->timezone(), " hour(s)<br>"; # Forecast Area print "<br><font size=\"3\">"; print "<br><b>Forcast for <font color=\"\#0000A0\">" . $location->name() . ": </FONT></b></font>"; # forecasts my $forecast = $location->forecast(); my $today = $forecast->day(0); print $today->date()->weekday(), ", ", $today->date()->date(), "<br>"; print "<hr><br>"; # print "- high for today will be: ", $today->high(), " deg $uotemp.<br>"; # print "- low for today will be: ", $today->low(), " deg $uotemp.<br>"; if ( $today->day() ) { # print "<b>Today's Forcast:</b><br>"; print "- high for today will be: ", $today->high(), " deg $uotemp.<br>"; print "- conditions: ", $today->day()->conditions(), "<br>"; print "- humidity: ", $today->day()->humidity(), "\%<br>"; print "- chance of precipitation: ", $today->day()->precipitation(), "\%<br>"; print "- wind speed: ", $today->day()->wind()->speed(), " $uospeed.<br>"; print "- max gust: ", $today->day()->wind()->maximum_gust(), " $uospeed.<br>" +; print "- wind dir: ", $today->day()->wind()->direction_long(), "<br>"; # print "- wind dir: ", $today->day()->wind()->direction_degrees(), "<br>"; } print "<br><b>Tonight:</b><br>"; print "- low for tonight will be: ", $today->low(), " deg $uotemp.<br>"; print "- conditions: ", $today->night()->conditions(), "<br>"; print "- humidity: ", $today->night()->humidity(), "\%<br>"; print "- chance of precipitation: ", $today->night()->precipitation(), "\%<br>"; print "- wind speed: ", $today->night()->wind()->speed(), "$uospeed.<br>"; print "- max gust: ", $today->night()->wind()->maximum_gust(), "$uospeed.<br>"; print "- wind dir: ", $today->night()->wind()->direction_long(), "<br>"; # print "- wind dir: ", $today->night()->wind()->direction_degrees(), "<br>"; my $tomorrow = $forecast->day(1); print "<br><b>Tomorrow: </b> "; print $tomorrow->date()->weekday(), ", ", $tomorrow->date()->date(), "<br>"; if ( $tomorrow->high() ) { # print "- high: ", $tomorrow->high(), " deg $uotemp.<br>"; } # print "- low: ", $tomorrow->low(), " deg $uotemp.<br>"; # print "- sunrise: ", $tomorrow->sunrise()->time(), "<br>"; # print "- sunset: ", $tomorrow->sunset()->time(), "<br>"; # print "<br>"; if ( $tomorrow->day() ) { # print "Daytime:<br>"; print "- high: ", $tomorrow->high(), " deg $uotemp.<br>"; print "- conditions: ", $tomorrow->day()->conditions(), "<br>"; print "- humidity: ", $tomorrow->day()->humidity(), "\%<br>"; print "- chance of precipitation: ", $tomorrow->day()->precipitation(), "\%<br>"; print "- wind speed: ", $tomorrow->day()->wind()->speed(), " $uospeed.<br>"; print "- max gust: ", $tomorrow->day()->wind()->maximum_gust(), " $uospeed.<b +r>"; print "- wind dir: ", $tomorrow->day()->wind()->direction_long(), "<br>"; # print "- wind dir: ", $tomorrow->day()->wind()->direction_degrees(),"<br>"; } print "<br><b>Tommorow Night:</b><br>"; print "- low: ", $tomorrow->low(), " deg $uotemp.<br>"; print "- conditions: ", $tomorrow->night()->conditions(), "<br>"; print "- humidity: ", $tomorrow->night()->humidity(), "\%<br>"; print "- chance of precipitation: ", $tomorrow->night()->precipitation(), "\%<br>"; print "- wind speed: ", $tomorrow->night()->wind()->speed(), " $uospeed.<br>"; print "- max gust: ", $tomorrow->night()->wind()->maximum_gust(), " $uospeed.<br>"; print "- wind dir: ", $tomorrow->night()->wind()->direction_long(), "<br>"; # print "- wind dir: ", $tomorrow->night()->wind()->direction_degrees(), "<br>"; print "<br><hr><br>"; print "<input type=\"button\" value=\"Close this window\" onclick=\"self.close()\">"; print "<br><font size=\"3\">"; print "<br><b>Long Range Forcast for <font color=\"\#0000A0\">" . $location->name() . ": </FONT></ +b></font>"; print "<br><hr><br>"; #Long Range Forecast for next 10 days or so for (my $i=2; $i<10; $i++) { if ( my $day = $forecast->day($i) ) { #make sure a forecast exists for this +date print "<b>", $day->date()->weekday(), "</b>, ", $day->date()->date(), "<br>"; if ( $day->high() ) { print "- high: ", $day->high(), " deg $uotemp.<br>"; } # print "- low: ", $day->low(), " deg $uotemp.<br>"; # print "- sunrise: ", $day->sunrise()->time(), "<br>"; # print "- sunset: ", $day->sunset()->time(), "<br>"; # print "<br>"; if ( $day->day() ) { #make sure a day forecast exists, these get pulled aft +er dark I think # print "Daytime:<br>"; print "- conditions: ", $day->day()->conditions(), "<br>"; print "- humidity: ", $day->day()->humidity(), "\%<br>"; print "- chance of precipitation: ", $day->day()->precipitation(), + "\%<br>"; print "- wind speed: ", $day->day()->wind()->speed(), " $uospee +d.<br>"; print "- max gust: ", $day->day()->wind()->maximum_gust(), " +$uospeed.<br>"; print "- wind dir: ", $day->day()->wind()->direction_long(), "<br>" +; # print "- wind dir: ", $day->day()->wind()->direction_degrees(),"<b +r>"; } if ( $day->night() ) { #this may not be necessary, but just check to see if + a night forecast is out there print "<br><b>", $day->date()->weekday(), " Night:</b><br>"; print "- low: ", $day->low(), " deg $uotemp.<br>"; print "- conditions: ", $day->night()->conditions(), "<br>"; print "- humidity: ", $day->night()->humidity(), "\%<br>"; print "- chance of precipitation: ", $day->night()->precipitation(), "\ +%<br>"; print "- wind speed: ", $day->night()->wind()->speed(), " $uospeed.< +br>"; print "- max gust: ", $day->night()->wind()->maximum_gust(), " $uo +speed.<br>"; print "- wind dir: ", $day->night()->wind()->direction_long(), "<br>"; # print "- wind dir: ", $day->night()->wind()->direction_degrees(), "<br> +"; print "<br>"; } #end of conditional on night }# end of conditional on date } # end of date loop } #end of location loop #Credits and Closing Section - edit to suit print "</FONT><br>"; print "<input type=\"button\" value=\"Close this window\" onclick=\"self.close()\">"; print "<br><br><font size=\"1\"><font color=\"000000\"><FONT FACE=\"Arial\"> Weather data courtesy +of weather.com and PERL Module Weather::Com by Thomas Schnucker</font>";
Direct Responses: 328 | Write a response