From 995b014e76c0d98d45bffdf4fbaaab3abfab8e38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Fri, 24 Aug 2018 11:22:15 +0200 Subject: [PATCH] Synchronize also regular subs --- clients/twitch.py | 36 ++++++++++++++++++++++++++++++++++-- commands.py | 39 ++++++++++++++++++++++++++++++++------- settings.cfg.example | 6 +++--- 3 files changed, 69 insertions(+), 12 deletions(-) diff --git a/clients/twitch.py b/clients/twitch.py index be8cf4b..d603314 100644 --- a/clients/twitch.py +++ b/clients/twitch.py @@ -31,6 +31,20 @@ QUOTE_REMOVED_PATTERN = re.compile(r'''^ Successfully\s+deleted\s+Quote\s+ \#(?P\d+)\.$''', re.VERBOSE) +# $user has joined the Cheese Horde! CHEESE HYPE!!! ♥♥ +SUB_PATTERN = re.compile(r'''^ + (?P.+)\s+ + has\s+joined\s+the\s+Cheese\s+Horde!\s+ + CHEESE\s+HYPE!!!\s+♥♥$''', re.VERBOSE) + +# $user, Thank you for the $rank months of cheesy support! ♥♥♥♥ CHEESE HYPE!!! +RESUB_PATTERN = re.compile(r'''^ + (?P.+),\s+ + Thank\s+you\s+for\s+the\s+ + (?P\d+)\s+ + months\s+of\s+cheesy\s+support!\s+ + ♥♥♥♥\s+CHEESE\s+HYPE!!!$''', re.VERBOSE) + # $giver, Thank you for gifting a sub to $receiver! So kind <3 ! SUB_GIFTED_PATTERN = re.compile(r'''^ (?P.+),\s+ @@ -49,6 +63,8 @@ class TwitchClient(irc.bot.SingleServerIRCBot): (QUOTE_ADDED_PATTERN, self._add_quote), (QUOTE_EDITED_PATTERN, self._edit_quote), (QUOTE_REMOVED_PATTERN, self._remove_quote), + (SUB_PATTERN, self._record_sub), + (RESUB_PATTERN, self._record_resub), (SUB_GIFTED_PATTERN, self._record_gifted_sub), ] self.supported_commands = [ @@ -95,6 +111,8 @@ class TwitchClient(irc.bot.SingleServerIRCBot): self._process_message(connection, event) def _is_mod(self, tags): + if not tags['badges']: + return False badges = [b.split('/')[0] for b in tags['badges'].split(',')] return bool(set(badges).intersection(['admin', 'broadcaster', 'moderator'])) @@ -147,6 +165,20 @@ class TwitchClient(irc.bot.SingleServerIRCBot): except CommandError as e: self.logger.error('Failed to remove quote: %s', e) + def _record_sub(self, tags, send_response, user, **kwargs): + self.logger.info('Recording new sub of %s', user) + try: + self.commands.record_regular_sub(user, 1, kwargs.get('time')) + except CommandError as e: + self.logger.error('Failed to record new sub: %s', e) + + def _record_resub(self, tags, send_response, user, rank, **kwargs): + self.logger.info('Recording %sx resub of %s', rank, user) + try: + self.commands.record_regular_sub(user, int(rank), kwargs.get('time')) + except CommandError as e: + self.logger.error('Failed to record resub: %s', e) + def _record_gifted_sub(self, tags, send_response, giver, receiver, **kwargs): self.logger.info('Recording gifted sub %s -> %s', giver, receiver) try: @@ -197,9 +229,9 @@ class TwitchClient(irc.bot.SingleServerIRCBot): send_response('Sorry @{0}, you are not allowed to do this'.format(tags['display-name'])) return try: - messages = self.commands.get_gifted_sub_messages() + messages = self.commands.get_sub_messages() except CommandError as e: - self.logger.error('Failed to get gifted sub messages: %s', e) + self.logger.error('Failed to get sub messages: %s', e) else: for message, time in messages: for pattern, action in self.patterns: diff --git a/commands.py b/commands.py index 8174d8c..ab5c9e9 100644 --- a/commands.py +++ b/commands.py @@ -47,6 +47,17 @@ class Commands(object): except requests.exceptions.HTTPError as e: raise CommandError(e) + def record_regular_sub(self, user, rank, time=None): + if time is None: + time = datetime.datetime.utcnow() + try: + self._post_regular_subs(dict( + user=user, + rank=rank, + time=time.isoformat())) + except requests.exceptions.HTTPError as e: + raise CommandError(e) + def record_gifted_sub(self, giver, receiver, time=None): if time is None: time = datetime.datetime.utcnow() @@ -69,9 +80,10 @@ class Commands(object): messages = [(m, t) for m, t in messages if t >= threshold] return messages - def get_gifted_sub_messages(self): + def get_sub_messages(self): try: - messages = self._get_messages('bellateeny', 'gift') + messages = self._get_messages('bellateeny', 'cheese hype') + messages.extend(self._get_messages('bellateeny', 'gift')) except CommandError: raise else: @@ -166,20 +178,33 @@ class Commands(object): r = requests.delete('{0}/quotes/{1}'.format(api_url, id), headers={'X-Quotes-API-Key': api_key}) r.raise_for_status() - # FIXME: reindex subsequent quotes + return r + + def _get_regular_subs(self, params): + api_url = self.config['TwitchSubs'].get('api_url') + r = requests.get('{0}/regular-subs'.format(api_url), params=params) + r.raise_for_status() + return r + + def _post_regular_subs(self, data): + api_url = self.config['TwitchSubs'].get('api_url') + api_key = self.config['TwitchSubs'].get('api_key') + r = requests.post('{0}/regular-subs'.format(api_url), data=data, + headers={'X-Twitch-Subs-API-Key': api_key}) + r.raise_for_status() return r def _get_gifted_subs(self, params): - api_url = self.config['GiftedSubs'].get('api_url') + api_url = self.config['TwitchSubs'].get('api_url') r = requests.get('{0}/gifted-subs'.format(api_url), params=params) r.raise_for_status() return r def _post_gifted_subs(self, data): - api_url = self.config['GiftedSubs'].get('api_url') - api_key = self.config['GiftedSubs'].get('api_key') + api_url = self.config['TwitchSubs'].get('api_url') + api_key = self.config['TwitchSubs'].get('api_key') r = requests.post('{0}/gifted-subs'.format(api_url), data=data, - headers={'X-Gifted-Subs-API-Key': api_key}) + headers={'X-Twitch-Subs-API-Key': api_key}) r.raise_for_status() return r diff --git a/settings.cfg.example b/settings.cfg.example index 096e68d..7fe3c5d 100644 --- a/settings.cfg.example +++ b/settings.cfg.example @@ -45,6 +45,6 @@ api_url = https://ladylilia.com/quotes/api api_key = __QUOTES_API_KEY__ act_as_proxy = true -[GiftedSubs] -api_url = https://ladylilia.com/gifted-subs/api -api_key = __GIFTED_SUBS_API_KEY__ +[TwitchSubs] +api_url = https://ladylilia.com/twitch-subs/api +api_key = __TWITCH_SUBS_API_KEY__