feat: Implement improved search service with supporting models, schemas, and API routers, and refactor site verification script.

This commit is contained in:
Michael committed 2025-11-30 23:33:05 +01:00
1 parent 4720ed6b93
commit 9855040f3e
21 files changed
+57932 -419

No files matched your search

+51 -129
View File
@@ -1,147 +1,69 @@
"""
Script de vérification complète du comparateur sur tous les sites configurés
"""
import asyncio
import logging
import sys
import os
from datetime import datetime
# Add project root to path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".")))
import time
from app.services.improved_search_service import ImprovedSearchService
from app.core.search_config import SITE_CONFIGS
from app.services.search_service import new_search_service
from app.services.browserless_service import browserless_service
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[logging.StreamHandler()]
)
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
async def test_site(site_key: str, query: str) -> dict:
"""Test a single site and return results"""
config = SITE_CONFIGS[site_key]
result = {
"site": config["name"],
"site_key": site_key,
"category": config["category"],
"success": False,
"count": 0,
"error": None,
"sample_results": []
}
async def verify_site(site_key: str):
print(f"\n{'='*50}")
print(f"🔍 Verifying {site_key}...")
print(f"{'='*50}")
start_time = time.time()
first_result_time = None
count = 0
try:
print(f"\n🔍 Testing {config['name']} ({site_key})...")
results = await new_search_service.search_site(site_key, query)
if results:
result["success"] = True
result["count"] = len(results)
# Store first 2 results as samples
result["sample_results"] = [
{
"title": r.title,
"url": r.url,
"price": r.price,
"in_stock": r.in_stock
}
for r in results[:2]
]
print(f" ✅ Found {len(results)} results")
for r in results[:2]:
price_str = f"{r.price}€" if r.price else "N/A"
stock_str = "✓" if r.in_stock else "✗" if r.in_stock is False else "?"
print(f" - {r.title[:60]}... [{price_str}] [Stock: {stock_str}]")
else:
print(f" ⚠️ No results found")
async for result in ImprovedSearchService.search_site_generator(site_key, "chaise"):
count += 1
if count == 1:
first_result_time = time.time()
elapsed = first_result_time - start_time
print(f"🚀 First result in {elapsed:.2f}s")
print(f" Title: {result.title}")
print(f" Price: {result.price} {result.currency}")
print(f" Image: {result.image_url}")
except Exception as e:
result["error"] = str(e)
print(f" ❌ Error: {e}")
logger.exception(f"Error testing {site_key}")
return result
# Print a dot for each result to show progress
print(".", end="", flush=True)
total_time = time.time() - start_time
print(f"\n✅ Finished {site_key}: {count} results in {total_time:.2f}s")
if count == 0:
print(f"❌ WARNING: 0 results found for {site_key}")
return False
return True
async def verify_all_sites():
"""Test all configured sites"""
query = "chaise" # Test query
print("=" * 80)
print(f"VERIFICATION DU COMPARATEUR - Query: '{query}'")
print(f"Started at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("=" * 80)
# 1. Test Browserless Connection
print("\n[1] Testing Browserless Connection...")
try:
await browserless_service.initialize()
print("✅ Browserless connected successfully")
except Exception as e:
print(f"❌ Browserless connection failed: {e}")
return
print(f"\n❌ ERROR verifying {site_key}: {e}")
return False
async def main():
await ImprovedSearchService.initialize()
# 2. Test all sites
print(f"\n[2] Testing {len(SITE_CONFIGS)} configured sites...")
sites = list(SITE_CONFIGS.keys())
results = {}
all_results = []
for site_key in SITE_CONFIGS.keys():
result = await test_site(site_key, query)
all_results.append(result)
# Small delay between sites to avoid overload
await asyncio.sleep(1)
# Test all sites sequentially to avoid overwhelming the browser/network
for site in sites:
success = await verify_site(site)
results[site] = success
# Small pause between sites
await asyncio.sleep(2)
await ImprovedSearchService.shutdown()
# 3. Summary
print("\n" + "=" * 80)
print("\n" + "="*50)
print("SUMMARY")
print("=" * 80)
successful = [r for r in all_results if r["success"]]
failed = [r for r in all_results if not r["success"]]
print(f"\n✅ Successful: {len(successful)}/{len(all_results)}")
for r in successful:
print(f" - {r['site']:20s} | {r['count']:2d} results | Category: {r['category']}")
if failed:
print(f"\n❌ Failed: {len(failed)}/{len(all_results)}")
for r in failed:
error_msg = r['error'][:50] if r['error'] else "No results"
print(f" - {r['site']:20s} | Error: {error_msg}")
# 4. Detailed results by category
print("\n" + "=" * 80)
print("BY CATEGORY")
print("=" * 80)
categories = {}
for r in all_results:
cat = r["category"]
if cat not in categories:
categories[cat] = []
categories[cat].append(r)
for cat, sites in categories.items():
successful_count = sum(1 for s in sites if s["success"])
print(f"\n{cat}: {successful_count}/{len(sites)} working")
for site in sites:
status = "✅" if site["success"] else "❌"
count = f"({site['count']} results)" if site["success"] else "(failed)"
print(f" {status} {site['site']:20s} {count}")
# 5. Cleanup
await browserless_service.shutdown()
print("\n" + "=" * 80)
print(f"Verification completed at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("=" * 80)
return all_results
print("="*50)
for site, success in results.items():
status = "✅ PASS" if success else "❌ FAIL"
print(f"{status} - {site}")
if __name__ == "__main__":
asyncio.run(verify_all_sites())
asyncio.run(main())