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.
84 lines
2.8 KiB
84 lines
2.8 KiB
# ============================================================
|
|
# File: scraper/tasks/save_tasks.py (RESTORED AUDIO LOGIC)
|
|
# ============================================================
|
|
|
|
print(">>> [IMPORT] save_tasks.py loaded")
|
|
|
|
from celery import shared_task
|
|
import os
|
|
|
|
from logbus.publisher import log
|
|
from scraper.logger_decorators import logcall
|
|
from scraper.utils.utils import get_save_path
|
|
from scraper.tasks.download_tasks import log_msg
|
|
from scraper.tasks.audio_tasks import generate_audio
|
|
|
|
from db.repository import inc_download_done, inc_download_skipped
|
|
|
|
|
|
@shared_task(bind=True, queue="save", ignore_result=False)
|
|
@logcall
|
|
def save_chapter(self, payload: dict):
|
|
|
|
if not payload:
|
|
log("[SAVE] ERROR: payload is None")
|
|
return {"error": True}
|
|
|
|
book_id = payload["book_id"]
|
|
chapter = payload["chapter"]
|
|
parsed = payload.get("parsed")
|
|
path = payload.get("path")
|
|
skipped = payload.get("skipped")
|
|
|
|
num = chapter["num"]
|
|
title = chapter.get("title") or f"Chapter {num}"
|
|
volume = chapter.get("volume_path")
|
|
volume_name = os.path.basename(volume.rstrip("/"))
|
|
|
|
# ============================================================
|
|
# SKIPPED CASE (restore old behavior)
|
|
# ============================================================
|
|
if skipped or not parsed:
|
|
log_msg(book_id, f"[SAVE] SKIP chapter {num}")
|
|
inc_download_skipped(book_id)
|
|
|
|
# Restore old behavior:
|
|
# If file already exists, STILL trigger audio.
|
|
if path and os.path.exists(path):
|
|
log_msg(book_id, f"[AUDIO] Queueing audio for SKIPPED chapter {num}")
|
|
try:
|
|
generate_audio.delay(book_id, volume_name, num, title, path)
|
|
except Exception as exc:
|
|
log_msg(book_id, f"[AUDIO] ERROR queueing skipped audio: {exc}")
|
|
|
|
return payload
|
|
|
|
# ============================================================
|
|
# NORMAL SAVE CASE
|
|
# ============================================================
|
|
try:
|
|
os.makedirs(volume, exist_ok=True)
|
|
save_path = get_save_path(num, volume)
|
|
|
|
with open(save_path, "w", encoding="utf-8") as f:
|
|
f.write(parsed)
|
|
|
|
log_msg(book_id, f"[SAVE] Saved chapter {num} → {save_path}")
|
|
|
|
inc_download_done(book_id)
|
|
|
|
# Restore old behavior → ALWAYS queue audio
|
|
try:
|
|
generate_audio.delay(book_id, volume_name, num, title, save_path)
|
|
log_msg(book_id, f"[AUDIO] Task queued for chapter {num}")
|
|
except Exception as exc:
|
|
log_msg(book_id, f"[AUDIO] ERROR queueing chapter {num}: {exc}")
|
|
|
|
payload["path"] = save_path
|
|
payload["skipped"] = False
|
|
return payload
|
|
|
|
except Exception as exc:
|
|
log_msg(book_id, f"[SAVE] ERROR saving chapter {num}: {exc}")
|
|
raise
|