Skip to content

Commit

Permalink
[tumblr] add fallback for failed higher-resolution images (#2957)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Sep 28, 2022
1 parent 6992d01 commit f728b5c
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions gallery_dl/extractor/tumblr.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,17 @@ def items(self):

if self.original and "/s2048x3072/" in photo["url"] and (
photo["width"] == 2048 or photo["height"] == 3072):
try:
photo["url"] = self._original_photo(photo["url"])
except Exception:
self._warn_original(photo["url"], post)
photo["url"], fb = self._original_photo(photo["url"])
if fb:
post["_fallback"] = self._original_image_fallback(
photo["url"], post["id"])

del photo["original_size"]
del photo["alt_sizes"]
posts.append(
self._prepare_image(photo["url"], post.copy()))
del post["photo"]
post.pop("_fallback", None)

url = post.get("audio_url") # type "audio"
if url and url.startswith("https://a.tumblr.com/"):
Expand All @@ -140,11 +142,12 @@ def items(self):
# API response, but they can't contain images/videos anyway
body = post["reblog"]["comment"] + post["reblog"]["tree_html"]
for url in _findall_image(body):
try:
url = self._original_inline_image(url)
except Exception:
self._warn_original(url, post)
url, fb = self._original_inline_image(url)
if fb:
post["_fallback"] = self._original_image_fallback(
url, post["id"])
posts.append(self._prepare_image(url, post.copy()))
post.pop("_fallback", None)
for url in _findall_video(body):
url = self._original_video(url)
posts.append(self._prepare(url, post.copy()))
Expand Down Expand Up @@ -231,25 +234,23 @@ def _original_inline_image(self, url):
resized, n = self._subn_orig_image("/s99999x99999/", url, 1)
if n:
return self._update_image_token(resized)
return self._sub_image(r"https://\1_1280.\2", url)
return self._sub_image(r"https://\1_1280.\2", url), False

def _original_video(self, url):
return self._sub_video(r"https://\1.\2", url)

def _update_image_token(self, resized):
headers = {"Accept": "text/html,*/*;q=0.8"}

for _ in range(3):
response = self.request(resized, headers=headers)
updated = text.extract(response.text, '" src="', '"')[0]
if updated != resized:
return updated

raise RuntimeError("invalid token")

def _warn_original(self, url, post):
response = self.request(resized, headers=headers)
updated = text.extract(response.text, '" src="', '"')[0]
return updated, (resized == updated)

def _original_image_fallback(self, url, post_id):
yield self._update_image_token(url)[0]
yield self._update_image_token(url)[0]
yield self._update_image_token(url)[0]
self.log.warning("Unable to fetch higher-resolution "
"version of %s (%s)", url, post["id"])
"version of %s (%s)", url, post_id)


class TumblrUserExtractor(TumblrExtractor):
Expand Down

0 comments on commit f728b5c

Please sign in to comment.