A command-line weather getter

Apr 16, '02 08:57:38AM

Contributed by: MarkProgrammer

I'm a big fan of the PERL scripts that have previously been posted here, especially the command line speech generator and the command-line alarm clock. I use these two so frequently that I wrote a script to go with them; it gets the current weather and daily forecast off of weather.com, outputting the information in an speech-friendly way. To use this script, you must have wget installed, which you can do in a number of ways (This hint explains how, or through fink if it's installed, or via macosx.forked.net).

Read the rest of the article for the script...

Enter the following script in your favorite text editor:

#!/usr/bin/perl
#
# getWeather--a script to get weather information from Weather.com
# by Mark T. Tomczak, 2002
#

$ZIPCODE=15213;

if($#ARGV == 0)
{
$ZIPCODE=$ARGV[0];
$ZIPCODE =~m/\d\d\d\d\d/gi;
if(length($ZIPCODE) != 5)
{
print "Invalid zipcode. Please enter only a five-digit zipcode.\n";
exit;
}
}

open(INPUT, "wget http://www.weather.com/weather/local/$ZIPCODE -q -O-|");
@stream=<INPUT>;
close(INPUT);
$stream=join(" ", @stream);

#extract the forecast
if($stream=~ m/\<!-- insert forecast text --\>.*\<\/td\>/gi)
{
$forecast=$&;
$forecast =~ s/\<!-- insert forecast text --\>//;
$forecast =~ s/\<\/td\>//;
}
else
{
$forecast= "... umm... I'm not sure...";
}

#extract the temperature
if($stream=~ m/\<!-- insert current temp --\>.*?&deg;F/gi)
{
$temp=$&;
$temp =~ s/\<!-- insert current temp --\>//;
$temp =~ s/&deg;F//;
}
else
{
$temp= "... umm... I'm not sure...";
}

# pick up on today's forecast
($stream=~m/\<!-- insert load change day\/date here --\>/) || die "Couldn't parse weather information.";
$stream=$';
$stream=~s/\n//gi;
$daycast=$stream;
$daycast=~m/\<TD CLASS="dataText" ALIGN=\"LEFT\">/gi || die "Couldn't parse weather information.";
$daycast=$';
$daycast=~s/\<\/TD\>.*//gi;

#trim whitespace from the variable
$daycast=~s/^\s*//gi;
$daycast=~s/\s*$//gi;

#pick up today's high
$daytemps=$stream;
($daytemps=~/&nbsp;/gi) || die "Couldn't parse weather information";
$stream=$';
$dayhigh=$stream;
$dayhigh =~ s/\<\/TD\>.*//gi;
$dayhigh =~ s/\&deg;F.*//gi;
$dayhigh=~s/^\s*//gi;
$dayhigh=~s/\s*$//gi;

#pick up today's low
($daytemps=~m/&nbsp;/gi) || die "Couldn't parse weather information";
$stream=$';
$daylow=$stream;
$daylow =~ s/\<\/TD\>.*//gi;
$daylow =~ s/\&deg;F.*//gi;
$daylow=~s/^\s*//gi;
$daylow=~s/\s*$//gi;

#Output the results.
$output="The current weather is $forecast, and the current temperature is $temp.\nThe forcast for today is $daycast.";
if(length($dayhigh) !=0)
{
$output=$output."\nThe high is $dayhigh.";
}
if(length($daylow) !=0)
{
$output=$output."\nThe low is $daylow.";
}
print "$output\n";
To use the program, type 'getWeather zipcode', where zipcode is the zipcode of your area (the default is 15213 for purely selfish reasons; you can modify it by changing the default value of the $ZIPCODE variable).

Some fun uses: if you have the previously mentioned scripts, you can make the output of the program spoken with 'getWeather | xspeak'. If you want the weather report given by the alarm clock script, you can insert the 'getWeather | xspeak' command in that script after
speak_message $message1
This is my first PERL script, so if you have any comments or suggestions please let me know! The primary problems with the script now are that it is inefficient and ugly (still getting a handle on those ornery regexps!) and it probably won't be able to handle changes to weather.com's site format.

Have fun!

[Editor's note: I have tested the above with a copy and paste into the Terminal, and it works as expected. Make sure you make the script executable (chmod 755 script_name) and then 'rehash' before you try to run it.]

Comments (8)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20020416085738936