From 6b9e832db7dedcd6f2e7be1bf44f56a91ff18737 Mon Sep 17 00:00:00 2001 From: pukkandan Date: Tue, 24 May 2022 17:30:28 +0530 Subject: [PATCH] `--config-location -` to provide options interactively --- README.md | 5 +++-- yt_dlp/__init__.py | 14 +++++--------- yt_dlp/options.py | 4 ++-- yt_dlp/utils.py | 9 +++++++++ 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index e71a150fd..fbbd6f3ab 100644 --- a/README.md +++ b/README.md @@ -358,8 +358,9 @@ ## General Options: defined in the current file --config-locations PATH Location of the main configuration file; either the path to the config or its - containing directory. Can be used multiple - times and inside other configuration files + containing directory ("-" for stdin). Can be + used multiple times and inside other + configuration files --flat-playlist Do not extract the videos of a playlist, only list them --no-flat-playlist Extract the videos of a playlist diff --git a/yt_dlp/__init__.py b/yt_dlp/__init__.py index 66fee95cd..12751b009 100644 --- a/yt_dlp/__init__.py +++ b/yt_dlp/__init__.py @@ -9,7 +9,7 @@ import re import sys -from .compat import compat_getpass, compat_os_name, compat_shlex_quote +from .compat import compat_getpass, compat_shlex_quote from .cookies import SUPPORTED_BROWSERS, SUPPORTED_KEYRINGS from .downloader import FileDownloader from .extractor import GenericIE, list_extractor_classes @@ -42,6 +42,7 @@ parse_duration, preferredencoding, read_batch_urls, + read_stdin, render_table, setproctitle, std_headers, @@ -63,14 +64,9 @@ def get_urls(urls, batchfile, verbose): batch_urls = [] if batchfile is not None: try: - if batchfile == '-': - write_string('Reading URLs from stdin - EOF (%s) to end:\n' % ( - 'Ctrl+Z' if compat_os_name == 'nt' else 'Ctrl+D')) - batchfd = sys.stdin - else: - batchfd = open( - expand_path(batchfile), encoding='utf-8', errors='ignore') - batch_urls = read_batch_urls(batchfd) + batch_urls = read_batch_urls( + read_stdin('URLs') if batchfile == '-' + else open(expand_path(batchfile), encoding='utf-8', errors='ignore')) if verbose: write_string('[debug] Batch file urls: ' + repr(batch_urls) + '\n') except OSError: diff --git a/yt_dlp/options.py b/yt_dlp/options.py index c0718e007..65391410f 100644 --- a/yt_dlp/options.py +++ b/yt_dlp/options.py @@ -366,8 +366,8 @@ def _alias_callback(option, opt_str, value, parser, opts, nargs): '--config-locations', dest='config_locations', metavar='PATH', action='append', help=( - 'Location of the main configuration file; either the path to the config or its containing directory. ' - 'Can be used multiple times and inside other configuration files')) + 'Location of the main configuration file; either the path to the config or its containing directory ' + '("-" for stdin). Can be used multiple times and inside other configuration files')) general.add_option( '--flat-playlist', action='store_const', dest='extract_flat', const='in_playlist', default=False, diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py index 2e3c51562..6701492f2 100644 --- a/yt_dlp/utils.py +++ b/yt_dlp/utils.py @@ -5163,6 +5163,12 @@ def parse_http_range(range): return int(crg.group(1)), int_or_none(crg.group(2)), int_or_none(crg.group(3)) +def read_stdin(what): + eof = 'Ctrl+Z' if compat_os_name == 'nt' else 'Ctrl+D' + write_string(f'Reading {what} from STDIN - EOF ({eof}) to end:\n') + return sys.stdin + + class Config: own_args = None parsed_args = None @@ -5188,6 +5194,9 @@ def init(self, args=None, filename=None): self.parsed_args, self.filename = args, filename for location in opts.config_locations or []: + if location == '-': + self.append_config(shlex.split(read_stdin('options'), comments=True), label='stdin') + continue location = os.path.join(directory, expand_path(location)) if os.path.isdir(location): location = os.path.join(location, 'yt-dlp.conf')