You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.2 KiB
45 lines
1.2 KiB
# ============================================================
|
|
# File: scraper/services/cover_service.py
|
|
# ============================================================
|
|
|
|
import os
|
|
import requests
|
|
from logbus.publisher import log
|
|
|
|
|
|
class CoverService:
|
|
|
|
@staticmethod
|
|
def download_main_cover(cover_url: str, book_id: str) -> str | None:
|
|
"""
|
|
Downloads cover image into: static/covers/<book_id>.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
|