Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""Unfriend the user.
:returns: The json response from the server.
"""
self.reddit_session.evict(self.reddit_session.config['friends'])
return _modify_relationship('friend', unlink=True)(
self.reddit_session.user, self)
class LoggedInRedditor(Redditor):
"""A class representing a currently logged in Redditor."""
get_hidden = restrict_access("history")(_get_redditor_listing('hidden'))
get_saved = restrict_access("history")(_get_redditor_listing('saved'))
def get_blocked(self):
"""Return a UserList of Redditors with whom the user has blocked."""
url = self.reddit_session.config['blocked']
return self.reddit_session.request_json(url)
def get_cached_moderated_reddits(self):
"""Return a cached dictionary of the user's moderated reddits.
This list is used internally. Consider using the `get_my_moderation`
function instead.
"""
if self._mod_subs is None:
self._mod_subs = {'mod': self.reddit_session.get_subreddit('mod')}
for sub in self.reddit_session.get_my_moderation(limit=None):
"""
def mark_as_nsfw_helper(self): # pylint: disable-msg=W0613
# It is necessary to have the 'self' argument as it's needed in
# restrict_access to determine what class the decorator is
# operating on.
url = self.reddit_session.config['unmarknsfw' if unmark_nsfw else
'marknsfw']
data = {'id': self.fullname}
return self.reddit_session.request_json(url, data=data)
is_author = (self.reddit_session.is_logged_in() and self.author ==
self.reddit_session.user)
if is_author:
return mark_as_nsfw_helper(self)
else:
return restrict_access('modposts')(mark_as_nsfw_helper)(self)
@decorators.restrict_access(scope='read')
def get_top(self, *args, **kwargs):
"""Return a get_content generator for top submissions.
Corresponds to the submissions provided by http://www.reddit.com/top/
for the session.
The additional parameters are passed directly into
:meth:`.get_content`. Note: the `url` parameter cannot be altered.
"""
return self.get_content(self.config['top'], *args, **kwargs)
@decorators.restrict_access(scope='modflair')
def delete_flair(self, subreddit, user):
"""Delete the flair for the given user on the given subreddit.
:returns: The json response from the server.
"""
data = {'r': six.text_type(subreddit),
'name': six.text_type(user)}
return self.request_json(self.config['deleteflair'], data=data)
@decorators.restrict_access(scope='mysubreddits')
def get_my_subreddits(self, *args, **kwargs):
"""Return a get_content generator of subreddits.
The subreddits generated are those that the session's user is
subscribed to.
The additional parameters are passed directly into
:meth:`.get_content`. Note: the `url` parameter cannot be altered.
"""
return self.get_content(self.config['my_subreddits'], *args, **kwargs)
@restrict_access(scope='vote')
def vote(self, direction=0):
"""Vote for the given item in the direction specified.
Note: votes must be cast by humans. That is, API clients proxying a
human's action one-for-one are OK, but bots deciding how to vote on
content or amplifying a human's vote are not. See the reddit rules for
more details on what constitutes vote cheating.
Source for note: http://www.reddit.com/dev/api#POST_api_vote
:returns: The json response from the server.
"""
url = self.reddit_session.config['vote']
data = {'id': self.fullname,
'dir': six.text_type(direction)}
@restrict_access(scope='report')
def report(self, reason=None):
"""Report this object to the moderators.
:param reason: The user-supplied reason for reporting a comment
or submission. Default: None (blank reason)
:returns: The json response from the server.
"""
url = self.reddit_session.config['report']
data = {'id': self.fullname}
if reason:
data['reason'] = reason
response = self.reddit_session.request_json(url, data=data)
# Reported objects are automatically hidden as well
# pylint: disable=W0212
self.reddit_session.evict(
@decorators.restrict_access(scope='mysubreddits')
def get_my_multireddits(self, *args, **kwargs):
"""Return a list of the authenticated Redditor's Multireddits."""
# The JSON data for multireddits is returned from Reddit as a list
# Therefore, we cannot use :meth:`get_content` to retrieve the objects
url = self.config['my_multis']
response = self.request_json(url)
return response
@decorators.restrict_access(scope='modflair')
def add_flair_template(self, subreddit, text='', css_class='',
text_editable=False, is_link=False):
"""Add a flair template to the given subreddit.
:returns: The json response from the server.
"""
data = {'r': six.text_type(subreddit),
'text': text,
'css_class': css_class,
'text_editable': six.text_type(text_editable),
'flair_type': 'LINK_FLAIR' if is_link else 'USER_FLAIR'}
return self.request_json(self.config['flairtemplate'], data=data)
@restrict_access(scope=None, login=True)
def hide(self, unhide=False):
"""Hide object in the context of the logged in user.
:returns: The json response from the server.
"""
url = self.reddit_session.config['unhide' if unhide else 'hide']
data = {'id': self.fullname,
'executed': 'unhide' if unhide else 'hide'}
response = self.reddit_session.request_json(url, data=data)
# pylint: disable-msg=W0212
urls = [urljoin(self.reddit_session.user._url, 'hidden')]
# pylint: enable-msg=W0212
self.reddit_session.evict(urls)
return response