From a1bb32792b91e03c8440ff4fffdced4fe87f6e6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Sun, 16 Jun 2024 18:31:39 +0200 Subject: [PATCH] do not try to read from stdin when it is non-interactive (#5733) add '--no-input' command-line option and 'input' config file option to allow users to manually configure this --- docs/configuration.rst | 11 +++++++++++ docs/options.md | 1 + gallery_dl/extractor/common.py | 19 ++++++++++++++++++- gallery_dl/option.py | 5 +++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index c32b60ab..7dfce735 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -461,6 +461,17 @@ Description (see `getpass() `__). +extractor.*.input +----------------- +Type + ``bool`` +Default + ``true`` if `stdin` is attached to a terminal , + ``false`` otherwise +Description + Allow prompting the user for interactive input. + + extractor.*.netrc ----------------- Type diff --git a/docs/options.md b/docs/options.md index 77c81ed0..902bf874 100644 --- a/docs/options.md +++ b/docs/options.md @@ -30,6 +30,7 @@ -x, --input-file-delete FILE Download URLs found in FILE. Delete them after they were downloaded successfully. + --no-input Do not prompt for passwords/tokens ## Output Options: -q, --quiet Activate quiet mode diff --git a/gallery_dl/extractor/common.py b/gallery_dl/extractor/common.py index d2e39dee..fb079c57 100644 --- a/gallery_dl/extractor/common.py +++ b/gallery_dl/extractor/common.py @@ -11,6 +11,7 @@ import os import re import ssl +import sys import time import netrc import queue @@ -263,6 +264,10 @@ class Extractor(): time.sleep(seconds) def input(self, prompt, echo=True): + if not self._input: + raise exception.StopExtraction( + "User input required (%s)", prompt.strip(" :")) + if echo: try: return input(prompt) @@ -277,7 +282,13 @@ class Extractor(): password = None if username: - password = self.config("password") or util.LazyPrompt() + password = self.config("password") + if not password: + if not self._input: + raise exception.StopExtraction( + "User input required (password)") + password = util.LazyPrompt() + elif self.config("netrc", False): try: info = netrc.netrc().authenticators(self.category) @@ -298,6 +309,7 @@ class Extractor(): self._retries = self.config("retries", 4) self._timeout = self.config("timeout", 30) self._verify = self.config("verify", True) + self._input = self.config("input") self._proxies = util.build_proxy_map(self.config("proxy"), self.log) self._interval = util.build_duration_func( self.config("sleep-request", self.request_interval), @@ -307,6 +319,11 @@ class Extractor(): self.config("sleep-429", 60), ) + if self._input is None: + try: + self._input = sys.stdin.isatty() + except Exception: + self._input = False if self._retries < 0: self._retries = float("inf") if not self._retry_codes: diff --git a/gallery_dl/option.py b/gallery_dl/option.py index 387a0678..f31d5ac7 100644 --- a/gallery_dl/option.py +++ b/gallery_dl/option.py @@ -256,6 +256,11 @@ def build_parser(): help=("Download URLs found in FILE. " "Delete them after they were downloaded successfully."), ) + input.add_argument( + "--no-input", + dest="input", nargs=0, action=ConfigConstAction, const=False, + help=("Do not prompt for passwords/tokens"), + ) output = parser.add_argument_group("Output Options") output.add_argument(