Added more granularity to scroll lookups
This commit is contained in:
parent
8b58939ff5
commit
e6ec258466
|
|
@ -90,9 +90,17 @@ def lookup_msg_brother(msg):
|
||||||
Finds the real-world name of whoever posted msg.
|
Finds the real-world name of whoever posted msg.
|
||||||
:return: brother dict or None
|
:return: brother dict or None
|
||||||
"""
|
"""
|
||||||
|
return lookup_slackid_brother(msg.get("user"))
|
||||||
|
|
||||||
|
|
||||||
|
def lookup_slackid_brother(slack_id):
|
||||||
|
"""
|
||||||
|
Gets whatever brother the userid is registered to
|
||||||
|
:return: Brother dict or None
|
||||||
|
"""
|
||||||
with shelve.open(DB_NAME) as db:
|
with shelve.open(DB_NAME) as db:
|
||||||
try:
|
try:
|
||||||
scroll = db[msg.get("user")]
|
scroll = db[slack_id]
|
||||||
return scroll_util.find_by_scroll(scroll)
|
return scroll_util.find_by_scroll(scroll)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return None
|
return None
|
||||||
|
|
|
||||||
|
|
@ -66,17 +66,17 @@ class Hook(object):
|
||||||
# Fail if pattern invalid
|
# Fail if pattern invalid
|
||||||
match = re.match(self.pattern, msg['text'], flags=re.IGNORECASE)
|
match = re.match(self.pattern, msg['text'], flags=re.IGNORECASE)
|
||||||
if match is None:
|
if match is None:
|
||||||
print("Missed pattern")
|
# print("Missed pattern")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Fail if whitelist defined, and we aren't there
|
# Fail if whitelist defined, and we aren't there
|
||||||
if self.channel_whitelist is not None and msg["channel"] not in self.channel_whitelist:
|
if self.channel_whitelist is not None and msg["channel"] not in self.channel_whitelist:
|
||||||
print("Missed whitelist")
|
# print("Missed whitelist")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Fail if blacklist defined, and we are there
|
# Fail if blacklist defined, and we are there
|
||||||
if self.channel_blacklist is not None and msg["channel"] in self.channel_blacklist:
|
if self.channel_blacklist is not None and msg["channel"] in self.channel_blacklist:
|
||||||
print("Hit blacklist")
|
# print("Hit blacklist")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Otherwise do callback and return success
|
# Otherwise do callback and return success
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,85 @@
|
||||||
|
import channel_util
|
||||||
|
import slack_util
|
||||||
|
import identifier
|
||||||
|
import re
|
||||||
|
import shelve
|
||||||
|
|
||||||
|
counted_data = ["flaked", "rolled", "replaced", "washed", "dryed"]
|
||||||
|
lookup_format = "{}\s+(\d+)"
|
||||||
|
|
||||||
|
DB_NAME = "towels_rolled"
|
||||||
|
|
||||||
|
|
||||||
|
def fmt_work_dict(work_dict):
|
||||||
|
return ", ".join(["{} * {}".format(job, count) for job, count in sorted(work_dict.items())])
|
||||||
|
|
||||||
|
|
||||||
|
def count_work_callback(slack, msg, match):
|
||||||
|
with shelve.open(DB_NAME) as db:
|
||||||
|
text = msg["text"].lower().strip()
|
||||||
|
|
||||||
|
# Couple things to work through.
|
||||||
|
# One: Who sent the message?
|
||||||
|
who_wrote = identifier.lookup_msg_brother(msg)
|
||||||
|
who_wrote_label = "{} [{}]".format(who_wrote["name"], who_wrote["scroll"])
|
||||||
|
|
||||||
|
# Two: What work did they do?
|
||||||
|
new_work = {}
|
||||||
|
for job in counted_data:
|
||||||
|
pattern = lookup_format.format(job)
|
||||||
|
match = re.search(pattern, text)
|
||||||
|
if match:
|
||||||
|
new_work[job] = int(match.group(1))
|
||||||
|
|
||||||
|
# Three: check if we found anything
|
||||||
|
if len(new_work) == 0:
|
||||||
|
slack_util.reply(slack, msg, "No work recognized. Use words {} or work will not be recorded".format(counted_data))
|
||||||
|
return
|
||||||
|
|
||||||
|
# Three: Add it to their total work. We key by user_id, to avoid annoying identity shit
|
||||||
|
db_key = msg["user"]
|
||||||
|
old_work = db.get(db_key) or {}
|
||||||
|
total_work = dict(old_work)
|
||||||
|
|
||||||
|
for job, count in new_work.items():
|
||||||
|
if job not in total_work:
|
||||||
|
total_work[job] = 0
|
||||||
|
total_work[job] += count
|
||||||
|
|
||||||
|
# Save
|
||||||
|
db[db_key] = total_work
|
||||||
|
|
||||||
|
# Four, congratulate them on their work
|
||||||
|
congrats = "{} recorded work: {}\nTotal work since last dump now {}".format(who_wrote_label,
|
||||||
|
fmt_work_dict(new_work),
|
||||||
|
fmt_work_dict(total_work))
|
||||||
|
slack_util.reply(slack, msg, congrats)
|
||||||
|
|
||||||
|
|
||||||
|
def dump_work_callback(slack, msg, match):
|
||||||
|
with shelve.open(DB_NAME) as db:
|
||||||
|
# Dump out each user
|
||||||
|
keys = db.keys()
|
||||||
|
result = ["All work:"]
|
||||||
|
for user_id in keys:
|
||||||
|
# Get the work
|
||||||
|
work = db[user_id]
|
||||||
|
del db[user_id]
|
||||||
|
|
||||||
|
# Get the name
|
||||||
|
brother_name = identifier.lookup_slackid_brother(user_id)
|
||||||
|
if brother_name is None:
|
||||||
|
brother_name = user_id
|
||||||
|
else:
|
||||||
|
brother_name = brother_name["name"]
|
||||||
|
|
||||||
|
result.append("{} has done {}".format(brother_name, fmt_work_dict(work)))
|
||||||
|
|
||||||
|
result.append("Database wiped. Next dump will show new work since the time of this message")
|
||||||
|
# Send it back
|
||||||
|
slack_util.reply(slack, msg, "\n".join(result))
|
||||||
|
|
||||||
|
|
||||||
|
# Make dem HOOKs
|
||||||
|
count_work_hook = slack_util.Hook(count_work_callback, channel_whitelist=[channel_util.SLAVES_TO_THE_MACHINE_ID])
|
||||||
|
dump_work_hook = slack_util.Hook(dump_work_callback, pattern="dump towel data", channel_whitelist=[channel_util.COMMAND_CENTER_ID])
|
||||||
Loading…
Reference in New Issue