diff options
-rw-r--r-- | main.py | 27 | ||||
-rw-r--r-- | printer.py | 15 |
2 files changed, 35 insertions, 7 deletions
@@ -14,14 +14,20 @@ def get_slack_client(): return WebClient(token=token) -def fetch_results(): - """Query slack for the latest results.""" +def get_messages(): + """Get messages.""" c = get_slack_client() r = c.search_messages(query="@cody", sort="timestamp") - - # Write the results if if r.data["ok"]: - messages = r.data["messages"]["matches"] + return r.data["messages"]["matches"] + else: + return [] + + +def fetch_results(): + """Query slack for the latest results.""" + messages = get_messages() + if messages: write_to_database(messages) print("Archival Complete.") else: @@ -34,7 +40,13 @@ def write_to_database(messages): for msg in messages: username = msg["username"] timestamp = datetime.fromtimestamp(int(msg["ts"].split(".")[0])) - if session.query(Message).filter(Message.username == username).filter(Message.timestamp == timestamp).count() != 0: + if ( + session.query(Message) + .filter(Message.username == username) + .filter(Message.timestamp == timestamp) + .count() + != 0 + ): continue print("New notification") session.add( @@ -49,4 +61,5 @@ def write_to_database(messages): session.commit() -fetch_results() +if __name__ == "__main__": + fetch_results() diff --git a/printer.py b/printer.py new file mode 100644 index 0000000..6e13f2c --- /dev/null +++ b/printer.py @@ -0,0 +1,15 @@ +"""Print the results.""" +from datetime import timedelta + +from mappings import Message, get_session + +session = get_session() +msgs = session.query(Message).order_by(Message.timestamp.desc()).all() + +my_timezone = timedelta(hours=7) + +for msg in msgs: + sent_at = msg.timestamp - my_timezone + sent_at_str = sent_at.strftime("[%m-%d %H:%M]") + text = msg.text[:70].replace("\n", "") + print(f"{sent_at_str} {msg.username:^10} {msg.type:^8} {text}") |