diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | main.py | 26 | ||||
-rw-r--r-- | mappings.py | 8 |
3 files changed, 23 insertions, 12 deletions
@@ -11,3 +11,4 @@ tags .envrc *.sqlite results.json +*.log @@ -1,6 +1,6 @@ """Slack stuff.""" -from datetime import datetime import os +from datetime import datetime from mappings import Message, get_session from slack_sdk import WebClient @@ -21,7 +21,7 @@ def fetch_results(): # Write the results if if r.data["ok"]: - messages = r.data['messages']['matches'] + messages = r.data["messages"]["matches"] write_to_database(messages) print("Archival Complete.") else: @@ -32,16 +32,20 @@ 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: + 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: continue - session.add(Message( - iid=iid, - type=msg["type"], - username=msg["username"], - text=msg["text"], - timestamp=datetime.fromtimestamp(int(msg["ts"].split('.')[0])) - )) + print("New notification") + session.add( + Message( + iid=msg["iid"], + type=msg["type"], + username=username, + text=msg["text"], + timestamp=timestamp, + ) + ) session.commit() diff --git a/mappings.py b/mappings.py index ae1f0b5..5470d55 100644 --- a/mappings.py +++ b/mappings.py @@ -1,10 +1,16 @@ """Database for project.""" +import logging + from sqlalchemy import Column, Integer, String, create_engine, DateTime from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker +handler = logging.FileHandler('sqlalchemy.engine.log') +handler.level = logging.DEBUG +logging.getLogger('sqlalchemy.engine.base.Engine').addHandler(handler) + DB_FILE = "sqlite:///db.sqlite" -engine = create_engine(DB_FILE, echo=True) +engine = create_engine(DB_FILE, echo=False) Base = declarative_base() |