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()
def target_hash(self) -> Text: """ Returns the hash to be searched. This allows us to work with just a URL or a full post :return: hash """ if self._target_hash: return self._target_hash hashes = get_image_hashes(self.checked_url, hash_size=16) self._target_hash = hashes['dhash_h'] return self._target_hash
def _get_meme_hash(self, url: Text) -> Optional[Text]: """ Take a given URL and return the hash that will be used for the meme filter :param url: URL to hash :return: Hash of the image :rtype: Optional[Text] """ try: meme_hashes = get_image_hashes( url, hash_size=self.config.default_meme_filter_hash_size) return meme_hashes['dhash_h'] except ImageConversioinException: log.error('Failed to get meme hash') return except Exception: log.exception('Failed to get meme hash for %s', url, exc_info=True) return
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')
config = Config( r'/home/barry/PycharmProjects/RedditRepostSleuth/sleuth_config.json') reddit = get_reddit_instance(config) uowm = SqlAlchemyUnitOfWorkManager(get_db_engine(config)) reddit_manager = RedditManager(reddit) event_logger = EventLogging(config=config) response_handler = ResponseHandler(reddit_manager, uowm, event_logger, source='submonitor') dup_image_svc = DuplicateImageService(uowm, event_logger, config=config) response_builder = ResponseBuilder(uowm) sub_monitor = SubMonitor(dup_image_svc, uowm, reddit_manager, response_builder, response_handler, event_logger=event_logger, config=config) with uowm.start() as uow: post = uow.posts.get_by_post_id('iirpkm') target_hashes = get_image_hashes(post.searched_url, hash_size=32) with uowm.start() as uow: monitored_subs = uow.monitored_sub.get_all() for sub in monitored_subs: #sub.target_image_match = 100 - (sub.target_hamming / 64) * 100 sub.target_image_meme_match = 92 uow.commit()