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'])
Beispiel #2
0
def check_meme_template_potential_votes(uowm: UnitOfWorkManager) -> NoReturn:
    with uowm.start() as uow:
        potential_templates = uow.meme_template_potential.get_all()
        for potential_template in potential_templates:
            if potential_template.vote_total >= 10:
                existing_template = uow.meme_template.get_by_post_id(potential_template.post_id)
                if existing_template:
                    log.info('Meme template already exists for %s. Removing', potential_template.post_id)
                    uow.meme_template_potential.remove(potential_template)
                    uow.commit()
                    return

                log.info('Post %s received %s votes.  Creating meme template', potential_template.post_id, potential_template.vote_total)
                post = uow.posts.get_by_post_id(potential_template.post_id)
                try:
                    meme_hashes = get_image_hashes(post.searched_url, hash_size=32)
                except Exception as e:
                    log.error('Failed to get meme hash for %s', post.post_id)
                    return

                meme_template = MemeTemplate(
                    dhash_h=post.dhash_h,
                    dhash_256=meme_hashes['dhash_h'],
                    post_id=post.post_id
                )
                uow.meme_template.add(meme_template)
                uow.meme_template_potential.remove(potential_template)
            elif potential_template.vote_total <= -10:
                log.info('Removing potential template with at least 10 negative votes')
                uow.meme_template_potential.remove(potential_template)
            else:
                continue
            uow.commit()
Beispiel #3
0
 def on_post(self, req: Request, resp: Response):
     token = req.get_param('token', required=True)
     user_data = get_user_data(token)
     data = json.load(req.bounded_stream)
     with self.uowm.start() as uow:
         post = uow.posts.get_by_post_id(data['post_id'])
         if not post:
             raise HTTPNotFound(title='Failed to create meme template', description=f'Failed to create meme template.  Cannot find post {data["post_id"]}')
         template = MemeTemplate(
             post_id=data['post_id'],
             dhash_h=post.dhash_h
         )
         uow.repostwatch.add(template)
         uow.commit()
Beispiel #4
0
def check_for_high_match_meme(search_results: ImageSearchResults,
                              uowm: UnitOfWorkManager) -> NoReturn:
    if search_results.meme_template is not None:
        return

    with uowm.start() as uow:
        meme_template = None
        # TODO - 1/12/2021 - Should probably remember the meme in subreddit check and generate more templates
        if len(
                search_results.matches
        ) > 5 and 'meme' in search_results.checked_post.subreddit.lower():
            try:
                meme_hashes = get_image_hashes(search_results.checked_post.url,
                                               hash_size=32)
            except Exception as e:
                log.error('Failed to get meme hash for %s',
                          search_results.checked_post.post_id)
                return

            try:
                meme_template = MemeTemplate(
                    dhash_h=search_results.checked_post.dhash_h,
                    dhash_256=meme_hashes['dhash_h'],
                    post_id=search_results.checked_post.post_id)

                uow.meme_template.add(meme_template)
                uow.commit()
            except IntegrityError as e:
                log.exception(
                    f'Failed to create meme template. Template already exists for post {search_results.checked_post.post_id}',
                    exc_info=True)
                meme_template = None

        if meme_template:
            log.info('Saved new meme template for post %s in %s',
                     search_results.checked_post.post_id,
                     search_results.checked_post.subreddit)
            # Raise exception so celery will retry the task and use the new meme template
            raise IngestHighMatchMeme(
                'Created meme template.  Post needs to be rechecked')