- Added a script that batch adds images to a bucket of choice

- Cleaned some junk strings
This commit is contained in:
2025-08-17 18:17:40 +03:00
parent 3692c0233d
commit 22f433828e
5 changed files with 102 additions and 60 deletions

98
minioPopulator.py Normal file
View File

@ -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])

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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)