mirror of
https://github.com/nethunterzist/trendyol-analiz
synced 2026-07-01 01:17:04 +00:00
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:
@@ -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)
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user