Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

500px: fallback when extended photo info missing #5035

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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