[reddit:user] provide 'user' metadata field (#8228)
This commit is contained in:
@@ -300,19 +300,24 @@ class RedditHomeExtractor(RedditSubredditExtractor):
|
||||
class RedditUserExtractor(RedditExtractor):
|
||||
"""Extractor for URLs from posts by a reddit user"""
|
||||
subcategory = "user"
|
||||
directory_fmt = ("{category}", "Users", "{user[name]}")
|
||||
pattern = (r"(?:https?://)?(?:\w+\.)?reddit\.com/u(?:ser)?/"
|
||||
r"([^/?#]+(?:/([a-z]+))?)/?(?:\?([^#]*))?$")
|
||||
example = "https://www.reddit.com/user/USER/"
|
||||
|
||||
def __init__(self, match):
|
||||
self.user, sub, params = match.groups()
|
||||
self.params = text.parse_query(params)
|
||||
if sub:
|
||||
if sub := match[2]:
|
||||
self.subcategory += "-" + sub
|
||||
RedditExtractor.__init__(self, match)
|
||||
|
||||
def submissions(self):
|
||||
return self.api.submissions_user(self.user, self.params)
|
||||
username, sub, qs = self.groups
|
||||
username = text.unquote(username)
|
||||
self.kwdict["user"] = user = self.api.user_about(username)
|
||||
|
||||
submissions = self.api.submissions_user(
|
||||
user["name"], text.parse_query(qs))
|
||||
return submissions
|
||||
|
||||
|
||||
class RedditSubmissionExtractor(RedditExtractor):
|
||||
@@ -323,12 +328,8 @@ class RedditSubmissionExtractor(RedditExtractor):
|
||||
r"comments|gallery)|redd\.it)/([a-z0-9]+)")
|
||||
example = "https://www.reddit.com/r/SUBREDDIT/comments/id/"
|
||||
|
||||
def __init__(self, match):
|
||||
RedditExtractor.__init__(self, match)
|
||||
self.submission_id = match[1]
|
||||
|
||||
def submissions(self):
|
||||
return (self.api.submission(self.submission_id),)
|
||||
return (self.api.submission(self.groups[0]),)
|
||||
|
||||
|
||||
class RedditImageExtractor(Extractor):
|
||||
@@ -449,9 +450,9 @@ class RedditAPI():
|
||||
endpoint = subreddit + "/.json"
|
||||
return self._pagination(endpoint, params)
|
||||
|
||||
def submissions_user(self, user, params):
|
||||
def submissions_user(self, username, params):
|
||||
"""Collect all (submission, comments)-tuples posted by a user"""
|
||||
endpoint = "/user/" + user + "/.json"
|
||||
endpoint = f"/user/{username}/.json"
|
||||
return self._pagination(endpoint, params)
|
||||
|
||||
def morechildren(self, link_id, children):
|
||||
@@ -472,6 +473,10 @@ class RedditAPI():
|
||||
else:
|
||||
yield thing["data"]
|
||||
|
||||
def user_about(self, username):
|
||||
endpoint = f"/user/{username}/about.json"
|
||||
return self._call(endpoint, {})["data"]
|
||||
|
||||
def authenticate(self):
|
||||
"""Authenticate the application by requesting an access token"""
|
||||
self.headers["Authorization"] = \
|
||||
|
||||
Reference in New Issue
Block a user