This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/weather.py

96 lines
3.2 KiB
Python
Raw Normal View History

2011-11-20 10:23:31 +01:00
"weather, thanks to google"
from util import hook, http
2012-05-08 21:54:55 +02:00
2012-04-26 03:12:04 +02:00
def fahrenheit_to_celcius(f):
2012-04-26 03:17:40 +02:00
return int(round((int(f) - 32) / 1.8, 0))
2012-02-29 07:09:19 +01:00
2012-05-08 21:54:55 +02:00
@hook.command(autohelp=False)
2012-02-29 07:09:19 +01:00
def forecast(inp, nick='', server='',
reply=None, db=None, notice=None, say=None):
".forecast <location> [dontsave] -- Gets a weather forecast" \
" for <location> from Google."
loc = inp
dontsave = loc.endswith(" dontsave")
if dontsave:
loc = loc[:-9].strip().lower()
db.execute("create table if not exists weather(nick primary key, loc)")
2012-02-29 07:09:19 +01:00
if not loc:
loc = db.execute("select loc from weather where nick=lower(?)",
(nick,)).fetchone()
if not loc:
notice(forecast.__doc__)
return
loc = loc[0]
w = http.get_xml('http://www.google.com/ig/api', weather=loc)
w = w.find('weather')
if w.find('problem_cause') is not None:
notice("Couldn't fetch weather data for '%s', try using a zip or " \
"postal code." % inp)
return
city = w.find('forecast_information/city').get('data')
out = "%s: " % city
for elem in w.findall('forecast_conditions'):
info = dict((e.tag, e.get('data')) for e in elem)
2012-05-13 10:13:07 +02:00
info['high'] = elem.find('high').get('data')
info['low'] = elem.find('low').get('data')
info['high_c'] = fahrenheit_to_celcius(elem.find('high').get('data'))
info['low_c'] = fahrenheit_to_celcius(elem.find('low').get('data'))
out += '\x02%(day_of_week)s\x02: %(condition)s (High: %(high)sF' \
'/%(high_c)sC) (Low: %(low)sF/%(low_c)sC) ' % info
return out
2011-11-20 10:23:31 +01:00
2012-02-29 07:09:19 +01:00
2011-11-20 10:23:31 +01:00
@hook.command(autohelp=False)
def weather(inp, nick='', server='', reply=None, db=None, notice=None):
2012-02-29 07:09:19 +01:00
".weather <location> [dontsave] -- Gets weather data"\
" for <location> from Google."
2011-11-20 10:23:31 +01:00
loc = inp
dontsave = loc.endswith(" dontsave")
if dontsave:
loc = loc[:-9].strip().lower()
db.execute("create table if not exists weather(nick primary key, loc)")
if not loc:
2011-11-20 10:23:31 +01:00
loc = db.execute("select loc from weather where nick=lower(?)",
(nick,)).fetchone()
if not loc:
notice(weather.__doc__)
return
loc = loc[0]
w = http.get_xml('http://www.google.com/ig/api', weather=loc)
w = w.find('weather')
if w.find('problem_cause') is not None:
notice("Couldn't fetch weather data for '%s', try using a zip or " \
"postal code." % inp)
return
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')
2012-04-26 03:12:04 +02:00
info['high_c'] = fahrenheit_to_celcius(info['high'])
info['low_c'] = fahrenheit_to_celcius(info['low'])
2011-11-20 10:23:31 +01:00
2012-04-26 12:15:33 +02:00
reply('%(city)s: %(condition)s, %(temp_f)sF/%(temp_c)sC (High: %(high)sF' \
'/%(high_c)sC) (Low: %(low)sF/%(low_c)sC), %(humidity)s, ' \
2012-04-26 03:12:04 +02:00
'%(wind_condition)s.' % info)
2011-11-20 10:23:31 +01:00
if inp and not dontsave:
db.execute("insert or replace into weather(nick, loc) values (?,?)",
(nick.lower(), loc))
db.commit()