From ad0c3294db8251dc6d2157e38a4f6a73fb9b2e5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Mon, 30 Apr 2018 17:04:17 +0200 Subject: [PATCH] Handle quote edit and removal --- bot.py | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/bot.py b/bot.py index 37604f3..5ffd983 100644 --- a/bot.py +++ b/bot.py @@ -20,6 +20,7 @@ log.addHandler(logging.StreamHandler()) log.setLevel(logging.DEBUG if bool(int(os.getenv('DEBUG', 0))) else logging.INFO) +# $username --> Sweet! Thanks for the quote! #$id: $response QUOTE_ADDED_PATTERN = re.compile(r'''^ (?P.+)\s+-->\s+ Sweet!\s+Thanks\s+for\s+the\s+quote!\s+ @@ -28,13 +29,28 @@ QUOTE_ADDED_PATTERN = re.compile(r'''^ \[(?P.+)\]\s+ \[(?P.+)\]$''', re.VERBOSE) -# TODO: quote_edited, quote_removed +# $username --> Successfully edited Quote #$id: $response +QUOTE_EDITED_PATTERN = re.compile(r'''^ + (?P.+)\s+-->\s+ + Successfully\s+edited\s+Quote\s+ + \#(?P\d+):\s+ + (?P.+)\s+ + \[(?P.+)\]\s+ + \[(?P.+)\]$''', re.VERBOSE) + +# $username --> Successfully deleted Quote #$id. +QUOTE_REMOVED_PATTERN = re.compile(r'''^ + (?P.+)\s+-->\s+ + Successfully\s+deleted\s+Quote\s+ + \#(?P\d+)\.$''', re.VERBOSE) class TwitchBot(irc.bot.SingleServerIRCBot): def __init__(self): self.patterns = [ (QUOTE_ADDED_PATTERN, self.add_quote), + (QUOTE_EDITED_PATTERN, self.edit_quote), + (QUOTE_REMOVED_PATTERN, self.remove_quote), ] self.commands = [ (re.compile(r'^!lastquote$'), self.last_quote), @@ -90,7 +106,7 @@ class TwitchBot(irc.bot.SingleServerIRCBot): return r.json() def delete(self, id): - r = requests.delete('{0}/quote/{1}'.format(self.api_url, id), + r = requests.delete('{0}/quotes/{1}'.format(self.api_url, id), headers={'X-Quotes-API-Key': self.api_key}) r.raise_for_status() return r.json() @@ -174,6 +190,26 @@ class TwitchBot(irc.bot.SingleServerIRCBot): except requests.exceptions.HTTPError as e: log.error('Failed to add quote: %s', str(e)) + def edit_quote(self, connection, tags, user, id, text, game, date, **kwargs): + if text[0] == text[-1] == '"': + text = text[1:-1] + log.info('Editing quote %s: %s', id, text) + try: + self.post(dict( + id=int(id), + date=dateutil.parser.parse(date, dayfirst=True).date().isoformat(), + game=game, + text=text)) + except requests.exceptions.HTTPError as e: + log.error('Failed to edit quote: %s', str(e)) + + def remove_quote(self, connection, tags, user, id, **kwargs): + log.info('Removing quote %s', id) + try: + self.delete(int(id)) + except requests.exceptions.HTTPError as e: + log.error('Failed to remove quote: %s', str(e)) + def main(): bot = TwitchBot()