diff --git a/Kyoo.Common/Models/Resources/Interfaces/IMetadata.cs b/Kyoo.Common/Models/Resources/Interfaces/IMetadata.cs index 1c937715..ef1e6bbc 100644 --- a/Kyoo.Common/Models/Resources/Interfaces/IMetadata.cs +++ b/Kyoo.Common/Models/Resources/Interfaces/IMetadata.cs @@ -1,4 +1,7 @@ +using System; using System.Collections.Generic; +using System.Linq; +using JetBrains.Annotations; using Kyoo.Models.Attributes; namespace Kyoo.Models @@ -14,4 +17,63 @@ namespace Kyoo.Models [EditableRelation] [LoadableRelation] public ICollection ExternalIDs { get; set; } } + + /// + /// A static class containing extensions method for every class. + /// This allow one to use metadata more easily. + /// + public static class MetadataExtension + { + /// + /// Retrieve the internal provider's ID of an item using it's provider slug. + /// + /// + /// This method will never return anything if the are not loaded. + /// + /// An instance of to retrieve the ID from. + /// The slug of the provider + /// The field of the asked provider. + [CanBeNull] + public static string GetID(this IMetadata self, string provider) + { + return self.ExternalIDs?.FirstOrDefault(x => x.Provider.Slug == provider)?.DataID; + } + + /// + /// Retrieve the internal provider's ID of an item using it's provider slug. + /// If the ID could be found, it is converted to the type and true is returned. + /// + /// + /// This method will never succeed if the are not loaded. + /// + /// An instance of to retrieve the ID from. + /// The slug of the provider + /// + /// The field of the asked provider parsed + /// and converted to the type. + /// It is only relevant if this method returns true. + /// + /// The type to convert the to. + /// true if this method succeeded, false otherwise. + public static bool TryGetID(this IMetadata self, string provider, out T id) + { + string dataID = self.ExternalIDs?.FirstOrDefault(x => x.Provider.Slug == provider)?.DataID; + if (dataID == null) + { + id = default; + return false; + } + + try + { + id = (T)Convert.ChangeType(dataID, typeof(T)); + } + catch + { + id = default; + return false; + } + return true; + } + } } \ No newline at end of file diff --git a/Kyoo.Common/Models/Resources/Show.cs b/Kyoo.Common/Models/Resources/Show.cs index a32a0169..90aa98f8 100644 --- a/Kyoo.Common/Models/Resources/Show.cs +++ b/Kyoo.Common/Models/Resources/Show.cs @@ -156,18 +156,6 @@ namespace Kyoo.Models [Link] public ICollection> GenreLinks { get; set; } #endif - /// - /// Retrieve the internal provider's ID of a show using it's provider slug. - /// - /// This method will never return anything if the are not loaded. - /// The slug of the provider - /// The field of the asked provider. - [CanBeNull] - public string GetID(string provider) - { - return ExternalIDs?.FirstOrDefault(x => x.Provider.Slug == provider)?.DataID; - } - /// public void OnMerge(object merged) { diff --git a/Kyoo.TheMovieDb/Convertors.cs b/Kyoo.TheMovieDb/Convertors.cs deleted file mode 100644 index cbd63e10..00000000 --- a/Kyoo.TheMovieDb/Convertors.cs +++ /dev/null @@ -1,319 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using Kyoo.Models; -using TMDbLib.Objects.General; -using TMDbLib.Objects.Movies; -using TMDbLib.Objects.Search; -using TMDbLib.Objects.TvShows; -using Genre = Kyoo.Models.Genre; -using TvCast = TMDbLib.Objects.TvShows.Cast; -using MovieCast = TMDbLib.Objects.Movies.Cast; - -namespace Kyoo.TheMovieDb -{ - public static class Convertors - { - /// - /// Convert a into a . - /// - /// The movie to convert. - /// The provider representing TheMovieDb. - /// The converted movie as a . - public static Show ToShow(this Movie movie, Provider provider) - { - return new() - { - Slug = Utility.ToSlug(movie.Title), - Title = movie.Title, - Aliases = movie.AlternativeTitles.Titles.Select(x => x.Title).ToArray(), - Overview = movie.Overview, - Status = movie.Status == "Released" ? Status.Finished : Status.Planned, - StartAir = movie.ReleaseDate, - EndAir = movie.ReleaseDate, - Images = new Dictionary - { - [Thumbnails.Poster] = movie.PosterPath != null - ? $"https://image.tmdb.org/t/p/original{movie.PosterPath}" - : null, - [Thumbnails.Thumbnail] = movie.BackdropPath != null - ? $"https://image.tmdb.org/t/p/original{movie.BackdropPath}" - : null, - [Thumbnails.Trailer] = movie.Videos?.Results - .Where(x => x.Type is "Trailer" or "Teaser" && x.Site == "YouTube") - .Select(x => "https://www.youtube.com/watch?v=" + x.Key).FirstOrDefault(), - }, - Genres = movie.Genres.Select(x => new Genre(x.Name)).ToArray(), - Studio = !string.IsNullOrEmpty(movie.ProductionCompanies.FirstOrDefault()?.Name) - ? new Studio(movie.ProductionCompanies.First().Name) - : null, - IsMovie = true, - People = movie.Credits.Cast - .Select(x => x.ToPeople(provider)) - .Concat(movie.Credits.Crew.Select(x => x.ToPeople(provider))) - .ToArray(), - ExternalIDs = new [] - { - new MetadataID - { - Provider = provider, - Link = $"https://www.themoviedb.org/movie/{movie.Id}", - DataID = movie.Id.ToString() - } - } - }; - } - - /// - /// Convert a to a . - /// - /// The show to convert. - /// The provider representing TheMovieDb. - /// A converted as a . - public static Show ToShow(this TvShow tv, Provider provider) - { - return new() - { - Slug = Utility.ToSlug(tv.Name), - Title = tv.Name, - Aliases = tv.AlternativeTitles.Results.Select(x => x.Title).ToArray(), - Overview = tv.Overview, - Status = tv.Status == "Ended" ? Status.Finished : Status.Planned, - StartAir = tv.FirstAirDate, - EndAir = tv.LastAirDate, - Images = new Dictionary - { - [Thumbnails.Poster] = tv.PosterPath != null - ? $"https://image.tmdb.org/t/p/original{tv.PosterPath}" - : null, - [Thumbnails.Thumbnail] = tv.BackdropPath != null - ? $"https://image.tmdb.org/t/p/original{tv.BackdropPath}" - : null, - [Thumbnails.Trailer] = tv.Videos?.Results - .Where(x => x.Type is "Trailer" or "Teaser" && x.Site == "YouTube") - .Select(x => "https://www.youtube.com/watch?v=" + x.Key).FirstOrDefault() - }, - Genres = tv.Genres.Select(x => new Genre(x.Name)).ToArray(), - Studio = !string.IsNullOrEmpty(tv.ProductionCompanies.FirstOrDefault()?.Name) - ? new Studio(tv.ProductionCompanies.First().Name) - : null, - IsMovie = true, - People = tv.Credits.Cast - .Select(x => x.ToPeople(provider)) - .Concat(tv.Credits.Crew.Select(x => x.ToPeople(provider))) - .ToArray(), - ExternalIDs = new [] - { - new MetadataID - { - Provider = provider, - Link = $"https://www.themoviedb.org/movie/{tv.Id}", - DataID = tv.Id.ToString() - } - } - }; - } - - - /// - /// Convert a into a . - /// - /// The collection to convert. - /// The provider representing TheMovieDb. - /// The converted collection as a . - public static Collection ToCollection(this SearchCollection collection, Provider provider) - { - return new() - { - Slug = Utility.ToSlug(collection.Name), - Name = collection.Name, - Images = new Dictionary - { - [Thumbnails.Poster] = collection.PosterPath != null - ? $"https://image.tmdb.org/t/p/original{collection.PosterPath}" - : null, - [Thumbnails.Thumbnail] = collection.BackdropPath != null - ? $"https://image.tmdb.org/t/p/original{collection.BackdropPath}" - : null - } - }; - } - - /// - /// Convert a into a . - /// - /// The movie to convert. - /// The provider representing TheMovieDb. - /// The converted movie as a . - public static Show ToShow(this SearchMovie movie, Provider provider) - { - return new() - { - Slug = Utility.ToSlug(movie.Title), - Title = movie.Title, - Overview = movie.Overview, - StartAir = movie.ReleaseDate, - EndAir = movie.ReleaseDate, - Images = new Dictionary - { - [Thumbnails.Poster] = movie.PosterPath != null - ? $"https://image.tmdb.org/t/p/original{movie.PosterPath}" - : null, - [Thumbnails.Thumbnail] = movie.BackdropPath != null - ? $"https://image.tmdb.org/t/p/original{movie.BackdropPath}" - : null, - }, - IsMovie = true, - ExternalIDs = new [] - { - new MetadataID - { - Provider = provider, - Link = $"https://www.themoviedb.org/movie/{movie.Id}", - DataID = movie.Id.ToString() - } - } - }; - } - - /// - /// Convert a to a . - /// - /// The show to convert. - /// The provider representing TheMovieDb. - /// A converted as a . - public static Show ToShow(this SearchTv tv, Provider provider) - { - return new() - { - Slug = Utility.ToSlug(tv.Name), - Title = tv.Name, - Overview = tv.Overview, - StartAir = tv.FirstAirDate, - Images = new Dictionary - { - [Thumbnails.Poster] = tv.PosterPath != null - ? $"https://image.tmdb.org/t/p/original{tv.PosterPath}" - : null, - [Thumbnails.Thumbnail] = tv.BackdropPath != null - ? $"https://image.tmdb.org/t/p/original{tv.BackdropPath}" - : null, - }, - IsMovie = true, - ExternalIDs = new [] - { - new MetadataID - { - Provider = provider, - Link = $"https://www.themoviedb.org/movie/{tv.Id}", - DataID = tv.Id.ToString() - } - } - }; - } - - /// - /// Convert a to a . - /// - /// An internal TheMovieDB cast. - /// The provider that represent TheMovieDB inside Kyoo. - /// A representing the movie cast. - public static PeopleRole ToPeople(this MovieCast cast, Provider provider) - { - return new() - { - People = new People - { - Slug = Utility.ToSlug(cast.Name), - Name = cast.Name, - Images = new Dictionary - { - [Thumbnails.Poster] = cast.ProfilePath != null - ? $"https://image.tmdb.org/t/p/original{cast.ProfilePath}" - : null - }, - ExternalIDs = new[] - { - new MetadataID - { - Provider = provider, - DataID = cast.Id.ToString(), - Link = $"https://www.themoviedb.org/person/{cast.Id}" - } - } - }, - Type = "Actor", - Role = cast.Character - }; - } - - /// - /// Convert a to a . - /// - /// An internal TheMovieDB cast. - /// The provider that represent TheMovieDB inside Kyoo. - /// A representing the movie cast. - public static PeopleRole ToPeople(this TvCast cast, Provider provider) - { - return new() - { - People = new People - { - Slug = Utility.ToSlug(cast.Name), - Name = cast.Name, - Images = new Dictionary - { - [Thumbnails.Poster] = cast.ProfilePath != null - ? $"https://image.tmdb.org/t/p/original{cast.ProfilePath}" - : null - }, - ExternalIDs = new[] - { - new MetadataID - { - Provider = provider, - DataID = cast.Id.ToString(), - Link = $"https://www.themoviedb.org/person/{cast.Id}" - } - } - }, - Type = "Actor", - Role = cast.Character - }; - } - - /// - /// Convert a to a . - /// - /// An internal TheMovieDB crew member. - /// The provider that represent TheMovieDB inside Kyoo. - /// A representing the movie crew. - public static PeopleRole ToPeople(this Crew crew, Provider provider) - { - return new() - { - People = new People - { - Slug = Utility.ToSlug(crew.Name), - Name = crew.Name, - Images = new Dictionary - { - [Thumbnails.Poster] = crew.ProfilePath != null - ? $"https://image.tmdb.org/t/p/original{crew.ProfilePath}" - : null - }, - ExternalIDs = new[] - { - new MetadataID - { - Provider = provider, - DataID = crew.Id.ToString(), - Link = $"https://www.themoviedb.org/person/{crew.Id}" - } - } - }, - Type = crew.Department, - Role = crew.Job - }; - } - } -} \ No newline at end of file diff --git a/Kyoo.TheMovieDb/Convertors/CollectionConvertors.cs b/Kyoo.TheMovieDb/Convertors/CollectionConvertors.cs new file mode 100644 index 00000000..c0108b81 --- /dev/null +++ b/Kyoo.TheMovieDb/Convertors/CollectionConvertors.cs @@ -0,0 +1,79 @@ +using System.Collections.Generic; +using Kyoo.Models; +using TMDbLib.Objects.Search; + +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 collection to convert. + /// The provider representing TheMovieDb. + /// The converted collection as a . + public static Collection ToCollection(this TMDbLib.Objects.Collections.Collection collection, Provider provider) + { + return new() + { + Slug = Utility.ToSlug(collection.Name), + Name = collection.Name, + Overview = collection.Overview, + Images = new Dictionary + { + [Thumbnails.Poster] = collection.PosterPath != null + ? $"https://image.tmdb.org/t/p/original{collection.PosterPath}" + : null, + [Thumbnails.Thumbnail] = collection.BackdropPath != null + ? $"https://image.tmdb.org/t/p/original{collection.BackdropPath}" + : null + }, + ExternalIDs = new [] + { + new MetadataID + { + Provider = provider, + Link = $"https://www.themoviedb.org/collection/{collection.Id}", + DataID = collection.Id.ToString() + } + } + }; + } + + /// + /// Convert a into a . + /// + /// The collection to convert. + /// The provider representing TheMovieDb. + /// The converted collection as a . + public static Collection ToCollection(this SearchCollection collection, Provider provider) + { + return new() + { + Slug = Utility.ToSlug(collection.Name), + Name = collection.Name, + Images = new Dictionary + { + [Thumbnails.Poster] = collection.PosterPath != null + ? $"https://image.tmdb.org/t/p/original{collection.PosterPath}" + : null, + [Thumbnails.Thumbnail] = collection.BackdropPath != null + ? $"https://image.tmdb.org/t/p/original{collection.BackdropPath}" + : null + }, + ExternalIDs = new [] + { + new MetadataID + { + Provider = provider, + Link = $"https://www.themoviedb.org/collection/{collection.Id}", + DataID = collection.Id.ToString() + } + } + }; + } + } +} \ No newline at end of file diff --git a/Kyoo.TheMovieDb/Convertors/MovieConvertors.cs b/Kyoo.TheMovieDb/Convertors/MovieConvertors.cs new file mode 100644 index 00000000..06ea3108 --- /dev/null +++ b/Kyoo.TheMovieDb/Convertors/MovieConvertors.cs @@ -0,0 +1,101 @@ +using System.Collections.Generic; +using System.Linq; +using Kyoo.Models; +using TMDbLib.Objects.Movies; +using TMDbLib.Objects.Search; + +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 movie to convert. + /// The provider representing TheMovieDb. + /// The converted movie as a . + public static Show ToShow(this Movie movie, Provider provider) + { + return new() + { + Slug = Utility.ToSlug(movie.Title), + Title = movie.Title, + Aliases = movie.AlternativeTitles.Titles.Select(x => x.Title).ToArray(), + Overview = movie.Overview, + Status = movie.Status == "Released" ? Status.Finished : Status.Planned, + StartAir = movie.ReleaseDate, + EndAir = movie.ReleaseDate, + Images = new Dictionary + { + [Thumbnails.Poster] = movie.PosterPath != null + ? $"https://image.tmdb.org/t/p/original{movie.PosterPath}" + : null, + [Thumbnails.Thumbnail] = movie.BackdropPath != null + ? $"https://image.tmdb.org/t/p/original{movie.BackdropPath}" + : null, + [Thumbnails.Trailer] = movie.Videos?.Results + .Where(x => x.Type is "Trailer" or "Teaser" && x.Site == "YouTube") + .Select(x => "https://www.youtube.com/watch?v=" + x.Key).FirstOrDefault(), + }, + Genres = movie.Genres.Select(x => new Genre(x.Name)).ToArray(), + Studio = !string.IsNullOrEmpty(movie.ProductionCompanies.FirstOrDefault()?.Name) + ? new Studio(movie.ProductionCompanies.First().Name) + : null, + IsMovie = true, + People = movie.Credits.Cast + .Select(x => x.ToPeople(provider)) + .Concat(movie.Credits.Crew.Select(x => x.ToPeople(provider))) + .ToArray(), + ExternalIDs = new [] + { + new MetadataID + { + Provider = provider, + Link = $"https://www.themoviedb.org/movie/{movie.Id}", + DataID = movie.Id.ToString() + } + } + }; + } + + /// + /// Convert a into a . + /// + /// The movie to convert. + /// The provider representing TheMovieDb. + /// The converted movie as a . + public static Show ToShow(this SearchMovie movie, Provider provider) + { + return new() + { + Slug = Utility.ToSlug(movie.Title), + Title = movie.Title, + Overview = movie.Overview, + StartAir = movie.ReleaseDate, + EndAir = movie.ReleaseDate, + Images = new Dictionary + { + [Thumbnails.Poster] = movie.PosterPath != null + ? $"https://image.tmdb.org/t/p/original{movie.PosterPath}" + : null, + [Thumbnails.Thumbnail] = movie.BackdropPath != null + ? $"https://image.tmdb.org/t/p/original{movie.BackdropPath}" + : null, + }, + IsMovie = true, + ExternalIDs = new [] + { + new MetadataID + { + Provider = provider, + Link = $"https://www.themoviedb.org/movie/{movie.Id}", + DataID = movie.Id.ToString() + } + } + }; + } + } +} \ No newline at end of file diff --git a/Kyoo.TheMovieDb/Convertors/PeopleConvertors.cs b/Kyoo.TheMovieDb/Convertors/PeopleConvertors.cs new file mode 100644 index 00000000..8e1a326d --- /dev/null +++ b/Kyoo.TheMovieDb/Convertors/PeopleConvertors.cs @@ -0,0 +1,119 @@ +using System.Collections.Generic; +using Kyoo.Models; +using TMDbLib.Objects.General; +using TvCast = TMDbLib.Objects.TvShows.Cast; +using MovieCast = TMDbLib.Objects.Movies.Cast; + +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 to a . + /// + /// An internal TheMovieDB cast. + /// The provider that represent TheMovieDB inside Kyoo. + /// A representing the movie cast. + public static PeopleRole ToPeople(this MovieCast cast, Provider provider) + { + return new() + { + People = new People + { + Slug = Utility.ToSlug(cast.Name), + Name = cast.Name, + Images = new Dictionary + { + [Thumbnails.Poster] = cast.ProfilePath != null + ? $"https://image.tmdb.org/t/p/original{cast.ProfilePath}" + : null + }, + ExternalIDs = new[] + { + new MetadataID + { + Provider = provider, + DataID = cast.Id.ToString(), + Link = $"https://www.themoviedb.org/person/{cast.Id}" + } + } + }, + Type = "Actor", + Role = cast.Character + }; + } + + /// + /// Convert a to a . + /// + /// An internal TheMovieDB cast. + /// The provider that represent TheMovieDB inside Kyoo. + /// A representing the movie cast. + public static PeopleRole ToPeople(this TvCast cast, Provider provider) + { + return new() + { + People = new People + { + Slug = Utility.ToSlug(cast.Name), + Name = cast.Name, + Images = new Dictionary + { + [Thumbnails.Poster] = cast.ProfilePath != null + ? $"https://image.tmdb.org/t/p/original{cast.ProfilePath}" + : null + }, + ExternalIDs = new[] + { + new MetadataID + { + Provider = provider, + DataID = cast.Id.ToString(), + Link = $"https://www.themoviedb.org/person/{cast.Id}" + } + } + }, + Type = "Actor", + Role = cast.Character + }; + } + + /// + /// Convert a to a . + /// + /// An internal TheMovieDB crew member. + /// The provider that represent TheMovieDB inside Kyoo. + /// A representing the movie crew. + public static PeopleRole ToPeople(this Crew crew, Provider provider) + { + return new() + { + People = new People + { + Slug = Utility.ToSlug(crew.Name), + Name = crew.Name, + Images = new Dictionary + { + [Thumbnails.Poster] = crew.ProfilePath != null + ? $"https://image.tmdb.org/t/p/original{crew.ProfilePath}" + : null + }, + ExternalIDs = new[] + { + new MetadataID + { + Provider = provider, + DataID = crew.Id.ToString(), + Link = $"https://www.themoviedb.org/person/{crew.Id}" + } + } + }, + Type = crew.Department, + Role = crew.Job + }; + } + } +} \ No newline at end of file diff --git a/Kyoo.TheMovieDb/Convertors/ShowConvertors.cs b/Kyoo.TheMovieDb/Convertors/ShowConvertors.cs new file mode 100644 index 00000000..f379e19b --- /dev/null +++ b/Kyoo.TheMovieDb/Convertors/ShowConvertors.cs @@ -0,0 +1,100 @@ +using System.Collections.Generic; +using System.Linq; +using Kyoo.Models; +using TMDbLib.Objects.Search; +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 to a . + /// + /// The show to convert. + /// The provider representing TheMovieDb. + /// A converted as a . + public static Show ToShow(this TvShow tv, Provider provider) + { + return new() + { + Slug = Utility.ToSlug(tv.Name), + Title = tv.Name, + Aliases = tv.AlternativeTitles.Results.Select(x => x.Title).ToArray(), + Overview = tv.Overview, + Status = tv.Status == "Ended" ? Status.Finished : Status.Planned, + StartAir = tv.FirstAirDate, + EndAir = tv.LastAirDate, + Images = new Dictionary + { + [Thumbnails.Poster] = tv.PosterPath != null + ? $"https://image.tmdb.org/t/p/original{tv.PosterPath}" + : null, + [Thumbnails.Thumbnail] = tv.BackdropPath != null + ? $"https://image.tmdb.org/t/p/original{tv.BackdropPath}" + : null, + [Thumbnails.Trailer] = tv.Videos?.Results + .Where(x => x.Type is "Trailer" or "Teaser" && x.Site == "YouTube") + .Select(x => "https://www.youtube.com/watch?v=" + x.Key).FirstOrDefault() + }, + Genres = tv.Genres.Select(x => new Genre(x.Name)).ToArray(), + Studio = !string.IsNullOrEmpty(tv.ProductionCompanies.FirstOrDefault()?.Name) + ? new Studio(tv.ProductionCompanies.First().Name) + : null, + IsMovie = true, + People = tv.Credits.Cast + .Select(x => x.ToPeople(provider)) + .Concat(tv.Credits.Crew.Select(x => x.ToPeople(provider))) + .ToArray(), + ExternalIDs = new [] + { + new MetadataID + { + Provider = provider, + Link = $"https://www.themoviedb.org/movie/{tv.Id}", + DataID = tv.Id.ToString() + } + } + }; + } + + /// + /// Convert a to a . + /// + /// The show to convert. + /// The provider representing TheMovieDb. + /// A converted as a . + public static Show ToShow(this SearchTv tv, Provider provider) + { + return new() + { + Slug = Utility.ToSlug(tv.Name), + Title = tv.Name, + Overview = tv.Overview, + StartAir = tv.FirstAirDate, + Images = new Dictionary + { + [Thumbnails.Poster] = tv.PosterPath != null + ? $"https://image.tmdb.org/t/p/original{tv.PosterPath}" + : null, + [Thumbnails.Thumbnail] = tv.BackdropPath != null + ? $"https://image.tmdb.org/t/p/original{tv.BackdropPath}" + : null, + }, + IsMovie = true, + ExternalIDs = new [] + { + new MetadataID + { + Provider = provider, + Link = $"https://www.themoviedb.org/movie/{tv.Id}", + DataID = tv.Id.ToString() + } + } + }; + } + } +} \ No newline at end of file diff --git a/Kyoo.TheMovieDb/ProviderTmdb.cs b/Kyoo.TheMovieDb/ProviderTmdb.cs index d5f9f7ef..b2ec68da 100644 --- a/Kyoo.TheMovieDb/ProviderTmdb.cs +++ b/Kyoo.TheMovieDb/ProviderTmdb.cs @@ -52,11 +52,30 @@ namespace Kyoo.TheMovieDb { return item switch { + Collection collection => _GetCollection(collection) as Task, Show show => _GetShow(show) as Task, _ => null }; } - + + /// + /// Get a collection using it's id, if the id is not present in the collection, fallback to a name search. + /// + /// The show to collection for + /// A collection containing metadata from TheMovieDb + private async Task _GetCollection(Collection collection) + { + if (!collection.TryGetID(Provider.Slug, out int id)) + { + Collection found = (await _SearchCollections(collection.Name ?? collection.Slug)).FirstOrDefault(); + if (found?.TryGetID(Provider.Slug, out id) != true) + return found; + } + + TMDbClient client = new(_apiKey.Value.ApiKey); + return (await client.GetCollectionAsync(id)).ToCollection(Provider); + } + /// /// Get a show using it's id, if the id is not present in the show, fallback to a title search. /// @@ -64,8 +83,13 @@ namespace Kyoo.TheMovieDb /// A show containing metadata from TheMovieDb private async Task _GetShow(Show show) { - if (!int.TryParse(show.GetID(Provider.Name), out int id)) - return (await _SearchShows(show.Title ?? show.Slug)).FirstOrDefault(); + if (!show.TryGetID(Provider.Slug, out int id)) + { + Show found = (await _SearchShows(show.Title ?? show.Slug)).FirstOrDefault(); + if (found?.TryGetID(Provider.Slug, out id) != true) + return found; + } + TMDbClient client = new(_apiKey.Value.ApiKey); if (show.IsMovie)