Skip to content

Commit

Permalink
[reddit] implement Reddit Mobile share links
Browse files Browse the repository at this point in the history
  • Loading branch information
interruptinuse committed Oct 21, 2023
1 parent 95a74be commit c6e173a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
22 changes: 22 additions & 0 deletions gallery_dl/extractor/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,28 @@ def items(self):
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
Expand Down
8 changes: 8 additions & 0 deletions test/results/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,12 @@
"#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",
},

)

0 comments on commit c6e173a

Please sign in to comment.