Sqlite3 database for seen.py

master
Sfan5 2013-10-22 20:35:33 +02:00
parent 7657df6794
commit bbc86c98c5
1 changed files with 50 additions and 31 deletions

81
seen.py
View File

@ -2,7 +2,7 @@
""" """
seen.py - Phenny Seen Module seen.py - Phenny Seen Module
Copyright 2008, Sean B. Palmer, inamidst.com Copyright 2008, Sean B. Palmer, inamidst.com
Modified by Sfan5 2013 Modified by sfan5 2013
Licensed under the Eiffel Forum License 2. Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/ http://inamidst.com/phenny/
@ -10,43 +10,62 @@ http://inamidst.com/phenny/
import time import time
from tools import deprecated from tools import deprecated
import sqlite3
def opendb():
db = sqlite3.connect("seen.sqlite")
c = db.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS seen (nick text, channel text, time int)''')
c.close()
return db
def seen(phenny, input): def seen(phenny, input):
""".seen <nick> - Reports when <nick> was last seen.""" """.seen <nick> - Reports when <nick> was last seen."""
for x in phenny.bot.commands["high"].values(): for x in phenny.bot.commands["high"].values():
if x[0].__name__ == "aa_hook": if x[0].__name__ == "aa_hook":
if x[0](phenny, input): if x[0](phenny, input):
return # Abort function return # Abort function
nick = input.group(2) nick = input.group(2)
if not nick: if not nick:
return phenny.reply("Need a nickname to search for...") return phenny.reply("Need a nickname to search for...")
nick = nick.lower() nick = nick.lower()
print("[LOG]: %s queried Seen Result for %s" % (input.nick,nick))
if not hasattr(phenny, 'seen'):
return phenny.reply("?")
if phenny.seen.has_key(nick): print("[LOG]: %s queried Seen Result for %s" % (input.nick,nick))
channel, t = phenny.seen[nick]
t = time.strftime('%Y-%m-%d %H:%M:%S UTC', time.gmtime(t)) db = opendb()
c = db.cursor()
c.execute("SELECT channel, time FROM seen WHERE nick = ?", (nick,))
r = c.fetchone()
c.close()
db.close()
if r:
channel, t = r[0], r[1]
t = time.strftime('%Y-%m-%d %H:%M:%S UTC', time.gmtime(t))
msg = "%s was last seen at %s on %s" % (nick, t, channel)
phenny.reply(msg)
else:
phenny.reply("Sorry, I haven't seen %s around." % nick)
msg = "%s was last seen at %s on %s" % (nick, t, channel)
phenny.reply(msg)
else: phenny.reply("Sorry, I haven't seen %s around." % nick)
seen.rule = (['seen'], r'(\S+)') seen.rule = (['seen'], r'(\S+)')
@deprecated def note(phenny, input):
def f_note(self, origin, match, args): db = opendb()
def note(self, origin, match, args): if input.sender.startswith('#'):
if not hasattr(self.bot, 'seen'): c = db.cursor()
self.bot.seen = {} c.execute("SELECT * FROM seen WHERE nick = ?", (input.nick.lower(),))
if origin.sender.startswith('#'): if c.fetchone() != None:
self.seen[origin.nick.lower()] = (origin.sender, time.time()) d = (input.sender, int(time.time()), input.nick.lower())
c.execute('UPDATE seen SET channel = ?, time = ? WHERE nick = ?', d)
else:
d = (input.nick.lower(), input.sender, int(time.time()))
c.execute('INSERT INTO seen VALUES (?,?,?)', d)
db.commit()
c.close()
db.close()
try: note(self, origin, match, args) note.rule = r'.*'
except Exception, e: print e note.priority = 'low'
f_note.rule = r'(.*)'
f_note.priority = 'low'
if __name__ == '__main__': if __name__ == '__main__':
print __doc__.strip() print __doc__.strip()