improve quote db schema, add channel random quotes

This commit is contained in:
Ryan Hitchman 2010-01-22 04:19:16 -07:00
parent 9f0c2fe9f1
commit 26515cf14b
1 changed files with 40 additions and 23 deletions

View File

@ -10,48 +10,67 @@ dbname = "skybot.db"
def db_connect(db):
conn = sqlite3.connect(db)
conn.execute("create table if not exists quote"
"(server, nick, adder, msg unique, time real)")
conn.execute('''create table if not exists quotes
(server, chan, nick, add_nick, msg, time real, deleted default 0,
primary key (server, chan, nick, msg))''')
conn.commit()
return conn
def add_quote(conn, server, adder, nick, msg):
def add_quote(conn, server, chan, nick, add_nick, msg):
now = time.time()
print repr((conn, server, adder, nick, msg, time))
conn.execute("insert or fail into quote(server, nick, adder, msg, time) "
"values(?,?,?,?,?)", (server, nick, adder, msg, now))
print repr((conn, server, add_nick, nick, msg, time))
conn.execute('''insert or fail into quotes (server, chan, nick, add_nick,
msg, time) values(?,?,?,?,?,?)''',
(server, chan, nick, add_nick, msg, now))
conn.commit()
def get_quotes(conn, server, nick):
return conn.execute("select time, nick, msg from quote where server=?"
" and nick LIKE ? order by time", (server, nick)).fetchall()
# note: nick_name matches nick-name -- _ in a LIKE indicates any character
# this will probably be unnoticeable, and the fix is easy enough
def get_quotes_by_nick(conn, server, chan, nick):
return conn.execute("select time, nick, msg from quotes where deleted!=1 "
"and server=? and chan=? and lower(nick)=lower(?) order by time",
(server, chan, nick)).fetchall()
def get_quotes_by_chan(conn, server, chan):
return conn.execute("select time, nick, msg from quotes where deleted!=1 "
"and server=? and chan=? order by time", (server, chan)).fetchall()
def format_quote(q, num, n_quotes):
ctime, nick, msg = q
return "[%d/%d] %s <%s> %s" % (num, n_quotes,
time.strftime("%Y-%m-%d", time.gmtime(ctime)), nick, msg)
@hook.command('q')
@hook.command
def quote(bot, input):
".q/.quote <nick> [#n]/.quote add <nick> <msg> -- retrieves " \
"random/numbered quote, adds quote"
".q/.quote <nick/#chan> [#n]/.quote add <nick> <msg> -- gets " \
"random or [#n]th quote by <nick> or from <#chan>/adds quote"
dbpath = os.path.join(bot.persist_dir, dbname)
conn = db_connect(dbpath)
try:
add = re.match(r"add\s+<?[^\w]?(\S+?)>?\s+(.*)", input.inp, re.I)
retrieve = re.match(r"(\S+)(?:\s+#?(\d+))?", input.inp)
retrieve = re.match(r"(\S+)(?:\s+#?(-?\d+))?", input.inp)
chan = input.chan
if add:
nick, msg = add.groups()
try:
add_quote(conn, input.server, input.nick, nick, msg)
except sqlite3.IntegrityError: # message already in DB
add_quote(conn, input.server, chan, nick, input.nick, msg)
except sqlite3.IntegrityError:
return "message already stored, doing nothing."
return "quote added."
elif retrieve:
nick, num = retrieve.groups()
select, num = retrieve.groups()
by_chan = False
if select.startswith('#'):
by_chan = True
quotes = get_quotes_by_chan(conn, input.server, select)
else:
quotes = get_quotes_by_nick(conn, input.server, chan, select)
quotes = get_quotes(conn, input.server, nick)
n_quotes = len(quotes)
if not n_quotes:
@ -63,16 +82,14 @@ def quote(bot, input):
if num:
if num > n_quotes:
return "I only have %d quote%s for %s" % (n_quotes,
('s', '')[n_quotes == 1], nick)
('s', '')[n_quotes == 1], select)
else:
selected_quote = quotes[num - 1]
else:
num = random.randint(1, n_quotes)
selected_quote = quotes[num - 1]
ctime, nick, msg = selected_quote
return "[%d/%d] %s <%s> %s" % (num, n_quotes,
time.strftime("%Y-%m-%d", time.gmtime(ctime)), nick, msg)
return format_quote(selected_quote, num, n_quotes)
else:
return quote.__doc__
finally: