Synchronize also regular subs

master
Nikola Forró 6 years ago
parent d386edd143
commit 995b014e76

@ -31,6 +31,20 @@ QUOTE_REMOVED_PATTERN = re.compile(r'''^
Successfully\s+deleted\s+Quote\s+ Successfully\s+deleted\s+Quote\s+
\#(?P<id>\d+)\.$''', re.VERBOSE) \#(?P<id>\d+)\.$''', re.VERBOSE)
# $user has joined the Cheese Horde! CHEESE HYPE!!! ♥♥
SUB_PATTERN = re.compile(r'''^
(?P<user>.+)\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<user>.+),\s+
Thank\s+you\s+for\s+the\s+
(?P<rank>\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 ! # $giver, Thank you for gifting a sub to $receiver! So kind <3 !
SUB_GIFTED_PATTERN = re.compile(r'''^ SUB_GIFTED_PATTERN = re.compile(r'''^
(?P<giver>.+),\s+ (?P<giver>.+),\s+
@ -49,6 +63,8 @@ class TwitchClient(irc.bot.SingleServerIRCBot):
(QUOTE_ADDED_PATTERN, self._add_quote), (QUOTE_ADDED_PATTERN, self._add_quote),
(QUOTE_EDITED_PATTERN, self._edit_quote), (QUOTE_EDITED_PATTERN, self._edit_quote),
(QUOTE_REMOVED_PATTERN, self._remove_quote), (QUOTE_REMOVED_PATTERN, self._remove_quote),
(SUB_PATTERN, self._record_sub),
(RESUB_PATTERN, self._record_resub),
(SUB_GIFTED_PATTERN, self._record_gifted_sub), (SUB_GIFTED_PATTERN, self._record_gifted_sub),
] ]
self.supported_commands = [ self.supported_commands = [
@ -95,6 +111,8 @@ class TwitchClient(irc.bot.SingleServerIRCBot):
self._process_message(connection, event) self._process_message(connection, event)
def _is_mod(self, tags): def _is_mod(self, tags):
if not tags['badges']:
return False
badges = [b.split('/')[0] for b in tags['badges'].split(',')] badges = [b.split('/')[0] for b in tags['badges'].split(',')]
return bool(set(badges).intersection(['admin', 'broadcaster', 'moderator'])) return bool(set(badges).intersection(['admin', 'broadcaster', 'moderator']))
@ -147,6 +165,20 @@ class TwitchClient(irc.bot.SingleServerIRCBot):
except CommandError as e: except CommandError as e:
self.logger.error('Failed to remove quote: %s', 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): def _record_gifted_sub(self, tags, send_response, giver, receiver, **kwargs):
self.logger.info('Recording gifted sub %s -> %s', giver, receiver) self.logger.info('Recording gifted sub %s -> %s', giver, receiver)
try: 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'])) send_response('Sorry @{0}, you are not allowed to do this'.format(tags['display-name']))
return return
try: try:
messages = self.commands.get_gifted_sub_messages() messages = self.commands.get_sub_messages()
except CommandError as e: 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: else:
for message, time in messages: for message, time in messages:
for pattern, action in self.patterns: for pattern, action in self.patterns:

@ -47,6 +47,17 @@ class Commands(object):
except requests.exceptions.HTTPError as e: except requests.exceptions.HTTPError as e:
raise CommandError(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): def record_gifted_sub(self, giver, receiver, time=None):
if time is None: if time is None:
time = datetime.datetime.utcnow() time = datetime.datetime.utcnow()
@ -69,9 +80,10 @@ class Commands(object):
messages = [(m, t) for m, t in messages if t >= threshold] messages = [(m, t) for m, t in messages if t >= threshold]
return messages return messages
def get_gifted_sub_messages(self): def get_sub_messages(self):
try: try:
messages = self._get_messages('bellateeny', 'gift') messages = self._get_messages('bellateeny', 'cheese hype')
messages.extend(self._get_messages('bellateeny', 'gift'))
except CommandError: except CommandError:
raise raise
else: else:
@ -166,20 +178,33 @@ class Commands(object):
r = requests.delete('{0}/quotes/{1}'.format(api_url, id), r = requests.delete('{0}/quotes/{1}'.format(api_url, id),
headers={'X-Quotes-API-Key': api_key}) headers={'X-Quotes-API-Key': api_key})
r.raise_for_status() 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 return r
def _get_gifted_subs(self, params): 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 = requests.get('{0}/gifted-subs'.format(api_url), params=params)
r.raise_for_status() r.raise_for_status()
return r return r
def _post_gifted_subs(self, data): def _post_gifted_subs(self, data):
api_url = self.config['GiftedSubs'].get('api_url') api_url = self.config['TwitchSubs'].get('api_url')
api_key = self.config['GiftedSubs'].get('api_key') api_key = self.config['TwitchSubs'].get('api_key')
r = requests.post('{0}/gifted-subs'.format(api_url), data=data, 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() r.raise_for_status()
return r return r

@ -45,6 +45,6 @@ api_url = https://ladylilia.com/quotes/api
api_key = __QUOTES_API_KEY__ api_key = __QUOTES_API_KEY__
act_as_proxy = true act_as_proxy = true
[GiftedSubs] [TwitchSubs]
api_url = https://ladylilia.com/gifted-subs/api api_url = https://ladylilia.com/twitch-subs/api
api_key = __GIFTED_SUBS_API_KEY__ api_key = __TWITCH_SUBS_API_KEY__

Loading…
Cancel
Save