From b68aad3dab1aed8184abe46180f62003c75c6e8c Mon Sep 17 00:00:00 2001 From: inty Date: Sat, 21 Oct 2023 19:19:22 +0000 Subject: [PATCH] [reddit] implement Reddit Mobile share links --- docs/supportedsites.md | 2 +- gallery_dl/extractor/reddit.py | 23 +++++++++++++++++++++++ test/results/reddit.py | 8 ++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/docs/supportedsites.md b/docs/supportedsites.md index 3924cd39..9ca6c705 100644 --- a/docs/supportedsites.md +++ b/docs/supportedsites.md @@ -712,7 +712,7 @@ Consider all sites to be NSFW unless otherwise known. Reddit https://www.reddit.com/ - Home Feed, individual Images, Submissions, Subreddits, User Profiles + Home Feed, individual Images, Redirects, Submissions, Subreddits, User Profiles OAuth diff --git a/gallery_dl/extractor/reddit.py b/gallery_dl/extractor/reddit.py index cd2ba3d2..c0bf5b3e 100644 --- a/gallery_dl/extractor/reddit.py +++ b/gallery_dl/extractor/reddit.py @@ -292,6 +292,29 @@ class RedditImageExtractor(Extractor): yield Message.Url, url, data +class RedditRedirectExtractor(Extractor): + """Extractor for personalized share URLs produced by the mobile app""" + category = "reddit" + subcategory = "redirect" + pattern = (r"(?:https?://)?(?:" + r"(?:\w+\.)?reddit\.com/(?:(?:r)/([^/?#]+)))" + r"/s/([a-zA-Z0-9]{10})") + example = "https://www.reddit.com/r/SUBREDDIT/s/abc456GHIJ" + + def __init__(self, match): + Extractor.__init__(self, match) + self.subreddit = match.group(1) + self.share_url = match.group(2) + + def items(self): + url = "https://www.reddit.com/r/" + self.subreddit + "/s/" + \ + self.share_url + data = {"_extractor": RedditSubmissionExtractor} + response = self.request(url, method="HEAD", allow_redirects=False, + notfound="submission") + yield Message.Queue, response.headers["Location"], data + + class RedditAPI(): """Interface for the Reddit API diff --git a/test/results/reddit.py b/test/results/reddit.py index 330ef5b6..8a4359cf 100644 --- a/test/results/reddit.py +++ b/test/results/reddit.py @@ -240,4 +240,12 @@ __tests__ = ( "#pattern" : r"^https://i\.redd\.it/00af44lpn0u51\.jpg$", }, +{ + "#url" : "https://www.reddit.com/r/analog/s/hKrTTvFVwZ", + "#comment" : "Mobile share URL", + "#category": ("", "reddit", "redirect"), + "#class" : reddit.RedditRedirectExtractor, + "#pattern" : r"^https://www\.reddit\.com/r/analog/comments/179exao/photographing_the_recent_annular_eclipse_with_a", +}, + )