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 | |
parent | 2b007a5a500b635ddcfe4b9a512777f3d21fa6b6 (diff) |
Initial working version of archiver
-rw-r--r-- | README.md | 9 | ||||
-rw-r--r-- | alembic/versions/81ed3c3502f7_add_timestamp.py | 28 | ||||
-rw-r--r-- | main.py | 45 | ||||
-rw-r--r-- | mappings.py | 6 |
4 files changed, 62 insertions, 26 deletions
@@ -1,7 +1,6 @@ -# Generating Migrations - -``` +```bash +# Generate Migration alembic revision --autogenerate --message "Create the example Database" -``` - +# Apply Migrations +alembic upgrade head diff --git a/alembic/versions/81ed3c3502f7_add_timestamp.py b/alembic/versions/81ed3c3502f7_add_timestamp.py new file mode 100644 index 0000000..063b76a --- /dev/null +++ b/alembic/versions/81ed3c3502f7_add_timestamp.py @@ -0,0 +1,28 @@ +"""add timestamp + +Revision ID: 81ed3c3502f7 +Revises: a773ce65a262 +Create Date: 2021-02-03 21:40:35.364113 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '81ed3c3502f7' +down_revision = 'a773ce65a262' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('message', sa.Column('timestamp', sa.TIMESTAMP(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('message', 'timestamp') + # ### end Alembic commands ### @@ -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() diff --git a/mappings.py b/mappings.py index e80756f..ae1f0b5 100644 --- a/mappings.py +++ b/mappings.py @@ -1,5 +1,5 @@ """Database for project.""" -from sqlalchemy import Column, Integer, String, create_engine +from sqlalchemy import Column, Integer, String, create_engine, DateTime from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker @@ -18,14 +18,16 @@ class Message(Base): type = Column(String) username = Column(String) text = Column(String) + timestamp = Column(DateTime) # ---------------------------------------------------------------------- - def __init__(self, iid, type, username, text): + def __init__(self, iid, type, username, text, timestamp): """Create record.""" self.iid = iid self.type = type self.username = username self.text = text + self.timestamp = timestamp def create_tables(): |