当前位置: 首页>>代码示例>>Python>>正文


Python Reddit.submission方法代码示例

本文整理汇总了Python中praw.Reddit.submission方法的典型用法代码示例。如果您正苦于以下问题:Python Reddit.submission方法的具体用法?Python Reddit.submission怎么用?Python Reddit.submission使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在praw.Reddit的用法示例。


在下文中一共展示了Reddit.submission方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: SubRedditStats

# 需要导入模块: from praw import Reddit [as 别名]
# 或者: from praw.Reddit import submission [as 别名]
class SubRedditStats(object):
    """Contain all the functionality of the subreddit_stats command."""

    post_prefix = tt('Subreddit Stats:')
    post_header = tt('---\n###{}\n')
    post_footer = tt('>Generated with [BBoe](/u/bboe)\'s [Subreddit Stats]'
                     '(https://github.com/praw-dev/prawtools)  \n{}'
                     'SRS Marker: {}')
    re_marker = re.compile('SRS Marker: (\d+)')

    @staticmethod
    def _previous_max(submission):
        return float(SubRedditStats.re_marker.findall(submission.selftext)[-1])

    @staticmethod
    def _permalink(item):
        if isinstance(item, Submission):
            return tt('/comments/{}').format(item.id)
        else:  # comment
            return tt('/comments/{}//{}?context=1').format(item.submission.id,
                                                           item.id)

    @staticmethod
    def _pts(points):
        return '1 pt' if points == 1 else '{} pts'.format(points)

    @staticmethod
    def _user(user):
        if user is None:
            return '_deleted_'
        elif isinstance(user, Redditor):
            user = str(user)
        return tt('[{}](/user/{})').format(user.replace('_', '\_'), user)

    def __init__(self, subreddit, site, verbosity, distinguished):
        """Initialize the SubRedditStats instance with config options."""
        self.reddit = Reddit(site, disable_update_check=True,
                             user_agent='prawtools/{}'.format(__version__))
        self.subreddit = self.reddit.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 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_id):
        """Load the previous subreddit stat."""
        self.prev_srs = self.reddit.submission(prev_id)
        self.min_date = self._previous_max(self.prev_srs)

    def fetch_recent_submissions(self, max_duration, after, exclude_self,
                                 exclude_link, since_last=True):
        """Fetch recent submissions in subreddit with boundaries.

        Does not include posts within the last three days as their scores may
        not be representative.

        :param max_duration: When set, specifies the number of days to include
        :param after: When set, fetch all submission after this submission id.
        :param exclude_self: When true, don't include self posts.
        :param exclude_link:  When true, don't include links.
        :param since_last: When true use info from last submission to determine
            the stop point
        :returns: True if any submissions were found.

        """
        if exclude_self and exclude_link:
            raise TypeError('Cannot set both exclude_self and exclude_link.')
        if max_duration:
            self.min_date = self.max_date - DAYS_IN_SECONDS * max_duration
        params = {'after': after} if after else None
        self.msg('DEBUG: Fetching submissions', 1)
        for submission in self.subreddit.new(limit=None, params=params):
            if submission.created_utc <= self.min_date:
                break
            if since_last and submission.title.startswith(self.post_prefix) \
               and submission.author == self.reddit.config.username:
                # Use info in this post to update the min_date
                # And don't include this post
                self.msg(tt('Found previous: {}')
                         .format(safe_title(submission)), 2)
                if self.prev_srs is None:  # Only use the most recent
                    self.min_date = max(self.min_date,
                                        self._previous_max(submission))
                    self.prev_srs = submission
                continue
#.........这里部分代码省略.........
开发者ID:CeruleanSky,项目名称:prawtools,代码行数:103,代码来源:stats.py


注:本文中的praw.Reddit.submission方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。