Skip to content

Commit

Permalink
500px: fallback when extended photo info missing
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickkfkan committed Jan 8, 2024
1 parent 6e10260 commit e34fa05
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions gallery_dl/extractor/500px.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def items(self):

for photo in self.photos():
url = photo["images"][-1]["url"]
photo["extension"] = photo["image_format"]
photo["extension"] = photo["image_format"] if "image_format" in photo else None
if data:
photo.update(data)
yield Message.Directory, photo
Expand All @@ -42,7 +42,17 @@ def photos(self):

def _extend(self, edges):
"""Extend photos with additional metadata and higher resolution URLs"""
ids = [str(edge["node"]["legacyId"]) for edge in edges]

"""Sometimes v1 API does not return all the requested photos, so keep edges
data as fallback (mapped by legacyId for easy retrieval)"""
fallbacks = {}
for edge in edges:
id = str(edge["node"]["legacyId"])
fallbacks[id] = edge["node"]
"""Replace v2 ID with legacyId to keep filenames consistent"""
fallbacks[id]["id"] = id

ids = list(fallbacks.keys())

url = "https://api.500px.com/v1/photos"
params = {
Expand All @@ -60,11 +70,17 @@ def _extend(self, edges):
}

photos = self._request_api(url, params)["photos"]
return [
photos[pid] for pid in ids
if pid in photos or
self.log.warning("Unable to fetch photo %s", pid)
]

result = list()
for pid in ids:
if pid in photos:
result.append(photos[pid])
elif "images" in fallbacks[pid]:
result.append(fallbacks[pid])
else:
self.log.warning("Unable to fetch photo %s", pid)

return result

def _request_api(self, url, params):
headers = {
Expand Down Expand Up @@ -415,7 +431,7 @@ def photos(self):
buttonName
externalUrl
cover {
images(sizes: [35, 33]) {
images(sizes: [33, 35]) {
size
webpUrl
jpegUrl
Expand Down Expand Up @@ -453,7 +469,7 @@ def photos(self):
id
}
cover {
images(sizes: [33, 32, 36, 2048]) {
images(sizes: [32, 33, 36, 2048]) {
url
size
webpUrl
Expand Down Expand Up @@ -498,7 +514,7 @@ def photos(self):
isFollowedByMe
}
}
images(sizes: [33, 32]) {
images(sizes: [32, 33, 35]) {
size
url
webpUrl
Expand Down Expand Up @@ -540,7 +556,7 @@ def photos(self):
id
}
cover {
images(sizes: [33, 32, 36, 2048]) {
images(sizes: [32, 33, 36, 2048]) {
url
size
webpUrl
Expand Down Expand Up @@ -585,7 +601,7 @@ def photos(self):
isFollowedByMe
}
}
images(sizes: [33, 32]) {
images(sizes: [32, 33, 35]) {
size
url
webpUrl
Expand Down

0 comments on commit e34fa05

Please sign in to comment.