2024-01-21 17:45:01 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import ssl
|
2024-10-30 11:12:21 +03:00
|
|
|
from functools import cache
|
2024-01-21 17:45:01 +00:00
|
|
|
from types import MappingProxyType
|
2024-07-09 03:08:30 +03:00
|
|
|
from typing import TYPE_CHECKING
|
2024-01-21 17:45:01 +00:00
|
|
|
|
|
|
|
|
import certifi
|
2024-07-09 03:08:30 +03:00
|
|
|
from aiohttp import DummyCookieJar, hdrs
|
2024-01-23 09:37:29 +03:00
|
|
|
|
2024-11-22 14:32:29 +03:00
|
|
|
from proxy_scraper_checker.utils import bytes_decode
|
2024-01-21 17:45:01 +00:00
|
|
|
|
2024-07-09 03:08:30 +03:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from aiohttp import ClientResponse
|
|
|
|
|
|
2024-02-07 10:25:01 +03:00
|
|
|
HEADERS: MappingProxyType[str, str] = MappingProxyType({
|
|
|
|
|
hdrs.USER_AGENT: (
|
2024-11-28 08:53:20 +03:00
|
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" # noqa: E501
|
2024-02-07 10:25:01 +03:00
|
|
|
)
|
|
|
|
|
})
|
2024-01-21 17:45:01 +00:00
|
|
|
SSL_CONTEXT = ssl.create_default_context(cafile=certifi.where())
|
2024-01-23 09:37:29 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class NoCharsetHeaderError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2024-02-07 10:25:01 +03:00
|
|
|
def fallback_charset_resolver(r: ClientResponse, b: bytes) -> str: # noqa: ARG001
|
|
|
|
|
raise NoCharsetHeaderError
|
2024-01-21 17:45:01 +00:00
|
|
|
|
|
|
|
|
|
2024-10-30 11:12:21 +03:00
|
|
|
@cache
|
2024-01-21 17:45:01 +00:00
|
|
|
def get_cookie_jar() -> DummyCookieJar:
|
|
|
|
|
return DummyCookieJar()
|
2024-01-23 09:37:29 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_response_text(*, response: ClientResponse, content: bytes) -> str:
|
|
|
|
|
try:
|
|
|
|
|
return content.decode(response.get_encoding())
|
|
|
|
|
except (NoCharsetHeaderError, UnicodeDecodeError):
|
|
|
|
|
return bytes_decode(content)
|