Skip to content

Commit

Permalink
[reddit] download preview for 404ed imgur links (#4322)
Browse files Browse the repository at this point in the history
This is a pretty ugly hack as the internal infrastructure doesn't
really support switching from external URL to regular download in
case the former fails, but it kind of works ...

Can be disabled by setting 'reddit.fallback' to 'false'.
  • Loading branch information
mikf committed Aug 24, 2023
1 parent d12a5e4 commit 14af15b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
11 changes: 11 additions & 0 deletions gallery_dl/extractor/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ def items(self):
if match:
extra.append(match.group(1))
elif not match_user(url) and not match_subreddit(url):
if "preview" in data:
data["_fallback"] = self._previews(data)
yield Message.Queue, text.unescape(url), data
if "_fallback" in data:
del data["_fallback"]

if not extra or depth == max_depth:
return
Expand Down Expand Up @@ -165,6 +169,13 @@ def _extract_video(self, submission):
submission["_ytdl_extra"] = {"title": submission["title"]}
return submission["url"]

def _previews(self, post):
try:
for image in post["preview"]["images"]:
yield image["source"]["url"]
except Exception as exc:
self.log.debug("%s: %s", exc.__class__.__name__, exc)


class RedditSubredditExtractor(RedditExtractor):
"""Extractor for URLs from subreddits on reddit.com"""
Expand Down
17 changes: 15 additions & 2 deletions gallery_dl/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,23 @@ def handle_queue(self, url, kwdict):
try:
if pextr.config("parent-skip"):
job._skipcnt = self._skipcnt
self.status |= job.run()
status = job.run()
self._skipcnt = job._skipcnt
else:
self.status |= job.run()
status = job.run()

if status:
self.status |= status
if "_fallback" in kwdict and self.fallback:
fallback = kwdict["_fallback"] = \
iter(kwdict["_fallback"])
try:
url = next(fallback)
except StopIteration:
pass
else:
text.nameext_from_url(url, kwdict)
self.handle_url(url, kwdict)
break
except exception.RestartExtraction:
pass
Expand Down

0 comments on commit 14af15b

Please sign in to comment.