nyanimedb/modules/anime_etl/jikan_studio_enricher.py
2025-12-05 22:59:33 +03:00

38 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from __future__ import annotations
from typing import Optional
from models import Studio
from sources.jikan_async_client import search_producer, fetch_producer_full
async def enrich_studio_with_jikan_desc(studio: Studio) -> Studio:
"""
Если у студии нет description — ищем её в Jikan и подтягиваем about.
Ничего не ломает:
- если не нашли / нет about — возвращаем студию как есть
- poster/id не трогаем
"""
if not studio or studio.description:
return studio
matches = await search_producer(studio.name, limit=1)
if not matches:
return studio
mal_id = matches[0].get("mal_id")
if not isinstance(mal_id, int):
return studio
full = await fetch_producer_full(mal_id)
if not full:
return studio
about = full.get("about")
if not isinstance(about, str) or not about.strip():
return studio
# лёгкая нормализация: убираем лишние переносы/пробелы
studio.description = " ".join(about.split())
return studio