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


Python Store.save_friend方法代码示例

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


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

示例1: Autofriend

# 需要导入模块: from store import Store [as 别名]
# 或者: from store.Store import save_friend [as 别名]
class Autofriend(TwitterBot):

    def bot_init(self):

        ############################
        # REQUIRED: LOGIN DETAILS! #
        ############################

        self.config['api_key'] = os.environ['TWITTER_API_KEY']
        self.config['api_secret'] = os.environ['TWITTER_API_SECRET']
        self.config['access_key'] = os.environ['TWITTER_ACCESS_KEY']
        self.config['access_secret'] = os.environ['TWITTER_ACCESS_SECRET']

        ######################################
        # SEMI-OPTIONAL: OTHER CONFIG STUFF! #
        ######################################

        # how often to tweet, in seconds
        self.config['tweet_interval'] = 30 * 60     # default: 30 minutes

        # only include bot followers (and original tweeter) in @-replies
        self.config['reply_followers_only'] = True

        # fav any tweets that mention this bot?
        self.config['autofav_mentions'] = False

        # fav any tweets containing these keywords?
        self.config['autofav_keywords'] = []

        # follow back all followers?
        self.config['autofollow'] = True

        ###########################################
        # CUSTOM: your bot's own state variables! #
        ###########################################

        self.register_custom_handler(openshift_wake_up, 60 * 60 * 12)

        self.face_regions = partial(
            core.face_regions,
            core.load_face_detector())

        self.face_recognizer = FaceRecognizer()

        self.store = Store()

    def get_confidence(self):
        return float(os.environ.get('AUTOFRIEND_CONFIDENCE') or 50)

    def on_scheduled_tweet(self):
        pass

    def on_follow(self, follower_id):

        TwitterBot.on_follow(self, follower_id)

        try:
            self.store.save_friend((follower_id,))
        except pg.IntegrityError:
            # aborting is harmless, most likely an unfollow/refollow
            self.log("tried to add duplicate twitter friend %s" % follower_id)

    def _process_photo(self, friend_id, url):
        with DownloadedFile(url) as downloaded:
            if self.store.photo_seen(downloaded):
                logging.info(
                    '{} tried to add duplicate photo'.format(friend_id))
            else:
                face_regions = self.face_regions(
                    core.prepare_image(downloaded))
                self.face_recognizer.update(
                    [(face_region, friend_id)
                     for face_region in face_regions])
                self.store.remember_photo(downloaded)

    def on_direct_message(self, dm):

        media = dm.entities.get('media', [])
        photo = media[0] if len(media) > 0 else None

        if photo:
            friend_id = self.store.get_or_create_twitter_friend(
                dm.sender.id)['id']
            self._process_photo(friend_id, get_photo_url(photo))

        self.send_direct_message(dm.sender, compliments.get_compliment())

    def on_mention(self, tweet, prefix):

        if 'PLEASE FORGET ME' in tweet.text.upper():
            self.store.forget_friend(
                self.store.get_twitter_friend(tweet.author.id))
            self.api.destroy_friendship(tweet.author.id)
        else:

            friend_id = self.store.get_or_create_twitter_friend(
                tweet.author.id)['id']

            photo_urls = [get_photo_url(photo) for photo in get_photos(tweet)]

#.........这里部分代码省略.........
开发者ID:wohanley,项目名称:autofriend,代码行数:103,代码来源:twitter.py


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