Skip to content

Commit

Permalink
Update utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMurd committed Jan 15, 2024
1 parent 7d117a3 commit 96e78c0
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions ghunt/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -121,4 +130,4 @@ def unicode_patch(txt: str):
"ç": "c",
"à": "a"
}
return txt.replace(''.join([*bad_chars.keys()]), ''.join([*bad_chars.values()]))
return txt.replace(''.join([*bad_chars.keys()]), ''.join([*bad_chars.values()]))

0 comments on commit 96e78c0

Please sign in to comment.