def set_image_hashes(post: Post, hash_size: int = 16) -> Post: log.debug('%s - Hashing image post %s', os.getpid(), post.post_id) try: img = generate_img_by_url(post.url) except ImageConversioinException as e: raise try: dhash_h = imagehash.dhash(img, hash_size=hash_size) dhash_v = imagehash.dhash_vertical(img, hash_size=hash_size) ahash = imagehash.average_hash(img, hash_size=hash_size) post.dhash_h = str(dhash_h) post.dhash_v = str(dhash_v) post.ahash = str(ahash) except Exception as e: # TODO: Specific exception log.exception('Error creating hash', exc_info=True) raise return post
def set_image_hashes_api(post: Post, api_url: str) -> Post: """ Call an external API to create image hashes. This allows us to offload bandwidth to another server. In the current case, a Digital Ocean Load Balancer :param post: Post to hash :param api_url: API URL to call :return: Dict of hashes """ log.debug('Hashing image post using api %s', post.post_id) r = requests.get(api_url, params={'url': post.url}) if r.status_code != 200: log.error('Back statuscode from DO API %s', r.status_code) raise ImageConversioinException('Bad response from DO API') hashes = json.loads(r.text) log.debug(hashes) post.dhash_h = hashes['dhash_h'] post.dhash_v = hashes['dhash_v'] post.ahash = hashes['ahash'] return post