# ============================================================ # File: scraper/services/cover_service.py # ============================================================ import os import requests from logbus.publisher import log from typing import Optional class CoverService: @staticmethod def download_main_cover(cover_url: str, book_id: str) -> Optional[str]: """ Downloads cover image into: static/covers/.jpg. Returns local path or None. """ if not cover_url: log(f"[COVER] No cover URL for book={book_id}") return None static_dir = os.path.join("static", "covers") os.makedirs(static_dir, exist_ok=True) dst_path = os.path.join(static_dir, f"{book_id}.jpg") try: log(f"[COVER] Downloading: {cover_url}") resp = requests.get( cover_url, timeout=10, headers={"User-Agent": "Mozilla/5.0"} ) resp.raise_for_status() with open(dst_path, "wb") as f: f.write(resp.content) log(f"[COVER] Stored: {dst_path}") return dst_path except Exception as e: log(f"[COVER] FAILED ({cover_url}) → {e}") return None