From cd279d98e0574c396c0a35984e46658151e54fc0 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 2 Dec 2013 11:46:25 -0500 Subject: [PATCH] added IHasTrailers --- MediaBrowser.Api/UserLibrary/ItemsService.cs | 14 ++++++++++- .../UserLibrary/UserLibraryService.cs | 10 +++++++- MediaBrowser.Controller/Entities/BaseItem.cs | 21 +++++++---------- .../Entities/Extensions.cs | 2 +- MediaBrowser.Controller/Entities/Game.cs | 12 +++++++++- .../Entities/IHasTrailers.cs | 21 +++++++++++++++++ .../Entities/Movies/BoxSet.cs | 20 ++++++++++++++-- .../Entities/Movies/Movie.cs | 8 ++++++- MediaBrowser.Controller/Entities/TV/Series.cs | 8 ++++++- MediaBrowser.Controller/Entities/Trailer.cs | 7 +++++- .../MediaBrowser.Controller.csproj | 1 + .../Providers/BaseItemXmlParser.cs | 23 +++++++++++++++---- .../Movies/MovieDbProvider.cs | 16 ++++++++----- .../Savers/XmlSaverHelpers.cs | 18 +++++++++------ .../Dto/DtoService.cs | 19 +++++++-------- 15 files changed, 151 insertions(+), 49 deletions(-) create mode 100644 MediaBrowser.Controller/Entities/IHasTrailers.cs diff --git a/MediaBrowser.Api/UserLibrary/ItemsService.cs b/MediaBrowser.Api/UserLibrary/ItemsService.cs index d6f348836..0e40ef395 100644 --- a/MediaBrowser.Api/UserLibrary/ItemsService.cs +++ b/MediaBrowser.Api/UserLibrary/ItemsService.cs @@ -885,7 +885,19 @@ namespace MediaBrowser.Api.UserLibrary if (request.HasTrailer.HasValue) { - items = items.Where(i => request.HasTrailer.Value ? i.LocalTrailerIds.Count > 0 : i.LocalTrailerIds.Count == 0); + var val = request.HasTrailer.Value; + items = items.Where(i => + { + var trailerCount = 0; + + var hasTrailers = i as IHasTrailers; + if (hasTrailers != null) + { + trailerCount = hasTrailers.LocalTrailerIds.Count; + } + + return val ? trailerCount > 0 : trailerCount == 0; + }); } if (request.HasThemeSong.HasValue) diff --git a/MediaBrowser.Api/UserLibrary/UserLibraryService.cs b/MediaBrowser.Api/UserLibrary/UserLibraryService.cs index 6b7980b1f..e9fc52468 100644 --- a/MediaBrowser.Api/UserLibrary/UserLibraryService.cs +++ b/MediaBrowser.Api/UserLibrary/UserLibraryService.cs @@ -489,7 +489,15 @@ namespace MediaBrowser.Api.UserLibrary // Get everything var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)).ToList(); - var dtos = item.LocalTrailerIds + var trailerIds = new List(); + + var hasTrailers = item as IHasTrailers; + if (hasTrailers != null) + { + trailerIds = hasTrailers.LocalTrailerIds; + } + + var dtos = trailerIds .Select(_libraryManager.GetItemById) .OrderBy(i => i.SortName) .Select(i => _dtoService.GetBaseItemDto(i, fields, user, item)); diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index f5cdaa988..4f7889f97 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -37,10 +37,8 @@ namespace MediaBrowser.Controller.Entities Tags = new List(); ThemeSongIds = new List(); ThemeVideoIds = new List(); - LocalTrailerIds = new List(); LockedFields = new List(); Taglines = new List(); - RemoteTrailers = new List(); ImageSources = new List(); } @@ -92,12 +90,6 @@ namespace MediaBrowser.Controller.Entities /// The taglines. public List Taglines { get; set; } - /// - /// Gets or sets the trailer URL. - /// - /// The trailer URL. - public List RemoteTrailers { get; set; } - /// /// Return the id that should be used to key display prefs for this item. /// Default is based on the type for everything except actual generic folders. @@ -654,7 +646,6 @@ namespace MediaBrowser.Controller.Entities public List ThemeSongIds { get; set; } public List ThemeVideoIds { get; set; } - public List LocalTrailerIds { get; set; } [IgnoreDataMember] public virtual string OfficialRatingForComparison @@ -897,7 +888,11 @@ namespace MediaBrowser.Controller.Entities themeVideosChanged = await RefreshThemeVideos(cancellationToken, forceSave, forceRefresh, allowSlowProviders).ConfigureAwait(false); - localTrailersChanged = await RefreshLocalTrailers(cancellationToken, forceSave, forceRefresh, allowSlowProviders).ConfigureAwait(false); + var hasTrailers = this as IHasTrailers; + if (hasTrailers != null) + { + localTrailersChanged = await RefreshLocalTrailers(hasTrailers, cancellationToken, forceSave, forceRefresh, allowSlowProviders).ConfigureAwait(false); + } } cancellationToken.ThrowIfCancellationRequested(); @@ -917,18 +912,18 @@ namespace MediaBrowser.Controller.Entities return changed; } - private async Task RefreshLocalTrailers(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true) + private async Task RefreshLocalTrailers(IHasTrailers item, CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true) { var newItems = LoadLocalTrailers().ToList(); var newItemIds = newItems.Select(i => i.Id).ToList(); - var itemsChanged = !LocalTrailerIds.SequenceEqual(newItemIds); + var itemsChanged = !item.LocalTrailerIds.SequenceEqual(newItemIds); var tasks = newItems.Select(i => i.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders, resetResolveArgs: false)); var results = await Task.WhenAll(tasks).ConfigureAwait(false); - LocalTrailerIds = newItemIds; + item.LocalTrailerIds = newItemIds; return itemsChanged || results.Contains(true); } diff --git a/MediaBrowser.Controller/Entities/Extensions.cs b/MediaBrowser.Controller/Entities/Extensions.cs index d189f4e71..2a64bd3a4 100644 --- a/MediaBrowser.Controller/Entities/Extensions.cs +++ b/MediaBrowser.Controller/Entities/Extensions.cs @@ -16,7 +16,7 @@ namespace MediaBrowser.Controller.Entities /// The URL. /// if set to true [is direct link]. /// url - public static void AddTrailerUrl(this BaseItem item, string url, bool isDirectLink) + public static void AddTrailerUrl(this IHasTrailers item, string url, bool isDirectLink) { if (string.IsNullOrWhiteSpace(url)) { diff --git a/MediaBrowser.Controller/Entities/Game.cs b/MediaBrowser.Controller/Entities/Game.cs index ea39cf50a..e15b7e4c9 100644 --- a/MediaBrowser.Controller/Entities/Game.cs +++ b/MediaBrowser.Controller/Entities/Game.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; namespace MediaBrowser.Controller.Entities { - public class Game : BaseItem, IHasSoundtracks + public class Game : BaseItem, IHasSoundtracks, IHasTrailers { public List SoundtrackIds { get; set; } @@ -12,8 +12,18 @@ namespace MediaBrowser.Controller.Entities { MultiPartGameFiles = new List(); SoundtrackIds = new List(); + RemoteTrailers = new List(); + LocalTrailerIds = new List(); } + public List LocalTrailerIds { get; set; } + + /// + /// Gets or sets the remote trailers. + /// + /// The remote trailers. + public List RemoteTrailers { get; set; } + /// /// Gets the type of the media. /// diff --git a/MediaBrowser.Controller/Entities/IHasTrailers.cs b/MediaBrowser.Controller/Entities/IHasTrailers.cs new file mode 100644 index 000000000..47779064b --- /dev/null +++ b/MediaBrowser.Controller/Entities/IHasTrailers.cs @@ -0,0 +1,21 @@ +using MediaBrowser.Model.Entities; +using System; +using System.Collections.Generic; + +namespace MediaBrowser.Controller.Entities +{ + public interface IHasTrailers + { + /// + /// Gets or sets the remote trailers. + /// + /// The remote trailers. + List RemoteTrailers { get; set; } + + /// + /// Gets or sets the local trailer ids. + /// + /// The local trailer ids. + List LocalTrailerIds { get; set; } + } +} diff --git a/MediaBrowser.Controller/Entities/Movies/BoxSet.cs b/MediaBrowser.Controller/Entities/Movies/BoxSet.cs index e52ece502..4a6221ee9 100644 --- a/MediaBrowser.Controller/Entities/Movies/BoxSet.cs +++ b/MediaBrowser.Controller/Entities/Movies/BoxSet.cs @@ -1,10 +1,26 @@ - +using System; +using MediaBrowser.Model.Entities; +using System.Collections.Generic; + namespace MediaBrowser.Controller.Entities.Movies { /// /// Class BoxSet /// - public class BoxSet : Folder + public class BoxSet : Folder, IHasTrailers { + public BoxSet() + { + RemoteTrailers = new List(); + LocalTrailerIds = new List(); + } + + public List LocalTrailerIds { get; set; } + + /// + /// Gets or sets the remote trailers. + /// + /// The remote trailers. + public List RemoteTrailers { get; set; } } } diff --git a/MediaBrowser.Controller/Entities/Movies/Movie.cs b/MediaBrowser.Controller/Entities/Movies/Movie.cs index 30babe238..473ea4996 100644 --- a/MediaBrowser.Controller/Entities/Movies/Movie.cs +++ b/MediaBrowser.Controller/Entities/Movies/Movie.cs @@ -11,7 +11,7 @@ namespace MediaBrowser.Controller.Entities.Movies /// /// Class Movie /// - public class Movie : Video, IHasCriticRating, IHasSoundtracks, IHasBudget + public class Movie : Video, IHasCriticRating, IHasSoundtracks, IHasBudget, IHasTrailers { public List SpecialFeatureIds { get; set; } @@ -21,8 +21,14 @@ namespace MediaBrowser.Controller.Entities.Movies { SpecialFeatureIds = new List(); SoundtrackIds = new List(); + RemoteTrailers = new List(); + LocalTrailerIds = new List(); } + public List LocalTrailerIds { get; set; } + + public List RemoteTrailers { get; set; } + /// /// Gets or sets the budget. /// diff --git a/MediaBrowser.Controller/Entities/TV/Series.cs b/MediaBrowser.Controller/Entities/TV/Series.cs index 02ea50c6b..f3c7b088a 100644 --- a/MediaBrowser.Controller/Entities/TV/Series.cs +++ b/MediaBrowser.Controller/Entities/TV/Series.cs @@ -11,7 +11,7 @@ namespace MediaBrowser.Controller.Entities.TV /// /// Class Series /// - public class Series : Folder, IHasSoundtracks + public class Series : Folder, IHasSoundtracks, IHasTrailers { public List SpecialFeatureIds { get; set; } public List SoundtrackIds { get; set; } @@ -24,8 +24,14 @@ namespace MediaBrowser.Controller.Entities.TV SpecialFeatureIds = new List(); SoundtrackIds = new List(); + RemoteTrailers = new List(); + LocalTrailerIds = new List(); } + public List LocalTrailerIds { get; set; } + + public List RemoteTrailers { get; set; } + /// /// Gets or sets the status. /// diff --git a/MediaBrowser.Controller/Entities/Trailer.cs b/MediaBrowser.Controller/Entities/Trailer.cs index 7c14c9865..77efe8e8c 100644 --- a/MediaBrowser.Controller/Entities/Trailer.cs +++ b/MediaBrowser.Controller/Entities/Trailer.cs @@ -8,7 +8,7 @@ namespace MediaBrowser.Controller.Entities /// /// Class Trailer /// - public class Trailer : Video, IHasCriticRating, IHasSoundtracks, IHasBudget + public class Trailer : Video, IHasCriticRating, IHasSoundtracks, IHasBudget, IHasTrailers { public List SoundtrackIds { get; set; } @@ -17,8 +17,13 @@ namespace MediaBrowser.Controller.Entities RemoteTrailers = new List(); Taglines = new List(); SoundtrackIds = new List(); + LocalTrailerIds = new List(); } + public List LocalTrailerIds { get; set; } + + public List RemoteTrailers { get; set; } + /// /// Gets or sets the budget. /// diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index e5fe6a04f..64d5c5226 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -93,6 +93,7 @@ + diff --git a/MediaBrowser.Controller/Providers/BaseItemXmlParser.cs b/MediaBrowser.Controller/Providers/BaseItemXmlParser.cs index 44138a598..617e4fd81 100644 --- a/MediaBrowser.Controller/Providers/BaseItemXmlParser.cs +++ b/MediaBrowser.Controller/Providers/BaseItemXmlParser.cs @@ -68,7 +68,12 @@ namespace MediaBrowser.Controller.Providers item.Genres.Clear(); item.People.Clear(); item.Tags.Clear(); - item.RemoteTrailers.Clear(); + + var hasTrailers = item as IHasTrailers; + if (hasTrailers != null) + { + hasTrailers.RemoteTrailers.Clear(); + } //Fetch(item, metadataFile, settings, Encoding.GetEncoding("ISO-8859-1"), cancellationToken); Fetch(item, metadataFile, settings, Encoding.UTF8, cancellationToken); @@ -484,9 +489,13 @@ namespace MediaBrowser.Controller.Providers { var val = reader.ReadElementContentAsString(); - if (!string.IsNullOrWhiteSpace(val)) + var hasTrailers = item as IHasTrailers; + if (hasTrailers != null) { - item.AddTrailerUrl(val, false); + if (!string.IsNullOrWhiteSpace(val)) + { + hasTrailers.AddTrailerUrl(val, false); + } } break; } @@ -495,7 +504,11 @@ namespace MediaBrowser.Controller.Providers { using (var subtree = reader.ReadSubtree()) { - FetchDataFromTrailersNode(subtree, item); + var hasTrailers = item as IHasTrailers; + if (hasTrailers != null) + { + FetchDataFromTrailersNode(subtree, hasTrailers); + } } break; } @@ -940,7 +953,7 @@ namespace MediaBrowser.Controller.Providers } } - private void FetchDataFromTrailersNode(XmlReader reader, T item) + private void FetchDataFromTrailersNode(XmlReader reader, IHasTrailers item) { reader.MoveToContent(); diff --git a/MediaBrowser.Providers/Movies/MovieDbProvider.cs b/MediaBrowser.Providers/Movies/MovieDbProvider.cs index b812c9324..51a2362c0 100644 --- a/MediaBrowser.Providers/Movies/MovieDbProvider.cs +++ b/MediaBrowser.Providers/Movies/MovieDbProvider.cs @@ -874,14 +874,18 @@ namespace MediaBrowser.Providers.Movies if (movieData.trailers != null && movieData.trailers.youtube != null && movieData.trailers.youtube.Count > 0) { - movie.RemoteTrailers = movieData.trailers.youtube.Select(i => new MediaUrl + var hasTrailers = movie as IHasTrailers; + if (hasTrailers != null) { - Url = string.Format("http://www.youtube.com/watch?v={0}", i.source), - IsDirectLink = false, - Name = i.name, - VideoSize = string.Equals("hd", i.size, StringComparison.OrdinalIgnoreCase) ? VideoSize.HighDefinition : VideoSize.StandardDefinition + hasTrailers.RemoteTrailers = movieData.trailers.youtube.Select(i => new MediaUrl + { + Url = string.Format("http://www.youtube.com/watch?v={0}", i.source), + IsDirectLink = false, + Name = i.name, + VideoSize = string.Equals("hd", i.size, StringComparison.OrdinalIgnoreCase) ? VideoSize.HighDefinition : VideoSize.StandardDefinition - }).ToList(); + }).ToList(); + } } } diff --git a/MediaBrowser.Providers/Savers/XmlSaverHelpers.cs b/MediaBrowser.Providers/Savers/XmlSaverHelpers.cs index 4c032c8e7..186941988 100644 --- a/MediaBrowser.Providers/Savers/XmlSaverHelpers.cs +++ b/MediaBrowser.Providers/Savers/XmlSaverHelpers.cs @@ -268,16 +268,20 @@ namespace MediaBrowser.Providers.Savers } } - if (item.RemoteTrailers.Count > 0) + var hasTrailers = item as IHasTrailers; + if (hasTrailers != null) { - builder.Append(""); - - foreach (var trailer in item.RemoteTrailers) + if (hasTrailers.RemoteTrailers.Count > 0) { - builder.Append("" + SecurityElement.Escape(trailer.Url) + ""); - } + builder.Append(""); - builder.Append(""); + foreach (var trailer in hasTrailers.RemoteTrailers) + { + builder.Append("" + SecurityElement.Escape(trailer.Url) + ""); + } + + builder.Append(""); + } } var hasBudget = item as IHasBudget; diff --git a/MediaBrowser.Server.Implementations/Dto/DtoService.cs b/MediaBrowser.Server.Implementations/Dto/DtoService.cs index b43449858..90ca64058 100644 --- a/MediaBrowser.Server.Implementations/Dto/DtoService.cs +++ b/MediaBrowser.Server.Implementations/Dto/DtoService.cs @@ -808,11 +808,17 @@ namespace MediaBrowser.Server.Implementations.Dto } } - var localTrailerCount = item.LocalTrailerIds.Count; - - if (localTrailerCount > 0) + var hasTrailers = item as IHasTrailers; + if (hasTrailers != null) { - dto.LocalTrailerCount = localTrailerCount; + dto.LocalTrailerCount = hasTrailers.LocalTrailerIds.Count; + } + + if (fields.Contains(ItemFields.RemoteTrailers)) + { + dto.RemoteTrailers = hasTrailers != null ? + hasTrailers.RemoteTrailers : + new List(); } dto.Name = item.Name; @@ -925,11 +931,6 @@ namespace MediaBrowser.Server.Implementations.Dto dto.Taglines = item.Taglines; } - if (fields.Contains(ItemFields.RemoteTrailers)) - { - dto.RemoteTrailers = item.RemoteTrailers; - } - dto.Type = item.GetClientTypeName(); dto.CommunityRating = item.CommunityRating; dto.VoteCount = item.VoteCount;