lanonna/jellyfin-fml/jellyfin-fml
2024-03-25 15:38:02 +01:00

89 lines
2.4 KiB
Python
Executable file

#!/usr/bin/python3
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'],
conf['configuration']['matrix_room'])
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, matrix_room, 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}'
response = {
'content': msg,
'source_message_id': None,
'room_id': matrix_room,
'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, matrix_room):
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, matrix_room, 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, matrix_room = read_conf()
main(mq_host, mq_user, mq_pass, matrix_room)