Esempio n. 1
0
def save_followers(channel, data):
    """
    TODO: PostFilterStream still has twitter_handle used, need
    to refactor it to make it more generic to multiple sources
    like fb/email/chat.
    """
    from solariat_bottle.db.tracking import PostFilterStream
    from solariat_bottle.db.channel.twitter import \
        FollowerTrackingChannel, FollowerTrackingStatus

    data = [str(i) for i in data]
    stream = PostFilterStream.get()

    if isinstance(channel, FollowerTrackingStatus):
        main_channel = FollowerTrackingChannel.objects.get(channel.channel)
        if main_channel.tracking_mode == 'Passive':
            stream.track_passive(data, [main_channel],
                                 twitter_handle=channel.twitter_handle)
        else:
            stream.track('USER_ID',
                         data, [main_channel],
                         twitter_handle=channel.twitter_handle)
    else:
        stream.track('USER_ID',
                     data, [channel],
                     twitter_handle=channel.twitter_handle)
Esempio n. 2
0
def tw_drop_followers(channel):
    """
    When switching a channel to inactive, drop all the stored
    followers from the channel from actual twitter tracking.
    """
    from solariat_bottle.db.tracking import PostFilterStream
    from solariat_bottle.db.channel.twitter import \
        FollowerTrackingChannel, FollowerTrackingStatus

    if isinstance(channel, FollowerTrackingChannel):
        for fts in FollowerTrackingStatus.objects.find(channel=channel.id):
            fts.update(set__followers_synced=0, set__sync_status='idle')
    else:
        channel.update(set__followers_synced=0, set__sync_status='idle')

    stream = PostFilterStream.get()

    if isinstance(channel, FollowerTrackingStatus):
        main_channel = FollowerTrackingChannel.objects.get(channel.channel)
        if main_channel.tracking_mode == 'Passive':
            stream.untrack_channel_passive(main_channel,
                                           channel.twitter_handle)
        elif main_channel.tracking_mode == 'Active':
            stream.untrack_channel(main_channel, channel.twitter_handle)

    elif isinstance(channel, FollowerTrackingChannel):
        if channel.tracking_mode == 'Passive':
            stream.untrack_channel_passive(channel)
        elif channel.tracking_mode == 'Active':
            stream.untrack_channel(channel)
Esempio n. 3
0
    def pre_save(self):
        "Track/untrack twitter_handle"
        from solariat_bottle.db.tracking import PostFilterStream

        stream = PostFilterStream.get()
        stream.untrack_channel(self)
        if self.twitter_handle and self.status != 'Archived':
            stream.track('USER_NAME', [self.twitter_handle], [self])
Esempio n. 4
0
    def create_by_user(self, user, **kw):
        from solariat_bottle.db.tracking import PostFilterStream
        channel = super(EnterpriseTwitterChannelManager,
                        self).create_by_user(user, **kw)
        twitter_handle = kw.get('twitter_handle')
        if twitter_handle:
            stream = PostFilterStream.get()
            stream.track('USER_NAME', [twitter_handle], [channel])

        return channel
Esempio n. 5
0
    def _untrack_usernames(self, usernames=None):
        usernames = usernames or self.usernames
        from solariat_bottle.db.tracking import PostFilterStream
        from solariat_bottle.utils.post import normalize_screen_name

        stream = PostFilterStream.get()
        stream.untrack('KEYWORD',
                       map(normalize_screen_name, usernames),
                       [self.inbound_channel],
                       langs=self.langs)
Esempio n. 6
0
    def del_skipword(self, skipword):
        " del skipword "
        from solariat_bottle.db.tracking import PostFilterStream

        _skipwords = set(self.skipwords)
        _skipwords.discard(skipword)
        self.skipwords = list(_skipwords)
        self.update(pull__skipwords=skipword)
        stream = PostFilterStream.get()
        stream.untrack('SKIPWORD', [LingualToken.unlangify(skipword)], [self],
                       langs=self.__get_token_langs(skipword))
Esempio n. 7
0
    def del_keyword(self, keyword):
        " del keyword "
        from solariat_bottle.db.tracking import PostFilterStream

        _keywords = set(self.keywords)
        _keywords.discard(keyword)
        self.keywords = list(_keywords)
        self.update(pull__keywords=keyword)
        stream = PostFilterStream.get()
        stream.untrack('KEYWORD', [LingualToken.unlangify(keyword)], [self],
                       langs=self.__get_token_langs(keyword))
Esempio n. 8
0
    def del_username(self, username):
        " del username "

        from solariat_bottle.db.tracking import PostFilterStream

        _usernames = set(self.usernames)
        _usernames.discard(username)
        self.usernames = list(_usernames)

        self.update(pull__usernames=username)
        stream = PostFilterStream.get()
        stream.untrack('USER_NAME', [username], [self], langs=self.langs)
Esempio n. 9
0
    def __fix_tokens_langs(self, langs, action):

        keywords = [
            key for key in self.keywords if not is_token_lang_adopted(key)
        ]
        skipwords = [
            skip for skip in self.skipwords if not is_token_lang_adopted(skip)
        ]

        from solariat_bottle.db.tracking import PostFilterStream
        stream = PostFilterStream.get()

        if hasattr(stream, action):
            getattr(stream, action)('KEYWORD', keywords, [self], langs=langs)
            getattr(stream, action)('SKIPWORD', skipwords, [self], langs=langs)
Esempio n. 10
0
    def add_username(self, username):
        " add username "
        # self.sync_contacts(user=username, platform='Twitter')

        from solariat_bottle.db.tracking import PostFilterStream

        _usernames = set(self.usernames)
        _usernames.add(username)
        self.usernames = list(_usernames)

        self.update(addToSet__usernames=username)

        if self.status in {'Active', 'Interim'}:
            stream = PostFilterStream.get()
            stream.track('USER_NAME', [username], [self], langs=self.langs)
Esempio n. 11
0
    def remove_langs(self, langs):

        super(TwitterServiceChannel, self).remove_langs(langs)
        from solariat_bottle.db.tracking import PostFilterStream
        from solariat_bottle.utils.post import normalize_screen_name

        usernames = self.usernames
        stream = PostFilterStream.get()
        stream.untrack('KEYWORD',
                       map(normalize_screen_name, usernames),
                       [self.inbound_channel],
                       langs=langs)
        stream.untrack('USER_NAME',
                       usernames, [self.outbound_channel],
                       langs=langs)
Esempio n. 12
0
    def on_active(self):
        " run this handler when channel activated "

        # It's a temporary status which will be overwritten
        # by datasift_sync2 script in 1.5 mins (at max)
        super(KeywordTrackingChannel, self).on_active()

        from solariat_bottle.db.tracking import PostFilterStream
        stream = PostFilterStream.get()
        for keyword in self.keywords:
            stream.track('KEYWORD', [LingualToken.unlangify(keyword)], [self],
                         langs=self.__get_token_langs(keyword))
        for skipword in self.skipwords:
            stream.track('SKIPWORD', [LingualToken.unlangify(skipword)],
                         [self],
                         langs=self.__get_token_langs(skipword))
Esempio n. 13
0
    def add_skipword(self, skipword):
        " add skipword "
        if not self.__is_token_valid(skipword, self.skipwords):
            return False

        from solariat_bottle.db.tracking import PostFilterStream

        _skipwords = set(self.skipwords)
        _skipwords.add(skipword)
        self.skipwords = list(_skipwords)
        self.update(addToSet__skipwords=skipword)

        if self.status in ('Active', 'Interim'):
            stream = PostFilterStream.get()
            stream.track('SKIPWORD', [LingualToken.unlangify(skipword)],
                         [self],
                         langs=self.__get_token_langs(skipword))
        return True
Esempio n. 14
0
    def set_allowed_langs(self, langs, clear_previous=False):

        current_langs = set(self.langs)

        super(TwitterServiceChannel,
              self).set_allowed_langs(langs, clear_previous)
        from solariat_bottle.db.tracking import PostFilterStream
        from solariat_bottle.utils.post import normalize_screen_name

        new_langs = set(langs) - current_langs
        usernames = self.usernames
        stream = PostFilterStream.get()
        stream.track('KEYWORD',
                     map(normalize_screen_name, usernames),
                     [self.inbound_channel],
                     langs=new_langs)
        stream.track('USER_NAME',
                     usernames, [self.outbound_channel],
                     langs=new_langs)
Esempio n. 15
0
    def check_post_filter_entries(self, log_level=None, track_missing=False):
        from solariat_bottle.db.tracking import PostFilterStream

        if log_level:
            log = getattr(LOGGER, log_level)

        stream = PostFilterStream.get()
        tests_results = []
        for (filter_type, entries, channel, langs) in self.tracked_entities():
            tracked = stream.tracking_state(filter_type, entries, [channel],
                                            langs)
            all_tracked = all(is_tracked for (_, _, is_tracked) in tracked)
            tests_results.append(all_tracked)

            if not all_tracked:
                if log_level and log:
                    log("Missing PostFilterEntries ({}) for channel '{}'\n"
                        "Details: {}".format(filter_type, channel, tracked))
                # track missing PostFilterEntries
                if track_missing:
                    stream.track(filter_type, entries, [channel], langs=langs)

        return all(tests_results)
Esempio n. 16
0
    def test_contacts_added_when_post_created(self):
        user_profile = UserProfile.objects.upsert(
            "Twitter", dict(screen_name="screen_name1"))
        from solariat_bottle.db.tracking import PostFilterStream

        stream = PostFilterStream.get()

        #setup accounts
        account = Account.objects.get_or_create(name="Test Account")
        account2 = Account.objects.get_or_create(name="Test Account2")
        self.user.account = account
        self.user.save()
        account2.add_user(self.user)

        from ..db.channel.twitter import TwitterServiceChannel
        service_channel1 = TwitterServiceChannel.objects.create_by_user(
            self.user, account=account, title='Service')
        service_channel2 = TwitterServiceChannel.objects.create_by_user(
            self.user, account=account2, title='Service')

        self.assertFalse(user_profile.screen_name in
                         account.get_contact_channel("Twitter").usernames)
        self.assertFalse(user_profile.screen_name in
                         account2.get_contact_channel("Twitter").usernames)

        channels = [service_channel1, service_channel2]
        #create inbound post from user_profile
        self._create_db_post(channels=channels,
                             content="Test Post",
                             user_profile=user_profile,
                             url='https://twitter.com/fake/status/fake.status')

        #test user_profile is in contact channels for both accounts
        self.assertTrue(user_profile.screen_name in
                        account.get_contact_channel("Twitter").usernames)
        self.assertTrue(user_profile.screen_name in
                        account2.get_contact_channel("Twitter").usernames)
Esempio n. 17
0
    def on_active(self):
        super(UserTrackingChannel, self).on_active()

        from solariat_bottle.db.tracking import PostFilterStream
        stream = PostFilterStream.get()
        stream.track('USER_NAME', self.usernames, [self], langs=self.langs)
Esempio n. 18
0
    def on_suspend(self):
        super(KeywordTrackingChannel, self).on_suspend()

        from solariat_bottle.db.tracking import PostFilterStream
        stream = PostFilterStream.get()
        stream.untrack_channel(self)
Esempio n. 19
0
 def setUp(self):
     MainCase.setUp(self)
     self.stream =  PostFilterStream.get()