Improve !sync command and rename it to !syncquotes

master
Nikola Forró 6 years ago
parent e2ef6c5588
commit 24ae0dcce0

@ -14,6 +14,5 @@ using [HTTP API](https://gitea.brno.mraveniste.cc/turbotraktor/ladylilia.com/src
* `!lastquote` - requests the most recent quote * `!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 * `!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 - `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` * `!syncquotes` - performs synchronization of the quotes database with Twitch VODs
- if `SINCE` is not specified, date of the most recent quote is used
- only master user is allowed to use this command - only master user is allowed to use this command

@ -45,7 +45,7 @@ class TwitchClient(irc.bot.SingleServerIRCBot):
self.supported_commands = [ self.supported_commands = [
(re.compile(r'^!lastquote$'), self._do_lastquote), (re.compile(r'^!lastquote$'), self._do_lastquote),
(re.compile(r'^!findquote\s+(?P<q>")?(?P<filter>.+)(?(q)")$'), self._do_findquote), (re.compile(r'^!findquote\s+(?P<q>")?(?P<filter>.+)(?(q)")$'), self._do_findquote),
(re.compile(r'^!sync(\s+(?P<since>.+))?$'), 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'^!(bella(gram|pics)|insta(gram|bella))$'), self._do_bellagram),
(re.compile(r'^!yt\s+(?P<q>")?(?P<query>.+)(?(q)")$'), self._do_yt), (re.compile(r'^!yt\s+(?P<q>")?(?P<query>.+)(?(q)")$'), self._do_yt),
] ]
@ -137,13 +137,13 @@ class TwitchClient(irc.bot.SingleServerIRCBot):
else: else:
send_response('!quote {0}'.format(quote['id'])) 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') 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'])) respond('Sorry @{0}, you are not allowed to do this'.format(tags['display-name']))
return return
try: try:
messages = self.commands.get_twitch_messages(since) messages = self.commands.get_twitch_messages()
except CommandError as e: except CommandError as e:
self.logger.error('Failed to get Twitch messages: %s', e) self.logger.error('Failed to get Twitch messages: %s', e)
else: else:

@ -44,29 +44,15 @@ class Commands(object):
except requests.exceptions.HTTPError as e: except requests.exceptions.HTTPError as e:
raise CommandError(e) raise CommandError(e)
def get_twitch_messages(self, since): def get_twitch_messages(self):
if since is None: api_url = self.config['Twitch Logs'].get('api_url')
try: params = dict(commenter='bellateeny', term='quote')
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)
try: try:
return twitch.get_messages(channel_id, since) r = requests.get('{0}/search'.format(api_url), params=params)
except TwitchError as e: r.raise_for_status()
except requests.exceptions.HTTPError:
raise CommandError(e) raise CommandError(e)
return [m.get('message_body', '') for m in r.json()]
def last_quote(self): def last_quote(self):
try: try:

@ -28,3 +28,6 @@ channel_ids = UC5970RJMoEcRNZl0MNp8tlQ,UCHNjavmkFUf2n0uzWSgj16w
[Quotes] [Quotes]
api_url = https://ladylilia.com/quotes/api api_url = https://ladylilia.com/quotes/api
api_key = __QUOTES_API_KEY__ api_key = __QUOTES_API_KEY__
[Twitch Logs]
api_url = https://ladylilia.com/twitch-logs/api

Loading…
Cancel
Save