From db797b4fef2830b91a100e16533443875d88f907 Mon Sep 17 00:00:00 2001 From: jkhsjdhjs Date: Sun, 8 Dec 2019 19:35:10 +0000 Subject: [PATCH] quotes: add quote searching --- bot/quotes.py | 40 +++++++++++++++++++++++++++++++++++----- bot/utils.py | 4 ++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/bot/quotes.py b/bot/quotes.py index a59abf1..c78c337 100644 --- a/bot/quotes.py +++ b/bot/quotes.py @@ -7,7 +7,7 @@ from irc3.utils import IrcString from psycopg2 import Error from . import DatabasePlugin, Bot -from .utils import parse_int +from .utils import is_int, parse_int class Quotes(DatabasePlugin): @@ -70,9 +70,10 @@ class Quotes(DatabasePlugin): """ cmd = args.get('') nick = args[''] - quote = ' '.join(args.get('')) + quote = args.get('') - if cmd and quote: + if (cmd == 'add' or cmd == 'del') and quote: + quote = ' '.join(quote) try: # Anybody can add if cmd == 'add': @@ -87,9 +88,37 @@ class Quotes(DatabasePlugin): print(ex) self.con.rollback() else: + query = None index = args.get('') + where = '' + + # search query support + if cmd or index and not is_int(index): + if cmd: + query = nick + nick = cmd + else: + query = index + + # if last entry in quotes list is a number, use it as the index + if quote and is_int(quote[-1]): + index = quote[-1] + quote = quote[:-1] + # else get random quote + else: + index = None + + if quote: + query += ' ' + ' '.join(quote) + else: + quote = ' '.join(quote) + values = [nick] + if query: + values.append(query) + where = 'AND item ILIKE \'%%\' || %s || \'%%\'' + if index: index = parse_int(index) if not index: @@ -119,13 +148,14 @@ class Quotes(DatabasePlugin): FROM ranked_quotes WHERE - lower(nick) LIKE lower(%s) + nick ILIKE %s + {where} ORDER BY {order} LIMIT 1 {offset} - '''.format(order=order, offset=offset), values) + '''.format(where=where, order=order, offset=offset), values) result = self.cur.fetchone() if result: diff --git a/bot/utils.py b/bot/utils.py index 01ba465..a3b3de9 100644 --- a/bot/utils.py +++ b/bot/utils.py @@ -12,6 +12,10 @@ def date_from_iso(date: str) -> datetime: return datetime.strptime(date, '%Y-%m-%dT%H:%M:%S.%fZ') +def is_int(val: str) -> bool: + return val.isdigit() or (val.startswith('+') or val.startswith('-')) and val[1:].isdigit() + + def parse_int(val: str, select: bool = True) -> Tuple[int, str]: try: val = int(val)