[twitter] implement 'relogin' option (#5445)

pull/5479/head
Mike Fährmann 5 months ago
parent e02d2ff45d
commit 85bbb59483
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -3916,6 +3916,19 @@ Description
* ``"wait"``: Wait until rate limit reset * ``"wait"``: Wait until rate limit reset
extractor.twitter.relogin
-------------------------
Type
``bool``
Default
``true``
Description
| When receiving a "Could not authenticate you" error while logged in with
`username & passeword <extractor.*.username & .password_>`__,
| refresh the current login session and
try to continue from where it left off.
extractor.twitter.locked extractor.twitter.locked
------------------------ ------------------------
Type Type

@ -1294,42 +1294,62 @@ class TwitterAPI():
if csrf_token: if csrf_token:
self.headers["x-csrf-token"] = csrf_token self.headers["x-csrf-token"] = csrf_token
if response.status_code < 400: try:
data = response.json() data = response.json()
except ValueError:
data = {"errors": ({"message": response.text},)}
errors = data.get("errors")
if not errors:
return data
retry = False
for error in errors:
msg = error.get("message") or "Unspecified"
self.log.debug("API error: '%s'", msg)
if "this account is temporarily locked" in msg:
msg = "Account temporarily locked"
if self.extractor.config("locked") != "wait":
raise exception.AuthorizationError(msg)
self.log.warning("%s. Press ENTER to retry.", msg)
try:
input()
except (EOFError, OSError):
pass
retry = True
errors = data.get("errors") elif "Could not authenticate you" in msg:
if not errors: if not self.extractor.config("relogin", True):
return data continue
retry = False username, password = self.extractor._get_auth_info()
for error in errors: if not username:
msg = error.get("message") or "Unspecified" continue
self.log.debug("API error: '%s'", msg)
if "this account is temporarily locked" in msg: _login_impl.invalidate(username)
msg = "Account temporarily locked" self.extractor.cookies_update(
if self.extractor.config("locked") != "wait": _login_impl(self.extractor, username, password))
raise exception.AuthorizationError(msg) self.__init__(self.extractor)
self.log.warning("%s. Press ENTER to retry.", msg) retry = True
try:
input()
except (EOFError, OSError):
pass
retry = True
elif msg.lower().startswith("timeout"): elif msg.lower().startswith("timeout"):
retry = True retry = True
if not retry: if retry:
return data if self.headers["x-twitter-auth-type"]:
elif self.headers["x-twitter-auth-type"]:
self.log.debug("Retrying API request") self.log.debug("Retrying API request")
continue continue
else:
# fall through to "Login Required"
response.status_code = 404
# fall through to "Login Required" if response.status_code < 400:
response.status_code = 404 return data
elif response.status_code in (403, 404) and \
if response.status_code == 429: not self.headers["x-twitter-auth-type"]:
raise exception.AuthorizationError("Login required")
elif response.status_code == 429:
# rate limit exceeded # rate limit exceeded
if self.extractor.config("ratelimit") == "abort": if self.extractor.config("ratelimit") == "abort":
raise exception.StopExtraction("Rate limit exceeded") raise exception.StopExtraction("Rate limit exceeded")
@ -1339,18 +1359,11 @@ class TwitterAPI():
self.extractor.wait(until=until, seconds=seconds) self.extractor.wait(until=until, seconds=seconds)
continue continue
if response.status_code in (403, 404) and \
not self.headers["x-twitter-auth-type"]:
raise exception.AuthorizationError("Login required")
# error # error
try: try:
data = response.json() errors = ", ".join(e["message"] for e in errors)
errors = ", ".join(e["message"] for e in data["errors"])
except ValueError:
errors = response.text
except Exception: except Exception:
errors = data.get("errors", "") pass
raise exception.StopExtraction( raise exception.StopExtraction(
"%s %s (%s)", response.status_code, response.reason, errors) "%s %s (%s)", response.status_code, response.reason, errors)

Loading…
Cancel
Save