Skip to content

Commit

Permalink
Merge 8651ffe into c2b4c6b
Browse files Browse the repository at this point in the history
  • Loading branch information
jackodsteel committed Nov 21, 2019
2 parents c2b4c6b + 8651ffe commit 58123f3
Show file tree
Hide file tree
Showing 8 changed files with 395 additions and 9 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ Source Contributors
- Timendum `@timendum <https://github.com/timendum>`_
- vaclav-2012 `@vaclav-2012 <https://github.com/vaclav-2012>`_
- kungming2 `@kungming2 <https://github.com/kungming2>`_
- Jack Steel `@jackodsteel <https://github.com/jackodsteel>`_
- Add "Name <email (optional)> and github profile link" above this line.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Unreleased
unmarking a submission as original content.
* Parameter ``without_websockets`` to :meth:`~.Subreddit.submit_image` and
:meth:`~.Subreddit.submit_video` to submit without using WebSockets.
* :meth:`.Reddit.redditor` supports ``fullname`` param to fetch a Redditor
by the fullname instead of name.
:class:`.Redditor` constructor now also has ``fullname`` param.

6.4.0 (2019/09/21)
------------------
Expand Down
1 change: 1 addition & 0 deletions praw/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
"upload_image": "r/{subreddit}/api/upload_sr_img",
"user": "user/{user}/",
"user_about": "user/{user}/about/",
"user_by_fullname": "/api/user_data_by_account_ids",
"user_flair": "r/{subreddit}/api/user_flair_v2",
"users_new": "users/new",
"users_popular": "users/popular",
Expand Down
26 changes: 22 additions & 4 deletions praw/models/reddit/redditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,25 @@ def _kind(self):
"""Return the class's kind."""
return self._reddit.config.kinds["redditor"]

def __init__(self, reddit, name=None, _data=None):
@property
def _path(self):
return API_PATH["user"].format(user=self)

def __init__(self, reddit, name=None, fullname=None, _data=None):
"""Initialize a Redditor instance.
:param reddit: An instance of :class:`~.Reddit`.
:param name: The name of the redditor.
:param fullname: The fullname of the redditor, starting with ``t2_``.
Exactly one of ``name``, ``fullname`` or ``_data`` must be provided.
"""
if bool(name) == bool(_data):
raise TypeError("Either `name` or `_data` must be provided.")
if [name, fullname, _data].count(None) != 2:
raise TypeError(
"Exactly one of `name`, `fullname`, or `_data` must be "
"provided."
)
if _data:
assert (
isinstance(_data, dict) and "name" in _data
Expand All @@ -119,9 +129,17 @@ def __init__(self, reddit, name=None, _data=None):
self._listing_use_sort = True
if name:
self.name = name
self._path = API_PATH["user"].format(user=self)
elif fullname:
self._fullname = fullname

def _fetch_username(self, fullname):
return self._reddit.get(
API_PATH["user_by_fullname"], params={"ids": fullname}
)[fullname]["name"]

def _fetch_info(self):
if hasattr(self, "_fullname"):
self.name = self._fetch_username(self._fullname)
return ("user_about", {"user": self.name}, None)

def _fetch_data(self):
Expand Down
9 changes: 6 additions & 3 deletions praw/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,13 +556,16 @@ def random_subreddit(self, nsfw=False):
path = redirect.path
return models.Subreddit(self, path.split("/")[2])

def redditor(self, name):
"""Return a lazy instance of :class:`~.Redditor` for ``name``.
def redditor(self, name=None, fullname=None):
"""Return a lazy instance of :class:`~.Redditor`.
:param name: The name of the redditor.
:param fullname: The fullname of the redditor, starting with ``t2_``.
Either ``name`` or ``fullname`` can be provided, but not both.
"""
return models.Redditor(self, name)
return models.Redditor(self, name=name, fullname=fullname)

def request(self, method, path, params=None, data=None, files=None):
"""Return the parsed JSON data returned from a request to URL.
Expand Down
Loading

0 comments on commit 58123f3

Please sign in to comment.