weather plugin now remembers locations

This commit is contained in:
Ryan Hitchman 2009-04-02 22:18:43 -06:00
parent 73a43fc9f0
commit df7f39a1bb
2 changed files with 43 additions and 6 deletions

View File

@ -19,4 +19,3 @@ def bible(inp):
text = text[:text.rfind(' ', 0, 400)] + '...'
return text

View File

@ -1,19 +1,52 @@
#!/usr/bin/python
"weather, thanks to google"
from __future__ import with_statement
import os
import codecs
import thread
import urllib
from lxml import etree
import hook
lock = thread.allocate_lock()
stalk = {}
def load_stalk(filename, mtimes={}):
if not os.path.exists(filename):
return {}
mtime = os.stat(filename).st_mtime
if mtimes.get(filename, 0) != mtime:
mtimes[filename] = mtime
return dict(x.split(None, 1) for x in
codecs.open(filename, 'r', 'utf-8'))
def save_stalk(filename, houses):
out = codecs.open(filename, 'w', 'utf-8')
out.write('\n'.join('%s %s' % x for x in sorted(houses.iteritems()))) #_heh_
out.flush()
out.close()
@hook.command
def weather(bot, input):
".weather <location> -- queries the google weather API for weather data"
global stalk
if not input.inp.strip(): # blank line
return "welp"
filename = os.path.join(bot.persist_dir, 'weather')
if not stalk:
with lock:
stalk = load_stalk(filename)
nick = input.nick.lower()
loc = input.inp.strip().lower()
if not loc: # blank line
loc = stalk.get(nick, '')
if not loc:
return weather.__doc__
data = urllib.urlencode({'weather':input.inp.encode('utf-8')})
data = urllib.urlencode({'weather':loc.encode('utf-8')})
url = 'http://www.google.com/ig/api?' + data
w = etree.parse(url).find('weather')
@ -26,5 +59,10 @@ def weather(bot, input):
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
bot.reply('%(city)s: %(condition)s, %(temp_f)sF/%(temp_c)sC (H:%(high)sF'\
', L:%(low)sF), %(humidity)s, %(wind_condition)s.' % info)
if loc != stalk.get(nick, ''):
with lock:
stalk[nick] = loc
save_stalk(filename, stalk)