|
| 1 | +"""Scrapling-based HTTP client adapter. |
| 2 | +
|
| 3 | +Provides the same interface as HTTPClient (get, get_json, post_json) but uses |
| 4 | +Scrapling's curl_cffi-backed transport, which impersonates Chrome's TLS |
| 5 | +fingerprint and bypasses many bot-detection systems that block plain aiohttp. |
| 6 | +
|
| 7 | +Drop-in replacement:: |
| 8 | +
|
| 9 | + from core.scrapling_client import ScraplingClient |
| 10 | + async with ScraplingClient() as client: |
| 11 | + status, body, elapsed = await client.get(url) |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import asyncio |
| 17 | +import json as _json |
| 18 | +import logging |
| 19 | +import time |
| 20 | +from typing import Any |
| 21 | + |
| 22 | +log = logging.getLogger(__name__) |
| 23 | + |
| 24 | +try: |
| 25 | + from scrapling import AsyncFetcher |
| 26 | + |
| 27 | + SCRAPLING_AVAILABLE = True |
| 28 | +except ImportError: # pragma: no cover |
| 29 | + AsyncFetcher = None # type: ignore[assignment] |
| 30 | + SCRAPLING_AVAILABLE = False |
| 31 | + |
| 32 | + |
| 33 | +class ScraplingClient: |
| 34 | + """Async HTTP client backed by Scrapling's stealthy TLS transport.""" |
| 35 | + |
| 36 | + def __init__( |
| 37 | + self, |
| 38 | + *, |
| 39 | + proxy: str | None = None, |
| 40 | + request_timeout: float = 15.0, |
| 41 | + fingerprint: bool = True, |
| 42 | + ) -> None: |
| 43 | + self._proxy = proxy |
| 44 | + self._timeout = request_timeout |
| 45 | + self._fingerprint = fingerprint |
| 46 | + self._fetcher: Any = None |
| 47 | + |
| 48 | + async def __aenter__(self) -> ScraplingClient: |
| 49 | + if not SCRAPLING_AVAILABLE: |
| 50 | + raise RuntimeError("Scrapling is not installed") |
| 51 | + self._fetcher = AsyncFetcher() |
| 52 | + return self |
| 53 | + |
| 54 | + async def __aexit__(self, *args: object) -> None: |
| 55 | + self._fetcher = None |
| 56 | + |
| 57 | + async def get( |
| 58 | + self, |
| 59 | + url: str, |
| 60 | + headers: dict[str, str] | None = None, |
| 61 | + ) -> tuple[int, str, float]: |
| 62 | + """Fetch *url* and return ``(status, body, elapsed)``.""" |
| 63 | + t0 = time.monotonic() |
| 64 | + fetcher = self._fetcher |
| 65 | + if fetcher is None: |
| 66 | + return -1, "", 0.0 |
| 67 | + try: |
| 68 | + resp = await asyncio.wait_for( |
| 69 | + fetcher.get(url, headers=headers), |
| 70 | + timeout=self._timeout, |
| 71 | + ) |
| 72 | + elapsed = time.monotonic() - t0 |
| 73 | + return resp.status, resp.html_content, elapsed |
| 74 | + except asyncio.TimeoutError: |
| 75 | + return 0, "", time.monotonic() - t0 |
| 76 | + except Exception as exc: |
| 77 | + log.debug("Scrapling get failed for %s: %s", url, exc) |
| 78 | + return -1, "", time.monotonic() - t0 |
| 79 | + |
| 80 | + async def get_json( |
| 81 | + self, |
| 82 | + url: str, |
| 83 | + headers: dict[str, str] | None = None, |
| 84 | + ) -> tuple[int, dict | None, float]: |
| 85 | + """Fetch JSON from *url* and return ``(status, parsed, elapsed)``.""" |
| 86 | + t0 = time.monotonic() |
| 87 | + fetcher = self._fetcher |
| 88 | + if fetcher is None: |
| 89 | + return -1, None, 0.0 |
| 90 | + try: |
| 91 | + resp = await asyncio.wait_for( |
| 92 | + fetcher.get(url, headers=headers), |
| 93 | + timeout=self._timeout, |
| 94 | + ) |
| 95 | + elapsed = time.monotonic() - t0 |
| 96 | + if resp.status != 200: |
| 97 | + return resp.status, None, elapsed |
| 98 | + try: |
| 99 | + return resp.status, resp.json(), elapsed |
| 100 | + except Exception: |
| 101 | + try: |
| 102 | + return resp.status, _json.loads(resp.html_content), elapsed |
| 103 | + except Exception: |
| 104 | + return resp.status, None, elapsed |
| 105 | + except asyncio.TimeoutError: |
| 106 | + return 0, None, time.monotonic() - t0 |
| 107 | + except Exception as exc: |
| 108 | + log.debug("Scrapling get_json failed for %s: %s", url, exc) |
| 109 | + return -1, None, time.monotonic() - t0 |
| 110 | + |
| 111 | + async def post_json( |
| 112 | + self, |
| 113 | + url: str, |
| 114 | + json_body: dict, |
| 115 | + headers: dict[str, str] | None = None, |
| 116 | + ) -> tuple[int, dict | None, float]: |
| 117 | + """POST JSON and return ``(status, parsed, elapsed)``.""" |
| 118 | + t0 = time.monotonic() |
| 119 | + fetcher = self._fetcher |
| 120 | + if fetcher is None: |
| 121 | + return -1, None, 0.0 |
| 122 | + merged = dict(headers or {}) |
| 123 | + merged.setdefault("Content-Type", "application/json") |
| 124 | + try: |
| 125 | + resp = await asyncio.wait_for( |
| 126 | + fetcher.post(url, json=json_body, headers=merged), |
| 127 | + timeout=self._timeout, |
| 128 | + ) |
| 129 | + elapsed = time.monotonic() - t0 |
| 130 | + if resp.status != 200: |
| 131 | + return resp.status, None, elapsed |
| 132 | + try: |
| 133 | + return resp.status, resp.json(), elapsed |
| 134 | + except Exception: |
| 135 | + try: |
| 136 | + return resp.status, _json.loads(resp.html_content), elapsed |
| 137 | + except Exception: |
| 138 | + return resp.status, None, elapsed |
| 139 | + except asyncio.TimeoutError: |
| 140 | + return 0, None, time.monotonic() - t0 |
| 141 | + except Exception as exc: |
| 142 | + log.debug("Scrapling post failed for %s: %s", url, exc) |
| 143 | + return -1, None, time.monotonic() - t0 |
0 commit comments