Skip to content

Commit

Permalink
Simplify db connection
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanhigueram committed Mar 21, 2024
1 parent 0d23bcb commit c34ea2f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
8 changes: 4 additions & 4 deletions echolab_candidates_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand All @@ -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",
)

Expand Down
10 changes: 2 additions & 8 deletions src/retrieve_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def retrieve_messages(
client,
channel_id,
db_path,
db_conn,
messages_table,
save_data="data",
filter_users=None,
Expand All @@ -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")
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit c34ea2f

Please sign in to comment.