Add bomb plugin
This commit is contained in:
parent
501f4aeb6f
commit
dde3639f39
2 changed files with 64 additions and 72 deletions
64
plugins/bomb.py
Normal file
64
plugins/bomb.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
# pugz.py
|
||||
# Needs moar dogos
|
||||
# Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
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<collection>{})'.format(collection_commands_re)
|
||||
global CMD_RE
|
||||
CMD_RE = re.compile(r'!{}(\s+(me|(?P<count>\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])
|
|
@ -1,72 +0,0 @@
|
|||
# pugz.py
|
||||
# Needs moar dogos
|
||||
# Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
import requests
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
PUG_RE = re.compile(r'!pug(\s+(me|(?P<count>\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()
|
Loading…
Add table
Add a link
Reference in a new issue