diff options
author | Cody Hiar <cody@hiar.ca> | 2021-02-03 14:58:59 -0700 |
---|---|---|
committer | Cody Hiar <cody@hiar.ca> | 2021-02-03 14:58:59 -0700 |
commit | 2cf71f2ec7d2d6133b9ad2346e5e3ccb1410a3a0 (patch) | |
tree | f51b52f4e3878f398d7fcff1f1c605604c4b446a /main.py | |
parent | 2b007a5a500b635ddcfe4b9a512777f3d21fa6b6 (diff) |
Initial working version of archiver
Diffstat (limited to 'main.py')
-rw-r--r-- | main.py | 45 |
1 files changed, 26 insertions, 19 deletions
@@ -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() |