本文整理汇总了Python中praw.Reddit.get_my_subreddits方法的典型用法代码示例。如果您正苦于以下问题:Python Reddit.get_my_subreddits方法的具体用法?Python Reddit.get_my_subreddits怎么用?Python Reddit.get_my_subreddits使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类praw.Reddit
的用法示例。
在下文中一共展示了Reddit.get_my_subreddits方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: OAuth2RedditTest
# 需要导入模块: from praw import Reddit [as 别名]
# 或者: from praw.Reddit import get_my_subreddits [as 别名]
#.........这里部分代码省略.........
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(
self.refresh_token['privatemessages'])
self.assertTrue(list(self.r.get_inbox()))
@betamax()
def test_scope_read(self):
self.r.refresh_access_information(self.refresh_token['read'])
self.assertTrue(self.r.get_subreddit(self.priv_sr).subscribers > 0)
fullname = '{0}_{1}'.format(self.r.config.by_object[Submission],
self.priv_submission_id)
method1 = self.r.get_info(thing_id=fullname)
method2 = self.r.get_submission(submission_id=self.priv_submission_id)
self.assertEqual(method1, method2)
@betamax()
def test_scope_read_get_front_page(self):
self.r.refresh_access_information(self.refresh_token['mysubreddits'])
subscribed = list(self.r.get_my_subreddits(limit=None))
self.r.refresh_access_information(self.refresh_token['read'])
for post in self.r.get_front_page():
self.assertTrue(post.subreddit in subscribed)
@betamax()
def test_scope_read_get_sub_listingr(self):
self.r.refresh_access_information(self.refresh_token['read'])
subreddit = self.r.get_subreddit(self.priv_sr)
self.assertTrue(list(subreddit.get_top()))
@betamax()
def test_scope_read_get_submission_by_url(self):
url = ("https://www.reddit.com/r/reddit_api_test_priv/comments/16kbb7/"
"google/")
self.r.refresh_access_information(self.refresh_token['read'])
submission = Submission.from_url(self.r, url)
self.assertTrue(submission.num_comments != 0)
@betamax()
def test_scope_read_priv_sr_comments(self):
self.r.refresh_access_information(self.refresh_token['read'])
self.assertTrue(list(self.r.get_comments(self.priv_sr)))
@betamax()
def test_scope_wikiread_wiki_page(self):
self.r.refresh_access_information(self.refresh_token['wikiread'])
self.assertTrue(self.r.get_wiki_page(self.sr, 'index'))
@betamax()
def test_scope_read_priv_sub_comments(self):
self.r.refresh_access_information(self.refresh_token['read'])
submission = Submission.from_id(self.r, self.priv_submission_id)
self.assertTrue(submission.comments)
@betamax()
def test_scope_submit(self):
self.r.refresh_access_information(self.refresh_token['submit'])
result = self.r.submit(self.sr, 'OAuth Submit', text='Foo')
self.assertTrue(isinstance(result, Submission))
@betamax()
def test_scope_subscribe(self):
self.r.refresh_access_information(self.refresh_token['subscribe'])
self.r.get_subreddit(self.sr).subscribe()
@betamax()
def test_scope_vote(self):
self.r.refresh_access_information(self.refresh_token['vote'])
submission = Submission.from_id(self.r, self.submission_edit_id)
submission.clear_vote()
@betamax()
def test_set_access_credentials(self):
self.assertTrue(self.r.user is None)
result = self.r.refresh_access_information(
self.refresh_token['identity'], update_session=False)
self.assertTrue(self.r.user is None)
self.r.set_access_credentials(**result)
self.assertFalse(self.r.user is None)
@betamax()
def test_oauth_without_identy_doesnt_set_user(self):
self.assertTrue(self.r.user is None)
self.r.refresh_access_information(self.refresh_token['edit'])
self.assertTrue(self.r.user is None)
示例2: OAuth2RedditTest
# 需要导入模块: from praw import Reddit [as 别名]
# 或者: from praw.Reddit import get_my_subreddits [as 别名]
#.........这里部分代码省略.........
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(
self.refresh_token['privatemessages'])
self.assertTrue(list(self.r.get_inbox()))
@betamax()
def test_scope_read(self):
self.r.refresh_access_information(self.refresh_token['read'])
self.assertTrue(self.r.get_subreddit(self.priv_sr).subscribers > 0)
fullname = '{0}_{1}'.format(self.r.config.by_object[Submission],
self.priv_submission_id)
method1 = self.r.get_info(thing_id=fullname)
method2 = self.r.get_submission(submission_id=self.priv_submission_id)
self.assertEqual(method1, method2)
@betamax()
def test_scope_read_get_front_page(self):
self.r.refresh_access_information(self.refresh_token['mysubreddits'])
subscribed = list(self.r.get_my_subreddits(limit=None))
self.r.refresh_access_information(self.refresh_token['read'])
for post in self.r.get_front_page():
self.assertTrue(post.subreddit in subscribed)
@betamax()
def test_set_access_credentials(self):
self.assertTrue(self.r.user is None)
result = self.r.refresh_access_information(
self.refresh_token['identity'], update_session=False)
self.assertTrue(self.r.user is None)
self.r.set_access_credentials(**result)
self.assertFalse(self.r.user is None)
@betamax()
def test_solve_captcha(self):
# Use the alternate account because it has low karma,
# so we can test the captcha.
self.r.refresh_access_information(self.other_refresh_token['submit'])
original_stdin = sys.stdin
sys.stdin = FakeStdin('ljgtoo') # Comment this line when rebuilding
self.r.submit(self.sr, 'captcha test', 'body')
sys.stdin = original_stdin
@betamax()
def test_oauth_without_identy_doesnt_set_user(self):
self.assertTrue(self.r.user is None)
self.r.refresh_access_information(self.refresh_token['edit'])
self.assertTrue(self.r.user is None)
示例3: RedditBot
# 需要导入模块: from praw import Reddit [as 别名]
# 或者: from praw.Reddit import get_my_subreddits [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_my_subreddits [as 别名]
#.........这里部分代码省略.........
subreddit.remove_wiki_contributor(self.other_user_name)
@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(self.refresh_token["privatemessages"])
self.assertTrue(list(self.r.get_inbox()))
@betamax()
def test_scope_read(self):
self.r.refresh_access_information(self.refresh_token["read"])
self.assertTrue(self.r.get_subreddit(self.priv_sr).subscribers > 0)
fullname = "{0}_{1}".format(self.r.config.by_object[Submission], self.priv_submission_id)
method1 = self.r.get_info(thing_id=fullname)
method2 = self.r.get_submission(submission_id=self.priv_submission_id)
self.assertEqual(method1, method2)
@betamax()
def test_scope_read_get_front_page(self):
self.r.refresh_access_information(self.refresh_token["mysubreddits"])
subscribed = list(self.r.get_my_subreddits(limit=None))
self.r.refresh_access_information(self.refresh_token["read"])
for post in self.r.get_front_page():
self.assertTrue(post.subreddit in subscribed)
@betamax()
def test_scope_read_get_sub_listingr(self):
self.r.refresh_access_information(self.refresh_token["read"])
subreddit = self.r.get_subreddit(self.priv_sr)
self.assertTrue(list(subreddit.get_top()))
@betamax()
def test_scope_read_get_submission_by_url(self):
url = "https://www.reddit.com/r/reddit_api_test_priv/comments/16kbb7/" "google/"
self.r.refresh_access_information(self.refresh_token["read"])
submission = Submission.from_url(self.r, url)
self.assertTrue(submission.num_comments != 0)
@betamax()
def test_scope_read_priv_sr_comments(self):
self.r.refresh_access_information(self.refresh_token["read"])
self.assertTrue(list(self.r.get_comments(self.priv_sr)))
@betamax()
def test_scope_wikiread_wiki_page(self):
self.r.refresh_access_information(self.refresh_token["wikiread"])
self.assertTrue(self.r.get_wiki_page(self.sr, "index"))
@betamax()
def test_scope_read_priv_sub_comments(self):
self.r.refresh_access_information(self.refresh_token["read"])
submission = Submission.from_id(self.r, self.priv_submission_id)
self.assertTrue(submission.comments)
@betamax()
def test_scope_submit(self):
self.r.refresh_access_information(self.refresh_token["submit"])
result = self.r.submit(self.sr, "OAuth Submit", text="Foo")
self.assertTrue(isinstance(result, Submission))
@betamax()
def test_scope_subscribe(self):
self.r.refresh_access_information(self.refresh_token["subscribe"])
self.r.get_subreddit(self.sr).subscribe()
@betamax()
def test_scope_vote(self):
self.r.refresh_access_information(self.refresh_token["vote"])
submission = Submission.from_id(self.r, self.submission_edit_id)
submission.clear_vote()
@betamax()
def test_set_access_credentials(self):
self.assertTrue(self.r.user is None)
result = self.r.refresh_access_information(self.refresh_token["identity"], update_session=False)
self.assertTrue(self.r.user is None)
self.r.set_access_credentials(**result)
self.assertFalse(self.r.user is None)
@betamax()
def test_oauth_without_identy_doesnt_set_user(self):
self.assertTrue(self.r.user is None)
self.r.refresh_access_information(self.refresh_token["edit"])
self.assertTrue(self.r.user is None)