feat: Implement hybrid proxy strategy for Amazon scraping - fallback to Direct IP on last retry

This commit is contained in:
Michael committed 2025-11-29 01:17:03 +01:00
1 parent dd558b0e43
commit aa9d2af5b2
1 file changed
+31 -18
+31 -18
View File
@@ -481,6 +481,8 @@ class ScraperService:
timeout=timeout, timeout=timeout,
browser=browser, browser=browser,
is_amazon=is_amazon, is_amazon=is_amazon,
attempt=attempt,
max_retries=max_retries,
) )
if result[0] is not None: # Screenshot réussi if result[0] is not None: # Screenshot réussi
@@ -503,6 +505,8 @@ class ScraperService:
timeout: int = 90000, timeout: int = 90000,
browser=None, browser=None,
is_amazon: bool = False, is_amazon: bool = False,
attempt: int = 0,
max_retries: int = 1,
) -> tuple[str | None, str, bool]: ) -> tuple[str | None, str, bool]:
"""Exécute le scraping réel (appelé par scrape_item avec retries).""" """Exécute le scraping réel (appelé par scrape_item avec retries)."""
@@ -553,26 +557,35 @@ class ScraperService:
if is_amazon: if is_amazon:
extra_headers["Referer"] = "https://www.google.fr/" extra_headers["Referer"] = "https://www.google.fr/"
try: # Gestion intelligente du Proxy :
# Prepare context options # - Tentatives 0 à MAX-1 : Utiliser un Proxy (si dispo)
context_options = { # - Dernière tentative (max_retries) : Essayer SANS Proxy (Direct IP) en dernier recours
"viewport": {"width": 1920, "height": 1080}, # (car l'utilisateur signale que l'accès direct marche parfois mieux pour les pages produits)
"user_agent": current_user_agent, use_proxy = is_amazon and (attempt < max_retries)
"locale": "fr-FR",
"timezone_id": "Europe/Paris", if not use_proxy and is_amazon and attempt > 0:
"extra_http_headers": extra_headers, logger.info(f"Tentative {attempt + 1}/{max_retries + 1} : Passage en mode DIRECT (Sans Proxy) pour tenter de contourner le blocage proxy")
"java_script_enabled": True,
"bypass_csp": True,
}
# Add proxy for Amazon if configured # Prepare context options
if is_amazon: context_options = {
proxy_config = _get_amazon_proxy() "viewport": {"width": 1920, "height": 1080},
if proxy_config: "user_agent": current_user_agent,
# proxy_config is already a dict with {"server": "...", "username": "...", "password": "..."} "locale": "fr-FR",
context_options["proxy"] = proxy_config "timezone_id": "Europe/Paris",
"extra_http_headers": extra_headers,
"java_script_enabled": True,
"bypass_csp": True,
}
context = await current_browser.new_context(**context_options) # Add proxy for Amazon if configured AND enabled for this attempt
if use_proxy:
proxy_config = _get_amazon_proxy()
if proxy_config:
# proxy_config is already a dict with {"server": "...", "username": "...", "password": "..."}
context_options["proxy"] = proxy_config
# Création du contexte
context = await current_browser.new_context(**context_options)
# Inject stealth script before any page loads (especially for Amazon) # Inject stealth script before any page loads (especially for Amazon)
if is_amazon: if is_amazon: