quotes: add quote searching

This commit is contained in:
jkhsjdhjs 2019-12-08 19:35:10 +00:00
parent 81c22bf6a4
commit db797b4fef
2 changed files with 39 additions and 5 deletions

View File

@ -7,7 +7,7 @@ from irc3.utils import IrcString
from psycopg2 import Error from psycopg2 import Error
from . import DatabasePlugin, Bot from . import DatabasePlugin, Bot
from .utils import parse_int from .utils import is_int, parse_int
class Quotes(DatabasePlugin): class Quotes(DatabasePlugin):
@ -70,9 +70,10 @@ class Quotes(DatabasePlugin):
""" """
cmd = args.get('<cmd>') cmd = args.get('<cmd>')
nick = args['<nick>'] nick = args['<nick>']
quote = ' '.join(args.get('<quote>')) quote = args.get('<quote>')
if cmd and quote: if (cmd == 'add' or cmd == 'del') and quote:
quote = ' '.join(quote)
try: try:
# Anybody can add # Anybody can add
if cmd == 'add': if cmd == 'add':
@ -87,9 +88,37 @@ class Quotes(DatabasePlugin):
print(ex) print(ex)
self.con.rollback() self.con.rollback()
else: else:
query = None
index = args.get('<index>') index = args.get('<index>')
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] values = [nick]
if query:
values.append(query)
where = 'AND item ILIKE \'%%\' || %s || \'%%\''
if index: if index:
index = parse_int(index) index = parse_int(index)
if not index: if not index:
@ -119,13 +148,14 @@ class Quotes(DatabasePlugin):
FROM FROM
ranked_quotes ranked_quotes
WHERE WHERE
lower(nick) LIKE lower(%s) nick ILIKE %s
{where}
ORDER BY ORDER BY
{order} {order}
LIMIT LIMIT
1 1
{offset} {offset}
'''.format(order=order, offset=offset), values) '''.format(where=where, order=order, offset=offset), values)
result = self.cur.fetchone() result = self.cur.fetchone()
if result: if result:

View File

@ -12,6 +12,10 @@ def date_from_iso(date: str) -> datetime:
return datetime.strptime(date, '%Y-%m-%dT%H:%M:%S.%fZ') 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]: def parse_int(val: str, select: bool = True) -> Tuple[int, str]:
try: try:
val = int(val) val = int(val)