Compare commits

...

3 Commits

Author SHA1 Message Date
76bf8f9f26 Merge branch 'main' of https://git.frik.su/n0one/CatBot 2025-07-31 16:55:53 +03:00
4094312c37 Saved prototype comments in code 2025-07-31 16:49:44 +03:00
f53783efd3 New commit 2.0:
- Added test file to gitignore
    - Created table of users with chat_ids(UserTable.sql)
    - Added check of empty table into get_last_id
    - Modified get_user to return a chat_id instead of username
    - Changed download function in ISwork to send URL instead
2025-07-31 16:45:25 +03:00
4 changed files with 33 additions and 20 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@
.DS_Store .DS_Store
.vscode/ .vscode/
__pycache__/ __pycache__/
test.py

View File

@ -3,8 +3,10 @@ from src import config
def get_last_id(cursor): def get_last_id(cursor):
cursor.execute("SELECT MAX(id) FROM usernames") cursor.execute("SELECT MAX(id) FROM Users")
id = cursor.fetchall()[0][0] id = cursor.fetchall()[0][0]
if id == None:
return 0
return id return id
def set_connection(): def set_connection():
@ -23,19 +25,19 @@ def close_connection(connection, cursor):
connection.close() connection.close()
#Functions don't close connection automatically, it has to be closed manually #Functions don't close connection automatically, it has to be closed manually
def add_user(username, connection, cursor): def add_user(username, chat_id, connection, cursor):
cursor.execute("INSERT INTO usernames VALUES (%s, %s);", (get_last_id(cursor) + 1, username)) cursor.execute("INSERT INTO Users VALUES (%s, %s, %s);", (get_last_id(cursor) + 1, username, chat_id))
connection.commit() connection.commit()
def delete_user(username, connection, cursor): def delete_user(username, connection, cursor):
cursor.execute("DELETE FROM usernames WHERE username = %s;", (username,)) cursor.execute("DELETE FROM Users WHERE username = %s;", (username,))
connection.commit() connection.commit()
def change_name(old_username, new_username, connection, cursor): def change_name(old_username, new_username, connection, cursor):
cursor.execute("UPDATE usernames SET username = %s WHERE username = %s;", (new_username, old_username)) cursor.execute("UPDATE Users SET username = %s WHERE username = %s;", (new_username, old_username))
connection.commit() connection.commit()
def get_user(id, cursor): def get_chat_id(id, cursor):
cursor.execute("SELECT username FROM usernames WHERE id = %s", (id,)) cursor.execute("SELECT chatid FROM Users WHERE id = %s", (id,))
username = cursor.fetchall()[0][0] chat_id = cursor.fetchall()[0][0]
return username return chat_id

View File

@ -1,6 +1,7 @@
from minio import Minio from minio import Minio
from random import randint from random import randint
import DBwork from datetime import timedelta
from src.Backend import DBwork
from src import config from src import config
@ -23,25 +24,28 @@ def getObjectExtension(client, currentDay, fileNumber):
if counter == fileNumber: if counter == fileNumber:
return obj.object_name.split('.')[-1] return obj.object_name.split('.')[-1]
def getFileNames(currentDay, client, username): def getImageName(currentDay, client):
maxFiles = getNumberofObjects(client, currentDay) maxFiles = getNumberofObjects(client, currentDay)
fileNumber = randint(1, maxFiles) fileNumber = randint(1, maxFiles)
fileExtension = getObjectExtension(client, currentDay, fileNumber) fileExtension = '.' + getObjectExtension(client, currentDay, fileNumber)
desiredFile = currentDay + '/' + str(fileNumber) + '.' + fileExtension desiredFile = currentDay + '/' + str(fileNumber) + fileExtension
downloadName = username + '.' + fileExtension return desiredFile
return desiredFile, downloadName
def downloadImage(currentDay, username): def getDownloadURL(currentDay):
client = _setClient() client = _setClient()
object_name, file_name = getFileNames(currentDay, client, username) object_name = getImageName(currentDay, client)
client.fget_object(config.bucket_name, object_name, file_name) url = client.presigned_get_object(
config.bucket_name,
object_name,
expires=timedelta(days=1))
return url
def downloadForAll(currentDay): def downloadForAll(currentDay):
cur, conn = DBwork.set_connection() cur, conn = DBwork.set_connection()
max_id = DBwork.get_last_id(cur) max_id = DBwork.get_last_id(cur)
for id in range(1, max_id + 1): for id in range(1, max_id + 1):
# chat_id = DBwork.get_user(id, cur) chat_id = DBwork.get_chat_id(id, cur)
# image_URL(currentDay, user) image_URL = getDownloadURL(currentDay)
# await bot.send_photo(chat_id = chat_id, photo = image_URL # await bot.send_photo(chat_id = chat_id, photo = image_URL
DBwork.close_connection(conn, cur) DBwork.close_connection(conn, cur)

View File

@ -0,0 +1,6 @@
create table Users(
Id serial primary key,
username character varying(32),
ChatId bigint not null,
constraint chat_id_unique unique(ChatId)
);