diff --git a/app/routers/router_anime.py b/app/routers/router_anime.py index ff9f0ee..635c8b0 100644 --- a/app/routers/router_anime.py +++ b/app/routers/router_anime.py @@ -77,6 +77,20 @@ def get_download_manager() -> DownloadManager: # ==================== ANIME SEARCH ==================== +def _truncate_at_sentence(text: str, max_len: int = 500) -> str: + """Truncate text at the last sentence boundary before max_len.""" + if not text or len(text) <= max_len: + return text + truncated = text[:max_len] + last_period = truncated.rfind(".") + if last_period > 0: + return text[: last_period + 1] + last_space = truncated.rfind(" ") + if last_space > 0: + return text[:last_space] + "..." + return truncated + "..." + + @router.get("/anime/search") async def search_anime_unified( request: Request, @@ -189,11 +203,15 @@ async def search_anime_unified( if not isinstance(meta, Exception) and meta: results[pid][pos]["metadata"] = meta.model_dump() - # 5. Sort results + # 5. Sort results and truncate synopses for pid in results: results[pid].sort(key=lambda x: -x.get("_relevance_boost", 0)) for item in results[pid]: item.pop("_relevance_boost", None) + meta = item.get("metadata") or {} + syn = meta.get("synopsis") + if syn: + meta["synopsis"] = _truncate_at_sentence(syn, 500) elapsed = time.time() - start_time total_found = sum(len(r) for r in results.values()) @@ -302,6 +320,14 @@ async def search_series_unified( meta.model_dump() if hasattr(meta, "model_dump") else meta ) + # Truncate synopses at sentence boundaries + for pid in results: + for item in results[pid]: + meta = item.get("metadata") or {} + syn = meta.get("synopsis") + if syn: + meta["synopsis"] = _truncate_at_sentence(syn, 500) + elapsed = time.time() - start_time total_found = sum(len(r) for r in results.values()) print(