1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 18:53:21 +01:00

update output of 'oauth:…' (#616)

This commit is contained in:
Mike Fährmann 2020-05-31 15:34:05 +02:00
parent 0f459f340b
commit 864f4220d9
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

View File

@ -87,21 +87,22 @@ class OAuthBase(Extractor):
# exchange the request token for an access token
data = self.session.get(access_token_url, params=data).text
data = text.parse_query(data)
self.send(OAUTH1_MSG_TEMPLATE.format(
category=self.subcategory,
token=data["oauth_token"],
token_secret=data["oauth_token_secret"],
))
token = data["oauth_token"]
token_secret = data["oauth_token_secret"]
# write to cache
if self.cache:
key = (self.subcategory, self.session.auth.consumer_key)
tokens = (data["oauth_token"], data["oauth_token_secret"])
oauth._token_cache.update(key, tokens)
oauth._token_cache.update(key, (token, token_secret))
self.log.info("Writing tokens to cache")
# display tokens
self.send(self._generate_message(
("access-token", "access-token-secret"),
(token, token_secret),
))
def _oauth2_authorization_code_grant(
self, client_id, client_secret, auth_url, token_url,
scope="read", key="refresh_token", auth=True,
@ -156,24 +157,66 @@ class OAuthBase(Extractor):
self.send(data["error"])
return
# display token
part = key.partition("_")[0]
template = message_template or OAUTH2_MSG_TEMPLATE
self.send(template.format(
category=self.subcategory,
key=part,
Key=part.capitalize(),
token=data[key],
instance=getattr(self, "instance", ""),
client_id=client_id,
client_secret=client_secret,
))
# write to cache
if self.cache and cache:
cache.update("#" + str(client_id), data[key])
self.log.info("Writing 'refresh-token' to cache")
# display token
if message_template:
msg = message_template.format(
category=self.subcategory,
key=key.partition("_")[0],
token=data[key],
instance=getattr(self, "instance", ""),
client_id=client_id,
client_secret=client_secret,
)
else:
msg = self._generate_message(
("refresh-token",),
(data[key],),
)
self.send(msg)
def _generate_message(self, names, values):
if len(names) == 1:
_vh = "This value has"
_is = "is"
_it = "it"
_va = "this value"
else:
_vh = "These values have"
_is = "are"
_it = "them"
_va = "these values"
msg = "\nYour {} {}\n\n{}\n\n".format(
" and ".join("'" + n + "'" for n in names),
_is,
"\n".join(values),
)
if self.cache:
opt = self.oauth_config(names[0])
if opt is None or opt == "cache":
msg += _vh + " been cached and will automatically be used."
else:
msg += (
"Set 'extractor.{}.{}' to \"cache\" to use {}.".format(
self.subcategory, names[0], _it,
)
)
else:
msg += "Put " + _va + " into your configuration file as \n"
msg += " and\n".join(
"'extractor." + self.subcategory + "." + n + "'"
for n in names
)
msg += "."
return msg
class OAuthDeviantart(OAuthBase):
subcategory = "deviantart"
@ -326,49 +369,8 @@ class OAuthMastodon(OAuthBase):
return data
OAUTH1_MSG_TEMPLATE = """
Your Access Token and Access Token Secret are
{token}
{token_secret}
Put these values into your configuration file as
'extractor.{category}.access-token' and
'extractor.{category}.access-token-secret'.
Example:
{{
"extractor": {{
"{category}": {{
"access-token": "{token}",
"access-token-secret": "{token_secret}"
}}
}}
}}
"""
OAUTH2_MSG_TEMPLATE = """
Your {Key} Token is
{token}
Put this value into your configuration file as
'extractor.{category}.{key}-token'.
Example:
{{
"extractor": {{
"{category}": {{
"{key}-token": "{token}"
}}
}}
}}
"""
MASTODON_MSG_TEMPLATE = """
Your {Key} Token is
Your 'access-token' is
{token}