From 942c8d1244a0cdfc5bce6d58e69c7cbf15736ad5 Mon Sep 17 00:00:00 2001 From: furkanyigit34 <134547018+furkanyigit34@users.noreply.github.com> Date: Wed, 15 Apr 2026 01:53:45 +0300 Subject: [PATCH] fix: scraper Search API fallback + logging + auth MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ne yaptık: - queue_worker.py: TrendyolSearchScraper 0 ürün döndürdüğünde TrendyolScraper (Top Rankings API) ile fallback yap — abiye gibi kategoriler için kritik - logging_config.py: varsayılan log dizinini /tmp/logs olarak değiştir, container restart'ta /logs permission hatası düzeldi - main.py: API_KEY env var yoksa auth'u gerçekten atla (uyarıyla uyumlu hale getir) Neden yaptık: - TrendyolSearchScraper pathModel ile bazı kategoriler (abiye-elbise gibi) 0 ürün döndürüyor; eski Top Rankings API categoryId ile çalışıyor - /logs dizini container restart'ta izin hatası veriyordu - API_KEY yoksa tüm istekler 401 dönüyordu (yorum ile çelişki) --- backend/logging_config.py | 3 +-- backend/main.py | 3 +++ backend/queue_worker.py | 9 +++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/backend/logging_config.py b/backend/logging_config.py index e2c5acc..0b6baf5 100644 --- a/backend/logging_config.py +++ b/backend/logging_config.py @@ -121,12 +121,11 @@ def setup_logging(log_dir: str = None): - console output (INFO+, colored) """ if log_dir is None: - log_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "logs") + log_dir = "/tmp/logs" try: os.makedirs(log_dir, exist_ok=True) except PermissionError: - # Docker container'da /app/../logs yazılamayabilir, /tmp/logs kullan log_dir = "/tmp/logs" os.makedirs(log_dir, exist_ok=True) diff --git a/backend/main.py b/backend/main.py index f40fa2e..b14e504 100644 --- a/backend/main.py +++ b/backend/main.py @@ -61,6 +61,9 @@ async def verify_api_key(request: Request, api_key: Optional[str] = Security(API """ if request.url.path in PUBLIC_PATHS: return + if not API_KEY: + # API_KEY env var not set — authentication disabled + return if not api_key or api_key != API_KEY: raise HTTPException( status_code=401, diff --git a/backend/queue_worker.py b/backend/queue_worker.py index 1c0e7bc..b5851e6 100644 --- a/backend/queue_worker.py +++ b/backend/queue_worker.py @@ -168,6 +168,15 @@ class QueueWorker: scraper = TrendyolSearchScraper(path_model) products = scraper.fetch_all_products() + # Search API returned 0 — fallback to Top Rankings API + if not products and cat_id: + log.warning(f"Search API 0 ürün döndürdü ({path_model}), Top Rankings API ile fallback deneniyor...") + _trendyol_limiter.wait() + fallback_scraper = TrendyolScraper(cat_id, page_size=20) + products = fallback_scraper.fetch_all_products(delay=0.5, max_pages=5) + if products: + log.info(f"Fallback başarılı: {len(products)} ürün bulundu (cat_id={cat_id})") + # Enrich with socialProofs from Top Rankings API if products and cat_id and not any(p.get("socialProofs") for p in products): try: