Merge pull request #5270 from Bond-009/imdb

(cherry picked from commit 5ce4df4178)
Signed-off-by: Joshua M. Boniface <joshua@boniface.me>
pull/5640/head
Joshua M. Boniface 4 years ago
parent eb0621a354
commit 4220808b96

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace MediaBrowser.Model.Entities namespace MediaBrowser.Model.Entities
{ {
@ -9,14 +10,26 @@ namespace MediaBrowser.Model.Entities
public static class ProviderIdsExtensions public static class ProviderIdsExtensions
{ {
/// <summary> /// <summary>
/// Determines whether [has provider identifier] [the specified instance]. /// Gets a provider id.
/// </summary> /// </summary>
/// <param name="instance">The instance.</param> /// <param name="instance">The instance.</param>
/// <param name="provider">The provider.</param> /// <param name="name">The name.</param>
/// <returns><c>true</c> if [has provider identifier] [the specified instance]; otherwise, <c>false</c>.</returns> /// <param name="id">The provider id.</param>
public static bool HasProviderId(this IHasProviderIds instance, MetadataProvider provider) /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
public static bool TryGetProviderId(this IHasProviderIds instance, string name, [MaybeNullWhen(false)] out string id)
{
if (instance == null)
{ {
return !string.IsNullOrEmpty(instance.GetProviderId(provider.ToString())); throw new ArgumentNullException(nameof(instance));
}
if (instance.ProviderIds == null)
{
id = null;
return false;
}
return instance.ProviderIds.TryGetValue(name, out id);
} }
/// <summary> /// <summary>
@ -24,10 +37,11 @@ namespace MediaBrowser.Model.Entities
/// </summary> /// </summary>
/// <param name="instance">The instance.</param> /// <param name="instance">The instance.</param>
/// <param name="provider">The provider.</param> /// <param name="provider">The provider.</param>
/// <returns>System.String.</returns> /// <param name="id">The provider id.</param>
public static string? GetProviderId(this IHasProviderIds instance, MetadataProvider provider) /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
public static bool TryGetProviderId(this IHasProviderIds instance, MetadataProvider provider, [MaybeNullWhen(false)] out string id)
{ {
return instance.GetProviderId(provider.ToString()); return instance.TryGetProviderId(provider.ToString(), out id);
} }
/// <summary> /// <summary>
@ -38,18 +52,19 @@ namespace MediaBrowser.Model.Entities
/// <returns>System.String.</returns> /// <returns>System.String.</returns>
public static string? GetProviderId(this IHasProviderIds instance, string name) public static string? GetProviderId(this IHasProviderIds instance, string name)
{ {
if (instance == null) instance.TryGetProviderId(name, out string? id);
{ return id;
throw new ArgumentNullException(nameof(instance));
} }
if (instance.ProviderIds == null) /// <summary>
/// Gets a provider id.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="provider">The provider.</param>
/// <returns>System.String.</returns>
public static string? GetProviderId(this IHasProviderIds instance, MetadataProvider provider)
{ {
return null; return instance.GetProviderId(provider.ToString());
}
instance.ProviderIds.TryGetValue(name, out string? id);
return string.IsNullOrEmpty(id) ? null : id;
} }
/// <summary> /// <summary>
@ -68,13 +83,7 @@ namespace MediaBrowser.Model.Entities
// If it's null remove the key from the dictionary // If it's null remove the key from the dictionary
if (string.IsNullOrEmpty(value)) if (string.IsNullOrEmpty(value))
{ {
if (instance.ProviderIds != null) instance.ProviderIds?.Remove(name);
{
if (instance.ProviderIds.ContainsKey(name))
{
instance.ProviderIds.Remove(name);
}
}
} }
else else
{ {

@ -7,6 +7,8 @@ using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using TMDbLib.Objects.Find;
using TMDbLib.Objects.Search;
using MediaBrowser.Common.Net; using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies; using MediaBrowser.Controller.Entities.Movies;
@ -43,39 +45,14 @@ namespace MediaBrowser.Providers.Plugins.Tmdb.Movies
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(MovieInfo searchInfo, CancellationToken cancellationToken) public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(MovieInfo searchInfo, CancellationToken cancellationToken)
{ {
var tmdbId = Convert.ToInt32(searchInfo.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture); if (searchInfo.TryGetProviderId(MetadataProvider.Tmdb, out var id))
if (tmdbId == 0)
{
var movieResults = await _tmdbClientManager
.SearchMovieAsync(searchInfo.Name, searchInfo.Year ?? 0, searchInfo.MetadataLanguage, cancellationToken)
.ConfigureAwait(false);
var remoteSearchResults = new List<RemoteSearchResult>();
for (var i = 0; i < movieResults.Count; i++)
{
var movieResult = movieResults[i];
var remoteSearchResult = new RemoteSearchResult
{ {
Name = movieResult.Title ?? movieResult.OriginalTitle,
ImageUrl = _tmdbClientManager.GetPosterUrl(movieResult.PosterPath),
Overview = movieResult.Overview,
SearchProviderName = Name
};
var releaseDate = movieResult.ReleaseDate?.ToUniversalTime();
remoteSearchResult.PremiereDate = releaseDate;
remoteSearchResult.ProductionYear = releaseDate?.Year;
remoteSearchResult.SetProviderId(MetadataProvider.Tmdb, movieResult.Id.ToString(CultureInfo.InvariantCulture));
remoteSearchResults.Add(remoteSearchResult);
}
return remoteSearchResults;
}
var movie = await _tmdbClientManager var movie = await _tmdbClientManager
.GetMovieAsync(tmdbId, searchInfo.MetadataLanguage, TmdbUtils.GetImageLanguagesParam(searchInfo.MetadataLanguage), cancellationToken) .GetMovieAsync(
int.Parse(id, CultureInfo.InvariantCulture),
searchInfo.MetadataLanguage,
TmdbUtils.GetImageLanguagesParam(searchInfo.MetadataLanguage),
cancellationToken)
.ConfigureAwait(false); .ConfigureAwait(false);
var remoteResult = new RemoteSearchResult var remoteResult = new RemoteSearchResult
@ -103,6 +80,56 @@ namespace MediaBrowser.Providers.Plugins.Tmdb.Movies
return new[] { remoteResult }; return new[] { remoteResult };
} }
IReadOnlyList<SearchMovie> movieResults;
if (searchInfo.TryGetProviderId(MetadataProvider.Imdb, out id))
{
var result = await _tmdbClientManager.FindByExternalIdAsync(
id,
FindExternalSource.Imdb,
TmdbUtils.GetImageLanguagesParam(searchInfo.MetadataLanguage),
cancellationToken).ConfigureAwait(false);
movieResults = result.MovieResults;
}
else if (searchInfo.TryGetProviderId(MetadataProvider.Tvdb, out id))
{
var result = await _tmdbClientManager.FindByExternalIdAsync(
id,
FindExternalSource.TvDb,
TmdbUtils.GetImageLanguagesParam(searchInfo.MetadataLanguage),
cancellationToken).ConfigureAwait(false);
movieResults = result.MovieResults;
}
else
{
movieResults = await _tmdbClientManager
.SearchMovieAsync(searchInfo.Name, searchInfo.Year ?? 0, searchInfo.MetadataLanguage, cancellationToken)
.ConfigureAwait(false);
}
var len = movieResults.Count;
var remoteSearchResults = new RemoteSearchResult[len];
for (var i = 0; i < len; i++)
{
var movieResult = movieResults[i];
var remoteSearchResult = new RemoteSearchResult
{
Name = movieResult.Title ?? movieResult.OriginalTitle,
ImageUrl = _tmdbClientManager.GetPosterUrl(movieResult.PosterPath),
Overview = movieResult.Overview,
SearchProviderName = Name
};
var releaseDate = movieResult.ReleaseDate?.ToUniversalTime();
remoteSearchResult.PremiereDate = releaseDate;
remoteSearchResult.ProductionYear = releaseDate?.Year;
remoteSearchResult.SetProviderId(MetadataProvider.Tmdb, movieResult.Id.ToString(CultureInfo.InvariantCulture));
remoteSearchResults[i] = remoteSearchResult;
}
return remoteSearchResults;
}
public async Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, CancellationToken cancellationToken) public async Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, CancellationToken cancellationToken)
{ {
var tmdbId = info.GetProviderId(MetadataProvider.Tmdb); var tmdbId = info.GetProviderId(MetadataProvider.Tmdb);

@ -0,0 +1,139 @@
using System;
using System.Collections.Generic;
using MediaBrowser.Model.Entities;
using Xunit;
namespace Jellyfin.Model.Tests.Entities
{
public class ProviderIdsExtensionsTests
{
private const string ExampleImdbId = "tt0113375";
[Fact]
public void GetProviderId_NullInstance_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => ProviderIdsExtensions.GetProviderId(null!, MetadataProvider.Imdb));
}
[Fact]
public void GetProviderId_NullName_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => ProviderIdsExtensionsTestsObject.Empty.GetProviderId(null!));
}
[Fact]
public void GetProviderId_NotFoundName_Null()
{
Assert.Null(ProviderIdsExtensionsTestsObject.Empty.GetProviderId(MetadataProvider.Imdb));
}
[Fact]
public void GetProviderId_NullProvider_Null()
{
var nullProvider = new ProviderIdsExtensionsTestsObject()
{
ProviderIds = null!
};
Assert.Null(nullProvider.GetProviderId(MetadataProvider.Imdb));
}
[Fact]
public void TryGetProviderId_NotFoundName_False()
{
Assert.False(ProviderIdsExtensionsTestsObject.Empty.TryGetProviderId(MetadataProvider.Imdb, out _));
}
[Fact]
public void TryGetProviderId_NullProvider_False()
{
var nullProvider = new ProviderIdsExtensionsTestsObject()
{
ProviderIds = null!
};
Assert.False(nullProvider.TryGetProviderId(MetadataProvider.Imdb, out _));
}
[Fact]
public void GetProviderId_FoundName_Id()
{
var provider = new ProviderIdsExtensionsTestsObject();
provider.ProviderIds[MetadataProvider.Imdb.ToString()] = ExampleImdbId;
Assert.Equal(ExampleImdbId, provider.GetProviderId(MetadataProvider.Imdb));
}
[Fact]
public void TryGetProviderId_FoundName_True()
{
var provider = new ProviderIdsExtensionsTestsObject();
provider.ProviderIds[MetadataProvider.Imdb.ToString()] = ExampleImdbId;
Assert.True(provider.TryGetProviderId(MetadataProvider.Imdb, out var id));
Assert.Equal(ExampleImdbId, id);
}
[Fact]
public void SetProviderId_NullInstance_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => ProviderIdsExtensions.SetProviderId(null!, MetadataProvider.Imdb, ExampleImdbId));
}
[Fact]
public void SetProviderId_Null_Remove()
{
var provider = new ProviderIdsExtensionsTestsObject();
provider.SetProviderId(MetadataProvider.Imdb, null!);
Assert.Empty(provider.ProviderIds);
}
[Fact]
public void SetProviderId_EmptyName_Remove()
{
var provider = new ProviderIdsExtensionsTestsObject();
provider.ProviderIds[MetadataProvider.Imdb.ToString()] = ExampleImdbId;
provider.SetProviderId(MetadataProvider.Imdb, string.Empty);
Assert.Empty(provider.ProviderIds);
}
[Fact]
public void SetProviderId_NonEmptyId_Success()
{
var provider = new ProviderIdsExtensionsTestsObject();
provider.SetProviderId(MetadataProvider.Imdb, ExampleImdbId);
Assert.Single(provider.ProviderIds);
}
[Fact]
public void SetProviderId_NullProvider_Success()
{
var nullProvider = new ProviderIdsExtensionsTestsObject()
{
ProviderIds = null!
};
nullProvider.SetProviderId(MetadataProvider.Imdb, ExampleImdbId);
Assert.Single(nullProvider.ProviderIds);
}
[Fact]
public void SetProviderId_NullProviderAndEmptyName_Success()
{
var nullProvider = new ProviderIdsExtensionsTestsObject()
{
ProviderIds = null!
};
nullProvider.SetProviderId(MetadataProvider.Imdb, string.Empty);
Assert.Null(nullProvider.ProviderIds);
}
private class ProviderIdsExtensionsTestsObject : IHasProviderIds
{
public static readonly ProviderIdsExtensionsTestsObject Empty = new ProviderIdsExtensionsTestsObject();
public Dictionary<string, string> ProviderIds { get; set; } = new Dictionary<string, string>();
}
}
}
Loading…
Cancel
Save