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

40 lines
999 B
Python
Raw Normal View History

2024-01-21 17:45:01 +00:00
from __future__ import annotations
import ssl
from functools import lru_cache
from types import MappingProxyType
import certifi
2024-01-23 09:37:29 +03:00
from aiohttp import ClientResponse, DummyCookieJar, hdrs
from .utils import bytes_decode
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-01-21 17:45:01 +00:00
HEADERS: MappingProxyType[str, str] = MappingProxyType({
hdrs.USER_AGENT: (
2024-01-29 14:04:37 +03:00
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36" # noqa: E501
2024-01-21 17:45:01 +00:00
)
})
@lru_cache(None)
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)
def fallback_charset_resolver(r: ClientResponse, b: bytes) -> str: # noqa: ARG001
raise NoCharsetHeaderError