diff --git a/jellyfin-fml/main.py b/jellyfin-fml/main.py new file mode 100644 index 0000000..8aa0933 --- /dev/null +++ b/jellyfin-fml/main.py @@ -0,0 +1,75 @@ +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 pop(): + 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) + a.append((file, creation_time)) + if a: + a.sort(key=lambda t: t[1]) + return a[0] + else: + return None + + +def send_to_mq(mq_host, mq_user, mq_pass, space_rem): + 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}' + 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() + send_to_mq(mq_host, mq_user, mq_pass, space_rem) + if space_rem < 1.0: + send_to_mq(mq_host, mq_user, mq_pass, space_rem) + + while remaining_space_gb() < 2.0: + file = pop() + os.remove(file) + time.sleep(60) + + +if __name__ == '__main__': + mq_host, mq_user, mq_pass = read_conf() + main(mq_host, mq_user, mq_pass) diff --git a/lanonna/echobot.py b/lanonna/echobot.py index 22f0cf8..6c0b4b2 100644 --- a/lanonna/echobot.py +++ b/lanonna/echobot.py @@ -21,8 +21,6 @@ async def run(mq_url): async for message in queue_iter: async with message.process(): body = message.body.decode() - # br = protocol.json_to_bot_response(body) - print(body) br = json.loads(body) response = { 'content': br['content'],