diff --git a/ghunt/helpers/utils.py b/ghunt/helpers/utils.py index 28402d4e..f0a7ff62 100644 --- a/ghunt/helpers/utils.py +++ b/ghunt/helpers/utils.py @@ -48,22 +48,31 @@ def is_headers_syntax_good(headers: Dict[str, str]) -> bool: except: return False -async def get_url_image_flathash(as_client: httpx.AsyncClient, image_url: str) -> str: - req = await as_client.get(image_url) - img = Image.open(BytesIO(req.content)) - flathash = imagehash.average_hash(img) - return str(flathash) +async def get_url_image_flathash(as_client: httpx.AsyncClient, image_url: str) -> Optional[str]: + try: + req = await as_client.get(image_url) + if req.status_code != 200 or not req.content: + return None # Image could not be fetched + + img = Image.open(BytesIO(req.content)) + flathash = imagehash.average_hash(img) + return str(flathash) + + except Exception: + return None # Image processing failed + async def is_default_profile_pic(as_client: httpx.AsyncClient, image_url: str) -> Tuple[bool, str]: - """ - Returns a boolean which indicates if the image_url - is a default profile picture, and the flathash of - the image. - """ flathash = await get_url_image_flathash(as_client, image_url) - if imagehash.hex_to_flathash(flathash, 8) - imagehash.hex_to_flathash("000018183c3c0000", 8) < 10 : - return True, str(flathash) - return False, str(flathash) + + if flathash is None: + return False, "" # Unable to process image + + default_flathash = "000018183c3c0000" + if imagehash.hex_to_flathash(flathash, 8) - imagehash.hex_to_flathash(default_flathash, 8) < 10: + return True, flathash + + return False, flathash def get_class_name(obj) -> str: return str(obj).strip("<>").split(" ")[0] @@ -121,4 +130,4 @@ def unicode_patch(txt: str): "ç": "c", "à": "a" } - return txt.replace(''.join([*bad_chars.keys()]), ''.join([*bad_chars.values()])) \ No newline at end of file + return txt.replace(''.join([*bad_chars.keys()]), ''.join([*bad_chars.values()]))