Skip to content

Commit

Permalink
[reddit] support sorting options and sub-options (#15)
Browse files Browse the repository at this point in the history
Example:
    https://www.reddit.com/r/<subreddit>/top/?sort=top&t=month
    (the 'sort=top' parameter is irrelevant and can be omitted)
  • Loading branch information
mikf committed May 29, 2017
1 parent 5f45ce2 commit bce51e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions gallery_dl/extractor/deviantart.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ class DeviantartFavoriteExtractor(DeviantartExtractor):
r"(?:/((\d+)/([^/?]+)|\?catpath=/))?"]
test = [
("http://rosuuri.deviantart.com/favourites/58951174/Useful", {
"url": "6a10e8e05401d61696ecf7a54c174e1c8ece7ba1",
"keyword": "44fe61c5b20db8d90d4e06b86346630289f1db7d",
"url": "65d070eae215b9375b4437a1ab4659efdad204e3",
"keyword": "e5f3aaf35274e976ae81c5b151e650eea1dde775",
}),
("http://h3813067.deviantart.com/favourites/", {
"url": "71345ce3bef5b19bd2a56d7b96e6b5ddba747c2e",
Expand Down
16 changes: 10 additions & 6 deletions gallery_dl/extractor/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,18 @@ def _urls(self, submissions):
class RedditSubredditExtractor(RedditExtractor):
"""Extractor for images from subreddits on reddit.com"""
subcategory = "subreddit"
pattern = [r"(?:https?://)?(?:m\.|www\.)?reddit\.com/r/([^/]+)/?$"]
pattern = [r"(?:https?://)?(?:m\.|www\.)?reddit\.com/r/([^/?&#]+)"
r"(/[a-z]+)?/?"
r"(?:\?.*?(?:\bt=([a-z]+))?)?$"]

def __init__(self, match):
RedditExtractor.__init__(self)
self.subreddit = match.group(1)
self.subreddit, self.order, self.timeframe = match.groups()

def submissions(self):
return self.api.submissions_subreddit(self.subreddit)
subreddit = self.subreddit + (self.order or "")
params = {"t": self.timeframe} if self.timeframe else {}
return self.api.submissions_subreddit(subreddit, params)


class RedditSubmissionExtractor(RedditExtractor):
Expand Down Expand Up @@ -111,10 +115,11 @@ def submission(self, submission_id):
return (submission["data"]["children"][0]["data"],
self._unfold(comments))

def submissions_subreddit(self, subreddit):
def submissions_subreddit(self, subreddit, params):
"""Collect all (submission, comments)-tuples of a subreddit"""
endpoint = "/r/" + subreddit + "/.json"
params = {"raw_json": 1, "limit": 100}
params["raw_json"] = 1
params["limit"] = 100
return self._pagination(endpoint, params)

def authenticate(self):
Expand All @@ -130,7 +135,6 @@ def _authenticate_impl(self, client_id):
"grant_type": "https://oauth.reddit.com/grants/installed_client",
"device_id": "DO_NOT_TRACK_THIS_DEVICE",
}
self.log.info("Requesting access token")
response = self.session.post(url, data=data, auth=(client_id, ""))
if response.status_code != 200:
raise exception.AuthenticationError()
Expand Down

0 comments on commit bce51e9

Please sign in to comment.