fix: scraper Search API fallback + logging + auth

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)
This commit is contained in:
furkanyigit34
2026-04-15 01:53:45 +03:00
parent a087337239
commit 942c8d1244
3 changed files with 13 additions and 2 deletions

View File

@@ -121,12 +121,11 @@ def setup_logging(log_dir: str = None):
- console output (INFO+, colored) - console output (INFO+, colored)
""" """
if log_dir is None: if log_dir is None:
log_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "logs") log_dir = "/tmp/logs"
try: try:
os.makedirs(log_dir, exist_ok=True) os.makedirs(log_dir, exist_ok=True)
except PermissionError: except PermissionError:
# Docker container'da /app/../logs yazılamayabilir, /tmp/logs kullan
log_dir = "/tmp/logs" log_dir = "/tmp/logs"
os.makedirs(log_dir, exist_ok=True) os.makedirs(log_dir, exist_ok=True)

View File

@@ -61,6 +61,9 @@ async def verify_api_key(request: Request, api_key: Optional[str] = Security(API
""" """
if request.url.path in PUBLIC_PATHS: if request.url.path in PUBLIC_PATHS:
return return
if not API_KEY:
# API_KEY env var not set — authentication disabled
return
if not api_key or api_key != API_KEY: if not api_key or api_key != API_KEY:
raise HTTPException( raise HTTPException(
status_code=401, status_code=401,

View File

@@ -168,6 +168,15 @@ class QueueWorker:
scraper = TrendyolSearchScraper(path_model) scraper = TrendyolSearchScraper(path_model)
products = scraper.fetch_all_products() 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 # Enrich with socialProofs from Top Rankings API
if products and cat_id and not any(p.get("socialProofs") for p in products): if products and cat_id and not any(p.get("socialProofs") for p in products):
try: try: