From 22f433828eaf2311e0f102ea0c0f613b64251402 Mon Sep 17 00:00:00 2001 From: n0body Date: Sun, 17 Aug 2025 18:17:40 +0300 Subject: [PATCH] - Added a script that batch adds images to a bucket of choice - Cleaned some junk strings --- minioPopulator.py | 98 +++++++++++++++++++++++++++++++++++ src/Backend/DBwork.py | 2 +- src/Backend/ISwork.py | 1 + src/Backend/minioPopulator.py | 58 --------------------- src/Frontend/createbot.py | 3 +- 5 files changed, 102 insertions(+), 60 deletions(-) create mode 100644 minioPopulator.py delete mode 100644 src/Backend/minioPopulator.py diff --git a/minioPopulator.py b/minioPopulator.py new file mode 100644 index 0000000..e06bcfc --- /dev/null +++ b/minioPopulator.py @@ -0,0 +1,98 @@ +from minio import Minio +from minio.error import S3Error +from src import config +import glob +import os + + +def upload_image(bucket_name, object_name, file_path): + try: + if file_path.split('.')[-1] == 'jpeg': + client.fput_object(bucket_name, object_name, file_path, 'image/jpeg') + print(f'image {object_name} with local path \'{file_path}\' was successfully uploaded') + elif file_path.split('.')[-1] == 'jpg': + client.fput_object(bucket_name, object_name, file_path, 'image/jpg') + print(f'image {object_name} with local path \'{file_path}\' was successfully uploaded') + elif file_path.split('.')[-1] == 'png': + client.fput_object(bucket_name, object_name, file_path, 'image/png') + print(f'image {object_name} with local path \'{file_path}\' was successfully uploaded') + elif file_path.split('.')[-1] == 'pjpeg': + client.fput_object(bucket_name, object_name, file_path, 'image/pjpeg') + print(f'image {object_name} with local path \'{file_path}\' was successfully uploaded') + + except S3Error as e: + print(f'Error during MinIO operation: {e}') + + +# print('Please enter your MinIO instance cloud address:') +# IS_address = input() +# print('Please enter MinIO access key:') +# access_key = input() +# print('Please enter MinIO secret key:') +# secret_key = input() +# print('Please enter bucket name:') +# bucket = input() + +IS_address = config.IS_address +access_key = config.acc_key +secret_key = config.sec_key +bucket = config.bucket_name + +client = Minio( + IS_address, + access_key=access_key, + secret_key=secret_key, + secure=False +) + +is_found = client.bucket_exists(bucket) +if not is_found: + client.make_bucket(bucket) + print(f"Bucket '{bucket}' does not exist. A new bucket with that name have been created.") +else: + print(f"Bucket '{bucket}' exists. Proceeding...") + +script_path = os.path.abspath(__file__).replace('\\', '/') +for i in range(len(script_path) - 1, 0, -1): + if script_path[i] == '/': + script_path = script_path[:i + 1] + break +paths = glob.glob(script_path + '*') +inter_paths, shortened_paths = [], [] + +for i in range(len(paths)): + paths[i] = os.path.abspath(paths[i]).replace('\\', '/') + +for index, path in enumerate(paths, 0): + for j in range(len(path) - 1, 0, -1): + if path[j] == '/': + inter_paths.append(paths[index][j:]) + +for path in inter_paths: + if path.count('/') == 1 and (path[-3:] == 'jpg' or path[-3:] == 'png' or path[-4:] == 'jpeg' or path[-5:] == 'pjpeg'): + shortened_paths.append(path) + +c = 0 +for i in range(len(paths)): + i -= c + if not('.jpg' in paths[i] or '.jpeg' in paths[i] or '.pjpeg' in paths[i] or '.png' in paths[i]): + del paths[i] + c += 1 + if len(paths) < i: + break + +starting_weekday = 0 +for n in range(1, 8): + objects = client.list_objects(config.bucket_name, prefix=str(n) + '/') + obj_len = sum(1 for _ in objects) + if n == 1: + if obj_len < sum(1 for _ in client.list_objects(config.bucket_name, prefix='7/')): + starting_weekday = n - 1 + elif prev_obj_len > obj_len: + starting_weekday = n - 1 + break + prev_obj_len = obj_len + +for i in range(0, len(shortened_paths)): + weekday = (i + starting_weekday) % 7 + 1 + upload_image(bucket, str(weekday) + shortened_paths[i], paths[i]) \ No newline at end of file diff --git a/src/Backend/DBwork.py b/src/Backend/DBwork.py index 18f4e86..1f5edc2 100644 --- a/src/Backend/DBwork.py +++ b/src/Backend/DBwork.py @@ -43,7 +43,7 @@ def change_images_amount(chat_id, amount, connection, cursor): connection.commit() -def get_images_amount(chat_id, connection, cursor): +def get_images_amount(chat_id, cursor): cursor.execute('SELECT images_amount FROM Users WHERE chat_id = %s;', (chat_id,)) images_amount = cursor.fetchall()[0][0] return images_amount diff --git a/src/Backend/ISwork.py b/src/Backend/ISwork.py index afc16db..40f96c9 100644 --- a/src/Backend/ISwork.py +++ b/src/Backend/ISwork.py @@ -29,6 +29,7 @@ def getObjectExtension(client, currentDay, fileNumber): object_extension = obj.object_name.split('.')[-1] return object_extension + def getImageName(currentDay, client): maxFiles = getNumberofObjects(client, currentDay) fileNumber = randint(1, maxFiles) diff --git a/src/Backend/minioPopulator.py b/src/Backend/minioPopulator.py deleted file mode 100644 index d738bed..0000000 --- a/src/Backend/minioPopulator.py +++ /dev/null @@ -1,58 +0,0 @@ -from minio import Minio -from minio.error import S3Error -from src import config -import glob -import os - - -access_key = config.acc_key -secret_key = config.sec_key -bucket = config.bucket_name - - -def upload_file(access_key, secret_key, bucket_name, object_name, file_path): - try: - client = Minio( - config.IS_address, - access_key = access_key, - secret_key = secret_key, - secure = False - ) - if file_path.split('.')[-1] == 'jpeg': - client.fput_object(bucket_name, object_name, file_path, 'image/jpeg') - if file_path.split('.')[-1] == 'jpg': - client.fput_object(bucket_name, object_name, file_path, 'image/jpg') - elif file_path.split('.')[-1] == 'png': - client.fput_object(bucket_name, object_name, file_path, 'image/png') - elif file_path.split('.')[-1] == 'pjpeg': - client.fput_object(bucket_name, object_name, file_path, 'image/pjpeg') - print(f'image {object_name} with local path \'{file_path}\' was successfully uploaded') - - except S3Error: - print(f'Error during MinIO operation: {S3Error}') - - -paths = glob.glob('../../files/*/*') -shortened_paths = [] - -for i in range(len(paths)): - paths[i] = os.path.abspath(paths[i]).replace('\\', '/') - -for i in range(len(paths)): - shortened_paths.append(paths[i][paths[i].find('files/') + 6:]) - -# for i in range(0, len(paths)): -# upload_file(access_key, secret_key, bucket, f'{shortened_paths[i]}', f'{paths[i]}') -# print(paths[i]) - - - -client = Minio( - config.IS_address, - access_key = access_key, - secret_key = secret_key, - secure = False - ) -objects = client.list_objects(config.bucket_name, prefix='1' + '/') -for obj in objects: - print(obj.object_name) diff --git a/src/Frontend/createbot.py b/src/Frontend/createbot.py index acabf63..40d5a00 100644 --- a/src/Frontend/createbot.py +++ b/src/Frontend/createbot.py @@ -102,7 +102,7 @@ async def send_daily_images(): max_id = DBwork.get_last_id(cursor) for id in range(1, max_id + 1): chat_id = DBwork.get_chat_id(id, cursor) - images_amount = DBwork.get_images_amount(chat_id, connection, cursor) + images_amount = DBwork.get_images_amount(chat_id, cursor) for _ in range(images_amount): image_link = URLInputFile(ISwork.getDownloadURL(current_day), filename=datetime.now().strftime('%Y_%m_%d_%H_%M_%S')) await bot.send_photo(chat_id = chat_id, photo = image_link) @@ -115,3 +115,4 @@ scheduler.add_job(send_daily_images, 'cron', hour = 12, minute = 0) async def main(): scheduler.start() await dp.start_polling(bot) +