Пример #1
0
    def get_friends_required(self, post):
        text = preprocess_text(post['full_text'])

        # Create keyword mutations
        tag_keywords = create_keyword_mutations(
            *TwitterConfig.get().actions.tag_friend.tag_keywords)
        friend_keywords = create_keyword_mutations(
            *TwitterConfig.get().actions.tag_friend.friend_keywords)

        # Find all occurrences of the keywords
        tag_keywords_found = sorted(
            {i
             for x in tag_keywords for i in self.find_all(x, text)})

        friend_keywords_found = sorted(
            {i
             for x in friend_keywords for i in self.find_all(x, text)})

        # Remove indexes of friend keyword that are before any tag keyword
        friend_keywords_found = [
            x for x in friend_keywords_found if x > min(tag_keywords_found)
        ]

        # Create all combinations between occurrences
        indexes = list(product(tag_keywords_found, friend_keywords_found))

        # Find where the two keywords are closest
        closest_pair = [
            x for x in sorted(indexes, key=lambda x: x[1] - x[0])
            if x[1] - x[0] > 0
        ]
        if not closest_pair:
            raise ValueError("Could not find substring")

        closest_pair = closest_pair[0]

        substring = text[closest_pair[0]:closest_pair[1]]

        # Split substring to words and remove empty
        substring = list(filter(None, substring.split(' ')))

        if len(substring) != 2:
            raise ValueError('Could not find how many tag are needed')

        amount = substring[1]

        for number, keywords in TwitterConfig.get(
        ).actions.tag_friend.number_keywords.items():
            if amount in keywords:
                return number

        raise ValueError('Could not determinate how many tags are needed')
Пример #2
0
    def tag_needed(self, post):
        text = preprocess_text(post['full_text'])

        tag_keywords = create_keyword_mutations(
            *TwitterConfig.get().actions.tag_friend.tag_keywords)
        if not any(x in text for x in tag_keywords):
            return False

        friend_keywords = create_keyword_mutations(
            *TwitterConfig.get().actions.tag_friend.friend_keywords)
        if not any(x in text for x in friend_keywords):
            return False

        return True
Пример #3
0
 def process(self, post):
     text = preprocess_text(post['full_text'])
     keywords = create_keyword_mutations(
         *TwitterConfig.get().actions.follow.keywords)
     if any(x in text.lower() for x in keywords):
         self.remove_oldest_follow()
         self.follow(post)
Пример #4
0
 def process(self, post):
     text = preprocess_text(post['full_text'])
     keywords = create_keyword_mutations(
         *TwitterConfig.get().actions.favorite.keywords)
     if any(x in text.lower() for x in keywords):
         r = self.client.favorite(post['id'])
         logger.info("Favorite: {0}".format(post['id']))
Пример #5
0
 def test_get_keyword_mutations(self):
     keyword = 'keyword'
     target_mutations = ['#keyword', ' keyword ', '.keyword', 'keyword ', ' keyword', 'keyword.', ',keyword',
                         'keyword,', 'keyword!', '!keyword']
     mutations = create_keyword_mutations(keyword)
     self.assertEqual(len(mutations), len(target_mutations))
     for mutation in mutations:
         self.assertIn(mutation, target_mutations)
Пример #6
0
    def contains_keyword(self, post):
        text = preprocess_text(post['full_text'])
        keywords = create_keyword_mutations(
            *TwitterConfig.get().search.filter.blacklist.keywords)

        for keyword in keywords:
            if count_keyword_in_text(keyword, text) > 0:
                logger.info(
                    f"Skipping {post['id']} because it contains {keyword} keyword"
                )
                return True
        return False