From 05e8ab7f0408077ffd161c37ca42b942ec82399b Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 23 Feb 2010 21:19:43 +0100 Subject: Imported quote handler to modules.quote. This code is old and might need some minor cleaning. --- modules/quotes.py | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 4 deletions(-) (limited to 'modules') diff --git a/modules/quotes.py b/modules/quotes.py index 873f0f3..30a2c25 100644 --- a/modules/quotes.py +++ b/modules/quotes.py @@ -4,10 +4,110 @@ info = { 'description': 'Allows users to access a quote database.', } -import sys -sys.path.insert(0, '/home/snakebite/py') -from quotelib import IRCHandler as Quote_IRCHandler -quote_handler = Quote_IRCHandler() +from pyPgSQL import PgSQL + +class Quotes: + def __init__(self, db = None): + self.randseed = False + self.connect(db) + + def format_quote(self, q): + return q + + def connect(self, db): + self.db = PgSQL.connect() + self.updatecount() + + def updatecount(self): + cur = self.db.cursor() + cur.execute('select count(id) from quotes.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\')') + lastrowid = cur.fetchone()[0] + cur.close() + self.db.commit() + return lastrowid + + def delete(self, qid = None): + cur = self.db.cursor() + cur.execute('delete from quotes.quotes where id = %s limit 1', (qid,)) + cur.close() + self.db.commit() + + def find(self, search, func = None): + 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') + ret = [] + i = 0 + for q in cur.fetchall(): + i += 1 + if dosearch(q[1].lower()): + ret.append((q[0], self.format_quote(q[1]))) + cur.close() + return ret + + def get(self, qid = None): + cur = self.db.cursor() + +class IRCHandler(Quotes): + def add(self, *args): + 'Add a quote to the database.' + quote = ' '.join(args) + qid = Quotes.add(self, self.nick, quote) + yield '\002Quote #%d\002 has been added' % (qid) + + def format_quote(self, quote): + return quote + + def output(self, quotes): + for n, q in quotes: + yield '\002Quote #%d:\002 %s' % (n, q) + + def _find(self, args, func = 'find'): + if not len(args): + yield 'Quote: Missing search pattern.' + return + args = ' '.join(args) + results = Quotes.find(self, args, func) + leftovers = [] + if len(results) > 1: + yield 'Quote: Found \002%d\002 results for "%s"%s' % (len(results), args, ', here are the last two:' if len(results) > 2 else ':' if len(results) else '') + if len(results): + if len(results) > 2: + leftovers, results = results[:-2], results[-2:] + for l in self.output(results): + yield l + if leftovers: + yield 'Other quotes: %s' % ', '.join(['#%d' % x[0] for x in leftovers]) + + def find(self, *args): + 'Find quotes containing all given words.' + return self._find(args) + + def afind(self, *args): + 'Find quotes containing any given word.' + return self._find(args, 'afind') + + def efind(self, *args): + 'Find quotes containing the exact given phrase.' + return self._find(args, 'efind') + + def rfind(self, *args): + 'Find quotes using regexp search.' + return self._find(args, 'rfind') + + def get(self, *args): + 'Print a quote.' + if not len(args): + +quote_handler = IRCHandler() class Module: def __init__(self, bot): -- cgit v1.2.3