quotes: add quote searching
This commit is contained in:
parent
81c22bf6a4
commit
db797b4fef
|
@ -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('<cmd>')
|
||||
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:
|
||||
# Anybody can add
|
||||
if cmd == 'add':
|
||||
|
@ -87,9 +88,37 @@ class Quotes(DatabasePlugin):
|
|||
print(ex)
|
||||
self.con.rollback()
|
||||
else:
|
||||
query = None
|
||||
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]
|
||||
|
||||
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:
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user