From c34ea2f76bba0c03130414cff67b60a1e1ce85d6 Mon Sep 17 00:00:00 2001 From: Ivan Higuera-Mendieta Date: Thu, 21 Mar 2024 13:23:52 -0700 Subject: [PATCH] Simplify db connection --- echolab_candidates_bot.py | 8 ++++---- src/retrieve_messages.py | 10 ++-------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/echolab_candidates_bot.py b/echolab_candidates_bot.py index 0ade4b5..694b8c9 100644 --- a/echolab_candidates_bot.py +++ b/echolab_candidates_bot.py @@ -53,7 +53,7 @@ def event_test(say): def summary_command(say, ack): ack("Querying database... 👨🏽‍💻") - conn = sqlite3.connect("data/slackbot_messages.db") + conn = sqlite3.connect("/home/topcat/projects/python_slack_bot/data/slackbot_messages.db") query = """ WITH table_group AS ( @@ -102,9 +102,9 @@ def summary_command(say, ack): def reload_command(say, ack): channel_ids = ["C06PSDC08AX", "C06Q5A168DP", "C06PRB2EX61"] poster_ids = ["U06N7CSQQKZ", "WBA9HFDCL"] - save_data = "./data/downloads" + save_data = "/home/topcat/projects/python_slack_bot/data/downloads" - conn = sqlite3.connect("data/slackbot_messages.db") + conn = sqlite3.connect("/home/topcat/projects/python_slack_bot/data/slackbot_messages.db") """Reload database to include new candidates in channel""" ack("Loading database... 👨🏽‍💻") # Retrieve messages from the channel @@ -114,7 +114,7 @@ def reload_command(say, ack): channel_id, filter_users=poster_ids, save_data=save_data, - db_path="data/messages.db", + db_conn=conn, messages_table="messages", ) diff --git a/src/retrieve_messages.py b/src/retrieve_messages.py index 1aac2cf..4797c68 100644 --- a/src/retrieve_messages.py +++ b/src/retrieve_messages.py @@ -13,7 +13,7 @@ def retrieve_messages( client, channel_id, - db_path, + db_conn, messages_table, save_data="data", filter_users=None, @@ -33,12 +33,6 @@ def retrieve_messages( list: A list of messages from the channel """ - # Create SQLite connection - if not os.path.exists(os.path.join(save_data, db_path)): - conn, c = create_database() - else: - conn = sqlite3.connect(os.path.join(save_data, db_path)) - # Create folder if save_data doesn't exist if not os.path.exists("data/downloads"): os.makedirs("data/downloads") @@ -97,7 +91,7 @@ def retrieve_messages( ) # Update database - df.to_sql(messages_table, conn, if_exists="append", index=False) + df.to_sql(messages_table, db_conn, if_exists="append", index=False) return messages