From 8072059f3e5029cb5ae7b9b4a940bb5dbdea45df Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 30 Jan 2014 16:47:13 -0500 Subject: [PATCH] convert boxset image provider --- .../Entities/IHasImages.cs | 2 +- .../Providers/IHasMetadata.cs | 2 +- .../BoxSets/MovieDbBoxSetImageProvider.cs | 208 ++++++++++++++++++ .../BoxSets/MovieDbBoxSetProvider.cs | 39 +++- .../MediaBrowser.Providers.csproj | 1 + .../Movies/ManualMovieDbImageProvider.cs | 2 +- 6 files changed, 240 insertions(+), 14 deletions(-) create mode 100644 MediaBrowser.Providers/BoxSets/MovieDbBoxSetImageProvider.cs diff --git a/MediaBrowser.Controller/Entities/IHasImages.cs b/MediaBrowser.Controller/Entities/IHasImages.cs index 784337e3b..2fef9a035 100644 --- a/MediaBrowser.Controller/Entities/IHasImages.cs +++ b/MediaBrowser.Controller/Entities/IHasImages.cs @@ -5,7 +5,7 @@ using System.Threading.Tasks; namespace MediaBrowser.Controller.Entities { - public interface IHasImages + public interface IHasImages : IHasProviderIds { /// /// Gets the name. diff --git a/MediaBrowser.Controller/Providers/IHasMetadata.cs b/MediaBrowser.Controller/Providers/IHasMetadata.cs index 33c5184b8..4ee54f69e 100644 --- a/MediaBrowser.Controller/Providers/IHasMetadata.cs +++ b/MediaBrowser.Controller/Providers/IHasMetadata.cs @@ -8,7 +8,7 @@ namespace MediaBrowser.Controller.Providers /// /// Interface IHasMetadata /// - public interface IHasMetadata : IHasImages, IHasProviderIds + public interface IHasMetadata : IHasImages { /// /// Gets the preferred metadata country code. diff --git a/MediaBrowser.Providers/BoxSets/MovieDbBoxSetImageProvider.cs b/MediaBrowser.Providers/BoxSets/MovieDbBoxSetImageProvider.cs new file mode 100644 index 000000000..961982bfd --- /dev/null +++ b/MediaBrowser.Providers/BoxSets/MovieDbBoxSetImageProvider.cs @@ -0,0 +1,208 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +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 MediaBrowser.Model.Serialization; +using MediaBrowser.Providers.Movies; + +namespace MediaBrowser.Providers.BoxSets +{ + class MovieDbBoxSetImageProvider : IRemoteImageProvider + { + private readonly IJsonSerializer _jsonSerializer; + private readonly IHttpClient _httpClient; + + public MovieDbBoxSetImageProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient) + { + _jsonSerializer = jsonSerializer; + _httpClient = httpClient; + } + + public string Name + { + get { return ProviderName; } + } + + public static string ProviderName + { + get { return "TheMovieDb"; } + } + + public bool Supports(IHasImages item) + { + return item is BoxSet; + } + + public IEnumerable GetSupportedImages(IHasImages item) + { + return new List + { + ImageType.Primary, + ImageType.Backdrop + }; + } + + public async Task> GetImages(IHasImages item, ImageType imageType, CancellationToken cancellationToken) + { + var images = await GetAllImages(item, cancellationToken).ConfigureAwait(false); + + return images.Where(i => i.Type == imageType); + } + + public async Task> GetAllImages(IHasImages item, CancellationToken cancellationToken) + { + var tmdbId = item.GetProviderId(MetadataProviders.Tmdb); + + if (!string.IsNullOrEmpty(tmdbId)) + { + var language = item.GetPreferredMetadataLanguage(); + + var mainResult = await MovieDbBoxSetProvider.Current.GetMovieDbResult(tmdbId, language, cancellationToken).ConfigureAwait(false); + + if (mainResult != null) + { + var tmdbSettings = await MovieDbProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false); + + var tmdbImageUrl = tmdbSettings.images.base_url + "original"; + + return GetImages(mainResult, language, tmdbImageUrl); + } + } + + return new List(); + } + + private IEnumerable GetImages(MovieDbBoxSetProvider.RootObject obj, string language, string baseUrl) + { + var list = new List(); + + var images = obj.images ?? new MovieDbBoxSetProvider.Images(); + + list.AddRange(GetPosters(images).Select(i => new RemoteImageInfo + { + Url = baseUrl + i.file_path, + CommunityRating = i.vote_average, + VoteCount = i.vote_count, + Width = i.width, + Height = i.height, + Language = i.iso_639_1, + ProviderName = Name, + Type = ImageType.Primary, + RatingType = RatingType.Score + })); + + list.AddRange(GetBackdrops(images).Select(i => new RemoteImageInfo + { + Url = baseUrl + i.file_path, + CommunityRating = i.vote_average, + VoteCount = i.vote_count, + Width = i.width, + Height = i.height, + ProviderName = Name, + Type = ImageType.Backdrop, + 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) + .ToList(); + } + + /// + /// Gets the posters. + /// + /// The images. + /// IEnumerable{MovieDbProvider.Poster}. + private IEnumerable GetPosters(MovieDbBoxSetProvider.Images images) + { + return images.posters ?? new List(); + } + + /// + /// Gets the backdrops. + /// + /// The images. + /// IEnumerable{MovieDbProvider.Backdrop}. + private IEnumerable GetBackdrops(MovieDbBoxSetProvider.Images images) + { + var eligibleBackdrops = images.backdrops == null ? new List() : + images.backdrops + .ToList(); + + return eligibleBackdrops.OrderByDescending(i => i.vote_average) + .ThenByDescending(i => i.vote_count); + } + + /// + /// Fetches the images. + /// + /// The item. + /// The json serializer. + /// The cancellation token. + /// Task{MovieImages}. + private async Task FetchImages(BaseItem item, IJsonSerializer jsonSerializer, + CancellationToken cancellationToken) + { + await MovieDbProvider.Current.EnsureMovieInfo(item, cancellationToken).ConfigureAwait(false); + + var path = MovieDbProvider.Current.GetDataFilePath(item); + + if (!string.IsNullOrEmpty(path)) + { + var fileInfo = new FileInfo(path); + + if (fileInfo.Exists) + { + return jsonSerializer.DeserializeFromFile(path).images; + } + } + + return null; + } + + public int Order + { + get { return 0; } + } + + public Task GetImageResponse(string url, CancellationToken cancellationToken) + { + return _httpClient.GetResponse(new HttpRequestOptions + { + CancellationToken = cancellationToken, + Url = url, + ResourcePool = MovieDbProvider.Current.MovieDbResourcePool + }); + } + } +} diff --git a/MediaBrowser.Providers/BoxSets/MovieDbBoxSetProvider.cs b/MediaBrowser.Providers/BoxSets/MovieDbBoxSetProvider.cs index 2fe18fafa..3908535f4 100644 --- a/MediaBrowser.Providers/BoxSets/MovieDbBoxSetProvider.cs +++ b/MediaBrowser.Providers/BoxSets/MovieDbBoxSetProvider.cs @@ -20,8 +20,10 @@ namespace MediaBrowser.Providers.BoxSets public class MovieDbBoxSetProvider : IRemoteMetadataProvider { private readonly CultureInfo _enUs = new CultureInfo("en-US"); - private const string GetCollectionInfo3 = @"http://api.themoviedb.org/3/collection/{0}?api_key={1}&append_to_response=images"; - + private const string GetCollectionInfo3 = @"http://api.themoviedb.org/3/collection/{0}?api_key={1}&append_to_response=images"; + + internal static MovieDbBoxSetProvider Current; + private readonly ILogger _logger; private readonly IJsonSerializer _json; private readonly IServerConfigurationManager _config; @@ -33,6 +35,7 @@ namespace MediaBrowser.Providers.BoxSets _json = json; _config = config; _fileSystem = fileSystem; + Current = this; } public async Task> GetMetadata(ItemId id, CancellationToken cancellationToken) @@ -49,23 +52,37 @@ namespace MediaBrowser.Providers.BoxSets if (!string.IsNullOrEmpty(tmdbId)) { - await EnsureInfo(tmdbId, id.MetadataLanguage, cancellationToken).ConfigureAwait(false); + var mainResult = await GetMovieDbResult(tmdbId, id.MetadataLanguage, cancellationToken).ConfigureAwait(false); - var dataFilePath = GetDataFilePath(_config.ApplicationPaths, tmdbId, id.MetadataLanguage); - - if (!string.IsNullOrEmpty(dataFilePath)) + if (mainResult != null) { - var mainResult = _json.DeserializeFromFile(dataFilePath); - result.HasMetadata = true; result.Item = GetItem(mainResult); } - } return result; } + internal async Task GetMovieDbResult(string tmdbId, string language, CancellationToken cancellationToken) + { + if (string.IsNullOrEmpty(tmdbId)) + { + throw new ArgumentNullException("tmdbId"); + } + + await EnsureInfo(tmdbId, language, cancellationToken).ConfigureAwait(false); + + var dataFilePath = GetDataFilePath(_config.ApplicationPaths, tmdbId, language); + + if (!string.IsNullOrEmpty(dataFilePath)) + { + return _json.DeserializeFromFile(dataFilePath); + } + + return null; + } + private BoxSet GetItem(RootObject obj) { var item = new BoxSet(); @@ -219,7 +236,7 @@ namespace MediaBrowser.Providers.BoxSets public string file_path { get; set; } public int height { get; set; } public string iso_639_1 { get; set; } - public object vote_average { get; set; } + public double vote_average { get; set; } public int vote_count { get; set; } public int width { get; set; } } @@ -230,7 +247,7 @@ namespace MediaBrowser.Providers.BoxSets public string file_path { get; set; } public int height { get; set; } public string iso_639_1 { get; set; } - public object vote_average { get; set; } + public double vote_average { get; set; } public int vote_count { get; set; } public int width { get; set; } } diff --git a/MediaBrowser.Providers/MediaBrowser.Providers.csproj b/MediaBrowser.Providers/MediaBrowser.Providers.csproj index ce996ccfc..06c70d75d 100644 --- a/MediaBrowser.Providers/MediaBrowser.Providers.csproj +++ b/MediaBrowser.Providers/MediaBrowser.Providers.csproj @@ -66,6 +66,7 @@ + diff --git a/MediaBrowser.Providers/Movies/ManualMovieDbImageProvider.cs b/MediaBrowser.Providers/Movies/ManualMovieDbImageProvider.cs index c8cd746dc..8c6e2d92e 100644 --- a/MediaBrowser.Providers/Movies/ManualMovieDbImageProvider.cs +++ b/MediaBrowser.Providers/Movies/ManualMovieDbImageProvider.cs @@ -46,7 +46,7 @@ namespace MediaBrowser.Providers.Movies } // Don't support local trailers - return item is Movie || item is BoxSet || item is MusicVideo; + return item is Movie || item is MusicVideo; } public IEnumerable GetSupportedImages(IHasImages item)