diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2010-03-18 23:09:41 +0100 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2010-03-18 23:09:41 +0100 |
commit | 86a11ce7fc0181291115bb73f0215652106c261f (patch) | |
tree | 4bc76fb3a7a3c35eec3ee5d0c3a1cf58d1e98e4b | |
parent | 470e1ff2459727d3990ced413b616b459ae962df (diff) |
Added more missing code to the quotes module.
-rw-r--r-- | modules/quotes.py | 55 |
1 files changed, 49 insertions, 6 deletions
diff --git a/modules/quotes.py b/modules/quotes.py index 4f0816d..3592451 100644 --- a/modules/quotes.py +++ b/modules/quotes.py @@ -4,6 +4,7 @@ info = { 'description': 'Allows users to access a quote database.', } +import random from pyPgSQL import PgSQL class Quotes: @@ -15,19 +16,19 @@ class Quotes: return q def connect(self, db): - self.db = PgSQL.connect() + self.db = PgSQL.connect(database = 'fot') self.updatecount() def updatecount(self): cur = self.db.cursor() - cur.execute('select count(id) from quotes.quotes') + cur.execute('select count(id) from quotes') self.count = int(cur.fetchone()[0]) cur.close() def add(self, nick, quote): cur = self.db.cursor() - cur.execute('insert into quotes.quotes (nick, date, quote) values (%s, %s, %s)', (nick, int(time.time()), quote)) - cur.execute('select currval(\'quotes.quotes_id_seq\')') + cur.execute('insert into quotes (nick, date, quote) values (%s, %s, %s)', (nick, int(time.time()), quote)) + cur.execute('select currval(\'quotes_id_seq\')') lastrowid = cur.fetchone()[0] cur.close() self.db.commit() @@ -35,7 +36,7 @@ class Quotes: def delete(self, qid = None): cur = self.db.cursor() - cur.execute('delete from quotes.quotes where id = %s limit 1', (qid,)) + cur.execute('delete from quotes where id = %s limit 1', (qid,)) cur.close() self.db.commit() @@ -43,7 +44,7 @@ class Quotes: search = re.compile(search) if func == 'rfind' else search.lower() if func == 'efind' else [x.lower() for x in search.split(' ')] dosearch = lambda quote: search.search(quote) != None if func == 'rfind' else search in quote if func == 'efind' else len([x for x in search if x in quote]) if func == 'afind' else len([x for x in search if x in quote]) == len(search) cur = self.db.cursor() - cur.execute('select id, quote from quotes.quotes order by id asc') + cur.execute('select id, quote from quotes order by id asc') ret = [] i = 0 for q in cur.fetchall(): @@ -55,6 +56,39 @@ class Quotes: def get(self, qid = None): cur = self.db.cursor() + if not qid: + if not self.randseed: + random.seed() + self.randseed = True + n = random.randint(1, self.count) + cur.execute('select id, quote from quotes order by id asc limit 1 offset %s', (n - 1,)) + else: + qid = int(qid) + cur.execute('select quote from quotes where id = %s limit 1', (qid,)) + s = cur.fetchone() + cur.close() + if s and len(s) == 2: + qid, s = s + elif s: + s = s[0] + return (qid, self.format_quote(s)) if s else (None, None) + + def info(self, qid): + cur = self.db.cursor() + cur.execute('select id, nick, date, quote from quotes where id = %s limit 1', (qid,)) + s = cur.fetchone() + cur.close() + return s if s else None + + def range(self, p, pp): + cur = self.db.cursor() + cur.execute('select id from quotes order by id asc limit %s offset %s', (pp, (p - 1) * pp)) + s = cur.fetchall() + cur.close() + return [x[0] for x in s] + + def format_date(self, d): + return time.strftime('%c', time.localtime(d)) if d else '(Unknown date)' class IRCHandler(Quotes): def add(self, *args): @@ -153,6 +187,15 @@ class IRCHandler(Quotes): else: yield '\002Quote #%d\002 was added by %s at %s.' % (n, str(info[1]) if info[1] else '(unknown nick)', self.format_date(info[2]) if info[2] else '(unknown date)') + def random(self, *args): + 'Print a random quote. (Default)' + return self.output((Quotes.get(self), )) + + def stats(self, *args): + 'Print quote stats.' + yield 'There are \002%d\002 quotes in the database.' % (self.count) + + quote_handler = IRCHandler() class Module: |