Files
proxy-scraper-checker/proxy_scraper_checker/utils.py
T

38 lines
937 B
Python
Raw Normal View History

2024-01-21 17:45:01 +00:00
from __future__ import annotations
2024-02-07 13:03:31 +03:00
import asyncio
2024-02-08 09:12:13 +03:00
import functools
2024-02-01 07:24:26 +00:00
import os
2024-07-09 03:08:30 +03:00
from typing import TYPE_CHECKING
2024-01-21 17:45:01 +00:00
from urllib.parse import urlparse
import charset_normalizer
2024-02-08 09:12:13 +03:00
2024-07-09 03:08:30 +03:00
if TYPE_CHECKING:
from typing import Callable
2024-07-09 03:35:36 +03:00
from typing_extensions import ParamSpec, TypeVar
T = TypeVar("T")
P = ParamSpec("P")
2024-01-21 17:45:01 +00:00
2024-02-01 07:24:26 +00:00
IS_DOCKER = os.getenv("IS_DOCKER") == "1"
2024-01-21 17:45:01 +00:00
def is_http_url(value: str, /) -> bool:
2024-01-21 17:45:01 +00:00
parsed_url = urlparse(value)
return bool(parsed_url.scheme in {"http", "https"} and parsed_url.netloc)
2024-01-21 17:45:01 +00:00
def bytes_decode(value: bytes, /) -> str:
return str(charset_normalizer.from_bytes(value)[0])
2024-02-01 07:24:26 +00:00
2024-02-08 09:12:13 +03:00
def asyncify(f: Callable[P, T], /) -> Callable[P, asyncio.Future[T]]:
def wrapper(*args: P.args, **kwargs: P.kwargs) -> asyncio.Future[T]:
return asyncio.get_running_loop().run_in_executor(
None, functools.partial(f, *args, **kwargs)
)
return functools.update_wrapper(wrapper, f)