def test_build_image_msg_values_from_search_include_meme_template(self):
     search_results = self._get_image_search_results_one_match()
     search_results.matches[0].hamming_distance = 3
     search_results.meme_template = MemeTemplate(id=10)
     result = build_image_msg_values_from_search(search_results)
     self.assertIn('meme_template_id', result)
     self.assertEqual(10, result['meme_template_id'])
 def test_build_image_msg_values_from_search_correct_match_percent(self):
     search_results = self._get_image_search_results_one_match()
     search_results.matches[0].hamming_distance = 3
     result = build_image_msg_values_from_search(search_results)
     self.assertEqual('90.62%', result['newest_percent_match'])
Example #3
0
    def _check_sub(self, monitored_sub: MonitoredSub):
        log.info('Checking sub %s', monitored_sub.name)
        start_time = perf_counter()
        subreddit = self.reddit.subreddit(monitored_sub.name)
        if not subreddit:
            log.error('Failed to get Subreddit %s', monitored_sub.name)
            return

        submissions = subreddit.new(limit=monitored_sub.search_depth)
        checked_posts = 0
        for submission in submissions:
            if not self.should_check_post(submission):
                continue

            with self.uowm.start() as uow:
                post = uow.posts.get_by_post_id(submission.id)

            if post.post_type == 'image' and post.dhash_h is None:
                log.error('Post %s has no dhash', post.post_id)
                continue
            checked_posts += 1
            try:
                if post.post_type == 'image':
                    search_results = self._check_for_repost(
                        post, monitored_sub)
                elif post.post_type == 'link':
                    search_results = self._check_for_link_repost(post)
            except NoIndexException:
                log.error(
                    'No search index available.  Cannot check post %s in %s',
                    submission.id, submission.subreddit.display_name)
                continue
            except RedLockError:
                log.error(
                    'New search index is being loaded. Cannot check post %s in %s',
                    submission.id, submission.subreddit.display_name)
                continue

            if not search_results.matches and monitored_sub.only_comment_on_repost:
                log.debug(
                    'No matches for post %s and comment OC is disabled',
                    f'https://redd.it/{search_results.checked_post.post_id}')
                self._create_checked_post(post)
                continue

            try:
                comment = self._leave_comment(search_results, submission,
                                              monitored_sub)
            except APIException as e:
                error_type = None
                if hasattr(e, 'error_type'):
                    error_type = e.error_type
                log.exception('Praw API Exception.  Error Type: %s',
                              error_type,
                              exc_info=True)
                continue
            except RateLimitException:
                time.sleep(10)
                continue
            except Exception as e:
                log.exception('Failed to leave comment on %s in %s',
                              submission.id, submission.subreddit.display_name)
                continue

            msg_values = build_msg_values_from_search(
                search_results,
                self.uowm,
                target_days_old=monitored_sub.target_days_old)
            if search_results.checked_post.post_type == 'image':
                msg_values = build_image_msg_values_from_search(
                    search_results, self.uowm, **msg_values)

            self._sticky_reply(monitored_sub, comment)
            self._lock_post(monitored_sub, submission)
            self._remove_post(monitored_sub, submission)
            self._mark_post_as_oc(monitored_sub, submission)
            self._mark_post_as_comment_left(post)
            self._create_checked_post(post)

            if search_results.matches:
                self._report_submission(monitored_sub, submission)

        process_time = perf_counter() - start_time
        if self.event_logger:
            self.log_run(process_time, checked_posts, monitored_sub.name)
 def _(self, search_results: ImageSearchResults):
     return {
         **build_msg_values_from_search(search_results),
         **build_image_msg_values_from_search(search_results)
     }
Example #5
0
    def check_submission(self, monitored_sub: MonitoredSub, post: Post):
        log.info('Checking %s', post.post_id)
        if post.post_type == 'image' and post.dhash_h is None:
            log.error('Post %s has no dhash', post.post_id)
            return

        try:
            if post.post_type == 'image':
                search_results = self._check_for_repost(post, monitored_sub)
            elif post.post_type == 'link':
                search_results = self._check_for_link_repost(
                    post, monitored_sub)
                if search_results.matches:
                    # TODO - 1/12/2021 - Why are we doing this?
                    save_link_repost(post, search_results.matches[0].post,
                                     self.uowm, 'sub_monitor')
            else:
                log.error('Unsuported post type %s', post.post_type)
                return
        except NoIndexException:
            log.error('No search index available.  Cannot check post %s in %s',
                      post.post_id, post.subreddit)
            return
        except RedLockError:
            log.error(
                'New search index is being loaded. Cannot check post %s in %s',
                post.post_id, post.subreddit)
            return

        if not search_results.matches and not monitored_sub.comment_on_oc:
            log.debug(
                'No matches for post %s and comment OC is disabled',
                f'https://redd.it/{search_results.checked_post.post_id}')
            self._create_checked_post(post)
            return

        reply_comment = None
        try:
            if monitored_sub.comment_on_repost:
                reply_comment = self._leave_comment(search_results,
                                                    monitored_sub)
        except APIException as e:
            error_type = None
            if hasattr(e, 'error_type'):
                error_type = e.error_type
            log.exception('Praw API Exception.  Error Type: %s',
                          error_type,
                          exc_info=True)
            return
        except RateLimitException:
            time.sleep(10)
            return
        except Exception as e:
            log.exception('Failed to leave comment on %s in %s', post.post_id,
                          post.subreddit)
            return

        submission = self.reddit.submission(post.post_id)
        if not submission:
            log.error(
                'Failed to get submission %s for sub %s.  Cannot perform admin functions',
                post.post_id, post.subreddit)
            return

        if search_results.matches:
            msg_values = build_msg_values_from_search(
                search_results,
                self.uowm,
                target_days_old=monitored_sub.target_days_old)
            if search_results.checked_post.post_type == 'image':
                msg_values = build_image_msg_values_from_search(
                    search_results, self.uowm, **msg_values)

            report_msg = self.response_builder.build_report_msg(
                monitored_sub.name, msg_values)
            self._report_submission(monitored_sub, submission, report_msg)
            self._lock_post(monitored_sub, submission)
            self._remove_post(monitored_sub, submission)
        else:
            self._mark_post_as_oc(monitored_sub, submission)

        if reply_comment:
            self._sticky_reply(monitored_sub, reply_comment)
            self._lock_comment(monitored_sub, reply_comment)
        self._mark_post_as_comment_left(post)
        self._create_checked_post(post)