updated functions connected to images

This commit is contained in:
2025-07-27 20:36:52 +03:00
parent f5fe578a88
commit e35a82c0bc
3 changed files with 17 additions and 22 deletions

View File

@ -12,24 +12,25 @@ def _setClient():
) )
return minio_client return minio_client
def getImageName(currentDay): def getNumberofObjects(client, currentDay, bucket_name):
maxFiles = 2 objects = client.list_objects(bucket_name, prefix=currentDay+'/')
return sum(1 for _ in objects)
def getImageName(currentDay, client):
maxFiles = getNumberofObjects(client, currentDay, config.bucket_name)
fileNumber = randint(1, maxFiles) fileNumber = randint(1, maxFiles)
desiredFile = currentDay + '/' + str(fileNumber) + '.jpeg' desiredFile = currentDay + '/' + str(fileNumber) + '.jpeg'
return desiredFile return desiredFile
def downloadImage(currentDay, username): def downloadImage(currentDay, username):
bucket_name = "cat-images"
client = _setClient() client = _setClient()
client.fget_object(bucket_name, getImageName(currentDay), username + '.jpeg') client.fget_object(config.bucket_name, getImageName(currentDay, client), username + '.jpeg')
def downloadForAll(currentDay): def downloadForAll(currentDay):
cur, conn = DBwork.set_connection() cur, conn = DBwork.set_connection()
counter = 1 max_id = DBwork.get_last_id(cur)
user = DBwork.get_user(counter, cur) for id in range(1, max_id + 1):
while(user != 'Error'): user = DBwork.get_user(id, cur)
downloadImage(currentDay, user) downloadImage(currentDay, user)
counter += 1
user = DBwork.get_user(counter, cur)
DBwork.close_connection(conn, cur) DBwork.close_connection(conn, cur)

View File

@ -6,3 +6,4 @@ postgres_user = "postgres_user"
postgres_password = "postgres_password" postgres_password = "postgres_password"
host_name = "localhost" host_name = "localhost"
port = "5430" port = "5430"
bucket_name = "cat-images"

View File

@ -1,7 +1,7 @@
import psycopg2 import psycopg2
import config 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 usernames")
id = cursor.fetchall()[0][0] id = cursor.fetchall()[0][0]
return id return id
@ -23,25 +23,18 @@ def close_connection(connection, cursor):
#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, connection, cursor):
cursor = connection.cursor() cursor.execute("INSERT INTO usernames VALUES (%s, %s);", (get_last_id(cursor) + 1, username))
cursor.execute("INSERT INTO usernames VALUES (%s, %s);", (_get_last_id(cursor) + 1, username))
connection.commit() connection.commit()
def delete_user(username, connection, cursor): def delete_user(username, connection, cursor):
cursor = connection.cursor()
cursor.execute("DELETE FROM usernames WHERE username = %s;", (username,)) cursor.execute("DELETE FROM usernames 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 = connection.cursor()
cursor.execute("UPDATE usernames SET username = %s WHERE username = %s;", (new_username, old_username)) cursor.execute("UPDATE usernames SET username = %s WHERE username = %s;", (new_username, old_username))
connection.commit() connection.commit()
def get_user(id, cursor): def get_user(id, cursor):
max_id = _get_last_id(cursor) cursor.execute("SELECT username FROM usernames WHERE id = %s", (id,))
if id <= max_id: username = cursor.fetchall()[0][0]
cursor.execute("SELECT username FROM usernames WHERE id = %s", (id,)) return username
username = cursor.fetchall()[0][0]
else:
username = 'Error'
return username, cursor