本文整理汇总了Python中praw.Reddit.get_submission方法的典型用法代码示例。如果您正苦于以下问题:Python Reddit.get_submission方法的具体用法?Python Reddit.get_submission怎么用?Python Reddit.get_submission使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类praw.Reddit
的用法示例。
在下文中一共展示了Reddit.get_submission方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: OAuth2RedditTest
# 需要导入模块: from praw import Reddit [as 别名]
# 或者: from praw.Reddit import get_submission [as 别名]
#.........这里部分代码省略.........
result = self.r.get_subreddit(self.sr).get_mod_log(limit=num)
self.assertEqual(num, len(list(result)))
@betamax()
def test_scope_modothers_modself(self):
subreddit = self.r.get_subreddit(self.sr)
self.r.refresh_access_information(self.refresh_token['modothers'])
subreddit.add_moderator(self.other_user_name)
# log in as other user
self.r.refresh_access_information(self.other_refresh_token['modself'])
self.r.accept_moderator_invite(self.sr)
# now return to original user.
self.r.refresh_access_information(self.refresh_token['modothers'])
subreddit.remove_moderator(self.other_user_name)
@betamax()
def test_scope_modposts(self):
self.r.refresh_access_information(self.refresh_token['modposts'])
Submission.from_id(self.r, self.submission_edit_id).remove()
@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'])
示例2: SubRedditStats
# 需要导入模块: from praw import Reddit [as 别名]
# 或者: from praw.Reddit import get_submission [as 别名]
class SubRedditStats(object):
"""Contain all the functionality of the subreddit_stats command."""
post_prefix = tt('Subreddit Stats:')
post_header = tt('---\n###{0}\n')
post_footer = tt('>Generated with [BBoe](/u/bboe)\'s [Subreddit Stats]'
'(https://github.com/praw-dev/prawtools) \n{0}'
'SRS Marker: {1}')
re_marker = re.compile(r'SRS Marker: (\d+)')
@staticmethod
def _previous_max(submission):
try:
val = SubRedditStats.re_marker.findall(submission.selftext)[-1]
return float(val)
except (IndexError, TypeError):
print('End marker not found in previous submission. Aborting')
sys.exit(1)
@staticmethod
def _permalink(permalink):
tokens = permalink.split('/')
if tokens[8] == '': # submission
return tt('/comments/{0}/_/').format(tokens[6])
else: # comment
return tt('/comments/{0}/_/{1}?context=1').format(tokens[6],
tokens[8])
@staticmethod
def _pts(points):
return '1 pt' if points == 1 else '{0} pts'.format(points)
@staticmethod
def _user(user):
if user is None:
return '_deleted_'
elif isinstance(user, Redditor):
user = str(user)
return tt('[{0}](/user/{1})').format(user.replace('_', r'\_'), user)
@staticmethod
def _submit(func, *args, **kwargs):
def sleep(sleep_time):
print('\tSleeping for {0} seconds'.format(sleep_time))
time.sleep(sleep_time)
while True:
try:
return func(*args, **kwargs)
except RateLimitExceeded as error:
sleep(error.sleep_time)
except ExceptionList as exception_list:
for error in exception_list.errors:
if isinstance(error, RateLimitExceeded):
sleep(error.sleep_time)
break
else:
raise
def __init__(self, subreddit, site, verbosity, distinguished):
"""Initialize the SubRedditStats instance with config options."""
self.reddit = Reddit(str(self), site, disable_update_check=True)
self.subreddit = self.reddit.get_subreddit(subreddit)
self.verbosity = verbosity
self.distinguished = distinguished
self.submissions = []
self.comments = []
self.submitters = defaultdict(list)
self.commenters = defaultdict(list)
self.min_date = 0
self.max_date = time.time() - DAYS_IN_SECONDS * 3
self.prev_srs = None
def login(self, user, pswd):
"""Login and provide debugging output if so wanted."""
if self.verbosity > 0:
print('Logging in')
self.reddit.login(user, pswd)
def msg(self, msg, level, overwrite=False):
"""Output a messaage to the screen if the verbosity is sufficient."""
if self.verbosity and self.verbosity >= level:
sys.stdout.write(msg)
if overwrite:
sys.stdout.write('\r')
sys.stdout.flush()
else:
sys.stdout.write('\n')
def prev_stat(self, prev_url):
"""Load the previous subreddit stats page."""
submission = self.reddit.get_submission(prev_url)
self.min_date = self._previous_max(submission)
self.prev_srs = prev_url
def fetch_recent_submissions(self, max_duration, after, exclude_self,
exclude_link, since_last=True):
"""Fetch recent submissions in subreddit with boundaries.
#.........这里部分代码省略.........
示例3: OAuth2RedditTest
# 需要导入模块: from praw import Reddit [as 别名]
# 或者: from praw.Reddit import get_submission [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)
示例4: OAuth2RedditTest
# 需要导入模块: from praw import Reddit [as 别名]
# 或者: from praw.Reddit import get_submission [as 别名]
#.........这里部分代码省略.........
subreddit.refresh()
self.assertFalse(subreddit.user_is_moderator)
self.assertFalse(subreddit.user_is_contributor)
@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_modwiki(self):
self.r.refresh_access_information(self.refresh_token["modwiki"])
subreddit = self.r.get_subreddit(self.sr)
page = subreddit.get_wiki_page("index")
page.add_editor(self.other_user_name)
page.remove_editor(self.other_user_name)
@betamax()
def test_scope_modwiki_modcontributors(self):
self.r.refresh_access_information(self.refresh_token["modwiki+contr"])
subreddit = self.r.get_subreddit(self.sr)
subreddit.add_wiki_ban(self.other_user_name)
subreddit.remove_wiki_ban(self.other_user_name)
subreddit.add_wiki_contributor(self.other_user_name)
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)
示例5: SubRedditStats
# 需要导入模块: from praw import Reddit [as 别名]
# 或者: from praw.Reddit import get_submission [as 别名]
class SubRedditStats(object):
post_prefix = tt('Subreddit Stats:')
post_header = tt('---\n###{0}\n')
post_footer = tt('>Generated with [BBoe](/u/bboe)\'s [Subreddit Stats]'
'(https://github.com/praw-dev/prawtools) \n{0}'
'SRS Marker: {1}')
re_marker = re.compile('SRS Marker: (\d+)')
@staticmethod
def _previous_max(submission):
try:
val = SubRedditStats.re_marker.findall(submission.selftext)[-1]
return float(val)
except (IndexError, TypeError):
print('End marker not found in previous submission. Aborting')
sys.exit(1)
@staticmethod
def _permalink(permalink):
tokens = permalink.split('/')
if tokens[8] == '': # submission
return tt('/comments/{0}/_/').format(tokens[6])
else: # comment
return tt('/comments/{0}/_/{1}?context=1').format(tokens[6],
tokens[8])
@staticmethod
def _user(user):
if user is None:
return '_deleted_'
elif isinstance(user, Redditor):
user = str(user)
return tt('[{0}](/user/{1})').format(user.replace('_', '\_'), user)
@staticmethod
def _submit(func, *args, **kwargs):
def sleep(sleep_time):
print('\tSleeping for {0} seconds'.format(sleep_time))
time.sleep(sleep_time)
while True:
try:
return func(*args, **kwargs)
except RateLimitExceeded as error:
sleep(error.sleep_time)
except ExceptionList as exception_list:
for error in exception_list.errors:
if isinstance(error, RateLimitExceeded):
sleep(error.sleep_time)
break
else:
raise
def __init__(self, subreddit, site, verbosity):
self.reddit = Reddit(str(self), site)
self.subreddit = self.reddit.get_subreddit(subreddit)
self.verbosity = verbosity
self.submissions = []
self.comments = []
self.submitters = defaultdict(list)
self.commenters = defaultdict(list)
self.min_date = 0
self.max_date = time.time() - DAYS_IN_SECONDS * 3
self.prev_srs = None
# Config
self.reddit.config.comment_limit = -1 # Fetch max comments possible
self.reddit.config.comment_sort = 'top'
def login(self, user, pswd):
if self.verbosity > 0:
print('Logging in')
self.reddit.login(user, pswd)
def msg(self, msg, level, overwrite=False):
if self.verbosity >= level:
sys.stdout.write(msg)
if overwrite:
sys.stdout.write('\r')
sys.stdout.flush()
else:
sys.stdout.write('\n')
def prev_stat(self, prev_url):
submission = self.reddit.get_submission(prev_url)
self.min_date = self._previous_max(submission)
self.prev_srs = prev_url
def fetch_recent_submissions(self, max_duration, after, exclude_self,
since_last=True):
'''Fetches recent submissions in subreddit with boundaries.
Does not include posts within the last three days as their scores may
not be representative.
Keyword arguments:
max_duration -- When set, specifies the number of days to include
after -- When set, fetch all submission after this submission id.
exclude_self -- When true, don't include self posts.
since_last -- When true use info from last submission to determine the
stop point
#.........这里部分代码省略.........
示例6: error
# 需要导入模块: from praw import Reddit [as 别名]
# 或者: from praw.Reddit import get_submission [as 别名]
error("Couldn't login to reddit: {0}".format(e))
# create DB session
engine = create_engine(DB_PATH)
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
if __name__ == '__main__':
request_session = requests.session()
for post in session.query(Post).all():
# load reddit post
submission = reddit.get_submission(submission_id=post.reddit_post_id)
# load lepra post
debug("Fetching leprosorium post #{0}...".format(post.lepra_post_id))
lepra_url = "http://auto.leprosorium.ru/comments/{0}".format(post.lepra_post_id)
response = request_session.get(lepra_url, cookies=LEPROSORIUM_COOKIES)
bs_tree = BeautifulSoup(response.text)
# parse lepra post
debug("Parsing comments...")
# for comment in comments
for node in bs_tree.findAll('div', attrs={'class': 'post', 'class': 'indent_0'}):
comment_id = int(node['id'])
debug("Parsing comment #{0}:".format(comment_id))
content = node.find('div', attrs={'class': 'dt'})