From 726af5454eda3aa44b97312b856977a766e52615 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Thu, 15 May 2025 11:25:15 +0200 Subject: ... --- accounts/gkleen@sif/utils/async-yt-dlp.nix | 57 ++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 accounts/gkleen@sif/utils/async-yt-dlp.nix (limited to 'accounts/gkleen@sif/utils') diff --git a/accounts/gkleen@sif/utils/async-yt-dlp.nix b/accounts/gkleen@sif/utils/async-yt-dlp.nix new file mode 100644 index 00000000..c3b82ec5 --- /dev/null +++ b/accounts/gkleen@sif/utils/async-yt-dlp.nix @@ -0,0 +1,57 @@ +{ writers, python3Packages, ... }: + +writers.writePython3Bin "async-yt-dlp" { + libraries = with python3Packages; [ yt-dlp ]; + flakeIgnore = ["E501"]; +} '' +import sys +import os + +import yt_dlp +import yt_dlp.options +from collections import namedtuple +import socket +from pathlib import Path +import json + +create_parser = yt_dlp.options.create_parser + + +def parse_patched_options(opts): + patched_parser = create_parser() + patched_parser.defaults.update({ + 'ignoreerrors': False, + 'retries': 0, + 'fragment_retries': 0, + 'extract_flat': False, + 'concat_playlist': 'never', + }) + yt_dlp.options.create_parser = lambda: patched_parser + try: + return yt_dlp.parse_options(opts) + finally: + yt_dlp.options.create_parser = create_parser + + +default_opts = parse_patched_options([]).ydl_opts + + +def cli_to_api(opts): + opts = parse_patched_options(opts) + urls = opts.urls + opts = opts.ydl_opts + + diff = {k: v for k, v in opts.items() if default_opts[k] != v} + if 'postprocessors' in diff: + diff['postprocessors'] = [pp for pp in diff['postprocessors'] + if pp not in default_opts['postprocessors']] + return namedtuple('Options', ('params', 'urls'))(diff, urls) + + +if __name__ == '__main__': + opts = cli_to_api(sys.argv[1:]) + with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock: + sock.connect(str(Path(os.environ["XDG_RUNTIME_DIR"]) / "yt-dlp.sock").encode('utf-8')) + with sock.makefile(mode='w', buffering=1, encoding='utf-8') as fh: + json.dump(opts._asdict(), fh) +'' -- cgit v1.2.3