Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A command-line weather getter UNIX
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.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[9,685 views]  

A command-line weather getter | 8 comments | Create New Account
Click here to return to the 'A command-line weather getter' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Another method . . .
Authored by: Membranophonist on Apr 16, '02 11:29:09AM

Would be to use iAlarm, a nice free application that can wake you with iTunes, by speaking your local weather, news headlines, or a phrase of your choosing or by launching a user-selected AppleScript.

I don't how the weather report compares to that provided by the CLI method, but it gives you the current temperature and the estimated high for the day.

http://www.versiontracker.com/moreinfo.fcgi?id=13824&db=mac




[ Reply to This | # ]
Similar But Different
Authored by: FACEMILK on Apr 16, '02 05:06:24PM

I set up a system to automatically put current weather maps on my desktop at regular intervals. I'll leave the details up to you to figure out. It's a good learning excercise and not too difficult. May the man be with you.

Overview:
- cron runs the shell script
- shell script runs wget
- wget gets current weather maps
- ChangeDesktop (from Brian Bergstrand) sets the desktop to the maps

Notes:
- The weather maps come from weather.com, you can poke around there to find the map urls that you like.
- curl can replace wget if you want.



[ Reply to This | # ]
Newbie script problem
Authored by: ceevee on Apr 17, '02 02:24:04PM

I'm sure this will be obvious to the non-newbies out there, but I can't figure out what I'm doing wrong here. Here's what I did:

* copied the script into BBEdit
* saved it as "getweatherscript" into my home directory
* chmod 755 getweatherscript (in Terminal)
* rehash (in Terminal)

I try typing "getweatherscript" and it says "getweatherscript: Command not found."

What am I leaving out?



[ Reply to This | # ]
Newbie script problem
Authored by: Anonymous on Apr 17, '02 06:36:05PM

Wherever you saved the file to (your home directory, apparently) isn't in your PATH. Thus, you must move it to a directory in your PATH variable, or use explicit paths when running the app. eg:

% /Users/jdoe/getweather

or, if you are currently in your home directory:

% ./getweather



[ Reply to This | # ]
Newbie script problem
Authored by: ceevee on Apr 18, '02 10:02:36AM

Should have mentioned that I did try to use the absolute path. So trying it as /Users/ceevee/getweatherscript gives the same "command not found" error. I tried making a bin subdir in my user folder; still doesn't work... and /Users/ceevee/bin seems to be in my PATH. If I do "echo $PATH" it shows me /Users/ceevee/bin as one of the paths.



[ Reply to This | # ]
Newbie script problem
Authored by: George Frederick on Apr 19, '02 01:44:46PM

I had the same problem. The script isn't working for me.

Sounded kind of neat too. Oh well.



[ Reply to This | # ]
Newbie script problem
Authored by: ceevee on Apr 18, '02 04:49:01PM

OK, sorry for these messages but I did finally figure it out myself. When I saved the files out of BBEdit I didn't check the line ending style used. Using Mac line endings does NOT work. I changed to Unix line breaks and the scripts work fine now.



[ Reply to This | # ]
Another parse error
Authored by: Secret300 on Apr 18, '02 08:06:20PM

When I try to run this script, it gives me an error message:

Couldn't parse weather information. at ./weatherscript line 50.

Anyone know what this might be?



[ Reply to This | # ]