Compare commits

..

2 commits

Author SHA1 Message Date
c8cb828b70
Add caching support 2024-06-05 12:39:00 -05:00
eeef9c607e
Ignore cache files 2024-06-05 12:38:48 -05:00
2 changed files with 40 additions and 27 deletions

2
.gitignore vendored
View file

@ -1 +1,3 @@
.env .env
__pycache__/**
cache.py

27
main.py
View file

@ -1,4 +1,4 @@
import os import os, sys
from slack_bolt import App from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler from slack_bolt.adapter.socket_mode import SocketModeHandler
from dotenv import load_dotenv from dotenv import load_dotenv
@ -22,8 +22,15 @@ print("Establishing a connection to slack...")
app = App(token=os.environ.get("SLACK_BOT_TOKEN")) app = App(token=os.environ.get("SLACK_BOT_TOKEN"))
client = app.client client = app.client
print("Building a list of users, please stand by.") userMappings = {}
try:
if "--no-cache" in sys.argv:
raise ImportError("User requested to skip cache")
print("Trying to load user mappings from cache...")
from cache import userMappings
except ImportError:
users_list = [] users_list = []
print("Cache load failed, falling back to full load from slack...")
cursor = "N/A" cursor = "N/A"
pages = 0 pages = 0
while cursor: while cursor:
@ -36,17 +43,21 @@ while cursor:
users_list.extend(data["members"]) users_list.extend(data["members"])
pages += 1 pages += 1
print(f"Pages of users loaded: {pages}") print(f"Pages of users loaded: {pages}")
print("All pages loaded, generating user mappings now.")
del pages del pages
user_mappings = {} print("Building user mappings now, this shouldn't take long...")
userMappings = {}
for user in users_list: for user in users_list:
user_mappings[f"<@{user['id']}>"] = ( userMappings[f"<@{user['id']}>"] = (
f"<@{user['profile']['display_name']}>" f"<@{user['profile']['display_name']}>"
if user["profile"]["display_name"] if user["profile"]["display_name"]
else f"<@{user['id']}>" else f"<@{user['id']}>"
) )
print("All mappings generated, writing cache file now...")
with open("cache.py", "w") as cacheFile:
cacheFile.write(f"userMappings = {userMappings}")
print("Cache saved.")
print("User mappings loaded. User count:", len(user_mappings)) print("User mappings loaded. User count:", len(userMappings))
if __name__ == "__main__": if __name__ == "__main__":
print("^D at any time to terminate program") print("^D at any time to terminate program")
@ -74,8 +85,8 @@ if __name__ == "__main__":
print("Building messages, this might take a little bit...") print("Building messages, this might take a little bit...")
for i in range(len(messages)): for i in range(len(messages)):
label = f'{messages[i]["text"]} ({messages[i]["ts"]})' label = f'{messages[i]["text"]} ({messages[i]["ts"]})'
for user in user_mappings: for user in userMappings:
label = label.replace(user, user_mappings[user]) label = label.replace(user, userMappings[user])
texts[label] = i texts[label] = i
found = messages[ found = messages[
fp.menu( fp.menu(