Register
It is currently Thu Feb 22, 2018 12:38 am

Weather Conditions Script:


All times are UTC


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Sat Dec 05, 2009 4:19 pm   
Jr. Member
User avatar

Joined: Tue Nov 24, 2009 3:03 am
Posts: 53
Location: ~/
I noticed we have a very active weather conditions post in the community area,
so I created a simple little script using the google weather API to display conditions by zipcode.

Try it out, let me know what you think... :?:

Code:
#!/bin/bash

echo -n "Enter a valid 5-digit zipcode: "
read zipcode

var_url="http://www.google.com/ig/api?weather=$zipcode&hl=en"

var_weather_wget=`wget -q $var_url -O -`
var_weather_xml=`echo "$var_weather_wget" | sed 's/<forecast_conditions>.*//'`
var_weather=`echo "$var_weather_xml" | sed 's/></>\n</g'`

var_date=`echo "$var_weather" | grep -e '<forecast_date' | \
sed -e 's/<forecast_date data="//' -e 's/"\/>//'`

var_city=`echo "$var_weather" | grep -e '<city' | \
sed -e 's/<city data="//' -e 's/"\/>//'`

var_condition=`echo "$var_weather" | grep -e '<condition' | \
sed -e 's/<condition data="//' -e 's/"\/>//'`

var_temp_f=`echo "$var_weather" | grep -e '<temp_f' | \
sed -e 's/<temp_f data="//' -e 's/"\/>//'`

var_temp_c=`echo "$var_weather" | grep -e '<temp_c' | \
sed -e 's/<temp_c data="//' -e 's/"\/>//'`

var_humidity=`echo "$var_weather" | grep -e '<humidity' | \
sed -e 's/<humidity data="//' -e 's/"\/>//'`

var_wind=`echo "$var_weather" | grep -e '<wind' | \
sed -e 's/<wind_condition data="//' -e 's/"\/>//'`

echo "Date: $var_date"
echo "City: $var_city"
echo "Condition: $var_condition"
echo "Temp: $var_temp_f Deg. Fahrenheit / $var_temp_c Deg. Celsius"
echo "$var_humidity"
echo "$var_wind"

_________________
http://unixclubhouse.com


Top
 Profile  
 PostPosted: Sat Dec 05, 2009 8:53 pm   
Moderator
User avatar

Joined: Sat Nov 15, 2003 10:24 pm
Posts: 2765
Location: The Diamond State
Works fine on my box. Nice. :)
Code:
[[email protected] Desktop]$ ./weather
Enter a valid 5-digit zipcode: xxxxx
Date: 2009-12-05
City: xxxxxx xx
Condition: Showers
Temp: 37 Deg. Fahrenheit / 3 Deg. Celsius
Humidity: 95%
Wind: N at 17 mph
[[email protected] Desktop]$

_________________
openSUSE 11.4 (i586)
PCLinuxOS 2009 -2.6.26.8.tex3


Top
 Profile  
 PostPosted: Sat Dec 05, 2009 11:45 pm   
Sr. Member

Joined: Sun Dec 12, 2004 6:08 am
Posts: 1200
Location: Cupertino, CA
It's been awhile since I did anything with XML so I decided to dust off that part of my brain for a quick moment. I opted to do it structurally rather than hack and slash string/regular expression parsing.

Personally I'm not a big fan of the standard XML DOM interface...I find it rather unpythonic and clunky, but oh well - that's how it is I guess. It really surprises me they don't at least implement the __getitem__ (which is the Python magic method for "some_dictionary[some_key]" syntax) instead of having to use getElementsByTagName. I think some of the 3rd party XML parsing libraries do this, but I'm not sure. I know that beautifulSoup for HTML parsing does that, as well as allowing property-based access for child nodes.

Code:
import sys
from urllib2 import urlopen
import xml.dom.minidom as dom

zipcode = sys.argv[1]
xmlresponse = urlopen("http://www.google.com/ig/api?weather=%s" % zipcode)

doc = dom.parse(xmlresponse)

def get_data(doc,tag):
   return doc.getElementsByTagName(tag)[0].getAttribute("data")
   
data = get_data(doc,"forecast_date")
city = get_data(doc,"city")
condition = get_data(doc,"condition")
temp_f,temp_c = get_data(doc,"temp_f"),get_data(doc,"temp_c")
humidity_str = get_data(doc,"humidity")
wind_str = get_data(doc,"wind_condition")

print """Date: %s
City: %s
Condition: %s
Temp: %s Deg. Fahrenheit / %s Deg. Celsius
%s
%s""" % (data,city,condition,temp_f,temp_c,humidity_str,wind_str)


Zipcode is passed as the first command line argument rather than a prompt, but that could easily be done too.

Output:
Code:
Date: 2009-12-05
City: Rochester, NY
Condition: Mostly Cloudy
Temp: 31 Deg. Fahrenheit / -1 Deg. Celsius
Humidity: 67%
Wind: W at 3 mph


--edit
Python 3 version. Essentially the same, the urllib library was restructured a bit and print is a function.
Code:
import sys
from urllib.request import urlopen
import xml.dom.minidom as dom

zipcode = sys.argv[1]
xmlresponse = urlopen("http://www.google.com/ig/api?weather=%s" % zipcode)

doc = dom.parse(xmlresponse)

def get_data(doc,tag):
   return doc.getElementsByTagName(tag)[0].getAttribute("data")
   
data = get_data(doc,"forecast_date")
city = get_data(doc,"city")
condition = get_data(doc,"condition")
temp_f,temp_c = get_data(doc,"temp_f"),get_data(doc,"temp_c")
humidity_str = get_data(doc,"humidity")
wind_str = get_data(doc,"wind_condition")

print("""Date: %s
City: %s
Condition: %s
Temp: %s Deg. Fahrenheit / %s Deg. Celsius
%s
%s""" % (data,city,condition,temp_f,temp_c,humidity_str,wind_str))


Top
 Profile  
 PostPosted: Sun Dec 06, 2009 12:45 am   
Keeper of the BIG STICK
User avatar

Joined: Wed Apr 30, 2003 7:16 pm
Posts: 12679
Location: Planet Earth
Very nice, exiled.

Code:
[[email protected] ~]$ ./weather.sh
Enter a valid 5-digit zipcode: 74016
Date: 2009-12-05
City: Chelsea, OK
Condition: Clear
Temp: 38 Deg. Fahrenheit / 3 Deg. Celsius
Humidity: 50%
Wind: SE at 9 mph
[[email protected] ~]$

_________________
Laptop: Kubuntu 14.04
Desktop: Mageia 1 - kernel 2.6.38.7-desktop-mga


Top
 Profile YIM  
 PostPosted: Sun Dec 06, 2009 1:01 am   
Jr. Member
User avatar

Joined: Tue Nov 24, 2009 3:03 am
Posts: 53
Location: ~/
Thanks for the feedback everyone!! :D

_________________
http://unixclubhouse.com


Top
 Profile  
 PostPosted: Fri Oct 14, 2011 9:00 pm   
Linux Guru
User avatar

Joined: Sat Apr 03, 2004 12:39 am
Posts: 12346
Location: Clinton Township, Michigan
Hey, since we first started messing around with weather-related scripts, I have been collecting Bash, Perl, and Python, as well as TCL/Tk scripts for handling the weather. For a long time, a rather detailed and involved TCL/Tk script was my favorite, but the interface to Wunderground.com changed and I have not seen the TCL/Tk script change, so I have spent more time taking fairly simple Bash scripts, and I have found a few sites that have simple enough output that I can use straightforward editing to extract what I want from them.

Using Wunderground.com to get the current conditions, and the National Weather Service (NWS) to get the forecast, seems to be one of the best methods, overall, to generate a pretty good script. I did hard code in some of the values for my own convenience. You can modify one or two lines and use parameters for everything, and optionally create defaults, which is probably best practice. I've included my latest iteration in the code section below:

Code:
#!/bin/bash
# forecast2
#desc Find current weather stats and forecast for your zip code
#desc Usage: forecast2 48038
# weather 1.1 -Crouse
# With Updates by Jeo
# Modified to run stand alone by Brian Masinick,
# and also added the forecast logic contributed by Daenyth, and modified by Brian Masinick


getconditions() {
#  Should be one distinct line (using repeated slashes to help):
###################################################################################################################################################
  WEATHER="$(elinks -dump -dump-width 300 "http://mobile.wunderground.com/cgi-bin/findweather/getForecast?query=${1}" | grep -A16 Updated)"

  if [ -z "$WEATHER" ]; then
    response="No Results for $zipcode"
    echo "${response}"
  else
    response[1]="$(echo "$WEATHER" | grep -Eo 'Observed.*' | sed s/\ *\|\ */\|/g | awk -F\| '{print "Weather: " $1}')"
    response[2]="$(echo "$WEATHER" | grep -Eo 'Updated.*' |sed s/\ *\|\ */\|/g |awk -F\| '{print $1}')"
    response[3]="$(echo "$WEATHER" | grep -Eo 'Temperature.*' | sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}' | sed s/DEG/\ /g )"
    response[4]="$(echo "$WEATHER" | grep -Eo 'Windchill.*' | sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}'| sed s/DEG/\ /g)"
    response[5]="$(echo "$WEATHER" | grep -Eo 'Wind .*' | sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}')"
    response[6]="$(echo "$WEATHER" | grep -Eo 'Conditions.*' | sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}')"
    response[7]="$(echo "$WEATHER" | grep -Eo 'Humidity.*' |sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}')"
    response[8]="$(echo "$WEATHER" | grep -Eo 'Dew.Point.*' |sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}'| sed s/DEG/\ /g)"
    response[9]="$(echo "$WEATHER" | grep -Eo 'Pressure.*' |sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}')"

    for index in `seq 1 9`; do
      if [ -n "${response[$index]}" ]; then
        echo "${response[$index]}"
      fi
      let "index = $index + 1"
    done
  fi
}


# This section of code was originally written by Daenyth, modified by Brian Masinick.

DEFAULTZIP=48038

getforecast() {
        echo "Seven day forecast:"
        elinks -dump "http://mobile.weather.gov/port_mp_ns.php?select=1&CityName=Clinton%20Township&site=DTX&State=MI&warnzone=MIZ070"
        echo ""
}

if   [ $# -eq 1 ]; then
        if (echo "$1" | egrep -q '^[0-9][0-9][0-9][0-9][0-9]$'); then
                getconditions $1
        fi
else
        getconditions $DEFAULTZIP
fi

if   [ $# -eq 1 ]; then
        if (echo "$1" | egrep -q '^[0-9][0-9][0-9][0-9][0-9]$'); then
                getforecast $1
        fi
else
        getforecast $DEFAULTZIP
fi

_________________
Brian Masinick
Distros: MX-16, antiX, Debian


Top
 Profile WWW YIM  
 PostPosted: Fri Oct 14, 2011 9:01 pm   
Linux Guru
User avatar

Joined: Sat Apr 03, 2004 12:39 am
Posts: 12346
Location: Clinton Township, Michigan
Who wants to tackle that script in Perl or Python (or something else), possibly making it more general, and share it with us?

_________________
Brian Masinick
Distros: MX-16, antiX, Debian


Top
 Profile WWW YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


Powered by phpBB © 2012 phpBB Group
© 2003 - 2012 USA LINUX USERS GROUP