예제 #1
0
def get_link_search_results_no_match():
    search_times = ImageSearchTimes()
    search_times.total_search_time = 10
    return SearchResults('test.com',
                         get_search_settings(),
                         checked_post=Post(post_id='abc123',
                                           post_type='link',
                                           subreddit='test'),
                         search_times=search_times)
예제 #2
0
    def _get_link_search_results_matches_match(self):
        search_times = ImageSearchTimes()
        search_times.total_search_time = 10
        search_results = SearchResults(
            'test.com',
            self._get_search_settings(),
            checked_post=Post(post_id='abc123', post_type='link', subreddit='test'),
            search_times=search_times
        )
        search_results.matches.append(
            SearchMatch(
                'test.com',
                Post(post_id='123abc', created_at=datetime.strptime('2019-06-28 05:20:03', '%Y-%m-%d %H:%M:%S')),
            )
        )

        return search_results
def filter_search_results(search_results: SearchResults,
                          reddit: Reddit = None,
                          uitl_api: Text = None) -> SearchResults:
    """
    Filter a set of search results based on the image search settings
    :param reddit: Used for filter removed post
    :param uitl_api: Used for filtering removed posts
    :param search_results: SearchResults obj
    """
    log.debug('%s results pre-filter', len(search_results.matches))
    search_results.search_times.start_timer('total_filter_time')
    # Only run these if we are search for an existing post
    if search_results.checked_post:
        search_results.matches = list(
            filter(filter_same_post(search_results.checked_post.post_id),
                   search_results.matches))

        if search_results.search_settings.filter_same_author:
            search_results.matches = list(
                filter(filter_same_author(search_results.checked_post.author),
                       search_results.matches))

        if search_results.search_settings.filter_crossposts:
            search_results.matches = list(
                filter(cross_post_filter, search_results.matches))

        if search_results.search_settings.only_older_matches:
            search_results.matches = list(
                filter(
                    filter_newer_matches(
                        search_results.checked_post.created_at),
                    search_results.matches))

        if search_results.search_settings.same_sub:
            search_results.matches = list(
                filter(same_sub_filter(search_results.checked_post.subreddit),
                       search_results.matches))

        if search_results.search_settings.target_title_match:
            search_results.matches = list(
                filter(
                    filter_title_distance(
                        search_results.search_settings.target_title_match),
                    search_results.matches))

        if search_results.search_settings.max_days_old:
            search_results.matches = list(
                filter(
                    filter_days_old_matches(
                        search_results.search_settings.max_days_old),
                    search_results.matches))

    if reddit and uitl_api:
        if search_results.search_settings.filter_dead_matches:
            search_results.matches = filter_dead_urls_remote(
                uitl_api, search_results.matches)

    if search_results.search_settings.filter_removed_matches and reddit:
        search_results.matches = filter_removed_posts(reddit,
                                                      search_results.matches)

    search_results.search_times.stop_timer('total_filter_time')
    log.debug('%s results post-filter', len(search_results.matches))
    return search_results