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


Python Reddit.get_friends方法代码示例

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


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

示例1: RedditBot

# 需要导入模块: from praw import Reddit [as 别名]
# 或者: from praw.Reddit import get_friends [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))
开发者ID:reddit-bots,项目名称:reddit-bot,代码行数:104,代码来源:base.py


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