Cleaning up some stuff in the cookie module

This commit is contained in:
Eryn Wells 2016-09-06 11:07:38 -04:00
parent 1daed2c364
commit 94c738c53b

View file

@ -12,7 +12,7 @@ from service import slack
LOGGER = logging.getLogger('cookie') LOGGER = logging.getLogger('cookie')
MAX_PINS = 100 MAX_PINS = 100
CHANNELS = [] CHANNELS = {}
outputs = [] outputs = []
@ -70,7 +70,7 @@ class Channel(object):
def save_pin(self, pin): def save_pin(self, pin):
pins = self.saved_pins pins = self.saved_pins
if pins is None: if pins is None:
pins = [] pins = []
filtered_pins = list(filter(lambda p: p['created'] == pin['created'], pins)) filtered_pins = list(filter(lambda p: p['created'] == pin['created'], pins))
if len(filtered_pins) > 0: if len(filtered_pins) > 0:
LOGGER.info('Message already pinned; skipping') LOGGER.info('Message already pinned; skipping')
@ -101,39 +101,46 @@ def process_hello(data):
LOGGER.info('I am in these channels: {}'.format(', '.join(['#' + c['name'] for c in channels]))) LOGGER.info('I am in these channels: {}'.format(', '.join(['#' + c['name'] for c in channels])))
for c in channels: for c in channels:
ch = Channel(c) ch = Channel(c)
CHANNELS.append(ch) CHANNELS[ch.ident] = ch
ch.fetch_pins() ch.fetch_pins()
LOGGER.info(' {} has {} pins.'.format(ch.pretty_name, len(ch.pins))) LOGGER.info(' {} has {} pins.'.format(ch.pretty_name, len(ch.pins)))
ch.unpin_oldest_if_needed() ch.unpin_oldest_if_needed()
def process_channel_joined(data): def process_channel_joined(data):
LOGGER.info('Joined #{}'.format(data['channel']['name']))
ch = Channel(data['channel']) ch = Channel(data['channel'])
CHANNELS.append(ch) LOGGER.info('Joined {}'.format(ch.pretty_name))
CHANNELS[ch.ident] = ch
ch.fetch_pins() ch.fetch_pins()
ch.unpin_oldest_if_needed() ch.unpin_oldest_if_needed()
def process_pin_added(data): def process_pin_added(data):
LOGGER.info('Pin added') LOGGER.info('Pin added')
ch.fetch_pins() try:
ch.unpin_oldest_if_needed() ch = CHANNELS[data['channel_id']]
ch.fetch_pins()
ch.unpin_oldest_if_needed()
except KeyError as e:
LOGGER.error("Couldn't get channel for id {}: {}".format(data['channel_id'], e))
def process_message(data): def process_message(data):
try: try:
text = data['text'].strip() text = data['text'].strip()
except KeyError: except KeyError as e:
# TODO: Make this better. LOGGER.error("Couldn't extract text from message event: {}".format(e))
LOGGER.debug(json.dumps(data, indent=2))
return return
LOGGER.info('Received message: {}'.format(text))
LOGGER.debug('Received message: {}'.format(text))
if text == '!lore': if text == '!lore':
try: try:
ch = list(filter(lambda c: c.ident == data['channel'], CHANNELS))[0] chid = data['channel']
ch = CHANNELS[chid]
random_pin = _lore(ch) random_pin = _lore(ch)
if random_pin: if random_pin:
outputs.append([data['channel'], random_pin]) outputs.append([chid, random_pin])
except IndexError: except KeyError as e:
pass LOGGER.error("Couldn't process !lore command: {}".format(e))
# #
# Private # Private
@ -141,7 +148,9 @@ def process_message(data):
def _lore(channel): def _lore(channel):
pins = channel.saved_pins pins = channel.saved_pins
if not pins:
return None
random_pin = random.choice(pins) random_pin = random.choice(pins)
if random_pin['type'] == 'message': if random_pin['type'] == 'message':
return random_pin['message']['permalink'] return random_pin['message']['permalink']
return random_pin return '```\n' + str(random_pin) + '\n```'