View previous topic :: View next topic |
Author |
Message |
exiled Jr. Member

Joined: 24 Nov 2025 Posts: 51 Location: ~/
|
Posted: Sat Dec 05, 2025 4:19 pm Post subject: Weather Conditions Script: |
|
|
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" |
_________________ Currently: Frugalware, Fedora
Previously: Gentoo, Startcom, Debian, Sabayon, Mint, openSUSE, Mangaka Chu, #!CrunchBang
|
|
Back to top |
|
lynch Moderator

Joined: 15 Nov 2025 Posts: 2659 Location: The Diamond State
|
|
Back to top |
|
VHockey86 Advanced Member

Joined: 12 Dec 2025 Posts: 988 Location: Rochester
|
Posted: Sat Dec 05, 2025 11:45 pm Post subject: |
|
|
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: 2025-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 |
|
Germ Keeper of the BIG STICK

Joined: 30 Apr 2025 Posts: 12452 Location: Planet Earth
|
Posted: Sun Dec 06, 2025 12:45 am Post subject: |
|
|
Very nice, exiled.
Code: | [germ@localhost ~]$ ./weather.sh
Enter a valid 5-digit zipcode: 74016
Date: 2025-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 2025 PowerPack - 2.6.33.5-0.2mnb
Desktop: Mandriva 2025 Free - kernel 2.6.33.2-1mib
|
|
Back to top |
|
exiled Jr. Member

Joined: 24 Nov 2025 Posts: 51 Location: ~/
|
|
Back to top |
|
|