本文整理汇总了Python中praw.Reddit.get_redditor方法的典型用法代码示例。如果您正苦于以下问题:Python Reddit.get_redditor方法的具体用法?Python Reddit.get_redditor怎么用?Python Reddit.get_redditor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类praw.Reddit
的用法示例。
在下文中一共展示了Reddit.get_redditor方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get
# 需要导入模块: from praw import Reddit [as 别名]
# 或者: from praw.Reddit import get_redditor [as 别名]
def get(self, *args, **kwargs):
username = self.kwargs['username']
api = Reddit(user_agent=settings.USER_AGENT)
exists = False
redditor = None
try:
redditor = api.get_redditor(username)
exists = True
except HTTPError:
exists = False
data = {'exists': exists}
if redditor:
data['redditor'] = redditor.fullname
return Response(data)
示例2: OAuth2RedditTest
# 需要导入模块: from praw import Reddit [as 别名]
# 或者: from praw.Reddit import get_redditor [as 别名]
class OAuth2RedditTest(PRAWTest):
def setUp(self):
self.configure()
self.r = Reddit(USER_AGENT, site_name='reddit_oauth_test',
disable_update_check=True)
def test_authorize_url(self):
self.r.set_oauth_app_info(None, None, None)
self.assertRaises(errors.OAuthAppRequired, self.r.get_authorize_url,
'dummy_state')
self.r.set_oauth_app_info(self.r.config.client_id,
self.r.config.client_secret,
self.r.config.redirect_uri)
url, params = self.r.get_authorize_url('...').split('?', 1)
self.assertTrue('api/v1/authorize/' in url)
params = dict(x.split('=', 1) for x in params.split('&'))
expected = {'client_id': self.r.config.client_id,
'duration': 'temporary',
'redirect_uri': ('https%3A%2F%2F127.0.0.1%3A65010%2F'
'authorize_callback'),
'response_type': 'code', 'scope': 'identity',
'state': '...'}
self.assertEqual(expected, params)
@betamax()
def test_get_access_information(self):
# If this test fails, the following URL will need to be visted in order
# to obtain a new code to pass to `get_access_information`:
# self.r.get_authorize_url('...')
token = self.r.get_access_information('MQALrr1di8GzcnT8szbTWhLcBUQ')
expected = {'access_token': self.r.access_token,
'refresh_token': None,
'scope': set(('identity',))}
self.assertEqual(expected, token)
self.assertEqual('PyAPITestUser2', text_type(self.r.user))
@betamax()
def test_get_access_information_with_invalid_code(self):
self.assertRaises(errors.OAuthInvalidGrant,
self.r.get_access_information, 'invalid_code')
def test_invalid_app_access_token(self):
self.r.clear_authentication()
self.r.set_oauth_app_info(None, None, None)
self.assertRaises(errors.OAuthAppRequired,
self.r.get_access_information, 'dummy_code')
def test_invalid_app_authorize_url(self):
self.r.clear_authentication()
self.r.set_oauth_app_info(None, None, None)
self.assertRaises(errors.OAuthAppRequired,
self.r.get_authorize_url, 'dummy_state')
@betamax()
def test_invalid_set_access_credentials(self):
self.assertRaises(errors.OAuthInvalidToken,
self.r.set_access_credentials,
set(('identity',)), 'dummy_access_token')
def test_oauth_scope_required(self):
self.r.set_oauth_app_info('dummy_client', 'dummy_secret', 'dummy_url')
self.r.set_access_credentials(set('dummy_scope',), 'dummy_token')
self.assertRaises(errors.OAuthScopeRequired, self.r.get_me)
@betamax()
def test_scope_edit(self):
self.r.refresh_access_information(self.refresh_token['edit'])
submission = Submission.from_id(self.r, self.submission_edit_id)
self.assertEqual(submission, submission.edit('Edited text'))
@betamax()
def test_scope_history(self):
self.r.refresh_access_information(self.refresh_token['history'])
self.assertTrue(list(self.r.get_redditor(self.un).get_upvoted()))
@betamax()
def test_scope_identity(self):
self.r.refresh_access_information(self.refresh_token['identity'])
self.assertEqual(self.un, self.r.get_me().name)
@betamax()
def test_scope_modconfig(self):
self.r.refresh_access_information(self.refresh_token['modconfig'])
self.r.get_subreddit(self.sr).set_settings('foobar')
retval = self.r.get_subreddit(self.sr).get_stylesheet()
self.assertTrue('images' in retval)
@betamax()
def test_scope_modflair(self):
self.r.refresh_access_information(self.refresh_token['modflair'])
self.r.get_subreddit(self.sr).set_flair(self.un, 'foobar')
@betamax()
def test_scope_modlog(self):
num = 50
self.r.refresh_access_information(self.refresh_token['modlog'])
result = self.r.get_subreddit(self.sr).get_mod_log(limit=num)
self.assertEqual(num, len(list(result)))
@betamax()
#.........这里部分代码省略.........
示例3: RedditBot
# 需要导入模块: from praw import Reddit [as 别名]
# 或者: from praw.Reddit import get_redditor [as 别名]
#.........这里部分代码省略.........
@classmethod
def get_scope(cls):
"""Basic permission scope for RedditReplyBot operations."""
return super(RedditBot, cls).get_scope() | {
'identity',
'subscribe',
'mysubreddits',
}
def run_forever(self):
self.bot_start()
try:
while True:
self.do_loop()
self.refresh()
except Exception as e:
self.bot_error(e)
raise
finally:
self.bot_stop()
def refresh(self):
logger.info('Refreshing settings')
self.subreddits = self._get_subreddits()
self.blocked_users = self._get_blocked_users()
def do_loop(self):
for subreddit in cycle(self.subreddits):
try:
if self.loop(subreddit) == self.BOT_SHOULD_REFRESH:
break
except Forbidden as e:
logger.error('Forbidden in {}! Removing from whitelist.'.format(subreddit))
self.remove_subreddits(subreddit)
break
except RateLimitExceeded as e:
logger.warning('RateLimitExceeded! Sleeping {} seconds.'.format(e.sleep_time))
time.sleep(e.sleep_time)
except (ConnectionError, HTTPException) as e:
logger.warning('Error: Reddit down or no connection? {!r}'.format(e))
time.sleep(self.settings['loop_sleep'] * 10)
else:
time.sleep(self.settings['loop_sleep'])
else:
logger.error("No subreddits in file. Will read file again in 5 seconds.")
time.sleep(5)
def _get_subreddits(self):
subreddits = list(map(lambda s: s.display_name, self.r.get_my_subreddits()))
logger.info('Subreddits: {} entries'.format(len(subreddits)))
logger.debug('List: {!r}'.format(subreddits))
return subreddits
def _get_blocked_users(self, filename=None):
"""Friends are blocked users, because Reddit only allows blocking
users by private messages."""
blocked_users = list(map(lambda u: u.name, self.r.get_friends()))
logger.info('Blocked users: {} entries'.format(len(blocked_users)))
logger.debug('List: {!r}'.format(blocked_users))
return blocked_users
def is_user_blocked(self, user_name):
if user_name == self.bot_name:
return True
return user_name in self.blocked_users
def is_subreddit_whitelisted(self, subreddit):
return subreddit in self.subreddits
def remove_subreddits(self, *subreddits):
for sub_name in subreddits:
if sub_name in self.subreddits:
self.subreddits.remove(sub_name)
sub = self.r.get_subreddit(sub_name)
sub.unsubscribe()
logger.info('Unsubscribed from /r/{}'.format(sub_name))
def add_subreddits(self, *subreddits):
for sub_name in subreddits:
if sub_name not in self.subreddits:
self.subreddits.add(sub_name)
sub = self.r.get_subreddit(sub_name)
sub.subscribe()
logger.info('Subscribed to /r/{}'.format(sub_name))
def block_users(self, *users):
for user_name in users:
if user_name not in self.blocked_users:
self.blocked_users.add(user_name)
user = self.r.get_redditor(user_name)
user.friend()
logger.info('Blocked /u/{}'.format(user_name))
def unblock_users(self, *users):
for user_name in users:
if user_name in self.blocked_users:
self.blocked_users.remove(user_name)
user = self.r.get_redditor(user_name)
user.unfriend()
logger.info('Unblocked /u/{}'.format(user_name))
示例4: OAuth2RedditTest
# 需要导入模块: from praw import Reddit [as 别名]
# 或者: from praw.Reddit import get_redditor [as 别名]
#.........这里部分代码省略.........
raise errors.HTTPException('fakeraw')
self.assertRaises(errors.HTTPException, raise_http_exception)
http_exception = errors.HTTPException('fakeraw')
self.assertEqual(http_exception.message, str(http_exception))
def test_raise_oauth_exception(self):
oerrormessage = "fakemessage"
oerrorurl = "http://oauth.reddit.com/"
def raise_oauth_exception():
raise errors.OAuthException(oerrormessage, oerrorurl)
self.assertRaises(errors.OAuthException, raise_oauth_exception)
oauth_exception = errors.OAuthException(oerrormessage, oerrorurl)
self.assertEqual(oauth_exception.message +
" on url {0}".format(oauth_exception.url),
str(oauth_exception))
def test_raise_redirect_exception(self):
apiurl = "http://api.reddit.com/"
oauthurl = "http://oauth.reddit.com/"
def raise_redirect_exception():
raise errors.RedirectException(apiurl, oauthurl)
self.assertRaises(errors.RedirectException, raise_redirect_exception)
redirect_exception = errors.RedirectException(apiurl, oauthurl)
self.assertEqual(redirect_exception.message, str(redirect_exception))
@betamax()
def test_scope_history(self):
self.r.refresh_access_information(self.refresh_token['history'])
self.assertTrue(list(self.r.get_redditor(self.un).get_upvoted()))
@betamax()
def test_scope_identity(self):
self.r.refresh_access_information(self.refresh_token['identity'])
self.assertEqual(self.un, self.r.get_me().name)
@betamax()
def test_scope_mysubreddits(self):
self.r.refresh_access_information(self.refresh_token['mysubreddits'])
self.assertTrue(list(self.r.get_my_moderation()))
@betamax()
def test_scope_creddits(self):
# Assume there are insufficient creddits.
self.r.refresh_access_information(
self.refresh_token['creddits'])
redditor = self.r.get_redditor('bboe')
sub = self.r.get_submission(url=self.comment_url)
# Test error conditions
self.assertRaises(TypeError, sub.gild, months=1)
for value in (False, 0, -1, '0', '-1'):
self.assertRaises(TypeError, redditor.gild, value)
# Test object gilding
self.assertRaises(errors.InsufficientCreddits, redditor.gild)
self.assertRaises(errors.InsufficientCreddits, sub.gild)
self.assertRaises(errors.InsufficientCreddits, sub.comments[0].gild)
@betamax()
def test_scope_privatemessages(self):
self.r.refresh_access_information(
示例5: OAuth2RedditTest
# 需要导入模块: from praw import Reddit [as 别名]
# 或者: from praw.Reddit import get_redditor [as 别名]
class OAuth2RedditTest(PRAWTest):
def setUp(self):
self.configure()
self.r = Reddit(USER_AGENT, site_name="reddit_oauth_test", disable_update_check=True)
def test_authorize_url(self):
self.r.set_oauth_app_info(None, None, None)
self.assertRaises(errors.OAuthAppRequired, self.r.get_authorize_url, "dummy_state")
self.r.set_oauth_app_info(self.r.config.client_id, self.r.config.client_secret, self.r.config.redirect_uri)
url, params = self.r.get_authorize_url("...").split("?", 1)
self.assertTrue("api/v1/authorize/" in url)
params = dict(x.split("=", 1) for x in params.split("&"))
expected = {
"client_id": self.r.config.client_id,
"duration": "temporary",
"redirect_uri": ("https%3A%2F%2F127.0.0.1%3A65010%2F" "authorize_callback"),
"response_type": "code",
"scope": "identity",
"state": "...",
}
self.assertEqual(expected, params)
# @betamax() is currently broken for this test
def test_auto_refresh_token(self):
self.r.refresh_access_information(self.refresh_token["identity"])
old_token = self.r.access_token
self.r.access_token += "x" # break the token
self.r.user.refresh()
current_token = self.r.access_token
self.assertNotEqual(old_token, current_token)
self.r.user.refresh()
self.assertEqual(current_token, self.r.access_token)
@betamax()
def test_get_access_information(self):
# If this test fails, the following URL will need to be visted in order
# to obtain a new code to pass to `get_access_information`:
# self.r.get_authorize_url('...')
token = self.r.get_access_information("MQALrr1di8GzcnT8szbTWhLcBUQ")
expected = {"access_token": self.r.access_token, "refresh_token": None, "scope": set(("identity",))}
self.assertEqual(expected, token)
self.assertEqual("PyAPITestUser2", text_type(self.r.user))
@betamax()
def test_get_access_information_with_invalid_code(self):
self.assertRaises(errors.OAuthInvalidGrant, self.r.get_access_information, "invalid_code")
def test_invalid_app_access_token(self):
self.r.clear_authentication()
self.r.set_oauth_app_info(None, None, None)
self.assertRaises(errors.OAuthAppRequired, self.r.get_access_information, "dummy_code")
def test_invalid_app_authorize_url(self):
self.r.clear_authentication()
self.r.set_oauth_app_info(None, None, None)
self.assertRaises(errors.OAuthAppRequired, self.r.get_authorize_url, "dummy_state")
@betamax()
def test_invalid_set_access_credentials(self):
self.assertRaises(
errors.OAuthInvalidToken, self.r.set_access_credentials, set(("identity",)), "dummy_access_token"
)
def test_oauth_scope_required(self):
self.r.set_oauth_app_info("dummy_client", "dummy_secret", "dummy_url")
self.r.set_access_credentials(set("dummy_scope"), "dummy_token")
self.assertRaises(errors.OAuthScopeRequired, self.r.get_me)
@betamax()
def test_scope_edit(self):
self.r.refresh_access_information(self.refresh_token["edit"])
submission = Submission.from_id(self.r, self.submission_edit_id)
self.assertEqual(submission, submission.edit("Edited text"))
@betamax()
def test_scope_history(self):
self.r.refresh_access_information(self.refresh_token["history"])
self.assertTrue(list(self.r.get_redditor(self.un).get_upvoted()))
@betamax()
def test_scope_identity(self):
self.r.refresh_access_information(self.refresh_token["identity"])
self.assertEqual(self.un, self.r.get_me().name)
@betamax()
def test_scope_modconfig(self):
self.r.refresh_access_information(self.refresh_token["modconfig"])
self.r.get_subreddit(self.sr).set_settings("foobar")
retval = self.r.get_subreddit(self.sr).get_stylesheet()
self.assertTrue("images" in retval)
@betamax()
def test_scope_modflair(self):
self.r.refresh_access_information(self.refresh_token["modflair"])
self.r.get_subreddit(self.sr).set_flair(self.un, "foobar")
@betamax()
def test_scope_modlog(self):
#.........这里部分代码省略.........