Пример #1
0
def get_image_search_results_multi_match():
    search_results = ImageSearchResults('test.com',
                                        get_image_search_settings(),
                                        checked_post=Post(post_id='abc123',
                                                          post_type='image',
                                                          subreddit='test'))
    search_results.search_times = ImageSearchTimes()
    search_results.search_times.total_search_time = 10
    search_results.matches.append(
        ImageSearchMatch(
            'test.com', 1,
            Post(id=1,
                 post_id='1111',
                 created_at=datetime.strptime('2019-01-28 05:20:03',
                                              '%Y-%m-%d %H:%M:%S')), 10, 10,
            32))
    search_results.matches.append(
        ImageSearchMatch(
            'test.com', 1,
            Post(id=2,
                 post_id='2222',
                 created_at=datetime.strptime('2019-06-28 05:20:03',
                                              '%Y-%m-%d %H:%M:%S')), 10, 10,
            32))
    search_results.matches.append(
        ImageSearchMatch('test.com', 1,
                         Post(id=3, post_id='3333', title='some normal title'),
                         10, 0.250, 32))
    return search_results
Пример #2
0
 def _get_image_search_results_multi_match(self):
     search_results = ImageSearchResults('test.com', self._get_image_search_settings(),
                                         checked_post=Post(post_id='abc123', post_type='image', subreddit='test'))
     search_results.search_times = ImageSearchTimes()
     search_results.search_times.total_search_time = 10
     search_results.matches.append(
         ImageSearchMatch(
             'test.com',
             1,
             Post(post_id='abc123', created_at=datetime.strptime('2019-01-28 05:20:03', '%Y-%m-%d %H:%M:%S')),
             10,
             10,
             32
         )
     )
     search_results.matches.append(
         ImageSearchMatch(
             'test.com',
             1,
             Post(post_id='123abc', created_at=datetime.strptime('2019-06-28 05:20:03', '%Y-%m-%d %H:%M:%S')),
             10,
             10,
             32
         )
     )
     return search_results
Пример #3
0
def get_image_search_results_no_match():
    search_results = ImageSearchResults('test.com',
                                        get_image_search_settings(),
                                        checked_post=Post(post_id='abc123',
                                                          post_type='image',
                                                          subreddit='test'))
    search_results.search_times = ImageSearchTimes()
    search_results.search_times.total_search_time = 10
    return search_results
    def check_image(
            self,
            url: Text,
            post: Post = None,
            source='unknown',
            sort_by='created',
            search_settings: ImageSearchSettings = None) -> ImageSearchResults:
        log.info('Checking URL for matches: %s', url)

        if not search_settings:
            log.info('No search settings provided, using default')
            search_settings = get_default_image_search_settings(self.config)

        search_results = ImageSearchResults(url,
                                            checked_post=post,
                                            search_settings=search_settings)

        search_results.search_times.start_timer('total_search_time')

        if search_settings.meme_filter:
            search_results.search_times.start_timer('meme_detection_time')
            search_results.meme_template = self._get_meme_template(
                search_results.target_hash)
            search_results.search_times.stop_timer('meme_detection_time')
            if search_results.meme_template:
                search_settings.target_match_percent = 100  # Keep only 100% matches on default hash size
                search_results.meme_hash = self._get_meme_hash(url)
                if not search_results.meme_hash:
                    log.error('No meme hash, disabled meme filter')
                    search_results.meme_template = None
                else:
                    log.info('Using meme filter %s',
                             search_results.meme_template.id)

        log.debug('Search Settings: %s', search_settings)

        api_search_results = self._get_matches(
            search_results.target_hash,
            search_results.target_hamming_distance,
            search_settings.target_annoy_distance,
            max_matches=search_settings.max_matches,
            max_depth=search_settings.max_depth,
            search_times=search_results.search_times)

        search_results.search_times.index_search_time = api_search_results.index_search_time
        search_results.total_searched = api_search_results.total_searched

        search_results.search_times.start_timer('set_match_post_time')
        search_results.matches = self._build_search_results(
            api_search_results.historical_matches, url,
            search_results.target_hash)
        search_results.matches += self._build_search_results(
            api_search_results.current_matches,
            url,
            search_results.target_hash,
            historical_index=False)
        search_results.search_times.stop_timer('set_match_post_time')

        search_results.search_times.start_timer('remove_duplicate_time')
        search_results.matches = self._remove_duplicates(
            search_results.matches)
        if post:
            search_results.matches = set_all_title_similarity(
                search_results.checked_post.title, search_results.matches)
        search_results.search_times.stop_timer('remove_duplicate_time')

        search_results = self._filter_results_for_reposts(search_results,
                                                          sort_by=sort_by)
        search_results.search_times.stop_timer('total_search_time')
        self._log_search_time(search_results, source)

        search_results = self._log_search(
            search_results,
            source,
            api_search_results.used_current_index,
            api_search_results.used_historical_index,
        )

        log.info('Seached %s items and found %s matches',
                 search_results.total_searched, len(search_results.matches))
        return search_results
Пример #5
0
 def test_build_image_report_link_positive(self):
     search_results = ImageSearchResults('test.com', Mock(), checked_post=Post(post_id='abc123'))
     search_results.matches.append(ImageSearchMatch('test.com', 123, Mock(), 1, 1, 32))
     result = build_image_report_link(search_results)
     expected = "*I'm not perfect, but you can help. Report [ [False Positive](https://www.reddit.com/message/compose/?to=RepostSleuthBot&subject=False%20Positive&message={\"post_id\": \"abc123\", \"meme_template\": null}) ]*"
     self.assertEqual(expected, result)