From 625fe0efce66da4eea04b27ae7b43b46bf3910d6 Mon Sep 17 00:00:00 2001 From: inty Date: Sun, 28 Jul 2024 08:43:12 +0000 Subject: [PATCH 1/2] [reddit] support user profile share links --- gallery_dl/extractor/reddit.py | 11 ++++++----- test/results/reddit.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/gallery_dl/extractor/reddit.py b/gallery_dl/extractor/reddit.py index 8577e74b..8b6df063 100644 --- a/gallery_dl/extractor/reddit.py +++ b/gallery_dl/extractor/reddit.py @@ -340,18 +340,19 @@ class RedditRedirectExtractor(Extractor): category = "reddit" subcategory = "redirect" pattern = (r"(?:https?://)?(?:" - r"(?:\w+\.)?reddit\.com/(?:(?:r)/([^/?#]+)))" + r"(?:\w+\.)?reddit\.com/(?:(r|u|user)/([^/?#]+)))" 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) + self.sub_type = "user" if match.group(1) == "u" else match.group(1) + self.subreddit = match.group(2) + self.share_url = match.group(3) def items(self): - url = "https://www.reddit.com/r/" + self.subreddit + "/s/" + \ - self.share_url + url = "https://www.reddit.com/" + self.sub_type + "/" + \ + self.subreddit + "/s/" + self.share_url data = {"_extractor": RedditSubmissionExtractor} response = self.request(url, method="HEAD", allow_redirects=False, notfound="submission") diff --git a/test/results/reddit.py b/test/results/reddit.py index 1221fa5c..457aaf4b 100644 --- a/test/results/reddit.py +++ b/test/results/reddit.py @@ -268,4 +268,20 @@ __tests__ = ( "#pattern" : r"^https://www\.reddit\.com/r/analog/comments/179exao/photographing_the_recent_annular_eclipse_with_a", }, +{ + "#url" : "https://www.reddit.com/u/Tailhook91/s/w4yAMbtOYm", + "#comment" : "Mobile share URL, user submission", + "#category": ("", "reddit", "redirect"), + "#class" : reddit.RedditRedirectExtractor, + "#pattern" : r"^https://www.reddit.com/user/Tailhook91/comments/znfxbr/prove_it/", +}, + +{ + "#url" : "https://www.reddit.com/user/Tailhook91/s/w4yAMbtOYm", + "#comment" : "Mobile share URL, user submission", + "#category": ("", "reddit", "redirect"), + "#class" : reddit.RedditRedirectExtractor, + "#pattern" : r"^https://www.reddit.com/user/Tailhook91/comments/znfxbr/prove_it/", +}, + ) From 604879d1a35b537f33c592c73e16e2feb7952811 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Mon, 28 Oct 2024 14:44:15 +0100 Subject: [PATCH 2/2] [reddit] simplify - use 'self.groups' to access matched values - use 'str.format()' to build URL --- gallery_dl/extractor/reddit.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/gallery_dl/extractor/reddit.py b/gallery_dl/extractor/reddit.py index 8b6df063..89eafc8a 100644 --- a/gallery_dl/extractor/reddit.py +++ b/gallery_dl/extractor/reddit.py @@ -344,15 +344,12 @@ class RedditRedirectExtractor(Extractor): 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.sub_type = "user" if match.group(1) == "u" else match.group(1) - self.subreddit = match.group(2) - self.share_url = match.group(3) - def items(self): - url = "https://www.reddit.com/" + self.sub_type + "/" + \ - self.subreddit + "/s/" + self.share_url + sub_type, subreddit, share_url = self.groups + if sub_type == "u": + sub_type = "user" + url = "https://www.reddit.com/{}/{}/s/{}".format( + sub_type, subreddit, share_url) data = {"_extractor": RedditSubmissionExtractor} response = self.request(url, method="HEAD", allow_redirects=False, notfound="submission")