improve a1bb3279, fix oauth:pixiv (#5757)

Check 'input' option only when required.

This also fixes an exception in oauth:pixiv caused by using the same
'_input' name  as a method defined there.
pull/3626/merge
Mike Fährmann 3 months ago
parent 9671bd6d40
commit 60b4541199
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -264,9 +264,7 @@ class Extractor():
time.sleep(seconds) time.sleep(seconds)
def input(self, prompt, echo=True): def input(self, prompt, echo=True):
if not self._input: self._check_input_allowed(prompt)
raise exception.StopExtraction(
"User input required (%s)", prompt.strip(" :"))
if echo: if echo:
try: try:
@ -276,6 +274,19 @@ class Extractor():
else: else:
return getpass.getpass(prompt) return getpass.getpass(prompt)
def _check_input_allowed(self, prompt=""):
input = self.config("input")
if input is None:
try:
input = sys.stdin.isatty()
except Exception:
input = False
if not input:
raise exception.StopExtraction(
"User input required (%s)", prompt.strip(" :"))
def _get_auth_info(self): def _get_auth_info(self):
"""Return authentication information as (username, password) tuple""" """Return authentication information as (username, password) tuple"""
username = self.config("username") username = self.config("username")
@ -284,9 +295,7 @@ class Extractor():
if username: if username:
password = self.config("password") password = self.config("password")
if not password: if not password:
if not self._input: self._check_input_allowed("password")
raise exception.StopExtraction(
"User input required (password)")
password = util.LazyPrompt() password = util.LazyPrompt()
elif self.config("netrc", False): elif self.config("netrc", False):
@ -309,7 +318,6 @@ class Extractor():
self._retries = self.config("retries", 4) self._retries = self.config("retries", 4)
self._timeout = self.config("timeout", 30) self._timeout = self.config("timeout", 30)
self._verify = self.config("verify", True) self._verify = self.config("verify", True)
self._input = self.config("input")
self._proxies = util.build_proxy_map(self.config("proxy"), self.log) self._proxies = util.build_proxy_map(self.config("proxy"), self.log)
self._interval = util.build_duration_func( self._interval = util.build_duration_func(
self.config("sleep-request", self.request_interval), self.config("sleep-request", self.request_interval),
@ -319,11 +327,6 @@ class Extractor():
self.config("sleep-429", 60), 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: if self._retries < 0:
self._retries = float("inf") self._retries = float("inf")
if not self._retry_codes: if not self._retry_codes:

@ -424,7 +424,7 @@ class OAuthPixiv(OAuthBase):
"code_challenge_method": "S256", "code_challenge_method": "S256",
"client": "pixiv-android", "client": "pixiv-android",
} }
code = self.open(url, params, self._input) code = self.open(url, params, self._input_code)
url = "https://oauth.secure.pixiv.net/auth/token" url = "https://oauth.secure.pixiv.net/auth/token"
headers = { headers = {
@ -459,7 +459,7 @@ class OAuthPixiv(OAuthBase):
stdout_write(self._generate_message(("refresh-token",), (token,))) stdout_write(self._generate_message(("refresh-token",), (token,)))
def _input(self): def _input_code(self):
stdout_write("""\ stdout_write("""\
1) Open your browser's Developer Tools (F12) and switch to the Network tab 1) Open your browser's Developer Tools (F12) and switch to the Network tab
2) Login 2) Login
@ -471,5 +471,5 @@ class OAuthPixiv(OAuthBase):
like the entire URL or several query parameters. like the entire URL or several query parameters.
""") """)
code = input("code: ") code = self.input("code: ")
return code.rpartition("=")[2].strip() return code.rpartition("=")[2].strip()

Loading…
Cancel
Save