From 913b8333cc1278e19849b68c5adee828874537e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Sun, 23 Feb 2020 21:11:19 +0100 Subject: [PATCH] write DeviantArt refresh-tokens to cache (#616) Writing the token is currently disabled by default and must be enabled with 'extractor.oauth.cache'. 'extractor.deviantart.refresh-token' must be set to '"cache"' to use the cached token. --- gallery_dl/extractor/deviantart.py | 8 ++++++-- gallery_dl/extractor/oauth.py | 8 +++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/gallery_dl/extractor/deviantart.py b/gallery_dl/extractor/deviantart.py index 98f8e5e9..90b27d15 100644 --- a/gallery_dl/extractor/deviantart.py +++ b/gallery_dl/extractor/deviantart.py @@ -961,11 +961,15 @@ class DeviantartAPI(): self.folders = extractor.config("folders", False) self.metadata = extractor.extra or extractor.config("metadata", False) - self.refresh_token = extractor.config("refresh-token") - self.client_id = extractor.config("client-id", self.CLIENT_ID) + self.client_id = extractor.config( + "client-id", self.CLIENT_ID) self.client_secret = extractor.config( "client-secret", self.CLIENT_SECRET) + self.refresh_token = extractor.config("refresh-token") + if self.refresh_token == "cache": + self.refresh_token = "#" + str(self.client_id) + self.log.debug( "Using %s API credentials (client-id %s)", "default" if self.client_id == self.CLIENT_ID else "custom", diff --git a/gallery_dl/extractor/oauth.py b/gallery_dl/extractor/oauth.py index b0706661..2f5b4297 100644 --- a/gallery_dl/extractor/oauth.py +++ b/gallery_dl/extractor/oauth.py @@ -98,7 +98,7 @@ class OAuthBase(Extractor): def _oauth2_authorization_code_grant( self, client_id, client_secret, auth_url, token_url, scope="read", key="refresh_token", auth=True, - message_template=None): + message_template=None, cache=None): """Perform an OAuth2 authorization code grant""" state = "gallery-dl_{}_{}".format( @@ -162,6 +162,11 @@ class OAuthBase(Extractor): client_secret=client_secret, )) + # write to cache + if cache and config.get(("extractor", self.category), "cache"): + cache.update("#" + str(client_id), data[key]) + self.log.info("Writing 'refresh-token' to cache") + class OAuthDeviantart(OAuthBase): subcategory = "deviantart" @@ -179,6 +184,7 @@ class OAuthDeviantart(OAuthBase): "https://www.deviantart.com/oauth2/authorize", "https://www.deviantart.com/oauth2/token", scope="browse", + cache=deviantart._refresh_token_cache, )