[deviantart] reduce refresh-token usage

Instead of using a refresh-token-based access-token for every API
request, they are now only used for paginated results.

API requests to get a user's profile and the original download URL
now always use a public access-token.
This commit is contained in:
Mike Fährmann
2018-07-23 21:40:59 +02:00
parent 54a0d72dc8
commit d98e47817d
2 changed files with 6 additions and 6 deletions

View File

@@ -542,9 +542,9 @@ class DeviantartAPI():
endpoint = "user/profile/" + username
return self._call(endpoint, expect_error=True)
def authenticate(self):
def authenticate(self, refresh_token):
"""Authenticate the application by requesting an access token"""
access_token = self._authenticate_impl(self.refresh_token)
access_token = self._authenticate_impl(refresh_token)
self.headers["Authorization"] = access_token
@cache(maxage=3590, keyarg=1)
@@ -570,14 +570,14 @@ class DeviantartAPI():
_refresh_token_cache(refresh_token, data["refresh_token"])
return "Bearer " + data["access_token"]
def _call(self, endpoint, params=None, expect_error=False):
def _call(self, endpoint, params=None, expect_error=False, public=True):
"""Call an API endpoint"""
url = "https://www.deviantart.com/api/v1/oauth2/" + endpoint
while True:
if self.delay >= 0:
time.sleep(2 ** self.delay)
self.authenticate()
self.authenticate(None if public else self.refresh_token)
response = self.session.get(
url, headers=self.headers, params=params)
data = response.json()
@@ -604,7 +604,7 @@ class DeviantartAPI():
def _pagination(self, endpoint, params=None):
while True:
data = self._call(endpoint, params)
data = self._call(endpoint, params, public=False)
if "results" in data:
yield from data["results"]
if not data["has_more"]:

View File

@@ -53,7 +53,7 @@ class IdolcomplexPostExtractor(IdolcomplexExtractor,
"tags_character": "shani_(the_witcher)",
"tags_copyright": "the_witcher",
"tags_idol": "lyumos",
"tags_medium": "3:2_aspect_ratio cosplay",
"tags_medium": "3:2_aspect_ratio",
"tags_general": str,
},
})]