Reduce @names to just names

This commit is contained in:
Eryn Wells 2016-09-11 20:01:21 -04:00
parent 0f9ba2bf0a
commit 9beca45ac7
2 changed files with 84 additions and 13 deletions

View file

@ -8,6 +8,8 @@ import os.path
import random import random
import re import re
from service import slack
HEARTS_FILE = 'hearts.json' HEARTS_FILE = 'hearts.json'
PLUSES = ['++', '<3', '&lt;3', ':heart:', ':yellow_heart:', ':green_heart:', ':blue_heart:', ':purple_heart:', '❤️', '💛', '💚', '💙', '💜'] PLUSES = ['++', '<3', '&lt;3', ':heart:', ':yellow_heart:', ':green_heart:', ':blue_heart:', ':purple_heart:', '❤️', '💛', '💚', '💙', '💜']
@ -18,12 +20,24 @@ LOGGER = logging.getLogger('hearts')
LEADERS_RE = re.compile('!(top|bottom)(\d+)') LEADERS_RE = re.compile('!(top|bottom)(\d+)')
WHITESPACE_RE = re.compile('\s+') WHITESPACE_RE = re.compile('\s+')
LINK_RE = re.compile(r'<(?P<type>[@#])(?P<id>[A-Z0-9]+)(\|(?P<name>\w+))?>') LINK_RE = re.compile(r'<(?P<type>[@#!])(?P<id>\w+)(\|(?P<name>\w+))?>')
VAR_RE = re.compile(r'<!(?P<name>\w+)>')
EMOJI_RE = re.compile(r':\w+:') EMOJI_RE = re.compile(r':\w+:')
# rtmbot interface
outputs = [] outputs = []
USERS = []
CHANNELS = []
#
# RTM
#
def process_hello(data):
global USERS
USERS = slack.users()
CHANNELS = slack.channels()
def process_message(data): def process_message(data):
try: try:
text = data['text'].strip() text = data['text'].strip()
@ -65,6 +79,10 @@ def process_message(data):
else: else:
outputs.append([data['channel'], 'No score change for _{}_.'.format(name)]) outputs.append([data['channel'], 'No score change for _{}_.'.format(name)])
#
# Hearts
#
def calculate_score_and_find_operators(text): def calculate_score_and_find_operators(text):
original_text = text original_text = text
score = 0 score = 0
@ -87,28 +105,25 @@ def calculate_score_and_find_operators(text):
if not found: if not found:
break break
did_change = original_text != text did_change = original_text != text
if did_change: if did_change:
# Strip off the trailing : if the message is a user or group tokenized = WHITESPACE_RE.split(text)
for regex in [LINK_RE, VAR_RE]:
m = regex.match(text)
if not m:
continue
# string is just a username with a colon at the end so strip off the colon
if (m.end() == len(text)-1) and text.endswith(':'):
text = text[:-1]
break
# If the remaining string is all emojis, ignore it # If the remaining string is all emojis, ignore it
tokenized = WHITESPACE_RE.split(text)
emoji_matches = map(EMOJI_RE.match, tokenized) emoji_matches = map(EMOJI_RE.match, tokenized)
all_emoji = all(emoji_matches) all_emoji = all(emoji_matches)
if all_emoji: if all_emoji:
LOGGER.debug('Message is all emoji, skipping')
return None, None return None, None
tokenized = map(strip_colon, tokenized)
tokenized = map(swap_links_and_vars, tokenized)
text = ' '.join(tokenized)
LOGGER.debug('Score {} for message: {}'.format(score, text))
return score, text return score, text
else: else:
LOGGER.debug('No score adjustment for message: {}'.format(text))
return None, None return None, None
def strip_operator(text, operator, is_prefix): def strip_operator(text, operator, is_prefix):
@ -126,6 +141,56 @@ def has_operator(text, operators):
return op, False return op, False
return None, None return None, None
def strip_colon(item):
'''Remove trailing colon from messages that @ a particular user.'''
m = LINK_RE.match(item)
if not m:
return item
if not (m.end() == (len(item)-1) and item.endswith(':')):
return item
return item[:-1]
def swap_links_and_vars(item):
'''
Swap links and variables for their names. This is for things like @eryn, #general, and !everyone.
'''
m = LINK_RE.match(item)
if not m:
return item
link_type = m.group('type')
# Users
if link_type == '@':
name = m.group('name')
if name:
return name
ident = m.group('id')
users = [u for u in USERS if u['id'] == ident]
try:
return users[0]['name']
except IndexError:
return item
# Channels
elif link_type == '#':
name = m.group('name')
if name:
return name
ident = m.group('id')
channels = [c for c in CHANNELS if c['id'] == ident]
try:
return channels[0]['name']
except IndexError:
return item
# Variables (e.g. everyone, channel, here, etc)
elif link_type == '!':
name = m.group('name')
return name if name else m.group('id')
return item
def leaders(n, top=True): def leaders(n, top=True):
if n == 0: if n == 0:
return return

View file

@ -38,6 +38,12 @@ class SlackService(object):
json = self.__extract_json(r) json = self.__extract_json(r)
return json is not None return json is not None
def users(self):
params = self.__params()
r = requests.get(self.__url('users.list'), params=params)
json = self.__extract_json(r)
return json['members'] if json else None
# #
# Private # Private
# #