aboutsummaryrefslogtreecommitdiff
path: root/main.py
diff options
context:
space:
mode:
Diffstat (limited to 'main.py')
-rw-r--r--main.py45
1 files changed, 26 insertions, 19 deletions
diff --git a/main.py b/main.py
index 4daad96..e0ebcb9 100644
--- a/main.py
+++ b/main.py
@@ -1,7 +1,8 @@
"""Slack stuff."""
-import json
+from datetime import datetime
import os
+from mappings import Message, get_session
from slack_sdk import WebClient
RESULTS_FILE = "results.json"
@@ -13,18 +14,6 @@ def get_slack_client():
return WebClient(token=token)
-def write_results(data):
- """Write the results to a file."""
- with open(RESULTS_FILE, "w") as f:
- json.dump(data, f)
-
-
-def load_results():
- """Load the recent search results."""
- with open(RESULTS_FILE, "r") as f:
- return json.load(f)
-
-
def fetch_results():
"""Query slack for the latest results."""
c = get_slack_client()
@@ -32,10 +21,28 @@ def fetch_results():
# Write the results if
if r.data["ok"]:
- write_results(r.data)
- print("Search complete.")
+ messages = r.data['messages']['matches']
+ write_to_database(messages)
+ print("Archival Complete.")
else:
- print("Failed to search.")
-
-
-def write_to_database
+ print("Failed to search slack.")
+
+
+def write_to_database(messages):
+ """Write messages to database."""
+ session = get_session()
+ for msg in messages:
+ iid = msg["iid"]
+ if session.query(Message).filter(Message.iid == iid).count() != 0:
+ continue
+ session.add(Message(
+ iid=iid,
+ type=msg["type"],
+ username=msg["username"],
+ text=msg["text"],
+ timestamp=datetime.fromtimestamp(int(msg["ts"].split('.')[0]))
+ ))
+ session.commit()
+
+
+fetch_results()