From dde3639f39038c6f2642d06c27da7ea9df983671 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sun, 16 Oct 2016 15:54:10 -0400 Subject: [PATCH] Add bomb plugin --- plugins/bomb.py | 64 +++++++++++++++++++++++++++++++++++++++++++ plugins/pugz.py | 72 ------------------------------------------------- 2 files changed, 64 insertions(+), 72 deletions(-) create mode 100644 plugins/bomb.py delete mode 100644 plugins/pugz.py diff --git a/plugins/bomb.py b/plugins/bomb.py new file mode 100644 index 0000000..845ad13 --- /dev/null +++ b/plugins/bomb.py @@ -0,0 +1,64 @@ +# pugz.py +# Needs moar dogos +# Eryn Wells + +import json +import glob +import logging +import os.path +import random +import re + +LOGGER = logging.getLogger(__name__) +CMD_RE = None + +COLLECTIONS = {} + +# rtmbot interface +outputs = [] + +def process_hello(data): + bomb_collections = glob.glob('bomb.*.json') + LOGGER.info('Found some bomb collections: %s', bomb_collections) + + global COLLECTIONS + COLLECTIONS = {} + for c in bomb_collections: + base = os.path.basename(c) + parts = base.split('.') + LOGGER.info('Loading collection %s: %s', parts[1], c) + with open(c, 'r') as f: + COLLECTIONS[parts[1]] = json.load(f) + + collection_commands_re = '|'.join(COLLECTIONS.keys()) + collection_commands_re = r'(?P{})'.format(collection_commands_re) + global CMD_RE + CMD_RE = re.compile(r'!{}(\s+(me|(?P\d+)))?'.format(collection_commands_re)) + +def process_message(data): + try: + text = data['text'] + except KeyError: + LOGGER.error('Missing "text" key in data.') + return + + match = CMD_RE.match(text) + if not match: + return + + collection = match.group('collection') + + count = match.group('count') + try: + count = int(count) + except ValueError: # count isn't an int + count = 3 + except TypeError: # count is None + count = 1 + + LOGGER.info('Getting %d item%s from %s', count, '' if count == 1 else 's', collection) + items = random.sample(COLLECTIONS[collection], count) + LOGGER.debug('I HAVE BOMB FOOD: %s', items) + + for i in items: + outputs.append([data['channel'], i]) diff --git a/plugins/pugz.py b/plugins/pugz.py deleted file mode 100644 index c19a01e..0000000 --- a/plugins/pugz.py +++ /dev/null @@ -1,72 +0,0 @@ -# pugz.py -# Needs moar dogos -# Eryn Wells - -import json -import logging -import re -import requests - -LOGGER = logging.getLogger(__name__) -PUG_RE = re.compile(r'!pug(\s+(me|(?P\d+)))?') - -PUGS_COUNT_URL = 'http://pugme.herokuapp.com/count' -PUGS_BOMB_URL = 'http://pugme.herokuapp.com/bomb' -PUGS_RANDOM_URL = 'http://pugme.herokuapp.com/random' - -MAX_MAX_PUGS = 30 # :sweat_smile: -MAX_PUGS = -1 - -# rtmbot interface -outputs = [] - -def process_hello(data): - global MAX_PUGS - - r = requests.get(PUGS_COUNT_URL) - if r.status_code != 200: - MAX_PUGS = MAX_MAX_PUGS - else: - json = r.json() - count = json.get('pug_count', MAX_MAX_PUGS) - MAX_PUGS = count - LOGGER.info('Maximum pugs: %d', MAX_PUGS) - -def process_message(data): - try: - text = data['text'] - except KeyError: - LOGGER.error('Missing "text" key in data.') - return - - match = PUG_RE.match(text) - if not match: - return - - pugs = [] - - count = match.group('count') - if not count: - LOGGER.info('Getting random pug') - r = _get(PUGS_RANDOM_URL) - pug = r.get('pug') - if pug: - pugs.append(pug) - else: - try: - count = int(count) - except ValueError: - count = 1 - LOGGER.info('Getting %d pug%s', count, '' if count == 1 else 's') - r = _get(PUGS_BOMB_URL, count=count) - pugs = r.get('pugs', []) - LOGGER.debug('I HAVE PUGS: %s', pugs) - - for p in pugs: - outputs.append([data['channel'], p]) - -def _get(url, **kwargs): - r = requests.get(url, params=kwargs) - if r.status_code != 200: - return {} - return r.json()