Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhaev26 committed Jun 1, 2024
1 parent f1e0a49 commit 75f1865
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,5 @@ dist
.pnp.*

chowkidaar
./bot/__pycache__
./bot/__pycache__
list.csv
Binary file modified __pycache__/config.cpython-310.pyc
Binary file not shown.
Binary file modified __pycache__/db.cpython-310.pyc
Binary file not shown.
Binary file modified __pycache__/dfa.cpython-310.pyc
Binary file not shown.
Binary file modified __pycache__/parse_message.cpython-310.pyc
Binary file not shown.
Binary file modified __pycache__/time_check.cpython-310.pyc
Binary file not shown.
14 changes: 11 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from db import connect_to_database, save_log, check_intext_validity, update_log, delete_log
import os
from time_check import can_send_message, is_in_time_bracket

from prometheus_client import Counter , start_http_server

intents = discord.Intents.default()
intents.messages = True # Ensure the bot can read messages
Expand All @@ -17,11 +17,16 @@
bot.activity = discord.Activity(type=discord.ActivityType.watching, name="for message updates")


messages_sent_total = Counter('discord_messages_sent_total', 'Total number of messages sent')
messages_edited_total = Counter('discord_messages_edited_total', 'Total number of messages edited')
messages_deleted_total = Counter('discord_messages_deleted_total', 'Total number of messages deleted')

start_http_server(8000)

@bot.event
async def on_ready():
print(f"Bot is ready. Logged in as {bot.user}")


@bot.event
async def on_message(message):
if message.author == bot.user:
Expand Down Expand Up @@ -53,6 +58,7 @@ async def on_message(message):

except Exception as e:
print(f"Error saving message to database: {e}")
messages_sent_total.inc()
await bot.process_commands(message)

@bot.event
Expand All @@ -78,6 +84,7 @@ async def on_message_edit(old_message, new_message):
await new_message.add_reaction("👀")
except Exception as e:
print(f"Error updating message in database: {e}")
messages_edited_total.inc()
await bot.process_commands(new_message)

@bot.event
Expand All @@ -92,7 +99,8 @@ async def on_message_delete(message):
print(f"Message with ID {discord_message_id} was marked deleted.")
except Exception as e:
print(f"Error deleting message from database: {e}")


messages_deleted_total.inc()
await bot.process_commands(message)


Expand Down
66 changes: 66 additions & 0 deletions shifter_bulk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import pandas as pd
import psycopg2
from psycopg2 import Error
from config import DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, DATABASE_HOST
from db import connect_to_database

def upload_csv_to_pg(csv_file, table_name):
try:
# Connect to the PostgreSQL database
conn = connect_to_database()

# Create a cursor object using the connection
cursor = conn.cursor()

create_table_query = """
CREATE TABLE IF NOT EXISTS student_list_2023 (
sl INT PRIMARY KEY,
student_id VARCHAR(10) NOT NULL,
name VARCHAR(255) NOT NULL,
gender VARCHAR(10) NOT NULL,
branch VARCHAR(50) NOT NULL
);
"""

try:
cursor.execute(create_table_query)
conn.commit()
print("Table created successfully!")
except Exception as e:
print("Error:", e)
conn.rollback()

# Read the CSV file into a pandas DataFrame
df = pd.read_csv(csv_file)

# Convert DataFrame to list of tuples
data = [tuple(row) for row in df.to_numpy()]

# Generate the SQL INSERT statement
cols = ', '.join(df.columns)
placeholders = ', '.join(['%s'] * len(df.columns))
sql = f"INSERT INTO {table_name} ({cols}) VALUES ({placeholders})"

# Execute the SQL statement to insert data into the table
cursor.executemany(sql, data)

# Commit the transaction
conn.commit()

print("Data uploaded successfully")

except (Exception, Error) as e:
print(f"Error uploading data to PostgreSQL: {e}")

finally:
# Close the cursor and connection
if conn:
cursor.close()
conn.close()


csv_file = "./list.csv"

table_name = "student_list_2023"

upload_csv_to_pg(csv_file, table_name)

0 comments on commit 75f1865

Please sign in to comment.