[deviantart:avatar] add 'formats' option (#4995)

This commit is contained in:
Mike Fährmann
2024-01-10 17:13:34 +01:00
parent 5c43098a1a
commit 39904c9e4e
3 changed files with 68 additions and 18 deletions

View File

@@ -1559,6 +1559,19 @@ Description
Minimum wait time in seconds before API requests.
extractor.deviantart.avatar.formats
-----------------------------------
Type
``list`` of ``strings``
Example
``["original.jpg", "big.jpg", "big.gif", ".png"]``
Description
Avatar URL formats to return.
| Each format is parsed as ``SIZE.EXT``.
| Leave ``SIZE`` empty to download the regular, small avatar format.
extractor.[E621].metadata
-------------------------
Type

View File

@@ -547,23 +547,45 @@ class DeviantartAvatarExtractor(DeviantartExtractor):
example = "https://www.deviantart.com/USER/avatar/"
def deviations(self):
profile = self.api.user_profile(self.user.lower())
if profile:
url = profile["user"]["usericon"]
return ({
"author" : profile["user"],
"category" : "avatar",
"index" : text.parse_int(url.rpartition("?")[2]),
"is_deleted" : False,
"is_downloadable": False,
"published_time" : 0,
"title" : "avatar",
"stats" : {"comments": 0},
"content" : {
"src": url.replace("/avatars/", "/avatars-big/", 1),
},
},)
return ()
name = self.user.lower()
profile = self.api.user_profile(name)
if not profile:
return ()
user = profile["user"]
icon = user["usericon"]
index = icon.rpartition("?")[2]
formats = self.config("formats")
if not formats:
url = icon.replace("/avatars/", "/avatars-big/", 1)
return (self._make_deviation(url, user, index, ""),)
if isinstance(formats, str):
formats = formats.replace(" ", "").split(",")
results = []
for fmt in formats:
fmt, _, ext = fmt.rpartition(".")
if fmt:
fmt = "-" + fmt
url = "https://a.deviantart.net/avatars{}/{}/{}/{}.{}?{}".format(
fmt, name[0], name[1], name, ext, index)
results.append(self._make_deviation(url, user, index, fmt))
return results
def _make_deviation(self, url, user, index, fmt):
return {
"author" : user,
"category" : "avatar",
"index" : text.parse_int(index),
"is_deleted" : False,
"is_downloadable": False,
"published_time" : 0,
"title" : "avatar" + fmt,
"stats" : {"comments": 0},
"content" : {"src": url},
}
class DeviantartBackgroundExtractor(DeviantartExtractor):

View File

@@ -210,7 +210,7 @@ __tests__ = (
"#sha1_content": "abf2cc79b842315f2e54bfdd93bf794a0f612b6f",
"author" : {
"type" : "premium",
"type" : "regular",
"usericon": "https://a.deviantart.net/avatars/s/h/shimoda7.jpg?4",
"userid" : "9AE51FC7-0278-806C-3FFF-F4961ABF9E2B",
"username": "shimoda7",
@@ -237,6 +237,21 @@ __tests__ = (
"username" : "shimoda7",
},
{
"#url" : "https://deviantart.com/shimoda7/avatar",
"#comment" : "'formats' option",
"#category": ("", "deviantart", "avatar"),
"#class" : deviantart.DeviantartAvatarExtractor,
"#archive" : False,
"#options" : {"formats": ["original.jpg", "big.jpg", "big.png", "big.gif"]},
"#urls" : (
"https://a.deviantart.net/avatars-original/s/h/shimoda7.jpg?4",
"https://a.deviantart.net/avatars-big/s/h/shimoda7.jpg?4",
"https://a.deviantart.net/avatars-big/s/h/shimoda7.png?4",
"https://a.deviantart.net/avatars-big/s/h/shimoda7.gif?4",
),
},
{
"#url" : "https://deviantart.com/gdldev/banner",
"#category": ("", "deviantart", "background"),