Extract duplicate code, add test

pull/6763/head
Joe Rogers 3 years ago
parent 5529625025
commit 4a5e8b99a0
No known key found for this signature in database
GPG Key ID: 0074AD57B8FDBBB4

@ -67,40 +67,12 @@ namespace MediaBrowser.Providers.Plugins.Tmdb.BoxSets
return Enumerable.Empty<RemoteImageInfo>();
}
var remoteImages = new List<RemoteImageInfo>();
var posters = collection.Images.Posters;
var backdrops = collection.Images.Backdrops;
var remoteImages = new List<RemoteImageInfo>(posters.Count + backdrops.Count);
for (var i = 0; i < collection.Images.Posters.Count; i++)
{
var poster = collection.Images.Posters[i];
remoteImages.Add(new RemoteImageInfo
{
Url = _tmdbClientManager.GetPosterUrl(poster.FilePath),
CommunityRating = poster.VoteAverage,
VoteCount = poster.VoteCount,
Width = poster.Width,
Height = poster.Height,
Language = TmdbUtils.AdjustImageLanguage(poster.Iso_639_1, language),
ProviderName = Name,
Type = ImageType.Primary,
RatingType = RatingType.Score
});
}
for (var i = 0; i < collection.Images.Backdrops.Count; i++)
{
var backdrop = collection.Images.Backdrops[i];
remoteImages.Add(new RemoteImageInfo
{
Url = _tmdbClientManager.GetBackdropUrl(backdrop.FilePath),
CommunityRating = backdrop.VoteAverage,
VoteCount = backdrop.VoteCount,
Width = backdrop.Width,
Height = backdrop.Height,
ProviderName = Name,
Type = ImageType.Backdrop,
RatingType = RatingType.Score
});
}
TmdbUtils.ConvertToRemoteImageInfo(posters, _tmdbClientManager.GetPosterUrl, ImageType.Primary, language, remoteImages);
TmdbUtils.ConvertToRemoteImageInfo(backdrops, _tmdbClientManager.GetBackdropUrl, ImageType.Backdrop, language, remoteImages);
return remoteImages;
}

@ -13,7 +13,6 @@ using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using TMDbLib.Objects.Find;
@ -84,40 +83,12 @@ namespace MediaBrowser.Providers.Plugins.Tmdb.Movies
return Enumerable.Empty<RemoteImageInfo>();
}
var remoteImages = new List<RemoteImageInfo>();
var posters = movie.Images.Posters;
var backdrops = movie.Images.Backdrops;
var remoteImages = new List<RemoteImageInfo>(posters.Count + backdrops.Count);
for (var i = 0; i < movie.Images.Posters.Count; i++)
{
var poster = movie.Images.Posters[i];
remoteImages.Add(new RemoteImageInfo
{
Url = _tmdbClientManager.GetPosterUrl(poster.FilePath),
CommunityRating = poster.VoteAverage,
VoteCount = poster.VoteCount,
Width = poster.Width,
Height = poster.Height,
Language = TmdbUtils.AdjustImageLanguage(poster.Iso_639_1, language),
ProviderName = Name,
Type = ImageType.Primary,
RatingType = RatingType.Score
});
}
for (var i = 0; i < movie.Images.Backdrops.Count; i++)
{
var backdrop = movie.Images.Backdrops[i];
remoteImages.Add(new RemoteImageInfo
{
Url = _tmdbClientManager.GetPosterUrl(backdrop.FilePath),
CommunityRating = backdrop.VoteAverage,
VoteCount = backdrop.VoteCount,
Width = backdrop.Width,
Height = backdrop.Height,
ProviderName = Name,
Type = ImageType.Backdrop,
RatingType = RatingType.Score
});
}
TmdbUtils.ConvertToRemoteImageInfo(posters, _tmdbClientManager.GetPosterUrl, ImageType.Primary, language, remoteImages);
TmdbUtils.ConvertToRemoteImageInfo(backdrops, _tmdbClientManager.GetBackdropUrl, ImageType.Backdrop, language, remoteImages);
return remoteImages;
}

@ -60,21 +60,10 @@ namespace MediaBrowser.Providers.Plugins.Tmdb.People
return Enumerable.Empty<RemoteImageInfo>();
}
var remoteImages = new RemoteImageInfo[personResult.Images.Profiles.Count];
var profiles = personResult.Images.Profiles;
var remoteImages = new List<RemoteImageInfo>(profiles.Count);
for (var i = 0; i < personResult.Images.Profiles.Count; i++)
{
var image = personResult.Images.Profiles[i];
remoteImages[i] = new RemoteImageInfo
{
ProviderName = Name,
Type = ImageType.Primary,
Width = image.Width,
Height = image.Height,
Language = TmdbUtils.AdjustImageLanguage(image.Iso_639_1, language),
Url = _tmdbClientManager.GetProfileUrl(image.FilePath)
};
}
TmdbUtils.ConvertToRemoteImageInfo(profiles, _tmdbClientManager.GetProfileUrl, ImageType.Primary, language, remoteImages);
return remoteImages;
}

@ -12,7 +12,6 @@ using System.Threading.Tasks;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
@ -75,23 +74,9 @@ namespace MediaBrowser.Providers.Plugins.Tmdb.TV
return Enumerable.Empty<RemoteImageInfo>();
}
var remoteImages = new RemoteImageInfo[stills.Count];
for (var i = 0; i < stills.Count; i++)
{
var image = stills[i];
remoteImages[i] = new RemoteImageInfo
{
Url = _tmdbClientManager.GetStillUrl(image.FilePath),
CommunityRating = image.VoteAverage,
VoteCount = image.VoteCount,
Width = image.Width,
Height = image.Height,
Language = TmdbUtils.AdjustImageLanguage(image.Iso_639_1, language),
ProviderName = Name,
Type = ImageType.Primary,
RatingType = RatingType.Score
};
}
var remoteImages = new List<RemoteImageInfo>(stills.Count);
TmdbUtils.ConvertToRemoteImageInfo(stills, _tmdbClientManager.GetStillUrl, ImageType.Primary, language, remoteImages);
return remoteImages;
}

@ -11,7 +11,6 @@ using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
@ -62,23 +61,9 @@ namespace MediaBrowser.Providers.Plugins.Tmdb.TV
return Enumerable.Empty<RemoteImageInfo>();
}
var remoteImages = new RemoteImageInfo[posters.Count];
for (var i = 0; i < posters.Count; i++)
{
var image = posters[i];
remoteImages[i] = new RemoteImageInfo
{
Url = _tmdbClientManager.GetPosterUrl(image.FilePath),
CommunityRating = image.VoteAverage,
VoteCount = image.VoteCount,
Width = image.Width,
Height = image.Height,
Language = TmdbUtils.AdjustImageLanguage(image.Iso_639_1, language),
ProviderName = Name,
Type = ImageType.Primary,
RatingType = RatingType.Score
};
}
var remoteImages = new List<RemoteImageInfo>(posters.Count);
TmdbUtils.ConvertToRemoteImageInfo(posters, _tmdbClientManager.GetPosterUrl, ImageType.Primary, language, remoteImages);
return remoteImages;
}

@ -11,7 +11,6 @@ using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
@ -70,41 +69,10 @@ namespace MediaBrowser.Providers.Plugins.Tmdb.TV
var posters = series.Images.Posters;
var backdrops = series.Images.Backdrops;
var remoteImages = new List<RemoteImageInfo>(posters.Count + backdrops.Count);
var remoteImages = new RemoteImageInfo[posters.Count + backdrops.Count];
for (var i = 0; i < posters.Count; i++)
{
var poster = posters[i];
remoteImages[i] = new RemoteImageInfo
{
Url = _tmdbClientManager.GetPosterUrl(poster.FilePath),
CommunityRating = poster.VoteAverage,
VoteCount = poster.VoteCount,
Width = poster.Width,
Height = poster.Height,
Language = TmdbUtils.AdjustImageLanguage(poster.Iso_639_1, language),
ProviderName = Name,
Type = ImageType.Primary,
RatingType = RatingType.Score
};
}
for (var i = 0; i < backdrops.Count; i++)
{
var backdrop = series.Images.Backdrops[i];
remoteImages[posters.Count + i] = new RemoteImageInfo
{
Url = _tmdbClientManager.GetBackdropUrl(backdrop.FilePath),
CommunityRating = backdrop.VoteAverage,
VoteCount = backdrop.VoteCount,
Width = backdrop.Width,
Height = backdrop.Height,
ProviderName = Name,
Type = ImageType.Backdrop,
RatingType = RatingType.Score
};
}
TmdbUtils.ConvertToRemoteImageInfo(posters, _tmdbClientManager.GetPosterUrl, ImageType.Primary, language, remoteImages);
TmdbUtils.ConvertToRemoteImageInfo(backdrops, _tmdbClientManager.GetBackdropUrl, ImageType.Backdrop, language, remoteImages);
return remoteImages;
}

@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using System;
using System.Collections.Generic;
@ -471,33 +471,39 @@ namespace MediaBrowser.Providers.Plugins.Tmdb
}
/// <summary>
/// Gets the absolute URL of the poster.
/// Handles bad path checking and builds the absolute url.
/// </summary>
/// <param name="posterPath">The relative URL of the poster.</param>
/// <param name="size">The image size to fetch.</param>
/// <param name="path">The relative URL of the image.</param>
/// <returns>The absolute URL.</returns>
public string GetPosterUrl(string posterPath)
private string GetUrl(string size, string path)
{
if (string.IsNullOrEmpty(posterPath))
if (string.IsNullOrEmpty(path))
{
return null;
}
return _tmDbClient.GetImageUrl(_tmDbClient.Config.Images.PosterSizes[^1], posterPath).ToString();
return _tmDbClient.GetImageUrl(size, path).ToString();
}
/// <summary>
/// Gets the absolute URL of the backdrop image.
/// Gets the absolute URL of the poster.
/// </summary>
/// <param name="posterPath">The relative URL of the backdrop image.</param>
/// <param name="posterPath">The relative URL of the poster.</param>
/// <returns>The absolute URL.</returns>
public string GetBackdropUrl(string posterPath)
public string GetPosterUrl(string posterPath)
{
if (string.IsNullOrEmpty(posterPath))
{
return null;
}
return GetUrl(_tmDbClient.Config.Images.PosterSizes[^1], posterPath);
}
return _tmDbClient.GetImageUrl(_tmDbClient.Config.Images.BackdropSizes[^1], posterPath).ToString();
/// <summary>
/// Gets the absolute URL of the backdrop image.
/// </summary>
/// <param name="backdropPath">The relative URL of the backdrop image.</param>
/// <returns>The absolute URL.</returns>
public string GetBackdropUrl(string backdropPath)
{
return GetUrl(_tmDbClient.Config.Images.BackdropSizes[^1], backdropPath);
}
/// <summary>
@ -507,12 +513,7 @@ namespace MediaBrowser.Providers.Plugins.Tmdb
/// <returns>The absolute URL.</returns>
public string GetProfileUrl(string actorProfilePath)
{
if (string.IsNullOrEmpty(actorProfilePath))
{
return null;
}
return _tmDbClient.GetImageUrl(_tmDbClient.Config.Images.ProfileSizes[^1], actorProfilePath).ToString();
return GetUrl(_tmDbClient.Config.Images.ProfileSizes[^1], actorProfilePath);
}
/// <summary>
@ -522,12 +523,7 @@ namespace MediaBrowser.Providers.Plugins.Tmdb
/// <returns>The absolute URL.</returns>
public string GetStillUrl(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
return null;
}
return _tmDbClient.GetImageUrl(_tmDbClient.Config.Images.StillSizes[^1], filePath).ToString();
return GetUrl(_tmDbClient.Config.Images.StillSizes[^1], filePath);
}
private Task EnsureClientConfigAsync()
@ -542,7 +538,7 @@ namespace MediaBrowser.Providers.Plugins.Tmdb
GC.SuppressFinalize(this);
}
/// <summary>
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>

@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using TMDbLib.Objects.General;
namespace MediaBrowser.Providers.Plugins.Tmdb
@ -192,5 +194,33 @@ namespace MediaBrowser.Providers.Plugins.Tmdb
return newRating.Replace("DE-", "FSK-", StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Converts <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
/// </summary>
/// <param name="images">The input images.</param>
/// <param name="imageUrlConverter">The relevant <see cref="TmdbClientManager"/> Get<i>Type</i>Url function to get the absolute url of the image.</param>
/// <param name="type">The type of the image.</param>
/// <param name="requestLanguage">The requested language.</param>
/// <param name="results">The collection to add the remote images into.</param>
public static void ConvertToRemoteImageInfo(List<ImageData> images, Func<string, string> imageUrlConverter, ImageType type, string requestLanguage, List<RemoteImageInfo> results)
{
for (var i = 0; i < images.Count; i++)
{
var image = images[i];
results.Add(new RemoteImageInfo
{
Url = imageUrlConverter(image.FilePath),
CommunityRating = image.VoteAverage,
VoteCount = image.VoteCount,
Width = image.Width,
Height = image.Height,
Language = AdjustImageLanguage(image.Iso_639_1, requestLanguage),
ProviderName = ProviderName,
Type = type,
RatingType = RatingType.Score
});
}
}
}
}

@ -1,4 +1,9 @@
using MediaBrowser.Providers.Plugins.Tmdb;
using System.Collections.Generic;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using MediaBrowser.Providers.Plugins.Tmdb;
using TMDbLib.Objects.General;
using Xunit;
namespace Jellyfin.Providers.Tests.Tmdb
@ -23,5 +28,100 @@ namespace Jellyfin.Providers.Tests.Tmdb
{
Assert.Equal(expected, TmdbUtils.NormalizeLanguage(input!));
}
[Theory]
[InlineData(null, null, null)]
[InlineData(null, "en-US", null)]
[InlineData("en", null, "en")]
[InlineData("en", "en-US", "en-US")]
[InlineData("fr-CA", "fr-BE", "fr-CA")]
[InlineData("fr-CA", "fr", "fr-CA")]
[InlineData("de", "en-US", "de")]
public static void AdjustImageLanguage_Valid_Success(string imageLanguage, string requestLanguage, string expected)
{
Assert.Equal(expected, TmdbUtils.AdjustImageLanguage(imageLanguage, requestLanguage));
}
private static TheoryData<ImageType, ImageData, RemoteImageInfo> GetConvertedImages()
{
return new TheoryData<ImageType, ImageData, RemoteImageInfo>
{
{
ImageType.Primary,
new ()
{
Width = 1,
Height = 1,
AspectRatio = 1,
FilePath = "path 1",
Iso_639_1 = "en",
VoteAverage = 1.2,
VoteCount = 5
},
new ()
{
Type = ImageType.Primary,
Width = 1,
Height = 1,
Url = "converted path 1",
Language = "en-US",
CommunityRating = 1.2,
VoteCount = 5,
RatingType = RatingType.Score,
ProviderName = TmdbUtils.ProviderName
}
},
{
ImageType.Backdrop,
new ()
{
Width = 4,
Height = 2,
AspectRatio = 2,
FilePath = "path 2",
Iso_639_1 = null,
VoteAverage = 0,
VoteCount = 0
},
new ()
{
Type = ImageType.Backdrop,
Width = 4,
Height = 2,
Url = "converted path 2",
Language = null,
CommunityRating = 0,
VoteCount = 0,
RatingType = RatingType.Score,
ProviderName = TmdbUtils.ProviderName
}
}
};
}
[Theory]
[MemberData(nameof(GetConvertedImages))]
public static void ConvertToRemoteImageInfo_ImageList_ConvertsAll(ImageType type, ImageData input, RemoteImageInfo expected)
{
var images = new List<ImageData> { input };
string UrlConverter(string s)
=> "converted " + s;
var language = "en-US";
var results = new List<RemoteImageInfo>(images.Count);
TmdbUtils.ConvertToRemoteImageInfo(images, UrlConverter, type, language, results);
Assert.Single(results);
Assert.Equal(expected.Type, results[0].Type);
Assert.Equal(expected.Width, results[0].Width);
Assert.Equal(expected.Height, results[0].Height);
Assert.Equal(expected.Url, results[0].Url);
Assert.Equal(expected.Language, results[0].Language);
Assert.Equal(expected.CommunityRating, results[0].CommunityRating);
Assert.Equal(expected.VoteCount, results[0].VoteCount);
Assert.Equal(expected.RatingType, results[0].RatingType);
Assert.Equal(expected.ProviderName, results[0].ProviderName);
}
}
}

Loading…
Cancel
Save