import os import time def read_conf(): import tomllib with open('/etc/jellyfin-fml.toml', 'rb') as fp: conf = tomllib.load(fp) return (conf['configuration']['mq_host'], conf['configuration']['mq_user'], conf['configuration']['mq_password']) def remaining_space_gb(): from shutil import disk_usage total, used, free = disk_usage("/") return free / 1024**3 def all_transcodes(): folder_path = '/var/lib/jellyfin/transcodes' files = os.listdir(folder_path) a = [] for file in files: file_path = os.path.join(folder_path, file) creation_time = os.path.getctime(file_path) space = os.path.getsize(file_path) a.append((file, creation_time, space)) return a def pop(files): if files: files.sort(key=lambda t: t[1]) return files[0] else: return None def send_to_mq(mq_host, mq_user, mq_pass, space_rem, transcode_space): import pika import json creds = pika.PlainCredentials(mq_user, mq_pass) params = pika.ConnectionParameters(mq_host, 5672, "/", creds) conn = pika.BlockingConnection(params) channel = conn.channel() space_rem = round(space_rem, 2) msg = f'Paperino, manca spazio su disco: {space_rem}|{transcode_space}' ROOM_ID = '!KABwGlTSmXAbzCOhCX:goulash.lezzo.org' response = { 'content': msg, 'source_message_id': None, 'room_id': ROOM_ID, 'as_reply': False, 'as_markdown': False, } mq_msg = json.dumps(response).encode() # channel.queue_declare(queue='jellyfin-fml') channel.basic_publish(exchange='', routing_key='lanonna', body=mq_msg) conn.close() def main(mq_host, mq_user, mq_pass): while True: space_rem = remaining_space_gb() if space_rem >= 1.0: time.sleep(60) else: files = all_transcodes() total_space = sum(map(lambda t: t[2], files)) send_to_mq(mq_host, mq_user, mq_pass, space_rem, total_space) while remaining_space_gb() < 2.0: file = pop(files) if not file: break else: os.remove(file) files = all_transcodes() if __name__ == '__main__': mq_host, mq_user, mq_pass = read_conf() main(mq_host, mq_user, mq_pass)