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