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
def getImageName(currentDay):
maxFiles = 2
def getNumberofObjects(client, currentDay, bucket_name):
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)
desiredFile = currentDay + '/' + str(fileNumber) + '.jpeg'
return desiredFile
def downloadImage(currentDay, username):
bucket_name = "cat-images"
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):
cur, conn = DBwork.set_connection()
counter = 1
user = DBwork.get_user(counter, cur)
while(user != 'Error'):
max_id = DBwork.get_last_id(cur)
for id in range(1, max_id + 1):
user = DBwork.get_user(id, cur)
downloadImage(currentDay, user)
counter += 1
user = DBwork.get_user(counter, cur)
DBwork.close_connection(conn, cur)

View File

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

View File

@ -1,7 +1,7 @@
import psycopg2
import config
def _get_last_id(cursor):
def get_last_id(cursor):
cursor.execute("SELECT MAX(id) FROM usernames")
id = cursor.fetchall()[0][0]
return id
@ -23,25 +23,18 @@ def close_connection(connection, cursor):
#Functions don't close connection automatically, it has to be closed manually
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()
def delete_user(username, connection, cursor):
cursor = connection.cursor()
cursor.execute("DELETE FROM usernames WHERE username = %s;", (username,))
connection.commit()
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))
connection.commit()
def get_user(id, cursor):
max_id = _get_last_id(cursor)
if id <= max_id:
cursor.execute("SELECT username FROM usernames WHERE id = %s", (id,))
username = cursor.fetchall()[0][0]
else:
username = 'Error'
return username, cursor
return username