mirror of
https://github.com/R0m1k3/Priceflow.git
synced 2026-10-16 19:57:55 +02:00
feat: add Amazon scraper service using Playwright, Browserless, and stealth for persistent sessions.
This commit is contained in:
1 parent
fd34bc66b2
commit
0ab7ac5a38
3 files changed
+115
-60
No files matched your search
+30
-16
@@ -12,27 +12,36 @@ from pathlib import Path
|
||||
# Ajouter le répertoire app au path
|
||||
sys.path.insert(0, str(Path(__file__).parent))
|
||||
|
||||
from app.services.amazon_scraper import (
|
||||
scrape_amazon_search,
|
||||
test_amazon_scraper,
|
||||
)
|
||||
from app.services.amazon_scraper_service import amazon_scraper_service, AmazonScraperService
|
||||
from app.core.search_config import AMAZON_PROXY_LIST_RAW, USER_AGENT_DATA
|
||||
|
||||
# Define missing variable for test compatibility
|
||||
AMAZON_USER_AGENTS = USER_AGENT_DATA
|
||||
USER_AGENT_POOL = USER_AGENT_DATA
|
||||
|
||||
|
||||
# Configuration du logging
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# Monkey patch _connect_browser to use local launch for testing
|
||||
async def _connect_browser_local(p):
|
||||
logger.info("Launching local browser (headless)...")
|
||||
return await p.chromium.launch(headless=True)
|
||||
|
||||
|
||||
AmazonScraperService._connect_browser = _connect_browser_local
|
||||
|
||||
|
||||
async def test_basic_search():
|
||||
"""Test basique de recherche"""
|
||||
logger.info("=" * 80)
|
||||
logger.info("TEST 1: Recherche basique - 'aspirateur'")
|
||||
logger.info("=" * 80)
|
||||
|
||||
products = await scrape_amazon_search("aspirateur", max_results=5)
|
||||
products = await amazon_scraper_service.scrape_search("aspirateur", max_results=5)
|
||||
|
||||
if not products:
|
||||
logger.error("❌ Aucun produit trouvé - possibilité de détection ou problème réseau")
|
||||
@@ -42,7 +51,9 @@ async def test_basic_search():
|
||||
|
||||
for idx, product in enumerate(products, 1):
|
||||
logger.info(f"\n{idx}. {product.title[:60]}...")
|
||||
logger.info(f" 💰 Prix: {product.price}€" + (f" (était {product.original_price}€)" if product.original_price else ""))
|
||||
logger.info(
|
||||
f" 💰 Prix: {product.price}€" + (f" (était {product.original_price}€)" if product.original_price else "")
|
||||
)
|
||||
logger.info(f" ⭐ Note: {product.rating}/5" if product.rating else " ⭐ Pas de note")
|
||||
logger.info(f" 📦 {'En stock' if product.in_stock else 'Indisponible'}")
|
||||
logger.info(f" {'🚚 Prime' if product.prime else '📮 Standard'}")
|
||||
@@ -62,7 +73,7 @@ async def test_multiple_queries():
|
||||
|
||||
for query in queries:
|
||||
logger.info(f"\n🔍 Recherche: '{query}'")
|
||||
products = await scrape_amazon_search(query, max_results=3)
|
||||
products = await amazon_scraper_service.scrape_search(query, max_results=3)
|
||||
results[query] = len(products)
|
||||
logger.info(f" ✅ {len(products)} produits trouvés")
|
||||
|
||||
@@ -88,18 +99,21 @@ async def test_anti_detection():
|
||||
logger.info("TEST 3: Vérification anti-détection")
|
||||
logger.info("=" * 80)
|
||||
|
||||
from app.core.search_config import AMAZON_PROXY_LIST_RAW, USER_AGENT_POOL
|
||||
from app.services.amazon_scraper import get_random_proxy, AMAZON_USER_AGENTS
|
||||
# Updated to just check if we can run
|
||||
logger.info("Skipping specific proxy/agent checks for this service as it handles them internally")
|
||||
|
||||
logger.info(f"✓ {len(AMAZON_PROXY_LIST_RAW)} proxies disponibles")
|
||||
logger.info(f"✓ {len(USER_AGENT_POOL)} User-Agents standards")
|
||||
logger.info(f"✓ {len(AMAZON_USER_AGENTS)} User-Agents Amazon spécifiques")
|
||||
|
||||
# Test proxy
|
||||
proxy = get_random_proxy()
|
||||
# Test proxy
|
||||
import random
|
||||
|
||||
proxy = random.choice(AMAZON_PROXY_LIST_RAW) if AMAZON_PROXY_LIST_RAW else None
|
||||
if proxy:
|
||||
# Extract just the IP for logging (hide credentials)
|
||||
proxy_parts = proxy.split('@')
|
||||
proxy_parts = proxy.split("@")
|
||||
proxy_server = proxy_parts[1] if len(proxy_parts) > 1 else proxy
|
||||
logger.info(f"✓ Proxy test: {proxy_server}")
|
||||
else:
|
||||
@@ -107,7 +121,7 @@ async def test_anti_detection():
|
||||
|
||||
# Test d'une recherche simple
|
||||
logger.info("\n🧪 Test de recherche avec anti-détection...")
|
||||
products = await scrape_amazon_search("livre", max_results=3)
|
||||
products = await amazon_scraper_service.scrape_search("livre", max_results=3)
|
||||
|
||||
if products:
|
||||
logger.info(f"✅ Anti-détection fonctionnel - {len(products)} produits extraits")
|
||||
|
||||
Reference in new issue
Block a user