feat: improve preview logic

This commit is contained in:
Benexl
2025-07-24 16:11:24 +03:00
parent d299355d90
commit f76350bc5b
4 changed files with 39 additions and 19 deletions

View File

@@ -1,6 +1,7 @@
import concurrent.futures
import logging
import os
import re
from hashlib import sha256
from pathlib import Path
from threading import Thread
@@ -34,6 +35,9 @@ TEMPLATE_EPISODE_INFO_SCRIPT = Path(
).read_text(encoding="utf-8")
EPISODE_PATTERN = re.compile(r"^Episode\s+(\d+)\s-\s.*")
def get_anime_preview(
items: List[MediaItem], titles: List[str], config: AppConfig
) -> str:
@@ -206,7 +210,7 @@ def _episode_cache_worker(
episodes: List[str], media_item: MediaItem, config: AppConfig
):
"""Background task that fetches and saves episode preview data."""
streaming_episodes = {ep.title: ep for ep in media_item.streaming_episodes}
streaming_episodes = media_item.streaming_episodes
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
for episode_str in episodes:
@@ -217,13 +221,9 @@ def _episode_cache_worker(
# Find matching streaming episode
title = None
thumbnail = None
for title, ep in streaming_episodes.items():
if f"Episode {episode_str} -" in title or title.endswith(
f" {episode_str}"
):
title = title
thumbnail = ep.thumbnail
break
if ep := streaming_episodes.get(episode_str):
title = ep.title
thumbnail = ep.thumbnail
# Fallback if no streaming episode found
if not title:

View File

@@ -1,7 +1,25 @@
import re
from typing import Dict, List, Optional, Union
# def _extract_episode_number(episode_string: str) -> int | None:
# """
# Extracts the episode number from a string formatted as 'Episode {number} - desc'.
# Args:
# episode_string: The input string, e.g., "Episode 123 - The Grand Finale".
# Returns:
# The extracted episode number as an integer, or None if the format
# does not match.
# """
# match = EPISODE_PATTERN.search(episode_string)
# if match:
# episode_number_str = match.group(1)
# return int(episode_number_str)
# else:
# return None
def extract_episode_number(title: str) -> Optional[float]:
"""
Extracts the episode number (supports floats) from a title like:

View File

@@ -1,8 +1,12 @@
import logging
from datetime import datetime
from typing import List, Optional
from typing import Dict, List, Optional
from ....core.utils.formatting import renumber_titles, strip_original_episode_prefix
from ....core.utils.formatting import (
extract_episode_number,
renumber_titles,
strip_original_episode_prefix,
)
from ..types import (
AiringSchedule,
MediaFormat,
@@ -177,14 +181,14 @@ def _to_generic_tags(anilist_tags: list[AnilistMediaTag]) -> List[MediaTagItem]:
def _to_generic_streaming_episodes(
anilist_episodes: list[AnilistStreamingEpisode],
) -> List[StreamingEpisode]:
) -> Dict[str, StreamingEpisode]:
"""Maps a list of AniList streaming episodes to generic StreamingEpisode objects,
renumbering them fresh if they contain episode numbers."""
titles = [ep["title"] for ep in anilist_episodes if "title" in ep and ep["title"]]
renumber_map = renumber_titles(titles)
result = []
result = {}
for ep in anilist_episodes:
title = ep.get("title")
if not title:
@@ -197,11 +201,9 @@ def _to_generic_streaming_episodes(
else title
)
result.append(
StreamingEpisode(
title=display_title,
thumbnail=ep.get("thumbnail"),
)
result[str(renumbered_ep)] = StreamingEpisode(
title=display_title,
thumbnail=ep.get("thumbnail"),
)
return result

View File

@@ -2,7 +2,7 @@ from __future__ import annotations
from datetime import datetime
from enum import Enum
from typing import List, Optional
from typing import Dict, List, Optional
from pydantic import BaseModel, ConfigDict, Field
@@ -167,7 +167,7 @@ class MediaItem(BaseMediaApiModel):
next_airing: Optional[AiringSchedule] = None
# streaming episodes
streaming_episodes: List[StreamingEpisode] = Field(default_factory=list)
streaming_episodes: Dict[str, StreamingEpisode] = Field(default_factory=dict)
# user related
user_status: Optional[UserListItem] = None