Files
2022-02-12 17:21:42 +01:00

75 lines
2.3 KiB
Python

from pathlib import Path
from PIL import Image
import hashlib
from typing import *
from time import time
from datetime import datetime, timezone
from copy import deepcopy
import httpx
import imagehash
from io import BytesIO
from ghunt import globals as gb
def gprint(*args, **kwargs) -> None:
if not gb.config.silent_mode:
print(*args, **kwargs)
def within_docker() -> bool:
return Path('/.dockerenv').is_file()
def gen_sapisidhash(sapisid: str, origin: str, timestamp: str = str(int(time()))) -> str:
return f"{timestamp}_{hashlib.sha1(' '.join([timestamp, sapisid, origin]).encode()).hexdigest()}"
def extract_set_cookies(req: httpx.Response) -> Dict[str, str]:
return {pair[0]:''.join(pair[1:]) for x in req.headers.get_list("set-cookie") if (pair := x.split(";")[0].split("="))}
def inject_osid(cookies: Dict[str, str], osids: Dict[str, str], service: str) -> Dict[str, str]:
cookies_with_osid = deepcopy(cookies)
cookies_with_osid["OSID"] = osids[service]
return cookies_with_osid
def is_headers_syntax_good(headers: dict[str, str]) -> bool:
try:
httpx.Headers(headers)
return True
except:
return False
async def get_image_flathash(as_client: httpx.AsyncClient, image_url: str) -> imagehash.ImageHash:
req = await as_client.get(image_url)
img = Image.open(BytesIO(req.content))
flathash = imagehash.average_hash(img)
return flathash
async def is_default_profile_pic(as_client: httpx.AsyncClient, image_url: str):
flathash = await get_image_flathash(as_client, image_url)
if flathash - imagehash.hex_to_flathash("000018183c3c0000", 8) < 10 :
return True
return False
def get_class_name(obj) -> str:
return str(obj).strip("<>").split(" ")[0]
def get_datetime_utc(date_str):
"""Converts ISO to datetime object in UTC"""
date = datetime.fromisoformat(date_str)
margin = date.utcoffset()
return date.replace(tzinfo=timezone.utc) - margin
def ppnb(nb: float) -> float|int:
"""
Pretty print float number
Ex: 3.9 -> 3.9
4.0 -> 4
4.1 -> 4.1
"""
try:
return int(nb) if nb % int(nb) == 0.0 else nb
except ZeroDivisionError:
if nb == 0.0:
return 0
else:
return nb