[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:
@@ -542,9 +542,9 @@ class DeviantartAPI():
|
|||||||
endpoint = "user/profile/" + username
|
endpoint = "user/profile/" + username
|
||||||
return self._call(endpoint, expect_error=True)
|
return self._call(endpoint, expect_error=True)
|
||||||
|
|
||||||
def authenticate(self):
|
def authenticate(self, refresh_token):
|
||||||
"""Authenticate the application by requesting an access 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
|
self.headers["Authorization"] = access_token
|
||||||
|
|
||||||
@cache(maxage=3590, keyarg=1)
|
@cache(maxage=3590, keyarg=1)
|
||||||
@@ -570,14 +570,14 @@ class DeviantartAPI():
|
|||||||
_refresh_token_cache(refresh_token, data["refresh_token"])
|
_refresh_token_cache(refresh_token, data["refresh_token"])
|
||||||
return "Bearer " + data["access_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"""
|
"""Call an API endpoint"""
|
||||||
url = "https://www.deviantart.com/api/v1/oauth2/" + endpoint
|
url = "https://www.deviantart.com/api/v1/oauth2/" + endpoint
|
||||||
while True:
|
while True:
|
||||||
if self.delay >= 0:
|
if self.delay >= 0:
|
||||||
time.sleep(2 ** self.delay)
|
time.sleep(2 ** self.delay)
|
||||||
|
|
||||||
self.authenticate()
|
self.authenticate(None if public else self.refresh_token)
|
||||||
response = self.session.get(
|
response = self.session.get(
|
||||||
url, headers=self.headers, params=params)
|
url, headers=self.headers, params=params)
|
||||||
data = response.json()
|
data = response.json()
|
||||||
@@ -604,7 +604,7 @@ class DeviantartAPI():
|
|||||||
|
|
||||||
def _pagination(self, endpoint, params=None):
|
def _pagination(self, endpoint, params=None):
|
||||||
while True:
|
while True:
|
||||||
data = self._call(endpoint, params)
|
data = self._call(endpoint, params, public=False)
|
||||||
if "results" in data:
|
if "results" in data:
|
||||||
yield from data["results"]
|
yield from data["results"]
|
||||||
if not data["has_more"]:
|
if not data["has_more"]:
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ class IdolcomplexPostExtractor(IdolcomplexExtractor,
|
|||||||
"tags_character": "shani_(the_witcher)",
|
"tags_character": "shani_(the_witcher)",
|
||||||
"tags_copyright": "the_witcher",
|
"tags_copyright": "the_witcher",
|
||||||
"tags_idol": "lyumos",
|
"tags_idol": "lyumos",
|
||||||
"tags_medium": "3:2_aspect_ratio cosplay",
|
"tags_medium": "3:2_aspect_ratio",
|
||||||
"tags_general": str,
|
"tags_general": str,
|
||||||
},
|
},
|
||||||
})]
|
})]
|
||||||
|
|||||||
Reference in New Issue
Block a user