Skip to content

Commit

Permalink
feat: added AnimeEpisodes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
THEGOLDENPRO committed Dec 22, 2023
1 parent c05bdc4 commit 34c54f1
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 5 deletions.
3 changes: 2 additions & 1 deletion anmoku/resources/anime/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .anime import *
from .characters import *
from .staff import *
from .staff import *
from .episodes import *
4 changes: 2 additions & 2 deletions anmoku/resources/anime/characters.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

@dataclass
class VoiceActor():
data: VoiceActorData
data: VoiceActorData = field(repr = False)

id: int = field(init = False)
"""The MyAnimeList ID of this voice actor."""
Expand All @@ -46,7 +46,7 @@ def __post_init__(self):

@dataclass
class AnimeCharacter():
data: AnimeCharacterData
data: AnimeCharacterData = field(repr = False)

id: int = field(init = False)
"""The MyAnimeList ID of this character."""
Expand Down
87 changes: 87 additions & 0 deletions anmoku/resources/anime/episodes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from __future__ import annotations
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import List, Optional
from ...typing.jikan import (
AnimeEpisodeData,
JikanPageResponseData
)
from ...typing.jikan.title import TitleData

from datetime import datetime
from dataclasses import dataclass, field

from ..helpers import Title
from ..base import JikanResource

__all__ = (
"AnimeEpisodes",
)

@dataclass
class AnimeEpisode():
data: AnimeEpisodeData = field(repr = False)

id: int = field(init = False)
"""The MyAnimeList ID of this episode."""
title: Title = field(init = False)
"""The title of this episode."""
name: Title = field(init = False)
"""Alias to ``AnimeEpisode.title``."""
url: Optional[str] = field(init = False)
"""The MyAnimeList URL to this episode."""
aired: Optional[datetime] = field(init = False, default = None)
"""The date this episode was aired."""
filler: bool = field(init = False)
"""Is this episode a filler episode or not."""
recap: bool = field(init = False)
"""Is this episode a recap episode or not."""
forum_url: Optional[str] = field(init = False)
"""The URL to the forum discussion of this episode."""

def __post_init__(self):
self.id = self.data["mal_id"]

titles: List[TitleData] = []

japanese_title = self.data["title_japanese"]
if japanese_title is not None:
titles.append(
{"type": "Japanese", "title": japanese_title}
)

romanji_title = self.data["title_romanji"]
if romanji_title is not None:
titles.append(
{"type": "Japanese", "title": romanji_title}
)

titles.append(
{"type": "Default", "title": self.data["title"]}
)

self.title = Title(titles)
self.name = self.title

self.url = self.data["url"]

aired = self.data["aired"]
if aired is not None:
self.aired = datetime.fromisoformat(aired)

self.filler = self.data["filler"]
self.recap = self.data["recap"]
self.forum_url = self.data["forum_url"]

@dataclass
class AnimeEpisodes(JikanResource):
"""Get an anime's episodes."""
_get_endpoint = "/anime/{id}/episodes"

data: JikanPageResponseData[List[AnimeEpisodeData]]

def __iter__(self):

for episode in self.data["data"]:
yield AnimeEpisode(episode)
3 changes: 2 additions & 1 deletion anmoku/typing/jikan/anime/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .anime import *
from .characters import *
from .staff import *
from .staff import *
from .episode import *
19 changes: 19 additions & 0 deletions anmoku/typing/jikan/anime/episode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from __future__ import annotations
from typing import TypedDict, final, Optional

__all__ = (
"AnimeEpisodeData",
)

@final
class AnimeEpisodeData(TypedDict):
mal_id: int
url: Optional[str]
title: str
title_japanese: str
title_romanji: str
# duration: Optional[int] # this just doesn't exist lmao
aired: Optional[str]
filler: bool
recap: bool
forum_url: Optional[str]
10 changes: 9 additions & 1 deletion anmoku/typing/jikan/api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
from typing import TypedDict, TypeVar, Generic

__all__ = ("JikanResponseData",)
__all__ = (
"JikanResponseData",
"JikanPageResponseData"
)

T = TypeVar("T")

class JikanResponseData(TypedDict, Generic[T]):
"""Typical Jikan response data."""
data: T

class JikanPageResponseData(TypedDict, Generic[T]):
"""Jikan response data that contains pagination object."""
data: T

0 comments on commit 34c54f1

Please sign in to comment.