I finally got around to adding this script and entry to my openbox menu. When I first installed it, I made sure to change the commented portions so that it would show the Celsius data rather than the Fahrenheit (we're all metric-i-fied up here
). Anyway, I noticed that this only changed the 'current conditions' portion to metric, but the remaining high and low values for the next few days were funky. While it was showing a nice 'C' symbol, the values were obviously still metric (if it really was going to be 63C tomorrow there was NO way I was going to work!
).
So after gazing at the python script for a while trying to rassle back up my meager python skills, I decided to take a look at the XML file it was getting from Google and lo and behold Google was not providing consistently metric data. Notice the high and low values in the forecast_condition portion compared to the current conditions in this snippet from the xml:
<xml_api_reply version="1">
−
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
−
<forecast_information>
<city data="New Tecumseth, ON"/>
<postal_code data="alliston"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2009-04-28"/>
<current_date_time data="2009-04-29 02:41:39 +0000"/>
<unit_system data="US"/>
</forecast_information>
−
<current_conditions>
<condition data="Partly Cloudy"/>
<temp_f data="34"/>
<temp_c data="1"/>
<humidity data="Humidity: 100%"/>
<icon data="/ig/images/weather/partly_cloudy.png"/>
<wind_condition data="Wind: N at 2 mph"/>
</current_conditions>
−
<forecast_conditions>
<day_of_week data="Tue"/>
<low data="40"/>
<high data="64"/>
<icon data="/ig/images/weather/chance_of_rain.png"/>
<condition data="Scattered Showers"/>
</forecast_conditions>
So I made a slight modification to the script to do the conversion on the high and low values myself. And wonder of wonders it appears to work!
Here's the modified script. I don't know if this is a problem for anybody else running this thing with metric values, but it might be of use to someone. I've commented out the two lines I changed but left them in there for reference:
#!/usr/bin/python
import sys
import urllib
from string import maketrans
#from xml.sax import make_parser, handler
from xml.sax import handler, parseString
class ElementProcesser(handler.ContentHandler):
def startElement(self, name, attrs):
if name == "city":
print "<separator label='" + attrs["data"] + "' />"
elif name == "current_conditions":
print "<separator label='Current conditions' />"
elif name == "condition":
print "<item label='Weather: " + attrs["data"] + "' />"
elif name == "humidity":
print "<item label='" + attrs["data"] + "' />"
elif name == "wind_condition":
print "<item label='" + attrs["data"] + "' />"
elif name == "day_of_week":
print "<separator label='" + self.getDayOfWeek(attrs["data"]) + "' />"
#Celsius
elif name == "temp_c":
print "<item label='Temperature " + attrs["data"] + " C' />"
elif name == "low":
value = (int(attrs["data"])-32)*5/9
print "<item label='Minimum " + str(value) + " C' />"
# print "<item label='Minimum " + attrs["data"] + " C' />"
elif name == "high":
value = (int(attrs["data"])-32)*5/9
print "<item label='Maximum " + str(value) + " C' />"
# print "<item label='Maximum " + attrs["data"] + " C' />"
#Fahrenheit
#elif name == "temp_f":
#print "<item label='Temperature " + attrs["data"] + " F' />"
#elif name == "low":
#print "<item label='Minimun " + attrs["data"] + " F' />"
#elif name == "high":
#print "<item label='Maximun " + attrs["data"] + " F' />"
def endElement(self, name):
if name == "current_conditions":
print "<separator label='Forecast' />"
def startDocument(self):
print '<openbox_pipe_menu>'
def endDocument(self):
print '</openbox_pipe_menu>'
def getDayOfWeek(self,day):
#English
if day == "Mon":
return "Monday"
elif day == "Tue":
return "Tuesday"
elif day == "Wed":
return "Wednesday"
elif day == "Thu":
return "Thursday"
elif day == "Sat":
return "Saturday"
elif day == "Sun":
return "Sunday"
else:
return day
# You should use your local version of google to have the messages in your language and metric system
f = urllib.urlopen("http://www.google.ca/ig/api?weather="+sys.argv[1])
xml = f.read()
f.close()
#Avoid problems with non english characters
trans=maketrans("\xe1\xe9\xed\xf3\xfa","aeiou")
xml = xml.translate(trans)
#parser.parse("http://www.google.es/ig/api?weather="+sys.argv[1])
parseString(xml,ElementProcesser())