Handle quote edit and removal

master
Nikola Forró 7 years ago
parent 19209f1f76
commit ad0c3294db

@ -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<user>.+)\s+-->\s+
Sweet!\s+Thanks\s+for\s+the\s+quote!\s+
@ -28,13 +29,28 @@ QUOTE_ADDED_PATTERN = re.compile(r'''^
\[(?P<game>.+)\]\s+
\[(?P<date>.+)\]$''', re.VERBOSE)
# TODO: quote_edited, quote_removed
# $username --> Successfully edited Quote #$id: $response
QUOTE_EDITED_PATTERN = re.compile(r'''^
(?P<user>.+)\s+-->\s+
Successfully\s+edited\s+Quote\s+
\#(?P<id>\d+):\s+
(?P<text>.+)\s+
\[(?P<game>.+)\]\s+
\[(?P<date>.+)\]$''', re.VERBOSE)
# $username --> Successfully deleted Quote #$id.
QUOTE_REMOVED_PATTERN = re.compile(r'''^
(?P<user>.+)\s+-->\s+
Successfully\s+deleted\s+Quote\s+
\#(?P<id>\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()

Loading…
Cancel
Save