[Tell] Rewrite the db part once again, I hope it works good like this

master
Sfan5 2014-02-01 18:07:36 +01:00
parent 6702ceb2e6
commit d8ac35f1e7
1 changed files with 31 additions and 28 deletions

59
tell.py
View File

@ -5,12 +5,16 @@ Copyright 2013, sfan5
""" """
import random import random
from tools import deprecated
from thread import start_new_thread, allocate_lock from thread import start_new_thread, allocate_lock
import sqlite3 import sqlite3
import time
import hashlib
tell_list = [] tell_list = []
telldb_lock = allocate_lock() tell_pending = []
tell_lastdiskwrite = 0
tell_lastlisthash = ''
tell_diskwriteinterval = 60 # seconds
def tell(phenny, input): def tell(phenny, input):
for x in phenny.bot.commands["high"].values(): for x in phenny.bot.commands["high"].values():
@ -25,26 +29,18 @@ def tell(phenny, input):
teller = input.nick teller = input.nick
target = arg.split(" ")[0] target = arg.split(" ")[0]
text = " ".join(arg.split(" ")[1:]) text = " ".join(arg.split(" ")[1:])
d = (teller, target, text)
if target.lower() == teller.lower(): if target.lower() == teller.lower():
return phenny.say("You can tell that to yourself") return phenny.say("You can tell that to yourself")
if target.lower() == phenny.nick.lower(): if target.lower() == phenny.nick.lower():
return phenny.say("I'm not dumb, you know?") return phenny.say("I'm not dumb, you know?")
telldb_lock.acquire() d = (teller, target, text, int(time.time()))
tell_list.append(d) tell_pending.append(("INSERT INTO tell (nick, tellee, msg, time) VALUES (?,?,?,?)", d))
db = sqlite3.connect("tell.sqlite")
c = db.cursor()
c.execute("INSERT INTO tell VALUES (?,?,?)", d)
c.close()
db.commit()
db.close()
telldb_lock.release()
response = "I'll pass that on when %s is around" % target response = "I'll pass that on when %s is around" % target
rand = random.random() rand = random.random()
if rand > 0.99: response = "yeah, yeah" if rand > 0.75: response = "yeah, yeah"
elif rand > 0.9: response = "yeah, sure, whatever" elif rand > 0.85: response = "yeah, sure, whatever"
phenny.reply(response) phenny.reply(response)
@ -52,18 +48,26 @@ tell.commands = ["tell"]
def checktell(phenny, input): def checktell(phenny, input):
for e in tell_list: for e in tell_list:
if e[1].lower() == input.nick.lower(): if e[2].lower() == input.nick.lower():
phenny.say("%s: <%s> %s" % (input.nick, e[0], e[2])) phenny.say("%s: %s <%s> %s" % (input.nick, time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(e[4])), e[1], e[3]))
telldb_lock.acquire()
tell_list.remove(e) tell_list.remove(e)
db = sqlite3.connect("tell.sqlite") tell_pending.append(("DELETE FROM tell WHERE id = ?", (e[0],)))
c = db.cursor() break
c.execute("DELETE FROM tell WHERE nick = ? AND channel = ? AND msg = ?", e)
c.close() if time.time() - tell_diskwriteinterval > tell_lastdiskwrite:
db.commit() tell_lastdiskwrite = time.time()
db.close() current_hash = hashlib.sha1(','.join(str(e[0]) for e in tell_list)).hexdigest()
telldb_lock.release() if current_hash == tell_lastlisthash:
return return
tell_lastlisthash = current_hash
db = sqlite3.connect("tell.sqlite")
c = db.cursor()
for tr in tell_pending:
c.execute(*tr)
c.close()
db.commit()
db.close()
tell_pending = []
def note(phenny, input): def note(phenny, input):
if input.sender.startswith('#'): if input.sender.startswith('#'):
@ -80,19 +84,18 @@ note_join.rule = r'.*'
note_join.event = 'JOIN' note_join.event = 'JOIN'
note_join.priority = 'low' note_join.priority = 'low'
#telldb_lock.acquire() # Just to be safe
db = sqlite3.connect("tell.sqlite") db = sqlite3.connect("tell.sqlite")
c = db.cursor() c = db.cursor()
c.execute("CREATE TABLE IF NOT EXISTS tell (nick text, channel text, msg text)") c.execute("CREATE TABLE IF NOT EXISTS tell (id INTEGER PRIMARY KEY, nick TEXT, tellee TEXT, msg TEXT, time INTEGER)")
c.execute("SELECT nick, channel, msg FROM tell") c.execute("SELECT * FROM tell")
while True: while True:
e = c.fetchone() e = c.fetchone()
if not e: if not e:
break break
tell_list.append(e) tell_list.append(e)
c.close() c.close()
db.commit()
db.close() db.close()
#telldb_lock.acquire()
if __name__ == '__main__': if __name__ == '__main__':
print __doc__.strip() print __doc__.strip()