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

45 lines
1.1 KiB
Python
Raw Normal View History

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:
2024-12-26 10:13:54 +03:00
from typing import NoReturn
2024-07-09 03:08:30 +03:00
from aiohttp import ClientResponse
2024-02-07 10:25:01 +03:00
HEADERS: MappingProxyType[str, str] = MappingProxyType({
hdrs.USER_AGENT: (
2025-02-10 09:39:30 +03:00
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.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())
2025-02-05 06:22:56 +00:00
SSL_CONTEXT.set_alpn_protocols(("http/1.1",))
2024-01-23 09:37:29 +03:00
class NoCharsetHeaderError(Exception):
pass
2025-01-08 00:39:46 +03:00
def fallback_charset_resolver(_r: ClientResponse, _b: bytes) -> NoReturn:
2024-02-07 10:25:01 +03:00
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)