Skip to content

Commit

Permalink
[deviantart] use private tokens for 'is_mature' posts (#4563)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Sep 24, 2023
1 parent 0c5d8b1 commit bb39779
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions gallery_dl/extractor/deviantart.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,11 @@ def _folder_urls(self, folders, category, extractor):
yield url, folder

def _update_content_default(self, deviation, content):
public = False if "premium_folder_data" in deviation else None
if "premium_folder_data" in deviation or deviation.get("is_mature"):
public = False
else:
public = None

data = self.api.deviation_download(deviation["deviationid"], public)
content.update(data)
deviation["is_original"] = True
Expand Down Expand Up @@ -382,6 +386,7 @@ def _update_token(self, deviation, content):
)

deviation["_fallback"] = (content["src"],)
deviation["is_original"] = True
content["src"] = (
"{}?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.{}.".format(
url,
Expand Down Expand Up @@ -1077,7 +1082,12 @@ def comments(self, id, target, offset=0):
def deviation(self, deviation_id, public=None):
"""Query and return info about a single Deviation"""
endpoint = "/deviation/" + deviation_id

deviation = self._call(endpoint, public=public)
if deviation.get("is_mature") and public is None and \
self.refresh_token_key:
deviation = self._call(endpoint, public=False)

if self.metadata:
self._metadata((deviation,))
if self.folders:
Expand Down Expand Up @@ -1233,8 +1243,12 @@ def _call(self, endpoint, fatal=True, log=True, public=None, **kwargs):
return data
if not fatal and status != 429:
return None
if data.get("error_description") == "User not found.":

error = data.get("error_description")
if error == "User not found.":
raise exception.NotFoundError("user or group")
if error == "Deviation not downloadable.":
raise exception.AuthorizationError()

self.log.debug(response.text)
msg = "API responded with {} {}".format(
Expand All @@ -1258,6 +1272,17 @@ def _call(self, endpoint, fatal=True, log=True, public=None, **kwargs):
self.log.error(msg)
return data

def _switch_tokens(self, results, params):
if len(results) < params["limit"]:
return True

if not self.extractor.jwt:
for item in results:
if item.get("is_mature"):
return True

return False

def _pagination(self, endpoint, params,
extend=True, public=None, unpack=False, key="results"):
warn = True
Expand All @@ -1276,17 +1301,18 @@ def _pagination(self, endpoint, params,
results = [item["journal"] for item in results
if "journal" in item]
if extend:
if public and len(results) < params["limit"]:
if public and self._switch_tokens(results, params):
if self.refresh_token_key:
self.log.debug("Switching to private access token")
public = False
continue
elif data["has_more"] and warn:
warn = False
self.log.warning(
"Private deviations detected! Run 'gallery-dl "
"oauth:deviantart' and follow the instructions to "
"be able to access them.")
"Private or mature deviations detected! "
"Run 'gallery-dl oauth:deviantart' and follow the "
"instructions to be able to access them.")

# "statusid" cannot be used instead
if results and "deviationid" in results[0]:
if self.metadata:
Expand Down

0 comments on commit bb39779

Please sign in to comment.