diff --git a/Kyoo.TheMovieDb/Convertors/EpisodeConvertors.cs b/Kyoo.TheMovieDb/Convertors/EpisodeConvertors.cs new file mode 100644 index 00000000..97512aec --- /dev/null +++ b/Kyoo.TheMovieDb/Convertors/EpisodeConvertors.cs @@ -0,0 +1,47 @@ +using System.Collections.Generic; +using Kyoo.Models; +using TMDbLib.Objects.TvShows; + +namespace Kyoo.TheMovieDb +{ + /// + /// A class containing extensions methods to convert from TMDB's types to Kyoo's types. + /// + public static partial class Convertors + { + /// + /// Convert a into a . + /// + /// The episode to convert. + /// The ID of the show inside TheMovieDb. + /// The provider representing TheMovieDb. + /// The converted episode as a . + public static Episode ToEpisode(this TvEpisode episode, int showID, Provider provider) + { + return new() + { + SeasonNumber = episode.SeasonNumber, + EpisodeNumber = episode.EpisodeNumber, + Title = episode.Name, + Overview = episode.Overview, + ReleaseDate = episode.AirDate, + Images = new Dictionary + { + [Thumbnails.Thumbnail] = episode.StillPath != null + ? $"https://image.tmdb.org/t/p/original{episode.StillPath}" + : null + }, + ExternalIDs = new [] + { + new MetadataID + { + Provider = provider, + Link = $"https://www.themoviedb.org/tv/{showID}" + + $"/season/{episode.SeasonNumber}/episode/{episode.EpisodeNumber}", + DataID = episode.Id.ToString() + } + } + }; + } + } +} \ No newline at end of file diff --git a/Kyoo.TheMovieDb/Convertors/SeasonConvertors.cs b/Kyoo.TheMovieDb/Convertors/SeasonConvertors.cs new file mode 100644 index 00000000..f7912f9f --- /dev/null +++ b/Kyoo.TheMovieDb/Convertors/SeasonConvertors.cs @@ -0,0 +1,45 @@ +using System.Collections.Generic; +using Kyoo.Models; +using TMDbLib.Objects.TvShows; + +namespace Kyoo.TheMovieDb +{ + /// + /// A class containing extensions methods to convert from TMDB's types to Kyoo's types. + /// + public static partial class Convertors + { + /// + /// Convert a into a . + /// + /// The season to convert. + /// The ID of the show inside TheMovieDb. + /// The provider representing TheMovieDb. + /// The converted season as a . + public static Season ToSeason(this TvSeason season, int showID, Provider provider) + { + return new() + { + SeasonNumber = season.SeasonNumber, + Title = season.Name, + Overview = season.Overview, + StartDate = season.AirDate, + Images = new Dictionary + { + [Thumbnails.Poster] = season.PosterPath != null + ? $"https://image.tmdb.org/t/p/original{season.PosterPath}" + : null + }, + ExternalIDs = new [] + { + new MetadataID + { + Provider = provider, + Link = $"https://www.themoviedb.org/tv/{showID}/season/{season.SeasonNumber}", + DataID = season.Id?.ToString() + } + } + }; + } + } +} \ No newline at end of file diff --git a/Kyoo.TheMovieDb/Kyoo.TheMovieDb.csproj b/Kyoo.TheMovieDb/Kyoo.TheMovieDb.csproj index 3f50e6b3..99a49716 100644 --- a/Kyoo.TheMovieDb/Kyoo.TheMovieDb.csproj +++ b/Kyoo.TheMovieDb/Kyoo.TheMovieDb.csproj @@ -19,6 +19,7 @@ + diff --git a/Kyoo.TheMovieDb/ProviderTmdb.cs b/Kyoo.TheMovieDb/ProviderTmdb.cs index b2ec68da..deab03ab 100644 --- a/Kyoo.TheMovieDb/ProviderTmdb.cs +++ b/Kyoo.TheMovieDb/ProviderTmdb.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Kyoo.Controllers; using Kyoo.Models; using Kyoo.TheMovieDb.Models; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using TMDbLib.Client; using TMDbLib.Objects.Movies; @@ -22,7 +23,11 @@ namespace Kyoo.TheMovieDb /// The API key used to authenticate with TheMovieDb API. /// private readonly IOptions _apiKey; - + /// + /// The logger to use in ase of issue. + /// + private readonly ILogger _logger; + /// public Provider Provider => new() { @@ -35,14 +40,16 @@ namespace Kyoo.TheMovieDb "blue_short-8e7b30f73a4020692ccca9c88bafe5dcb6f8a62a4c6bc55cd9ba82bb2cd95f6c.svg" } }; - + /// /// Create a new using the given api key. /// /// The api key - public TheMovieDbProvider(IOptions apiKey) + /// The logger to use in case of issue. + public TheMovieDbProvider(IOptions apiKey, ILogger logger) { _apiKey = apiKey; + _logger = logger; } @@ -54,6 +61,8 @@ namespace Kyoo.TheMovieDb { Collection collection => _GetCollection(collection) as Task, Show show => _GetShow(show) as Task, + Season season => _GetSeason(season) as Task, + Episode episode => _GetEpisode(episode) as Task, _ => null }; } @@ -104,6 +113,45 @@ namespace Kyoo.TheMovieDb ?.ToShow(Provider); } + /// + /// Get a season using it's show and it's season number. + /// + /// The season to retrieve metadata for. + /// A season containing metadata from TheMovieDb + private async Task _GetSeason(Season season) + { + if (season.Show == null || !season.Show.TryGetID(Provider.Slug, out int id)) + { + _logger.LogWarning("Metadata for a season was requested but it's show is not loaded. " + + "This is unsupported"); + return null; + } + TMDbClient client = new(_apiKey.Value.ApiKey); + return (await client.GetTvSeasonAsync(id, season.SeasonNumber)) + .ToSeason(id, Provider); + } + + /// + /// Get an episode using it's show, it's season number and it's episode number. + /// Absolute numbering is not supported. + /// + /// The episode to retrieve metadata for. + /// An episode containing metadata from TheMovieDb + private async Task _GetEpisode(Episode episode) + { + if (episode.Show == null || !episode.Show.TryGetID(Provider.Slug, out int id)) + { + _logger.LogWarning("Metadata for a season was requested but it's show is not loaded. " + + "This is unsupported"); + return null; + } + if (episode.SeasonNumber == null || episode.EpisodeNumber == null) + return null; + + TMDbClient client = new(_apiKey.Value.ApiKey); + return (await client.GetTvEpisodeAsync(id, episode.SeasonNumber.Value, episode.EpisodeNumber.Value)) + .ToEpisode(id, Provider); + } /// public async Task> Search(string query) @@ -113,6 +161,10 @@ namespace Kyoo.TheMovieDb return (await _SearchCollections(query) as ICollection)!; if (typeof(T) == typeof(Show)) return (await _SearchShows(query) as ICollection)!; + // if (typeof(T) == typeof(People)) + // return (await _SearchPeople(query) as ICollection)!; + // if (typeof(T) == typeof(Studio)) + // return (await _SearchStudios(query) as ICollection)!; return ArraySegment.Empty; } @@ -120,7 +172,7 @@ namespace Kyoo.TheMovieDb /// Search for a collection using it's name as a query. /// /// The query to search for - /// A collection containing metadata from TheMovieDb + /// A list of collections containing metadata from TheMovieDb private async Task> _SearchCollections(string query) { TMDbClient client = new(_apiKey.Value.ApiKey); @@ -134,7 +186,7 @@ namespace Kyoo.TheMovieDb /// Search for a show using it's name as a query. /// /// The query to search for - /// A show containing metadata from TheMovieDb + /// A list of shows containing metadata from TheMovieDb private async Task> _SearchShows(string query) { TMDbClient client = new(_apiKey.Value.ApiKey); @@ -152,47 +204,5 @@ namespace Kyoo.TheMovieDb .Where(x => x != null) .ToArray(); } - - // public async Task GetSeason(Show show, int seasonNumber) - // { - // string id = show?.GetID(Provider.Name); - // if (id == null) - // return await Task.FromResult(null); - // TMDbClient client = new TMDbClient(APIKey); - // TvSeason season = await client.GetTvSeasonAsync(int.Parse(id), seasonNumber); - // if (season == null) - // return null; - // return new Season(show.ID, - // seasonNumber, - // season.Name, - // season.Overview, - // season.AirDate?.Year, - // season.PosterPath != null ? "https://image.tmdb.org/t/p/original" + season.PosterPath : null, - // new[] {new MetadataID(Provider, $"{season.Id}", $"https://www.themoviedb.org/tv/{id}/season/{season.SeasonNumber}")}); - // } - // - // public async Task GetEpisode(Show show, int seasonNumber, int episodeNumber, int absoluteNumber) - // { - // if (seasonNumber == -1 || episodeNumber == -1) - // return await Task.FromResult(null); - // - // string id = show?.GetID(Provider.Name); - // if (id == null) - // return await Task.FromResult(null); - // TMDbClient client = new(APIKey); - // TvEpisode episode = await client.GetTvEpisodeAsync(int.Parse(id), seasonNumber, episodeNumber); - // if (episode == null) - // return null; - // return new Episode(seasonNumber, episodeNumber, absoluteNumber, - // episode.Name, - // episode.Overview, - // episode.AirDate, - // 0, - // episode.StillPath != null ? "https://image.tmdb.org/t/p/original" + episode.StillPath : null, - // new [] - // { - // new MetadataID(Provider, $"{episode.Id}", $"https://www.themoviedb.org/tv/{id}/season/{episode.SeasonNumber}/episode/{episode.EpisodeNumber}") - // }); - // } } } \ No newline at end of file