From eb12754fc5b74d1bed7236c7f454903232a9b144 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 18 Dec 2019 17:18:55 +0100 Subject: [PATCH 1/5] Add Tmdb as a Provider for Season Images Changes to be committed: modified: CONTRIBUTORS.md new file: MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs modified: MediaBrowser.Providers/Tmdb/TV/TmdbSeasonProvider.cs --- CONTRIBUTORS.md | 1 + .../Tmdb/TV/TmdbSeasonImageProvider.cs | 152 ++++++++++++++++++ .../Tmdb/TV/TmdbSeasonProvider.cs | 3 + 3 files changed, 156 insertions(+) create mode 100644 MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index d69e6330b..458944778 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -31,6 +31,7 @@ - [fhriley](https://github.com/fhriley) - [nevado](https://github.com/nevado) - [mark-monteiro](https://github.com/mark-monteiro) + - [ullmie02](https://github.com/ullmie02) # Emby Contributors diff --git a/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs b/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs new file mode 100644 index 000000000..3a0062ef7 --- /dev/null +++ b/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs @@ -0,0 +1,152 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +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.Globalization; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Providers; +using MediaBrowser.Model.Serialization; +using MediaBrowser.Providers.Tmdb.Models.General; +using MediaBrowser.Providers.Tmdb.Movies; + +namespace MediaBrowser.Providers.Tmdb.TV +{ + public class TmdbSeasonImageProvider : IRemoteImageProvider, IHasOrder + { + private readonly IJsonSerializer _jsonSerializer; + private readonly IHttpClient _httpClient; + private readonly IFileSystem _fileSystem; + private readonly ILocalizationManager _localization; + + public TmdbSeasonImageProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, IFileSystem fileSystem, ILocalizationManager localization) + { + _jsonSerializer = jsonSerializer; + _httpClient = httpClient; + _fileSystem = fileSystem; + _localization = localization; + } + + public int Order => 1; + + public string Name => ProviderName; + + public static string ProviderName => TmdbUtils.ProviderName; + + public Task GetImageResponse(string url, CancellationToken cancellationToken) + { + return _httpClient.GetResponse(new HttpRequestOptions + { + CancellationToken = cancellationToken, + Url = url + }); + } + + public async Task> GetImages(BaseItem item, CancellationToken cancellationToken) + { + var season = (Season)item; + var series = season.Series; + + var seriesId = series != null ? series.GetProviderId(MetadataProviders.Tmdb) : null; + + var list = new List(); + + if (string.IsNullOrEmpty(seriesId)) + { + return list; + } + + var seasonNumber = season.IndexNumber; + + if (!seasonNumber.HasValue) + { + return list; + } + + var language = item.GetPreferredMetadataLanguage(); + + var results = await FetchImages(season, seriesId, language, _jsonSerializer, cancellationToken).ConfigureAwait(false); + + var tmdbSettings = await TmdbMovieProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false); + + var tmdbImageUrl = tmdbSettings.images.GetImageUrl("original"); + + list.AddRange(results.Select(i => new RemoteImageInfo + { + Url = tmdbImageUrl + i.File_Path, + CommunityRating = i.Vote_Average, + VoteCount = i.Vote_Count, + Width = i.Width, + Height = i.Height, + Language = TmdbMovieProvider.AdjustImageLanguage(i.Iso_639_1, language), + ProviderName = Name, + Type = ImageType.Primary, + RatingType = RatingType.Score + })); + + + var isLanguageEn = string.Equals(language, "en", StringComparison.OrdinalIgnoreCase); + + return list.OrderByDescending(i => + { + if (string.Equals(language, i.Language, StringComparison.OrdinalIgnoreCase)) + { + return 3; + } + if (!isLanguageEn) + { + if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase)) + { + return 2; + } + } + if (string.IsNullOrEmpty(i.Language)) + { + return isLanguageEn ? 3 : 2; + } + return 0; + }) + .ThenByDescending(i => i.CommunityRating ?? 0) + .ThenByDescending(i => i.VoteCount ?? 0); + } + + private async Task> FetchImages(Season item, string tmdbId, string language, IJsonSerializer jsonSerializer, + CancellationToken cancellationToken) + { + await TmdbSeasonProvider.Current.EnsureSeasonInfo(tmdbId, item.IndexNumber.GetValueOrDefault(), language, cancellationToken).ConfigureAwait(false); + + var path = TmdbSeriesProvider.Current.GetDataFilePath(tmdbId, language); + + if (!string.IsNullOrEmpty(path)) + { + var fileInfo = _fileSystem.GetFileInfo(path); + + if (fileInfo.Exists) + { + return jsonSerializer.DeserializeFromFile(path).Images.Posters; + } + } + + return null; + } + + public IEnumerable GetSupportedImages(BaseItem item) + { + return new List + { + ImageType.Primary + }; + } + + public bool Supports(BaseItem item) + { + return item is Season; + } + } +} diff --git a/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonProvider.cs b/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonProvider.cs index 2f2ac58e8..fc0cde8b3 100644 --- a/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonProvider.cs +++ b/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonProvider.cs @@ -32,6 +32,8 @@ namespace MediaBrowser.Providers.Tmdb.TV private readonly ILocalizationManager _localization; private readonly ILogger _logger; + internal static TmdbSeasonProvider Current { get; private set; } + public TmdbSeasonProvider(IHttpClient httpClient, IServerConfigurationManager configurationManager, IFileSystem fileSystem, ILocalizationManager localization, IJsonSerializer jsonSerializer, ILoggerFactory loggerFactory) { _httpClient = httpClient; @@ -40,6 +42,7 @@ namespace MediaBrowser.Providers.Tmdb.TV _localization = localization; _jsonSerializer = jsonSerializer; _logger = loggerFactory.CreateLogger(GetType().Name); + Current = this; } public async Task> GetMetadata(SeasonInfo info, CancellationToken cancellationToken) From 1f323683b3b8cfbfd64b74dfc862a678962c7d38 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 19 Dec 2019 18:59:55 +0100 Subject: [PATCH 2/5] Add requested changes --- MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs b/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs index 3a0062ef7..747676966 100644 --- a/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs +++ b/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs @@ -59,14 +59,14 @@ namespace MediaBrowser.Providers.Tmdb.TV if (string.IsNullOrEmpty(seriesId)) { - return list; + return Enumerable.Empty(); } var seasonNumber = season.IndexNumber; if (!seasonNumber.HasValue) { - return list; + return Enumerable.Empty(); } var language = item.GetPreferredMetadataLanguage(); @@ -88,7 +88,7 @@ namespace MediaBrowser.Providers.Tmdb.TV ProviderName = Name, Type = ImageType.Primary, RatingType = RatingType.Score - })); + }).ToList()); var isLanguageEn = string.Equals(language, "en", StringComparison.OrdinalIgnoreCase); From ce8754d05286f0cebf4710da928f91167137ddb2 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 19 Dec 2019 19:58:00 +0100 Subject: [PATCH 3/5] Refactor list of RemoteImageInfo --- MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs b/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs index 747676966..51db2fb13 100644 --- a/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs +++ b/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs @@ -55,8 +55,6 @@ namespace MediaBrowser.Providers.Tmdb.TV var seriesId = series != null ? series.GetProviderId(MetadataProviders.Tmdb) : null; - var list = new List(); - if (string.IsNullOrEmpty(seriesId)) { return Enumerable.Empty(); @@ -77,7 +75,7 @@ namespace MediaBrowser.Providers.Tmdb.TV var tmdbImageUrl = tmdbSettings.images.GetImageUrl("original"); - list.AddRange(results.Select(i => new RemoteImageInfo + var list = results.Select(i => new RemoteImageInfo { Url = tmdbImageUrl + i.File_Path, CommunityRating = i.Vote_Average, @@ -88,8 +86,7 @@ namespace MediaBrowser.Providers.Tmdb.TV ProviderName = Name, Type = ImageType.Primary, RatingType = RatingType.Score - }).ToList()); - + }).ToList(); var isLanguageEn = string.Equals(language, "en", StringComparison.OrdinalIgnoreCase); From d720d8f3569a0db11ba170c92d8d4ff055c4eaa9 Mon Sep 17 00:00:00 2001 From: David Date: Fri, 20 Dec 2019 20:04:27 +0100 Subject: [PATCH 4/5] Add requested changes --- .../Tmdb/TV/TmdbSeasonImageProvider.cs | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs b/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs index 51db2fb13..f0b2cd88e 100644 --- a/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs +++ b/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -22,15 +23,11 @@ namespace MediaBrowser.Providers.Tmdb.TV { private readonly IJsonSerializer _jsonSerializer; private readonly IHttpClient _httpClient; - private readonly IFileSystem _fileSystem; - private readonly ILocalizationManager _localization; - public TmdbSeasonImageProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, IFileSystem fileSystem, ILocalizationManager localization) + public TmdbSeasonImageProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient) { _jsonSerializer = jsonSerializer; _httpClient = httpClient; - _fileSystem = fileSystem; - _localization = localization; } public int Order => 1; @@ -69,7 +66,7 @@ namespace MediaBrowser.Providers.Tmdb.TV var language = item.GetPreferredMetadataLanguage(); - var results = await FetchImages(season, seriesId, language, _jsonSerializer, cancellationToken).ConfigureAwait(false); + var results = await FetchImages(season, seriesId, language, cancellationToken).ConfigureAwait(false); var tmdbSettings = await TmdbMovieProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false); @@ -86,7 +83,7 @@ namespace MediaBrowser.Providers.Tmdb.TV ProviderName = Name, Type = ImageType.Primary, RatingType = RatingType.Score - }).ToList(); + }); var isLanguageEn = string.Equals(language, "en", StringComparison.OrdinalIgnoreCase); @@ -96,6 +93,7 @@ namespace MediaBrowser.Providers.Tmdb.TV { return 3; } + if (!isLanguageEn) { if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase)) @@ -103,18 +101,19 @@ namespace MediaBrowser.Providers.Tmdb.TV return 2; } } + if (string.IsNullOrEmpty(i.Language)) { return isLanguageEn ? 3 : 2; } + return 0; }) .ThenByDescending(i => i.CommunityRating ?? 0) .ThenByDescending(i => i.VoteCount ?? 0); } - private async Task> FetchImages(Season item, string tmdbId, string language, IJsonSerializer jsonSerializer, - CancellationToken cancellationToken) + private async Task> FetchImages(Season item, string tmdbId, string language, CancellationToken cancellationToken) { await TmdbSeasonProvider.Current.EnsureSeasonInfo(tmdbId, item.IndexNumber.GetValueOrDefault(), language, cancellationToken).ConfigureAwait(false); @@ -122,11 +121,9 @@ namespace MediaBrowser.Providers.Tmdb.TV if (!string.IsNullOrEmpty(path)) { - var fileInfo = _fileSystem.GetFileInfo(path); - - if (fileInfo.Exists) + if (File.Exists(path)) { - return jsonSerializer.DeserializeFromFile(path).Images.Posters; + return _jsonSerializer.DeserializeFromFile(path).Images.Posters; } } From 94d3dda3243dae2daea564b9a1b5c6493b07061e Mon Sep 17 00:00:00 2001 From: David Date: Fri, 20 Dec 2019 20:10:02 +0100 Subject: [PATCH 5/5] Refactor seriesId --- MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs b/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs index f0b2cd88e..24cc8c73b 100644 --- a/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs +++ b/MediaBrowser.Providers/Tmdb/TV/TmdbSeasonImageProvider.cs @@ -50,7 +50,7 @@ namespace MediaBrowser.Providers.Tmdb.TV var season = (Season)item; var series = season.Series; - var seriesId = series != null ? series.GetProviderId(MetadataProviders.Tmdb) : null; + var seriesId = series?.GetProviderId(MetadataProviders.Tmdb); if (string.IsNullOrEmpty(seriesId)) {