lanonna/jellyfin-fml/main.py
2024-03-25 15:01:17 +01:00

75 lines
2 KiB
Python

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)