Add caching support

This commit is contained in:
Firepup Sixfifty 2024-06-05 12:39:00 -05:00
parent eeef9c607e
commit c8cb828b70
Signed by: Firepup650
GPG key ID: 7C92E2ABBBFAB9BA

65
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,31 +22,42 @@ 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 = {}
users_list = [] try:
cursor = "N/A" if "--no-cache" in sys.argv:
pages = 0 raise ImportError("User requested to skip cache")
while cursor: print("Trying to load user mappings from cache...")
data = "" from cache import userMappings
if cursor != "N/A": except ImportError:
data = client.users_list(cursor=cursor, limit=1000) users_list = []
else: print("Cache load failed, falling back to full load from slack...")
data = client.users_list(limit=1000) cursor = "N/A"
cursor = data["response_metadata"]["next_cursor"] pages = 0
users_list.extend(data["members"]) while cursor:
pages += 1 data = ""
print(f"Pages of users loaded: {pages}") if cursor != "N/A":
print("All pages loaded, generating user mappings now.") data = client.users_list(cursor=cursor, limit=1000)
del pages else:
user_mappings = {} data = client.users_list(limit=1000)
for user in users_list: cursor = data["response_metadata"]["next_cursor"]
user_mappings[f"<@{user['id']}>"] = ( users_list.extend(data["members"])
f"<@{user['profile']['display_name']}>" pages += 1
if user["profile"]["display_name"] print(f"Pages of users loaded: {pages}")
else f"<@{user['id']}>" del pages
) print("Building user mappings now, this shouldn't take long...")
userMappings = {}
for user in users_list:
userMappings[f"<@{user['id']}>"] = (
f"<@{user['profile']['display_name']}>"
if user["profile"]["display_name"]
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(