From 24ae0dcce0b7d72a90eaffb90f83fb31b1b5e1b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Tue, 3 Jul 2018 12:36:33 +0200 Subject: [PATCH] Improve !sync command and rename it to !syncquotes --- README.md | 3 +-- clients/twitch.py | 8 ++++---- commands.py | 28 +++++++--------------------- settings.cfg.example | 3 +++ 4 files changed, 15 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 13959ad..34990a1 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,5 @@ using [HTTP API](https://gitea.brno.mraveniste.cc/turbotraktor/ladylilia.com/src * `!lastquote` - requests the most recent quote * `!findquote PATTERN` - searches for quotes matching `PATTERN` and in case multiple matches are found, requests one of them randomly - `PATTERN` has to be at least 3 characters long and it can be enclosed in double quotes in case it contains spaces -* `!sync [SINCE]` - performs synchronization of the quotes database with Twitch VODs recorded since `SINCE` - - if `SINCE` is not specified, date of the most recent quote is used +* `!syncquotes` - performs synchronization of the quotes database with Twitch VODs - only master user is allowed to use this command diff --git a/clients/twitch.py b/clients/twitch.py index 16e60ed..2b32aec 100644 --- a/clients/twitch.py +++ b/clients/twitch.py @@ -45,7 +45,7 @@ class TwitchClient(irc.bot.SingleServerIRCBot): self.supported_commands = [ (re.compile(r'^!lastquote$'), self._do_lastquote), (re.compile(r'^!findquote\s+(?P")?(?P.+)(?(q)")$'), self._do_findquote), - (re.compile(r'^!sync(\s+(?P.+))?$'), self._do_sync), + (re.compile(r'^!syncquotes$'), self._do_syncquotes), (re.compile(r'^!(bella(gram|pics)|insta(gram|bella))$'), self._do_bellagram), (re.compile(r'^!yt\s+(?P")?(?P.+)(?(q)")$'), self._do_yt), ] @@ -137,13 +137,13 @@ class TwitchClient(irc.bot.SingleServerIRCBot): else: send_response('!quote {0}'.format(quote['id'])) - def _do_sync(self, tags, send_response, since=None, **kwargs): + def _do_syncquotes(self, tags, send_response, **kwargs): master_user_id = self.config['Twitch'].getint('master_user_id') - if int(tags['user-id']) != self.master_user_id: + if int(tags['user-id']) != master_user_id: respond('Sorry @{0}, you are not allowed to do this'.format(tags['display-name'])) return try: - messages = self.commands.get_twitch_messages(since) + messages = self.commands.get_twitch_messages() except CommandError as e: self.logger.error('Failed to get Twitch messages: %s', e) else: diff --git a/commands.py b/commands.py index 6f0177d..05717d0 100644 --- a/commands.py +++ b/commands.py @@ -44,29 +44,15 @@ class Commands(object): except requests.exceptions.HTTPError as e: raise CommandError(e) - def get_twitch_messages(self, since): - if since is None: - try: - quotes = self._get_quotes(dict( - sort_by='id', - sort_order='desc', - page_size=1)).json() - quote = quotes.pop(0) - except requests.exceptions.HTTPError: - raise CommandError(e) - except IndexError: - raise CommandError(e) - else: - since = quote['date'] - api_url = self.config['Twitch'].get('api_url') - client_id = self.config['Twitch'].get('client_id') - channel_id = self.config['Twitch'].getint('channel_id') - since = dateutil.parser.parse(since).date() - twitch = Twitch(api_url, client_id) + def get_twitch_messages(self): + api_url = self.config['Twitch Logs'].get('api_url') + params = dict(commenter='bellateeny', term='quote') try: - return twitch.get_messages(channel_id, since) - except TwitchError as e: + r = requests.get('{0}/search'.format(api_url), params=params) + r.raise_for_status() + except requests.exceptions.HTTPError: raise CommandError(e) + return [m.get('message_body', '') for m in r.json()] def last_quote(self): try: diff --git a/settings.cfg.example b/settings.cfg.example index f0a552b..b809962 100644 --- a/settings.cfg.example +++ b/settings.cfg.example @@ -28,3 +28,6 @@ channel_ids = UC5970RJMoEcRNZl0MNp8tlQ,UCHNjavmkFUf2n0uzWSgj16w [Quotes] api_url = https://ladylilia.com/quotes/api api_key = __QUOTES_API_KEY__ + +[Twitch Logs] +api_url = https://ladylilia.com/twitch-logs/api