Adding collection search/get

This commit is contained in:
Zoe Roux 2021-07-28 17:38:35 +02:00
parent 5f49986d9d
commit a43210365d
8 changed files with 488 additions and 334 deletions

View File

@ -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<MetadataID> ExternalIDs { get; set; }
}
/// <summary>
/// A static class containing extensions method for every <see cref="IMetadata"/> class.
/// This allow one to use metadata more easily.
/// </summary>
public static class MetadataExtension
{
/// <summary>
/// Retrieve the internal provider's ID of an item using it's provider slug.
/// </summary>
/// <remarks>
/// This method will never return anything if the <see cref="IMetadata.ExternalIDs"/> are not loaded.
/// </remarks>
/// <param name="self">An instance of <see cref="IMetadata"/> to retrieve the ID from.</param>
/// <param name="provider">The slug of the provider</param>
/// <returns>The <see cref="MetadataID.DataID"/> field of the asked provider.</returns>
[CanBeNull]
public static string GetID(this IMetadata self, string provider)
{
return self.ExternalIDs?.FirstOrDefault(x => x.Provider.Slug == provider)?.DataID;
}
/// <summary>
/// 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 <typeparamref name="T"/> type and <c>true</c> is returned.
/// </summary>
/// <remarks>
/// This method will never succeed if the <see cref="IMetadata.ExternalIDs"/> are not loaded.
/// </remarks>
/// <param name="self">An instance of <see cref="IMetadata"/> to retrieve the ID from.</param>
/// <param name="provider">The slug of the provider</param>
/// <param name="id">
/// The <see cref="MetadataID.DataID"/> field of the asked provider parsed
/// and converted to the <typeparamref name="T"/> type.
/// It is only relevant if this method returns <c>true</c>.
/// </param>
/// <typeparam name="T">The type to convert the <see cref="MetadataID.DataID"/> to.</typeparam>
/// <returns><c>true</c> if this method succeeded, <c>false</c> otherwise.</returns>
public static bool TryGetID<T>(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;
}
}
}

View File

@ -156,18 +156,6 @@ namespace Kyoo.Models
[Link] public ICollection<Link<Show, Genre>> GenreLinks { get; set; }
#endif
/// <summary>
/// Retrieve the internal provider's ID of a show using it's provider slug.
/// </summary>
/// <remarks>This method will never return anything if the <see cref="ExternalIDs"/> are not loaded.</remarks>
/// <param name="provider">The slug of the provider</param>
/// <returns>The <see cref="MetadataID.DataID"/> field of the asked provider.</returns>
[CanBeNull]
public string GetID(string provider)
{
return ExternalIDs?.FirstOrDefault(x => x.Provider.Slug == provider)?.DataID;
}
/// <inheritdoc />
public void OnMerge(object merged)
{

View File

@ -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
{
/// <summary>
/// Convert a <see cref="Movie"/> into a <see cref="Show"/>.
/// </summary>
/// <param name="movie">The movie to convert.</param>
/// <param name="provider">The provider representing TheMovieDb.</param>
/// <returns>The converted movie as a <see cref="Show"/>.</returns>
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<int, string>
{
[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()
}
}
};
}
/// <summary>
/// Convert a <see cref="TvShow"/> to a <see cref="Show"/>.
/// </summary>
/// <param name="tv">The show to convert.</param>
/// <param name="provider">The provider representing TheMovieDb.</param>
/// <returns>A converted <see cref="TvShow"/> as a <see cref="Show"/>.</returns>
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<int, string>
{
[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()
}
}
};
}
/// <summary>
/// Convert a <see cref="SearchCollection"/> into a <see cref="Collection"/>.
/// </summary>
/// <param name="collection">The collection to convert.</param>
/// <param name="provider">The provider representing TheMovieDb.</param>
/// <returns>The converted collection as a <see cref="Collection"/>.</returns>
public static Collection ToCollection(this SearchCollection collection, Provider provider)
{
return new()
{
Slug = Utility.ToSlug(collection.Name),
Name = collection.Name,
Images = new Dictionary<int, string>
{
[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
}
};
}
/// <summary>
/// Convert a <see cref="SearchMovie"/> into a <see cref="Show"/>.
/// </summary>
/// <param name="movie">The movie to convert.</param>
/// <param name="provider">The provider representing TheMovieDb.</param>
/// <returns>The converted movie as a <see cref="Show"/>.</returns>
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<int, string>
{
[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()
}
}
};
}
/// <summary>
/// Convert a <see cref="SearchTv"/> to a <see cref="Show"/>.
/// </summary>
/// <param name="tv">The show to convert.</param>
/// <param name="provider">The provider representing TheMovieDb.</param>
/// <returns>A converted <see cref="SearchTv"/> as a <see cref="Show"/>.</returns>
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<int, string>
{
[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()
}
}
};
}
/// <summary>
/// Convert a <see cref="MovieCast"/> to a <see cref="PeopleRole"/>.
/// </summary>
/// <param name="cast">An internal TheMovieDB cast.</param>
/// <param name="provider">The provider that represent TheMovieDB inside Kyoo.</param>
/// <returns>A <see cref="PeopleRole"/> representing the movie cast.</returns>
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<int, string>
{
[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
};
}
/// <summary>
/// Convert a <see cref="TvCast"/> to a <see cref="PeopleRole"/>.
/// </summary>
/// <param name="cast">An internal TheMovieDB cast.</param>
/// <param name="provider">The provider that represent TheMovieDB inside Kyoo.</param>
/// <returns>A <see cref="PeopleRole"/> representing the movie cast.</returns>
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<int, string>
{
[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
};
}
/// <summary>
/// Convert a <see cref="Crew"/> to a <see cref="PeopleRole"/>.
/// </summary>
/// <param name="crew">An internal TheMovieDB crew member.</param>
/// <param name="provider">The provider that represent TheMovieDB inside Kyoo.</param>
/// <returns>A <see cref="PeopleRole"/> representing the movie crew.</returns>
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<int, string>
{
[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
};
}
}
}

View File

@ -0,0 +1,79 @@
using System.Collections.Generic;
using Kyoo.Models;
using TMDbLib.Objects.Search;
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="SearchCollection"/> into a <see cref="Collection"/>.
/// </summary>
/// <param name="collection">The collection to convert.</param>
/// <param name="provider">The provider representing TheMovieDb.</param>
/// <returns>The converted collection as a <see cref="Collection"/>.</returns>
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<int, string>
{
[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()
}
}
};
}
/// <summary>
/// Convert a <see cref="SearchCollection"/> into a <see cref="Collection"/>.
/// </summary>
/// <param name="collection">The collection to convert.</param>
/// <param name="provider">The provider representing TheMovieDb.</param>
/// <returns>The converted collection as a <see cref="Collection"/>.</returns>
public static Collection ToCollection(this SearchCollection collection, Provider provider)
{
return new()
{
Slug = Utility.ToSlug(collection.Name),
Name = collection.Name,
Images = new Dictionary<int, string>
{
[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()
}
}
};
}
}
}

View File

@ -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
{
/// <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="Movie"/> into a <see cref="Show"/>.
/// </summary>
/// <param name="movie">The movie to convert.</param>
/// <param name="provider">The provider representing TheMovieDb.</param>
/// <returns>The converted movie as a <see cref="Show"/>.</returns>
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<int, string>
{
[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()
}
}
};
}
/// <summary>
/// Convert a <see cref="SearchMovie"/> into a <see cref="Show"/>.
/// </summary>
/// <param name="movie">The movie to convert.</param>
/// <param name="provider">The provider representing TheMovieDb.</param>
/// <returns>The converted movie as a <see cref="Show"/>.</returns>
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<int, string>
{
[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()
}
}
};
}
}
}

View File

@ -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
{
/// <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="MovieCast"/> to a <see cref="PeopleRole"/>.
/// </summary>
/// <param name="cast">An internal TheMovieDB cast.</param>
/// <param name="provider">The provider that represent TheMovieDB inside Kyoo.</param>
/// <returns>A <see cref="PeopleRole"/> representing the movie cast.</returns>
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<int, string>
{
[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
};
}
/// <summary>
/// Convert a <see cref="TvCast"/> to a <see cref="PeopleRole"/>.
/// </summary>
/// <param name="cast">An internal TheMovieDB cast.</param>
/// <param name="provider">The provider that represent TheMovieDB inside Kyoo.</param>
/// <returns>A <see cref="PeopleRole"/> representing the movie cast.</returns>
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<int, string>
{
[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
};
}
/// <summary>
/// Convert a <see cref="Crew"/> to a <see cref="PeopleRole"/>.
/// </summary>
/// <param name="crew">An internal TheMovieDB crew member.</param>
/// <param name="provider">The provider that represent TheMovieDB inside Kyoo.</param>
/// <returns>A <see cref="PeopleRole"/> representing the movie crew.</returns>
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<int, string>
{
[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
};
}
}
}

View File

@ -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
{
/// <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="TvShow"/> to a <see cref="Show"/>.
/// </summary>
/// <param name="tv">The show to convert.</param>
/// <param name="provider">The provider representing TheMovieDb.</param>
/// <returns>A converted <see cref="TvShow"/> as a <see cref="Show"/>.</returns>
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<int, string>
{
[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()
}
}
};
}
/// <summary>
/// Convert a <see cref="SearchTv"/> to a <see cref="Show"/>.
/// </summary>
/// <param name="tv">The show to convert.</param>
/// <param name="provider">The provider representing TheMovieDb.</param>
/// <returns>A converted <see cref="SearchTv"/> as a <see cref="Show"/>.</returns>
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<int, string>
{
[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()
}
}
};
}
}
}

View File

@ -52,11 +52,30 @@ namespace Kyoo.TheMovieDb
{
return item switch
{
Collection collection => _GetCollection(collection) as Task<T>,
Show show => _GetShow(show) as Task<T>,
_ => null
};
}
/// <summary>
/// Get a collection using it's id, if the id is not present in the collection, fallback to a name search.
/// </summary>
/// <param name="collection">The show to collection for</param>
/// <returns>A collection containing metadata from TheMovieDb</returns>
private async Task<Collection> _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);
}
/// <summary>
/// Get a show using it's id, if the id is not present in the show, fallback to a title search.
/// </summary>
@ -64,8 +83,13 @@ namespace Kyoo.TheMovieDb
/// <returns>A show containing metadata from TheMovieDb</returns>
private async Task<Show> _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)