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
.vscode/
__pycache__/
test.py

View File

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

View File

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