refactor(tradein/scraper-kit): yandex providers on shared _base.py (#2363)
All checks were successful
CI Trade-In / changes (pull_request) Successful in 10s
CI / changes (pull_request) Successful in 12s
CI Trade-In / frontend-checks (pull_request) Has been skipped
CI / backend-tests (pull_request) Has been skipped
CI / frontend-tests (pull_request) Has been skipped
CI / openapi-codegen-check (pull_request) Has been skipped
CI Trade-In / backend-tests (pull_request) Successful in 1m45s
All checks were successful
CI Trade-In / changes (pull_request) Successful in 10s
CI / changes (pull_request) Successful in 12s
CI Trade-In / frontend-checks (pull_request) Has been skipped
CI / backend-tests (pull_request) Has been skipped
CI / frontend-tests (pull_request) Has been skipped
CI / openapi-codegen-check (pull_request) Has been skipped
CI Trade-In / backend-tests (pull_request) Successful in 1m45s
F4c-yandex (epic #2277 Group F, parent #2352): migrate the SERP scraper's BrowserFetcher construction onto build_browser_fetcher() from the Foundation module (#2358, providers/_base.py) instead of local ad-hoc construction. - yandex/serp.py::__aenter__ now calls build_browser_fetcher(config, source=, proxy_provider=, fetch_timeout_s=) instead of constructing BrowserFetcher(...) directly. Preserves the existing fetch_timeout_s=30 (see caveat below), endpoint/proxy_provider/use_pool resolution byte-for-byte. The retry/tarpit logic in fetch_around() (rotate_ip + sleep on status==0/JSON-error) is untouched, per the Foundation module's documented exclusion. Left unchanged (deliberate, not an oversight): - yandex/serp.py::_rotate_ip's curl_cffi AsyncSession(timeout=30) — this hits the mobile-proxy provider's own changeip API, not Yandex; it intentionally carries no impersonate/headers/proxy (same as the mirrored avito/serp.py rotate-ip session). Not listed among the _base.py docstring's documented extraction sites -- consistent exclusion, not a new one. - yandex/detail.py -- no BrowserFetcher/curl_cffi construction exists in this file (uses BaseScraper's httpx-based _http_get). Nothing to migrate. - yandex/newbuilding.py -- both BrowserFetcher(...) call sites take an OPTIONAL `config: ScraperConfig | None`, a genuinely different contract from build_browser_fetcher's mandatory `config`. Migrating would require making config mandatory (breaking admin.py:1511, yandex_newbuilding_sweep.py:353, and several tests that call without config -- out of scope for a call-site swap) and breaks test_scraper_kit_newbuilding_endpoint.py's "without config, endpoint=None" regression case (its SimpleNamespace mock lacks use_proxy_pool_browser, and the mock config path can never satisfy a mandatory-config helper). Left untouched; the class-bug #2322/#2330 this file already fixed (config now threaded, just not via the shared helper) remains fixed. Also updates test_kit_serp_proxy_pool.py's 3 yandex-specific tests: they monkeypatched the module-local `yandex_serp.BrowserFetcher` name, which the migrated call path no longer references directly (it now goes through scraper_kit.providers._base.build_browser_fetcher -> _base's own BrowserFetcher import). Patch target moved to scraper_kit.providers._base.BrowserFetcher; every assertion is unchanged -- this adapts the mock to the new (intentional) call path, it does not weaken what the test verifies. Cian's tests in the same file are untouched (cian/serp.py is not migrated yet, separate F4b issue).
This commit is contained in:
parent
53e7834f19
commit
802b114e0d
2 changed files with 21 additions and 7 deletions
|
|
@ -12,6 +12,16 @@ proxy_provider + use_pool=config.use_proxy_pool_browser в BrowserFetcher.
|
||||||
|
|
||||||
BrowserFetcher монки-патчится на recording-заглушку в модуле каждого скрапера, так что
|
BrowserFetcher монки-патчится на recording-заглушку в модуле каждого скрапера, так что
|
||||||
реального HTTP к tradein-browser не происходит.
|
реального HTTP к tradein-browser не происходит.
|
||||||
|
|
||||||
|
Yandex (#2363, F4c миграция): `yandex/serp.py::__aenter__` больше не конструирует
|
||||||
|
`BrowserFetcher(...)` напрямую — вызывает shared `build_browser_fetcher()` из
|
||||||
|
`scraper_kit.providers._base`, который ВНУТРИ себя ссылается на СВОЙ собственный
|
||||||
|
импорт `BrowserFetcher`. Патч локального имени `yandex_serp.BrowserFetcher` больше
|
||||||
|
не перехватывает вызов (реальный `BrowserFetcher.__init__` конструировался бы, а
|
||||||
|
`calls` оставался пуст) — патчим `scraper_kit.providers._base.BrowserFetcher`
|
||||||
|
(реальную зависимость `build_browser_fetcher`), сохраняя все assertions как есть.
|
||||||
|
Cian (`cian/serp.py`) пока НЕ мигрирован (F4b, отдельный issue) — там патч
|
||||||
|
локального имени остаётся корректным.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
@ -84,7 +94,7 @@ def _config(*, use_pool: bool) -> types.SimpleNamespace:
|
||||||
async def test_yandex_serp_wires_pool_when_flag_on(monkeypatch: pytest.MonkeyPatch) -> None:
|
async def test_yandex_serp_wires_pool_when_flag_on(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||||
"""proxy_provider + флаг on → BrowserFetcher получает провайдер + use_pool=True."""
|
"""proxy_provider + флаг on → BrowserFetcher получает провайдер + use_pool=True."""
|
||||||
rec_fetcher, calls = _make_recorder()
|
rec_fetcher, calls = _make_recorder()
|
||||||
monkeypatch.setattr(yandex_serp, "BrowserFetcher", rec_fetcher)
|
monkeypatch.setattr("scraper_kit.providers._base.BrowserFetcher", rec_fetcher)
|
||||||
provider = _FakeProxyProvider()
|
provider = _FakeProxyProvider()
|
||||||
|
|
||||||
scraper = yandex_serp.YandexRealtyScraper(_config(use_pool=True), proxy_provider=provider)
|
scraper = yandex_serp.YandexRealtyScraper(_config(use_pool=True), proxy_provider=provider)
|
||||||
|
|
@ -101,7 +111,7 @@ async def test_yandex_serp_wires_pool_when_flag_on(monkeypatch: pytest.MonkeyPat
|
||||||
async def test_yandex_serp_no_pool_by_default(monkeypatch: pytest.MonkeyPatch) -> None:
|
async def test_yandex_serp_no_pool_by_default(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||||
"""Без provider/флага → BrowserFetcher(proxy_provider=None, use_pool=False) — ship-dark."""
|
"""Без provider/флага → BrowserFetcher(proxy_provider=None, use_pool=False) — ship-dark."""
|
||||||
rec_fetcher, calls = _make_recorder()
|
rec_fetcher, calls = _make_recorder()
|
||||||
monkeypatch.setattr(yandex_serp, "BrowserFetcher", rec_fetcher)
|
monkeypatch.setattr("scraper_kit.providers._base.BrowserFetcher", rec_fetcher)
|
||||||
|
|
||||||
scraper = yandex_serp.YandexRealtyScraper(_config(use_pool=False))
|
scraper = yandex_serp.YandexRealtyScraper(_config(use_pool=False))
|
||||||
async with scraper:
|
async with scraper:
|
||||||
|
|
@ -118,7 +128,7 @@ async def test_yandex_serp_provider_without_flag_stays_dark(
|
||||||
) -> None:
|
) -> None:
|
||||||
"""provider задан, но флаг off → use_pool=False (BrowserFetcher пул не трогает)."""
|
"""provider задан, но флаг off → use_pool=False (BrowserFetcher пул не трогает)."""
|
||||||
rec_fetcher, calls = _make_recorder()
|
rec_fetcher, calls = _make_recorder()
|
||||||
monkeypatch.setattr(yandex_serp, "BrowserFetcher", rec_fetcher)
|
monkeypatch.setattr("scraper_kit.providers._base.BrowserFetcher", rec_fetcher)
|
||||||
provider = _FakeProxyProvider()
|
provider = _FakeProxyProvider()
|
||||||
|
|
||||||
scraper = yandex_serp.YandexRealtyScraper(_config(use_pool=False), proxy_provider=provider)
|
scraper = yandex_serp.YandexRealtyScraper(_config(use_pool=False), proxy_provider=provider)
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,7 @@ from scraper_kit.browser_fetcher import BrowserFetcher
|
||||||
from scraper_kit.house_type_normalizer import normalize_house_type
|
from scraper_kit.house_type_normalizer import normalize_house_type
|
||||||
from scraper_kit.price_brackets import get_price_seed_brackets
|
from scraper_kit.price_brackets import get_price_seed_brackets
|
||||||
from scraper_kit.pricing import BisectionConfig, ProbeFailPolicy, ProbeResult, walk_price_range
|
from scraper_kit.pricing import BisectionConfig, ProbeFailPolicy, ProbeResult, walk_price_range
|
||||||
|
from scraper_kit.providers._base import build_browser_fetcher
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from scraper_kit.contracts import ProxyProvider, ScraperConfig
|
from scraper_kit.contracts import ProxyProvider, ScraperConfig
|
||||||
|
|
@ -506,13 +507,16 @@ class YandexRealtyScraper(BaseScraper):
|
||||||
|
|
||||||
fetch_timeout_s=_YANDEX_BROWSER_FETCH_TIMEOUT_S (30s) ограничивает потери
|
fetch_timeout_s=_YANDEX_BROWSER_FETCH_TIMEOUT_S (30s) ограничивает потери
|
||||||
на один тайм-аутовый запрос: worst-case 30s×(1+retries)=90s на combo вместо 360s.
|
на один тайм-аутовый запрос: worst-case 30s×(1+retries)=90s на combo вместо 360s.
|
||||||
|
|
||||||
|
Собрано через shared `build_browser_fetcher()` (#2358 Foundation, #2363
|
||||||
|
миграция) — endpoint/use_pool берутся из `config` mandatory, footgun-класс
|
||||||
|
#2322/#2330 (пропущенный endpoint/use_pool) закрыт структурно.
|
||||||
"""
|
"""
|
||||||
self._browser = BrowserFetcher(
|
self._browser = build_browser_fetcher(
|
||||||
|
self._config,
|
||||||
source="yandex",
|
source="yandex",
|
||||||
fetch_timeout_s=_YANDEX_BROWSER_FETCH_TIMEOUT_S,
|
|
||||||
endpoint=self._config.browser_http_endpoint,
|
|
||||||
proxy_provider=self._proxy_provider,
|
proxy_provider=self._proxy_provider,
|
||||||
use_pool=self._config.use_proxy_pool_browser,
|
fetch_timeout_s=_YANDEX_BROWSER_FETCH_TIMEOUT_S,
|
||||||
)
|
)
|
||||||
await self._browser.__aenter__()
|
await self._browser.__aenter__()
|
||||||
return self
|
return self
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue