USA Linux Users Group Forum Index
Log in Register FAQ Memberlist Search USA Linux Users Group Forum Index Album

Weather Conditions Script:

 
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming
View previous topic :: View next topic  
Author Message
exiled
Jr. Member


Joined: 24 Nov 2024
Posts: 51
Location: ~/

PostPosted: Sat Dec 05, 2024 4:19 pm    Post subject: Weather Conditions Script: Reply with quote

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... Question

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"



_________________
Currently: Frugalware, Fedora
Previously: Gentoo, Startcom, Debian, Sabayon, Mint, openSUSE, Mangaka Chu, #!CrunchBang
Back to top
View user's profile Send private message
lynch
Moderator


Joined: 15 Nov 2024
Posts: 2659
Location: The Diamond State

PostPosted: Sat Dec 05, 2024 8:53 pm    Post subject: Reply with quote

Works fine on my box. Nice. Smile
Code:
[lynch@localhost Desktop]$ ./weather
Enter a valid 5-digit zipcode: xxxxx
Date: 2024-12-05
City: xxxxxx xx
Condition: Showers
Temp: 37 Deg. Fahrenheit / 3 Deg. Celsius
Humidity: 95%
Wind: N at 17 mph
[lynch@localhost Desktop]$



_________________
Mandriva 2024 Spring -2.6.31.12-server-2mnb
PCLinuxOS 2024 -2.6.26.8.tex3
Back to top
View user's profile Send private message
VHockey86
Advanced Member


Joined: 12 Dec 2024
Posts: 988
Location: Rochester

PostPosted: Sat Dec 05, 2024 11:45 pm    Post subject: Reply with quote

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: 2024-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))



_________________
Main Desktops : Kubuntu 10.4. ArchLinux 64-bit. Windows7 64-bit. Windows XP 32-bit.

MacBook: OS X Snow Leopard (10.6)
Back to top
View user's profile Send private message
Germ
Keeper of the BIG STICK


Joined: 30 Apr 2024
Posts: 12452
Location: Planet Earth

PostPosted: Sun Dec 06, 2024 12:45 am    Post subject: Reply with quote

Very nice, exiled.

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



_________________
Laptop: Mandriva 2024 PowerPack - 2.6.33.5-0.2mnb
Desktop: Mandriva 2024 Free - kernel 2.6.33.2-1mib
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
exiled
Jr. Member


Joined: 24 Nov 2024
Posts: 51
Location: ~/

PostPosted: Sun Dec 06, 2024 1:01 am    Post subject: Reply with quote

Thanks for the feedback everyone!! Very Happy



_________________
Currently: Frugalware, Fedora
Previously: Gentoo, Startcom, Debian, Sabayon, Mint, openSUSE, Mangaka Chu, #!CrunchBang
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum
All content © 2024-2009 - Usa Linux Users Group
This forum is powered by phpBB. © 2024-2009 phpBB Group
Theme created by phpBBStyles.com and modified by Crouse