Showing posts with label zip code. Show all posts
Showing posts with label zip code. Show all posts

Friday, October 3, 2008

Bash Weather Script - World Update!

Hey There,

We're back from yesterday's insanity (you called my bluff and I hope my response was satisfactory ;) and, yeah, I wasted my entire evening watching the Vice Presidential debate and then making the mistake of discussing it with people who didn't agree with me. Politics and Religion... When am I going to learn? ;)

Today's Linux/Unix bash shell script is an update on our Bash Weather Script. the info is still grabbed from WeatherBug.com. Check out our older bash script posts on spewing out famous quotations on pretty much any subject, doing encylopedia lookups, accessing the online Thesaurus, translating between different languages and, of course, using the online dictionary.

This script is fairly easy to run and but takes a few more arguments now. You can run it like this:

host # ./weather.sh 65873

or

host # ./weather.sh Berlin Germany <-- capitalization doesn't count ;)

Below are a few screen shots of the script's new-fangled output (it's actually just "more of the same"... oh no, I'm channeling campaign slogans now ;).

Click on either picture and watch it grow into something you can actually read ;)

New Local Weather Output

New World Weather Output

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

Enjoy your weekend and learn from me: Don't argue politics with your wife. The couch is an uncomfortable bed ;)

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 - It's raining somewhere in the world :)
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

numargs=$#
arg1=$1
arg2=$2

if [ $numargs -lt 1 ]
then
echo "Usage: $0 [YourZipCode|Town Country]"
echo "Ex: $0 60015"
echo "Ex: $0 Berlin Germany"
exit 1
fi

if [ $numargs -eq 2 ]
then
args=`echo $args|sed 's/ /\+/g'`
elif [ $# -gt 2 ]
then
echo "Usage: $0 [YourZipCode|Town Country]"
echo "Ex: $0 60015"
echo "Ex: $0 Berlin Germany"
exit 1
fi

echo

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


echo -n "Your Forecast:"

if [ $numargs -eq 1 ]
then
$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" rel="nofollow" 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'
else
echo " For $arg1, $arg2"
$wget -nv -O - "http://weather.weatherbug.com/$2/$1-weather.html?nav_section=1&zcode=z6286" 2>&1|sed -e :a -e 's/<[^>]*>/ /g;/</N;//ba' -e 's/$/\n/'|sed -e "1,/Your Forecast for $arg1, $arg2/d" -e '/7-Day Forecast/,$d'|sed -e '/^[ \t]*$/d' -e '/^$/N;/\n$/D' -e 's/[ \t]*more/... more\n/'|sed 's/°/Degrees/g'
fi

exit 0


, Mike




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

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.