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/perlTo 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).
#
# 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 --\>.*?°F/gi)
{
$temp=$&;
$temp =~ s/\<!-- insert current temp --\>//;
$temp =~ s/°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=~/ /gi) || die "Couldn't parse weather information";
$stream=$';
$dayhigh=$stream;
$dayhigh =~ s/\<\/TD\>.*//gi;
$dayhigh =~ s/\°F.*//gi;
$dayhigh=~s/^\s*//gi;
$dayhigh=~s/\s*$//gi;
#pick up today's low
($daytemps=~m/ /gi) || die "Couldn't parse weather information";
$stream=$';
$daylow=$stream;
$daylow =~ s/\<\/TD\>.*//gi;
$daylow =~ s/\°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";
speak_message $message1This 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.
Mac OS X Hints
http://hints.macworld.com/article.php?story=20020416085738936