MovieDB: Implement episode get

This commit is contained in:
Zoe Roux 2021-07-28 19:30:28 +02:00
parent a43210365d
commit 8152787f47
4 changed files with 150 additions and 47 deletions

View File

@ -0,0 +1,47 @@
using System.Collections.Generic;
using Kyoo.Models;
using TMDbLib.Objects.TvShows;
namespace Kyoo.TheMovieDb
{
/// <summary>
/// A class containing extensions methods to convert from TMDB's types to Kyoo's types.
/// </summary>
public static partial class Convertors
{
/// <summary>
/// Convert a <see cref="TvEpisode"/> into a <see cref="Episode"/>.
/// </summary>
/// <param name="episode">The episode to convert.</param>
/// <param name="showID">The ID of the show inside TheMovieDb.</param>
/// <param name="provider">The provider representing TheMovieDb.</param>
/// <returns>The converted episode as a <see cref="Episode"/>.</returns>
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<int, string>
{
[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()
}
}
};
}
}
}

View File

@ -0,0 +1,45 @@
using System.Collections.Generic;
using Kyoo.Models;
using TMDbLib.Objects.TvShows;
namespace Kyoo.TheMovieDb
{
/// <summary>
/// A class containing extensions methods to convert from TMDB's types to Kyoo's types.
/// </summary>
public static partial class Convertors
{
/// <summary>
/// Convert a <see cref="TvSeason"/> into a <see cref="Season"/>.
/// </summary>
/// <param name="season">The season to convert.</param>
/// <param name="showID">The ID of the show inside TheMovieDb.</param>
/// <param name="provider">The provider representing TheMovieDb.</param>
/// <returns>The converted season as a <see cref="Season"/>.</returns>
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<int, string>
{
[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()
}
}
};
}
}
}

View File

@ -19,6 +19,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="5.0.0" />
<PackageReference Include="TMDbLib" Version="1.8.1" />

View File

@ -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,6 +23,10 @@ namespace Kyoo.TheMovieDb
/// The API key used to authenticate with TheMovieDb API.
/// </summary>
private readonly IOptions<TheMovieDbOptions> _apiKey;
/// <summary>
/// The logger to use in ase of issue.
/// </summary>
private readonly ILogger<TheMovieDbProvider> _logger;
/// <inheritdoc />
public Provider Provider => new()
@ -40,9 +45,11 @@ namespace Kyoo.TheMovieDb
/// Create a new <see cref="TheMovieDbProvider"/> using the given api key.
/// </summary>
/// <param name="apiKey">The api key</param>
public TheMovieDbProvider(IOptions<TheMovieDbOptions> apiKey)
/// <param name="logger">The logger to use in case of issue.</param>
public TheMovieDbProvider(IOptions<TheMovieDbOptions> apiKey, ILogger<TheMovieDbProvider> logger)
{
_apiKey = apiKey;
_logger = logger;
}
@ -54,6 +61,8 @@ namespace Kyoo.TheMovieDb
{
Collection collection => _GetCollection(collection) as Task<T>,
Show show => _GetShow(show) as Task<T>,
Season season => _GetSeason(season) as Task<T>,
Episode episode => _GetEpisode(episode) as Task<T>,
_ => null
};
}
@ -104,6 +113,45 @@ namespace Kyoo.TheMovieDb
?.ToShow(Provider);
}
/// <summary>
/// Get a season using it's show and it's season number.
/// </summary>
/// <param name="season">The season to retrieve metadata for.</param>
/// <returns>A season containing metadata from TheMovieDb</returns>
private async Task<Season> _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);
}
/// <summary>
/// Get an episode using it's show, it's season number and it's episode number.
/// Absolute numbering is not supported.
/// </summary>
/// <param name="episode">The episode to retrieve metadata for.</param>
/// <returns>An episode containing metadata from TheMovieDb</returns>
private async Task<Episode> _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);
}
/// <inheritdoc />
public async Task<ICollection<T>> Search<T>(string query)
@ -113,6 +161,10 @@ namespace Kyoo.TheMovieDb
return (await _SearchCollections(query) as ICollection<T>)!;
if (typeof(T) == typeof(Show))
return (await _SearchShows(query) as ICollection<T>)!;
// if (typeof(T) == typeof(People))
// return (await _SearchPeople(query) as ICollection<T>)!;
// if (typeof(T) == typeof(Studio))
// return (await _SearchStudios(query) as ICollection<T>)!;
return ArraySegment<T>.Empty;
}
@ -120,7 +172,7 @@ namespace Kyoo.TheMovieDb
/// Search for a collection using it's name as a query.
/// </summary>
/// <param name="query">The query to search for</param>
/// <returns>A collection containing metadata from TheMovieDb</returns>
/// <returns>A list of collections containing metadata from TheMovieDb</returns>
private async Task<ICollection<Collection>> _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.
/// </summary>
/// <param name="query">The query to search for</param>
/// <returns>A show containing metadata from TheMovieDb</returns>
/// <returns>A list of shows containing metadata from TheMovieDb</returns>
private async Task<ICollection<Show>> _SearchShows(string query)
{
TMDbClient client = new(_apiKey.Value.ApiKey);
@ -152,47 +204,5 @@ namespace Kyoo.TheMovieDb
.Where(x => x != null)
.ToArray();
}
// public async Task<Season> GetSeason(Show show, int seasonNumber)
// {
// string id = show?.GetID(Provider.Name);
// if (id == null)
// return await Task.FromResult<Season>(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<Episode> GetEpisode(Show show, int seasonNumber, int episodeNumber, int absoluteNumber)
// {
// if (seasonNumber == -1 || episodeNumber == -1)
// return await Task.FromResult<Episode>(null);
//
// string id = show?.GetID(Provider.Name);
// if (id == null)
// return await Task.FromResult<Episode>(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}")
// });
// }
}
}