60 lines
1.3 KiB
Python
60 lines
1.3 KiB
Python
"""
|
|
This file contains util for scroll polling
|
|
Only really kept separate for neatness sake.
|
|
"""
|
|
|
|
import re
|
|
|
|
from fuzzywuzzy import process
|
|
|
|
import slack_util
|
|
|
|
# load the family tree
|
|
familyfile = open("sortedfamilytree.txt", 'r')
|
|
|
|
# Parse out
|
|
brother_match = re.compile(r"([0-9]*)~(.*)")
|
|
brothers = [brother_match.match(line) for line in familyfile]
|
|
brothers = [m for m in brothers if m]
|
|
brothers = [{
|
|
"scroll": int(m.group(1)),
|
|
"name": m.group(2)
|
|
} for m in brothers]
|
|
|
|
|
|
def scroll_callback(slack, msg, match):
|
|
# Get the query
|
|
query = match.group(1).strip()
|
|
|
|
# Try to get as int or by name
|
|
try:
|
|
sn = int(query)
|
|
result = find_by_scroll(sn)
|
|
except ValueError:
|
|
result = find_by_name(query)
|
|
if result:
|
|
result = "Brother {} has scroll {}".format(result["name"], result["scroll"])
|
|
else:
|
|
result = "Couldn't find brother {}".format(query)
|
|
|
|
# Respond
|
|
slack_util.reply(slack, msg, result)
|
|
|
|
|
|
def find_by_scroll(scroll):
|
|
for b in brothers:
|
|
if b["scroll"] == scroll:
|
|
return b
|
|
return None
|
|
|
|
|
|
def find_by_name(name):
|
|
# coerce name into dict form
|
|
name = {"name": name}
|
|
|
|
# Do fuzzy match
|
|
return process.extractOne(name, brothers, processor=lambda b: b["name"])[0]
|
|
|
|
|
|
scroll_hook = slack_util.Hook(scroll_callback, pattern=r"scroll\s+(.*)")
|