Monday, September 29, 2008

Bash Script To Get Weather Forecasts For Your Zip Code

Hey There,

Today's Linux/Unix bash shell script is probably not the last follow up to the growing stable of scripts we've written to mine the knowledge on tap online. Today's info is grabbed from WeatherBug.com. If you missed any of the others scripts we've jammed out, you can still find them in our older bash script posts to spew out famous quotations on pretty much any subject, do encylopedia lookups, access the online Thesaurus, translate between different languages and, of course, the use the online dictionary. This time we're going to take a crack at getting online weather updates, using zip codes. If you check out the GET variables, you'll notice that the scope is the WORLD, but it doesn't seem to work well with foreign identifiers. Of course, I haven't tried all that hard to get it to work - setting me up for a post on the upgrade ;)

This script is fairly easy to run and somewhat limited. The basic limitations are that it will only work with 5 digit zip codes (no "plus 4") and it will only return a summary weather forecast. If there's more to report, it will indicate this, by letting you know there's ...more information available. You can run it like this:

host # ./weather.sh 45432

Like all of our previous scripts, this script uses wget and sed to get the job done. As per usual, this script is a work in progress and could use some spring cleaning, but, hopefully, it's good enough for you to enjoy :)

Below are a few screen shots of the script's output.

Click on either picture to see it as the artist intended it to be seen before the producers ruined everything ;)

Your Personalized Forecast

When Weather Goes Wrong


Hope you enjoy this script, and can find some use for it. Any suggestions for improvement would be greatly appreciated.

By the way, the forecast for today is cloudy with a chance of all Hell breaking loose in my neighborhood. Here's to your forecast coming back a little more sunny :)

Cheers,


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/bin/bash

#
# weather.sh - Now you can be wrong 50 percent of the time, too :)
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

if [ $# -lt 1 ]
then
echo "Usage: $0 Your Zip Code"
echo "Ex: $0 60015"
exit 1
fi

args="$@"
wget=/usr/bin/wget

if [ $# -gt 1 ]
then
echo "Usage: $0 Your Zip Code"
echo "Ex: $0 60015"
exit 1
fi
echo

$wget -nv -O - "http://weather.weatherbug.com/Common/SearchResults.html?is_search=true&nav_section=1&loc_country=WORLD&zcode=z6286&loc=$args" 2>&1|grep -i "No matches found for your request" >/dev/null 2>&1

anygood=$?

if [ $anygood -eq 0 ]
then
args=`echo $args|sed 's/%20/\+/g'`
echo "No results found for $args"
exit 2
fi

echo -n "Your Forecast:"

$wget -nv -O - "http://weather.weatherbug.com/Common/SearchResults.html?is_search=true&nav_section=1&loc_country=WORLD&zcode=z6286&loc=$args" 2>&1|grep -w For|grep h3|grep $args|sed -e :a -e 's/<[^>]*>/ /g;/</N;//ba' -e 's/$/\n/'

$wget -nv -O - "http://weather.weatherbug.com/Common/SearchResults.html?is_search=true&nav_section=1&loc_country=WORLD&zcode=z6286&loc=$args" 2>&1|sed -e :a -e 's/<[^>]*>/ /g;/</N;//ba' -e 's/$/\n/'|sed -e '1,/Your Forecast/d' -e '/7-Day Forecast/,$d'|sed -e '/^[ \t]*$/d' -e '/^$/N;/\n$/D' -e 's/[ \t]*more/... more\n/'|sed 's/°/Degrees/g'

exit 0



, Mike




Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.