h/plugins/weather.py

29 lines
1.0 KiB
Python
Raw Normal View History

2009-03-15 03:06:36 +00:00
#!/usr/bin/python
2008-06-09 11:59:56 +00:00
"weather, thanks to google"
2009-03-15 03:06:36 +00:00
import urllib
from xml.etree import ElementTree
2008-06-09 11:59:56 +00:00
2009-03-15 03:06:36 +00:00
#command: weather
2008-06-09 11:59:56 +00:00
def weather(bot, input):
2009-03-15 04:14:07 +00:00
".weather <location> -- queries the google weather API for weather data"
2009-03-15 03:06:36 +00:00
if not input.inp.strip(): # blank line
return "welp"
data = urllib.urlencode({'weather':input.inp.encode('utf-8')})
url = 'http://www.google.com/ig/api?' + data
w = ElementTree.parse(urllib.urlopen(url)).find('weather')
if w.find('problem_cause') is not None:
return "Couldn't fetch weather data for '%s', try using a zip or " \
"postal code." % input.inp
info = dict((e.tag, e.get('data')) for e in w.find('current_conditions'))
info['city'] = w.find('forecast_information/city').get('data')
info['high'] = w.find('forecast_conditions/high').get('data')
info['low'] = w.find('forecast_conditions/low').get('data')
return '%(city)s: %(condition)s, %(temp_f)sF/%(temp_c)sC (H:%(high)sF, ' \
'L:%(low)sF), %(humidity)s, %(wind_condition)s.' % info