From aa6d6d8ec7f5e9a53d9519daa24cbc6cbdb0b33a Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 21 Jan 2016 13:50:43 -0500 Subject: [PATCH] get people info during media refresh --- .../Entities/PeopleHelper.cs | 17 +++++-- MediaBrowser.Controller/Entities/Person.cs | 12 ++++- .../Manager/MetadataService.cs | 43 +++++++++++++++++ .../Movies/GenericMovieDbInfo.cs | 47 +++++++++++++++++-- .../TV/TvdbSeriesProvider.cs | 15 ++++++ 5 files changed, 126 insertions(+), 8 deletions(-) diff --git a/MediaBrowser.Controller/Entities/PeopleHelper.cs b/MediaBrowser.Controller/Entities/PeopleHelper.cs index 3468ca2d5..40a93d9e6 100644 --- a/MediaBrowser.Controller/Entities/PeopleHelper.cs +++ b/MediaBrowser.Controller/Entities/PeopleHelper.cs @@ -45,7 +45,7 @@ namespace MediaBrowser.Controller.Entities if (existing != null) { existing.Type = PersonType.GuestStar; - existing.SortOrder = person.SortOrder ?? existing.SortOrder; + MergeExisting(existing, person); return; } } @@ -67,7 +67,7 @@ namespace MediaBrowser.Controller.Entities existing.Role = person.Role; } - existing.SortOrder = person.SortOrder ?? existing.SortOrder; + MergeExisting(existing, person); } } else @@ -83,11 +83,22 @@ namespace MediaBrowser.Controller.Entities } else { - existing.SortOrder = person.SortOrder ?? existing.SortOrder; + MergeExisting(existing, person); } } } + private static void MergeExisting(PersonInfo existing, PersonInfo person) + { + existing.SortOrder = person.SortOrder ?? existing.SortOrder; + existing.ImageUrl = person.ImageUrl ?? existing.ImageUrl; + + foreach (var id in person.ProviderIds) + { + existing.SetProviderId(id.Key, id.Value); + } + } + public static bool ContainsPerson(List people, string name) { if (string.IsNullOrWhiteSpace(name)) diff --git a/MediaBrowser.Controller/Entities/Person.cs b/MediaBrowser.Controller/Entities/Person.cs index 6c277da56..a365b99c6 100644 --- a/MediaBrowser.Controller/Entities/Person.cs +++ b/MediaBrowser.Controller/Entities/Person.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; +using MediaBrowser.Model.Entities; namespace MediaBrowser.Controller.Entities { @@ -106,8 +107,13 @@ namespace MediaBrowser.Controller.Entities /// /// This is the small Person stub that is attached to BaseItems /// - public class PersonInfo + public class PersonInfo : IHasProviderIds { + public PersonInfo() + { + ProviderIds = new Dictionary(StringComparer.OrdinalIgnoreCase); + } + public Guid ItemId { get; set; } /// @@ -132,6 +138,10 @@ namespace MediaBrowser.Controller.Entities /// The sort order. public int? SortOrder { get; set; } + public string ImageUrl { get; set; } + + public Dictionary ProviderIds { get; set; } + /// /// Returns a that represents this instance. /// diff --git a/MediaBrowser.Providers/Manager/MetadataService.cs b/MediaBrowser.Providers/Manager/MetadataService.cs index 083c8f1f3..d9f5c30fd 100644 --- a/MediaBrowser.Providers/Manager/MetadataService.cs +++ b/MediaBrowser.Providers/Manager/MetadataService.cs @@ -254,10 +254,53 @@ namespace MediaBrowser.Providers.Manager if (result.Item.SupportsPeople && result.People != null) { await LibraryManager.UpdatePeople(result.Item as BaseItem, result.People.ToList()); + await SavePeopleMetadata(result.People, cancellationToken).ConfigureAwait(false); } await result.Item.UpdateToRepository(reason, cancellationToken).ConfigureAwait(false); } + private async Task SavePeopleMetadata(List people, CancellationToken cancellationToken) + { + foreach (var person in people) + { + cancellationToken.ThrowIfCancellationRequested(); + + if (person.ProviderIds.Any() || !string.IsNullOrWhiteSpace(person.ImageUrl)) + { + var updateType = ItemUpdateType.MetadataDownload; + + var saveEntity = false; + var personEntity = LibraryManager.GetPerson(person.Name); + foreach (var id in person.ProviderIds) + { + if (!string.Equals(personEntity.GetProviderId(id.Key), id.Value, StringComparison.OrdinalIgnoreCase)) + { + personEntity.SetProviderId(id.Key, id.Value); + saveEntity = true; + } + } + + if (!string.IsNullOrWhiteSpace(person.ImageUrl) && !personEntity.HasImage(ImageType.Primary)) + { + personEntity.SetImage(new ItemImageInfo + { + Path = person.ImageUrl, + Type = ImageType.Primary, + IsPlaceholder = true + }, 0); + + saveEntity = true; + updateType = updateType | ItemUpdateType.ImageUpdate; + } + + if (saveEntity) + { + await personEntity.UpdateToRepository(updateType, cancellationToken).ConfigureAwait(false); + } + } + } + } + private readonly Task _cachedTask = Task.FromResult(true); protected virtual Task AfterMetadataRefresh(TItemType item, MetadataRefreshOptions refreshOptions, CancellationToken cancellationToken) { diff --git a/MediaBrowser.Providers/Movies/GenericMovieDbInfo.cs b/MediaBrowser.Providers/Movies/GenericMovieDbInfo.cs index 3c2d9c82f..abd4a6202 100644 --- a/MediaBrowser.Providers/Movies/GenericMovieDbInfo.cs +++ b/MediaBrowser.Providers/Movies/GenericMovieDbInfo.cs @@ -104,7 +104,9 @@ namespace MediaBrowser.Providers.Movies dataFilePath = dataFilePath ?? MovieDbProvider.Current.GetDataFilePath(tmdbId, language); movieInfo = movieInfo ?? _jsonSerializer.DeserializeFromFile(dataFilePath); - ProcessMainInfo(item, preferredCountryCode, movieInfo); + var settings = await MovieDbProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false); + + ProcessMainInfo(item, settings, preferredCountryCode, movieInfo); item.HasMetadata = true; } @@ -115,9 +117,10 @@ namespace MediaBrowser.Providers.Movies /// Processes the main info. /// /// The result item. + /// The settings. /// The preferred country code. /// The movie data. - private void ProcessMainInfo(MetadataResult resultItem, string preferredCountryCode, MovieDbProvider.CompleteMovieData movieData) + private void ProcessMainInfo(MetadataResult resultItem, TmdbSettingsResult settings, string preferredCountryCode, MovieDbProvider.CompleteMovieData movieData) { var movie = resultItem.Item; @@ -247,6 +250,7 @@ namespace MediaBrowser.Providers.Movies } resultItem.ResetPeople(); + var tmdbImageUrl = settings.images.base_url + "original"; //Actors, Directors, Writers - all in People //actors come from cast @@ -254,7 +258,25 @@ namespace MediaBrowser.Providers.Movies { foreach (var actor in movieData.casts.cast.OrderBy(a => a.order)) { - resultItem.AddPerson(new PersonInfo { Name = actor.name.Trim(), Role = actor.character, Type = PersonType.Actor, SortOrder = actor.order }); + var personInfo = new PersonInfo + { + Name = actor.name.Trim(), + Role = actor.character, + Type = PersonType.Actor, + SortOrder = actor.order + }; + + if (!string.IsNullOrWhiteSpace(actor.profile_path)) + { + personInfo.ImageUrl = tmdbImageUrl + actor.profile_path; + } + + if (actor.id > 0) + { + personInfo.SetProviderId(MetadataProviders.Tmdb, actor.id.ToString(CultureInfo.InvariantCulture)); + } + + resultItem.AddPerson(personInfo); } } @@ -270,7 +292,24 @@ namespace MediaBrowser.Providers.Movies type = PersonType.Writer; } - resultItem.AddPerson(new PersonInfo { Name = person.name.Trim(), Role = person.job, Type = type }); + var personInfo = new PersonInfo + { + Name = person.name.Trim(), + Role = person.job, + Type = type + }; + + if (!string.IsNullOrWhiteSpace(person.profile_path)) + { + personInfo.ImageUrl = tmdbImageUrl + person.profile_path; + } + + if (person.id > 0) + { + personInfo.SetProviderId(MetadataProviders.Tmdb, person.id.ToString(CultureInfo.InvariantCulture)); + } + + resultItem.AddPerson(personInfo); } } diff --git a/MediaBrowser.Providers/TV/TvdbSeriesProvider.cs b/MediaBrowser.Providers/TV/TvdbSeriesProvider.cs index 9452438cb..00bc032ca 100644 --- a/MediaBrowser.Providers/TV/TvdbSeriesProvider.cs +++ b/MediaBrowser.Providers/TV/TvdbSeriesProvider.cs @@ -826,6 +826,21 @@ namespace MediaBrowser.Providers.TV break; } + case "id": + { + break; + } + + case "Image": + { + var url = (reader.ReadElementContentAsString() ?? string.Empty).Trim(); + if (!string.IsNullOrWhiteSpace(url)) + { + personInfo.ImageUrl = TVUtils.BannerUrl + url; + } + break; + } + case "SortOrder": { var val = reader.ReadElementContentAsString();