Example #1
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
Example #2
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
    def test_get_closest_image_match__return_closest(self):
        matches = []
        match1 = ImageSearchMatch('test.com', 1, Post(id=1), 3, .077, 32)
        match2 = ImageSearchMatch('test.com', 1, Post(id=2), 5, .077, 32)
        match3 = ImageSearchMatch('test.com', 1, Post(id=3), 7, .077, 32)
        matches.append(match1)
        matches.append(match2)
        matches.append(match3)

        r = get_closest_image_match(matches, check_url=False)
        self.assertEqual(r, match1)
 def test__remove_duplicates_one_dup_remove(self):
     matches = [
         ImageSearchMatch('test.com', 123, Post(id=1), 10, 10, 32),
         ImageSearchMatch('test.com', 123, Post(id=1), 10, 10, 32),
         ImageSearchMatch('test.com', 123, Post(id=2), 10, 10, 32)
     ]
     dup_svc = DuplicateImageService(Mock(),
                                     Mock(),
                                     Mock(),
                                     config=MagicMock())
     r = dup_svc._remove_duplicates(matches)
     self.assertEqual(2, len(r))
    def _set_match_post(self,
                        match: ImageSearchMatch,
                        historical: bool = True) -> Optional[ImageSearchMatch]:
        """
        Take a search match, lookup the Post object and attach to match
        :param match: Match object
        :param historical: Is the match from the historical index
        """
        with self.uowm.start() as uow:
            # Hit the correct table if historical or current
            if historical:
                original_image_post = uow.image_post.get_by_id(
                    match.index_match_id)
            else:
                original_image_post = uow.image_post_current.get_by_id(
                    match.index_match_id)

            if not original_image_post:
                log.error(
                    'Failed to lookup original match post. ID %s - Historical: %s',
                    match.index_match_id, historical)
                return

            match_post = uow.posts.get_by_post_id(original_image_post.post_id)
            if not match_post:
                log.error('Failed to find original reddit_post for match')
                return
            match.post = match_post
            return match
Example #6
0
 def test_build_default_comment__image_oc_all_enabled_close_match(self):
     response_builder = ResponseBuilder(MagicMock())
     search_results = self._get_image_search_results_no_match()
     search_results.closest_match = ImageSearchMatch('test.com', 1, Post(post_id='abc123',
                                                                         created_at=datetime.strptime(
                                                                             '2019-01-28 05:20:03',
                                                                             '%Y-%m-%d %H:%M:%S')), 5, 3, 32)
     result = response_builder.build_default_comment(search_results, signature=True, stats=True, search_link=True,
                                                     search_settings=True)
     self.assertEqual(IMAGE_OC_ALL_ENABLED_ALL_ENABLED_NO_MEME, result)
    def _get_image_search_match_from_index_result(
            self,
            result: dict,
            url: Text,
            searched_hash: Text,
            historical_index: bool = True) -> Optional[ImageSearchMatch]:

        post = self._get_post_from_index_id(result['id'],
                                            historical_index=historical_index)

        if not post:
            return

        if not post.dhash_h:
            log.error('Post %s missing dhash', post.post_id)
            return

        return ImageSearchMatch(url, result['id'], post,
                                hamming(searched_hash, post.dhash_h),
                                result['distance'], len(post.dhash_h))
Example #8
0
 def test_filter_search_results_hit_all_filters(self):
     search_results = get_image_search_results_multi_match()
     search_results.search_settings.filter_same_author = True
     search_results.search_settings.filter_crossposts = True
     search_results.search_settings.only_older_matches = True
     search_results.search_settings.same_sub = True
     search_results.search_settings.target_title_match = None
     search_results.search_settings.max_days_old = 4
     search_results.checked_post.author = 'barry'
     search_results.checked_post.subreddit = 'sub1'
     search_results.checked_post.post_id = '1111'
     search_results.checked_post.created_at = datetime.utcfromtimestamp(
         1573995250)
     matches = []
     # Dropped by same author
     matches.append(
         ImageSearchMatch(
             'test.com', 1,
             Post(id=1,
                  author='barry',
                  post_id='abc123',
                  created_at=datetime.strptime('2019-01-28 05:20:03',
                                               '%Y-%m-%d %H:%M:%S')), 10,
             10, 32))
     # Dropped by crosspost
     matches.append(
         ImageSearchMatch(
             'test.com', 1,
             Post(id=2,
                  author='steve',
                  post_id='123abc',
                  crosspost_parent='abc',
                  created_at=datetime.strptime('2019-06-28 05:20:03',
                                               '%Y-%m-%d %H:%M:%S')), 10,
             10, 32))
     # Dropped by only older
     matches.append(
         ImageSearchMatch(
             'test.com', 1,
             Post(id=3,
                  author='steve',
                  post_id='3333',
                  title='some normal title',
                  created_at=datetime.utcfromtimestamp(1574081650)), 10,
             0.250, 32))
     # Dropped by same sub
     matches.append(
         ImageSearchMatch(
             'test.com', 1,
             Post(id=4,
                  author='steve',
                  post_id='4444',
                  title='some normal title',
                  subreddit='sub2',
                  created_at=datetime.utcfromtimestamp(1573908850)), 10,
             0.250, 32))
     matches.append(
         ImageSearchMatch(
             'test.com', 1,
             Post(id=5,
                  author='steve',
                  post_id='5555',
                  title='some normal title',
                  subreddit='sub1',
                  created_at=datetime.utcfromtimestamp(1573988200)), 10,
             0.250, 32))
     # Dropped by same post
     matches.append(
         ImageSearchMatch(
             'test.com', 1,
             Post(id=6,
                  post_id='1111',
                  title='some normal title',
                  subreddit='sub1',
                  created_at=datetime.utcfromtimestamp(1573908850)), 10,
             0.250, 32))
     matches.append(
         ImageSearchMatch(
             'test.com', 1,
             Post(id=7,
                  post_id='6666',
                  title='some normal title',
                  subreddit='sub1',
                  created_at=datetime.utcfromtimestamp(1573908850)), 10,
             0.250, 32))
     search_results.matches = matches
     with patch('redditrepostsleuth.core.util.repost_filters.datetime'
                ) as mock_date:
         mock_date.utcnow.return_value = datetime.utcfromtimestamp(
             1574360460)
         r = filter_search_results(search_results)
     self.assertEqual(1, len(search_results.matches))
     self.assertEqual('5555', r.matches[0].post.post_id)
     print('')
 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)