From 2cf71f2ec7d2d6133b9ad2346e5e3ccb1410a3a0 Mon Sep 17 00:00:00 2001 From: Cody Hiar Date: Wed, 3 Feb 2021 14:58:59 -0700 Subject: Initial working version of archiver --- main.py | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) (limited to 'main.py') 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() -- cgit v1.2.3