From 8c68bbd1281455347b41fa675e5677a8cf42a210 Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Mon, 1 Feb 2010 01:33:06 -0700 Subject: [PATCH] rewrite weather.py to use sqlite db instead of flat files --- plugins/quote.py | 3 +-- plugins/tell.py | 3 +-- plugins/weather.py | 51 ++++++++++++---------------------------------- 3 files changed, 15 insertions(+), 42 deletions(-) diff --git a/plugins/quote.py b/plugins/quote.py index 6c0c333..1481a2a 100644 --- a/plugins/quote.py +++ b/plugins/quote.py @@ -1,6 +1,5 @@ import random import re -import sqlite3 import time from util import hook @@ -49,7 +48,7 @@ def quote(bot, input): nick, msg = add.groups() try: add_quote(conn, chan, nick, input.nick, msg) - except sqlite3.IntegrityError: + except conn.IntegrityError: return "message already stored, doing nothing." return "quote added." elif retrieve: diff --git a/plugins/tell.py b/plugins/tell.py index 48d770c..ba93f4d 100644 --- a/plugins/tell.py +++ b/plugins/tell.py @@ -1,7 +1,6 @@ " tell.py: written by sklnd in July 2009" " 2010.01.25 - modified by Scaevolus" -import sqlite3 import time from util import hook, timesince @@ -82,7 +81,7 @@ def tell(bot, input): "time) values(?,?,?,?,?)", (user_to, user_from, message, input.chan, time.time())) conn.commit() - except sqlite3.IntegrityError: + except conn.IntegrityError: return "Message has already been queued." return "I'll pass that along." diff --git a/plugins/weather.py b/plugins/weather.py index 9068b8e..d487319 100644 --- a/plugins/weather.py +++ b/plugins/weather.py @@ -1,54 +1,29 @@ "weather, thanks to google" -import os -import codecs -import thread -import urllib from lxml import etree +import urllib from util 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.strip().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 [dontsave] -- queries the google weather API for weather data" - global stalk - - filename = os.path.join(bot.persist_dir, 'weather') - if not stalk: - with lock: - stalk = load_stalk(filename) - - nick = input.nick.lower() loc = input.inp + dontsave = loc.endswith(" dontsave") if dontsave: loc = loc[:-9].strip().lower() + + conn = bot.get_db_connection(input.server) + conn.execute("create table if not exists weather(nick primary key, loc)") + if not loc: # blank line - loc = stalk.get(nick, '') + loc = conn.execute("select loc from weather where nick=lower(?)", + (input.nick,)).fetchone() if not loc: return weather.__doc__ + loc = loc[0] data = urllib.urlencode({'weather': loc.encode('utf-8')}) url = 'http://www.google.com/ig/api?' + data @@ -66,7 +41,7 @@ def weather(bot, input): input.reply('%(city)s: %(condition)s, %(temp_f)sF/%(temp_c)sC (H:%(high)sF'\ ', L:%(low)sF), %(humidity)s, %(wind_condition)s.' % info) - if not dontsave and loc != stalk.get(nick, ''): - with lock: - stalk[nick] = loc - save_stalk(filename, stalk) + if input.inp and not dontsave: + conn.execute("insert or replace into weather(nick, loc) values (?,?)", + (input.nick.lower(), loc)) + conn.commit()