MovieDB: Implementing studio get/search

This commit is contained in:
Zoe Roux 2021-07-28 20:11:56 +02:00
parent a4933e0a27
commit 75b97c0150
4 changed files with 147 additions and 4 deletions

View File

@ -85,7 +85,7 @@ namespace Kyoo.Models
public int? SeasonNumber { get; set; }
/// <summary>
/// The number of this episode is it's season.
/// The number of this episode in it's season.
/// </summary>
public int? EpisodeNumber { get; set; }

View File

@ -1,6 +1,7 @@
using System.Collections.Generic;
using Kyoo.Models;
using TMDbLib.Objects.General;
using TMDbLib.Objects.People;
using TMDbLib.Objects.Search;
using TvCast = TMDbLib.Objects.TvShows.Cast;
using MovieCast = TMDbLib.Objects.Movies.Cast;
@ -117,6 +118,36 @@ namespace Kyoo.TheMovieDb
};
}
/// <summary>
/// Convert a <see cref="Person"/> to a <see cref="People"/>.
/// </summary>
/// <param name="person">An internal TheMovieDB person.</param>
/// <param name="provider">The provider that represent TheMovieDB inside Kyoo.</param>
/// <returns>A <see cref="People"/> representing the person.</returns>
public static People ToPeople(this Person person, Provider provider)
{
return new()
{
Slug = Utility.ToSlug(person.Name),
Name = person.Name,
Images = new Dictionary<int, string>
{
[Thumbnails.Poster] = person.ProfilePath != null
? $"https://image.tmdb.org/t/p/original{person.ProfilePath}"
: null
},
ExternalIDs = new[]
{
new MetadataID
{
Provider = provider,
DataID = person.Id.ToString(),
Link = $"https://www.themoviedb.org/person/{person.Id}"
}
}
};
}
/// <summary>
/// Convert a <see cref="SearchPerson"/> to a <see cref="People"/>.
/// </summary>

View File

@ -0,0 +1,60 @@
using Kyoo.Models;
using TMDbLib.Objects.Companies;
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="Company"/> into a <see cref="Studio"/>.
/// </summary>
/// <param name="company">The company to convert.</param>
/// <param name="provider">The provider representing TheMovieDb.</param>
/// <returns>The converted company as a <see cref="Studio"/>.</returns>
public static Studio ToStudio(this Company company, Provider provider)
{
return new()
{
Slug = Utility.ToSlug(company.Name),
Name = company.Name,
ExternalIDs = new []
{
new MetadataID
{
Provider = provider,
Link = $"https://www.themoviedb.org/company/{company.Id}",
DataID = company.Id.ToString()
}
}
};
}
/// <summary>
/// Convert a <see cref="SearchCompany"/> into a <see cref="Studio"/>.
/// </summary>
/// <param name="company">The company to convert.</param>
/// <param name="provider">The provider representing TheMovieDb.</param>
/// <returns>The converted company as a <see cref="Studio"/>.</returns>
public static Studio ToStudio(this SearchCompany company, Provider provider)
{
return new()
{
Slug = Utility.ToSlug(company.Name),
Name = company.Name,
ExternalIDs = new[]
{
new MetadataID
{
Provider = provider,
Link = $"https://www.themoviedb.org/company/{company.Id}",
DataID = company.Id.ToString()
}
}
};
}
}
}

View File

@ -63,6 +63,8 @@ namespace Kyoo.TheMovieDb
Show show => _GetShow(show) as Task<T>,
Season season => _GetSeason(season) as Task<T>,
Episode episode => _GetEpisode(episode) as Task<T>,
People person => _GetPerson(person) as Task<T>,
Studio studio => _GetStudio(studio) as Task<T>,
_ => null
};
}
@ -70,7 +72,7 @@ namespace Kyoo.TheMovieDb
/// <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>
/// <param name="collection">The collection to search for</param>
/// <returns>A collection containing metadata from TheMovieDb</returns>
private async Task<Collection> _GetCollection(Collection collection)
{
@ -153,6 +155,42 @@ namespace Kyoo.TheMovieDb
.ToEpisode(id, Provider);
}
/// <summary>
/// Get a person using it's id, if the id is not present in the person, fallback to a name search.
/// </summary>
/// <param name="person">The person to search for</param>
/// <returns>A person containing metadata from TheMovieDb</returns>
private async Task<People> _GetPerson(People person)
{
if (!person.TryGetID(Provider.Slug, out int id))
{
People found = (await _SearchPeople(person.Name ?? person.Slug)).FirstOrDefault();
if (found?.TryGetID(Provider.Slug, out id) != true)
return found;
}
TMDbClient client = new(_apiKey.Value.ApiKey);
return (await client.GetPersonAsync(id)).ToPeople(Provider);
}
/// <summary>
/// Get a studio using it's id, if the id is not present in the studio, fallback to a name search.
/// </summary>
/// <param name="studio">The studio to search for</param>
/// <returns>A studio containing metadata from TheMovieDb</returns>
private async Task<Studio> _GetStudio(Studio studio)
{
if (!studio.TryGetID(Provider.Slug, out int id))
{
Studio found = (await _SearchStudios(studio.Name ?? studio.Slug)).FirstOrDefault();
if (found?.TryGetID(Provider.Slug, out id) != true)
return found;
}
TMDbClient client = new(_apiKey.Value.ApiKey);
return (await client.GetCompanyAsync(id)).ToStudio(Provider);
}
/// <inheritdoc />
public async Task<ICollection<T>> Search<T>(string query)
where T : class, IResource
@ -163,8 +201,8 @@ namespace Kyoo.TheMovieDb
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>)!;
if (typeof(T) == typeof(Studio))
return (await _SearchStudios(query) as ICollection<T>)!;
return ArraySegment<T>.Empty;
}
@ -218,5 +256,19 @@ namespace Kyoo.TheMovieDb
.Select(x => x.ToPeople(Provider))
.ToArray();
}
/// <summary>
/// Search for studios using there name as a query.
/// </summary>
/// <param name="query">The query to search for</param>
/// <returns>A list of studios containing metadata from TheMovieDb</returns>
private async Task<ICollection<Studio>> _SearchStudios(string query)
{
TMDbClient client = new(_apiKey.Value.ApiKey);
return (await client.SearchCompanyAsync(query))
.Results
.Select(x => x.ToStudio(Provider))
.ToArray();
}
}
}