Skip to content

Commit

Permalink
update output of 'oauth:…' (#616)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed May 31, 2020
1 parent 0f459f3 commit 864f422
Showing 1 changed file with 65 additions and 63 deletions.
128 changes: 65 additions & 63 deletions gallery_dl/extractor/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,22 @@ def _oauth1_authorization_flow(

# 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,
Expand Down Expand Up @@ -156,24 +157,66 @@ def _oauth2_authorization_code_grant(
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"
Expand Down Expand Up @@ -326,49 +369,8 @@ def _register(self, instance):
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}
Expand Down

0 comments on commit 864f422

Please sign in to comment.