Skip to content
This repository has been archived by the owner on Sep 6, 2024. It is now read-only.

Commit

Permalink
episode support work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
fieryash authored and Paillat-dev committed Jul 25, 2024
1 parent 1d11baf commit 248365e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
11 changes: 11 additions & 0 deletions src/exts/tvdb_info/episodes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import discord

from src.tvdb.client import Series
from src.tvdb.generated_models import EpisodeBaseRecord


class EpisodesView(discord.ui.View):
"""Episode View."""

def __init__(self, episodes: EpisodeBaseRecord, series: Series):
super().__init__()
23 changes: 15 additions & 8 deletions src/exts/tvdb_info/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,12 @@ def __init__(self, results: Sequence[Movie | Series]) -> None:
row=1,
)
)
self.add_item(
discord.ui.Button(
style=discord.ButtonStyle.danger,
label="View episodes",
emoji="📺",
disabled=True,
row=1,
)
self.episodes_button = discord.ui.Button(
style=discord.ButtonStyle.danger, label="View episodes", emoji="📺", row=1
)
self.add_item(self.episodes_button)

self.episodes_button.callback = self._episodes_callback
self.index = 0

def _get_embed(self) -> discord.Embed:
Expand Down Expand Up @@ -96,10 +93,20 @@ async def _dropdown_callback(self, interaction: discord.Interaction) -> None:
await self.message.edit(embed=self._get_embed(), view=self)
await interaction.response.defer()

async def _episodes_callback(self, interaction: discord.Interaction) -> None:
if isinstance(self._current_result, Movie):
raise TypeError("Cannot find episodes for a Movie.")
# async with aiohttp.ClientSession() as session:
# episodes = await self._current_result.find_episodes(session=session) # noqa:ERA001

async def send(self, ctx: ApplicationContext) -> None:
"""Send the view."""
await ctx.respond(embed=self._get_embed(), view=self)

@property
def _current_result(self) -> Movie | Series:
return self.results[self.index]


class InfoCog(Cog):
"""Cog to verify the bot is working."""
Expand Down
22 changes: 22 additions & 0 deletions src/tvdb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@

from src.settings import TVDB_API_KEY
from src.tvdb.generated_models import (
EpisodeBaseRecord,
MovieBaseRecord,
MovieExtendedRecord,
MoviesIdExtendedGetResponse,
MoviesIdGetResponse,
SearchGetResponse,
SearchResult,
SeasonBaseRecord,
SeriesBaseRecord,
SeriesExtendedRecord,
SeriesIdEpisodesSeasonTypeGetResponse,
SeriesIdExtendedGetResponse,
SeriesIdGetResponse,
)
Expand Down Expand Up @@ -212,12 +215,31 @@ class Series(_Media):
ResponseType = SeriesIdGetResponse
ExtendedResponseType = SeriesIdExtendedGetResponse

def __init__(self, client: "TvdbClient", data: AnyRecord | SearchResult | None):
super().__init__(client, data)
self.episodes: list[EpisodeBaseRecord] | None = None
self.seasons: list[SeasonBaseRecord] | None = None
if isinstance(self.data, SeriesExtendedRecord):
self.seasons = self.data.seasons

@override
@classmethod
async def supports_meta(cls, meta: FetchMeta) -> bool:
"""Check if the class supports a specific meta."""
return meta in {FetchMeta.TRANSLATIONS, FetchMeta.EPISODES}

async def find_episodes(self, session: aiohttp.ClientSession, season_type: str = "official") -> None:
"""Fetch episodes for the series based on the season type."""
self.client.http_session = session
endpoint = f"series/{self.id}/episodes/{season_type}"
response = await self.client.request("GET", endpoint)

# Assuming 'episodes' field contains the list of episodes
response = SeriesIdEpisodesSeasonTypeGetResponse(**response) # pyright: ignore[reportCallIssue]

if response.data:
self.episodes = response.data.episodes


class TvdbClient:
"""Class to interact with the TVDB API."""
Expand Down

0 comments on commit 248365e

Please sign in to comment.