From f4d01d2d9f7a921f40a3b192637959ddf9129669 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Wed, 8 Jul 2026 22:51:44 +0200 Subject: ... --- accounts/gkleen@sif/systemd.nix | 43 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) (limited to 'accounts/gkleen@sif/systemd.nix') diff --git a/accounts/gkleen@sif/systemd.nix b/accounts/gkleen@sif/systemd.nix index 2ccbaea0..955fe9f5 100644 --- a/accounts/gkleen@sif/systemd.nix +++ b/accounts/gkleen@sif/systemd.nix @@ -306,6 +306,7 @@ in { Service = { Type = "notify"; CacheDirectory = "yt-dlp/%N"; + RuntimeDirectory = "yt-dlp/%N"; StandardInput = "socket"; StandardOutput = "journal"; WatchdogSec = "30min"; @@ -315,6 +316,7 @@ in { ]; ExecStart = ''${lib.getExe pkgs.dscp} ${pkgs.writers.writePython3 "yt-dlp" { libraries = with pkgs.python3Packages; [ yt-dlp sdnotify ]; + flakeIgnore = [ "E501" ]; } '' import json from yt_dlp import YoutubeDL, parse_options @@ -322,14 +324,46 @@ in { from sdnotify import SystemdNotifier from os import environ from pathlib import Path + import threading + import socketserver - n = SystemdNotifier() + status = None + status_notifier = threading.Condition() + + + class StatusRequestHandler(socketserver.BaseRequestHandler): + def handle(self): + with self.request.makefile('w', encoding='utf-8') as fh: + while True: + with status_notifier: + json.dump(status, fh) + fh.write('\n') + fh.flush() + status_notifier.wait() + + + class ThreadedUnixStreamServer(socketserver.ThreadingMixIn, socketserver.UnixStreamServer): + pass + + + def progress_hook(d): + global status + n.notify('WATCHDOG=1\nSTATUS=' + d['status']) + with status_notifier: + status = { + **{k: v for k, v in d.items() if k in {'status', 'downloaded_bytes', 'total_bytes', 'total_bytes_estimate', 'fragment_count', 'fragment_index'}}, + **{k: v for k, v in (d['info_dict'] if 'info_dict' in d else {}).items() if k in {'title', 'filename'}}, + } + status_notifier.notify_all() + + + n = SystemdNotifier(debug=True) args = json.load(stdin) ydl_opts = { **parse_options().ydl_opts, - 'progress_hooks': [lambda _d: n.notify('WATCHDOG=1')], - 'postprocessor_hooks': [lambda _d: n.notify('WATCHDOG=1')], + 'progress_hooks': [progress_hook], + 'postprocessor_hooks': [progress_hook], 'progress_with_newline': True, 'progress_delta': 5, 'paths': { @@ -339,6 +373,9 @@ in { 'noplaylist': True, **(args['params'] if 'params' in args else {}), } + status_server = ThreadedUnixStreamServer(str(Path(environ['RUNTIME_DIRECTORY']) / 'status.sock'), StatusRequestHandler) + status_server.daemon_threads = True + threading.Thread(target=status_server.serve_forever, daemon=True).start() with YoutubeDL(ydl_opts) as ytdl: n.notify('READY=1') ytdl.download(args['urls']) -- cgit v1.2.3