2014-05-31 23:19:06 -04:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-02-02 14:31:56 -05:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2015-06-28 21:10:45 -04:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2014-01-28 13:37:01 -05:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-05-31 23:19:06 -04:00
|
|
|
|
using System;
|
2014-01-28 13:37:01 -05:00
|
|
|
|
using System.Collections.Generic;
|
2017-05-02 08:53:41 -04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using MediaBrowser.Controller.Extensions;
|
2014-01-28 13:37:01 -05:00
|
|
|
|
|
2014-02-08 15:02:35 -05:00
|
|
|
|
namespace MediaBrowser.Providers.Manager
|
2014-01-28 13:37:01 -05:00
|
|
|
|
{
|
|
|
|
|
public static class ProviderUtils
|
|
|
|
|
{
|
2015-06-28 21:10:45 -04:00
|
|
|
|
public static void MergeBaseItemData<T>(MetadataResult<T> sourceResult,
|
2016-04-13 16:49:16 -04:00
|
|
|
|
MetadataResult<T> targetResult,
|
|
|
|
|
List<MetadataFields> lockedFields,
|
|
|
|
|
bool replaceData,
|
2014-10-28 19:17:55 -04:00
|
|
|
|
bool mergeMetadataSettings)
|
2015-06-28 21:10:45 -04:00
|
|
|
|
where T : BaseItem
|
2014-01-28 13:37:01 -05:00
|
|
|
|
{
|
2015-06-28 21:10:45 -04:00
|
|
|
|
var source = sourceResult.Item;
|
|
|
|
|
var target = targetResult.Item;
|
|
|
|
|
|
2015-05-08 12:28:06 -04:00
|
|
|
|
if (source == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("source");
|
|
|
|
|
}
|
|
|
|
|
if (target == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("target");
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 13:37:01 -05:00
|
|
|
|
if (!lockedFields.Contains(MetadataFields.Name))
|
|
|
|
|
{
|
|
|
|
|
if (replaceData || string.IsNullOrEmpty(target.Name))
|
|
|
|
|
{
|
2014-05-31 23:19:06 -04:00
|
|
|
|
// Safeguard against incoming data having an emtpy name
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(source.Name))
|
|
|
|
|
{
|
|
|
|
|
target.Name = source.Name;
|
|
|
|
|
}
|
2014-01-28 13:37:01 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-20 01:21:40 -04:00
|
|
|
|
if (replaceData || string.IsNullOrEmpty(target.OriginalTitle))
|
|
|
|
|
{
|
|
|
|
|
// Safeguard against incoming data having an emtpy name
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(source.OriginalTitle))
|
|
|
|
|
{
|
|
|
|
|
target.OriginalTitle = source.OriginalTitle;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 13:37:01 -05:00
|
|
|
|
if (replaceData || !target.CommunityRating.HasValue)
|
|
|
|
|
{
|
|
|
|
|
target.CommunityRating = source.CommunityRating;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (replaceData || !target.EndDate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
target.EndDate = source.EndDate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!lockedFields.Contains(MetadataFields.Genres))
|
|
|
|
|
{
|
|
|
|
|
if (replaceData || target.Genres.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
target.Genres = source.Genres;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (replaceData || string.IsNullOrEmpty(target.HomePageUrl))
|
|
|
|
|
{
|
|
|
|
|
target.HomePageUrl = source.HomePageUrl;
|
2014-10-09 18:22:04 -04:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(target.HomePageUrl) && target.HomePageUrl.IndexOf("http", StringComparison.OrdinalIgnoreCase) != 0)
|
|
|
|
|
{
|
|
|
|
|
target.HomePageUrl = "http://" + target.HomePageUrl;
|
|
|
|
|
}
|
2014-01-28 13:37:01 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (replaceData || !target.IndexNumber.HasValue)
|
|
|
|
|
{
|
|
|
|
|
target.IndexNumber = source.IndexNumber;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!lockedFields.Contains(MetadataFields.OfficialRating))
|
|
|
|
|
{
|
|
|
|
|
if (replaceData || string.IsNullOrEmpty(target.OfficialRating))
|
|
|
|
|
{
|
|
|
|
|
target.OfficialRating = source.OfficialRating;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-01 12:53:56 -05:00
|
|
|
|
if (replaceData || string.IsNullOrEmpty(target.CustomRating))
|
|
|
|
|
{
|
|
|
|
|
target.CustomRating = source.CustomRating;
|
|
|
|
|
}
|
2016-04-13 16:49:16 -04:00
|
|
|
|
|
2016-10-08 01:57:38 -04:00
|
|
|
|
if (replaceData || string.IsNullOrEmpty(target.Tagline))
|
|
|
|
|
{
|
|
|
|
|
target.Tagline = source.Tagline;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 13:37:01 -05:00
|
|
|
|
if (!lockedFields.Contains(MetadataFields.Overview))
|
|
|
|
|
{
|
|
|
|
|
if (replaceData || string.IsNullOrEmpty(target.Overview))
|
|
|
|
|
{
|
|
|
|
|
target.Overview = source.Overview;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (replaceData || !target.ParentIndexNumber.HasValue)
|
|
|
|
|
{
|
|
|
|
|
target.ParentIndexNumber = source.ParentIndexNumber;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!lockedFields.Contains(MetadataFields.Cast))
|
|
|
|
|
{
|
2015-07-23 22:48:10 -04:00
|
|
|
|
if (replaceData || targetResult.People == null || targetResult.People.Count == 0)
|
2014-01-28 13:37:01 -05:00
|
|
|
|
{
|
2016-04-13 16:49:16 -04:00
|
|
|
|
targetResult.People = sourceResult.People;
|
2017-05-02 08:53:41 -04:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (targetResult.People != null && sourceResult.People != null)
|
|
|
|
|
{
|
|
|
|
|
MergePeople(sourceResult.People, targetResult.People);
|
2014-01-28 13:37:01 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (replaceData || !target.PremiereDate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
target.PremiereDate = source.PremiereDate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (replaceData || !target.ProductionYear.HasValue)
|
|
|
|
|
{
|
|
|
|
|
target.ProductionYear = source.ProductionYear;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!lockedFields.Contains(MetadataFields.Runtime))
|
|
|
|
|
{
|
|
|
|
|
if (replaceData || !target.RunTimeTicks.HasValue)
|
|
|
|
|
{
|
2014-02-05 23:39:16 -05:00
|
|
|
|
if (!(target is Audio) && !(target is Video))
|
|
|
|
|
{
|
|
|
|
|
target.RunTimeTicks = source.RunTimeTicks;
|
|
|
|
|
}
|
2014-01-28 13:37:01 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!lockedFields.Contains(MetadataFields.Studios))
|
|
|
|
|
{
|
|
|
|
|
if (replaceData || target.Studios.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
target.Studios = source.Studios;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 23:50:09 -05:00
|
|
|
|
if (!lockedFields.Contains(MetadataFields.Tags))
|
|
|
|
|
{
|
2016-06-02 13:43:29 -04:00
|
|
|
|
if (replaceData || target.Tags.Count == 0)
|
2014-01-30 23:50:09 -05:00
|
|
|
|
{
|
2016-06-02 13:43:29 -04:00
|
|
|
|
target.Tags = source.Tags;
|
2014-01-30 23:50:09 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!lockedFields.Contains(MetadataFields.Keywords))
|
|
|
|
|
{
|
2016-05-31 14:07:54 -04:00
|
|
|
|
if (replaceData || target.Keywords.Count == 0)
|
2014-01-30 23:50:09 -05:00
|
|
|
|
{
|
2016-05-31 14:07:54 -04:00
|
|
|
|
target.Keywords = source.Keywords;
|
2014-01-30 23:50:09 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-09 03:18:43 -04:00
|
|
|
|
if (!lockedFields.Contains(MetadataFields.ProductionLocations))
|
|
|
|
|
{
|
|
|
|
|
if (replaceData || target.ProductionLocations.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
target.ProductionLocations = source.ProductionLocations;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 13:37:01 -05:00
|
|
|
|
if (replaceData || !target.VoteCount.HasValue)
|
|
|
|
|
{
|
|
|
|
|
target.VoteCount = source.VoteCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var id in source.ProviderIds)
|
|
|
|
|
{
|
2014-02-13 00:11:54 -05:00
|
|
|
|
var key = id.Key;
|
|
|
|
|
|
|
|
|
|
// Don't replace existing Id's.
|
2014-07-07 21:41:03 -04:00
|
|
|
|
if (replaceData || !target.ProviderIds.ContainsKey(key))
|
2014-02-13 00:11:54 -05:00
|
|
|
|
{
|
|
|
|
|
target.ProviderIds[key] = id.Value;
|
|
|
|
|
}
|
2014-01-28 13:37:01 -05:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-02 14:31:56 -05:00
|
|
|
|
MergeAlbumArtist(source, target, lockedFields, replaceData);
|
2014-02-05 23:39:16 -05:00
|
|
|
|
MergeCriticRating(source, target, lockedFields, replaceData);
|
|
|
|
|
MergeAwards(source, target, lockedFields, replaceData);
|
|
|
|
|
MergeTrailers(source, target, lockedFields, replaceData);
|
2014-02-02 14:31:56 -05:00
|
|
|
|
|
2014-01-28 13:37:01 -05:00
|
|
|
|
if (mergeMetadataSettings)
|
|
|
|
|
{
|
2014-10-28 19:17:55 -04:00
|
|
|
|
MergeMetadataSettings(source, target);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-30 23:50:09 -05:00
|
|
|
|
|
2017-05-02 08:53:41 -04:00
|
|
|
|
private static void MergePeople(List<PersonInfo> source, List<PersonInfo> target)
|
|
|
|
|
{
|
|
|
|
|
foreach (var person in target)
|
|
|
|
|
{
|
|
|
|
|
var normalizedName = person.Name.RemoveDiacritics();
|
|
|
|
|
var personInSource = source.FirstOrDefault(i => string.Equals(i.Name.RemoveDiacritics(), normalizedName, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
if (personInSource != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var providerId in personInSource.ProviderIds)
|
|
|
|
|
{
|
|
|
|
|
if (!person.ProviderIds.ContainsKey(providerId.Key))
|
|
|
|
|
{
|
|
|
|
|
person.ProviderIds[providerId.Key] = providerId.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(person.ImageUrl))
|
|
|
|
|
{
|
|
|
|
|
person.ImageUrl = personInSource.ImageUrl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-28 19:17:55 -04:00
|
|
|
|
public static void MergeMetadataSettings(BaseItem source,
|
|
|
|
|
BaseItem target)
|
|
|
|
|
{
|
|
|
|
|
target.ForcedSortName = source.ForcedSortName;
|
|
|
|
|
target.LockedFields = source.LockedFields;
|
|
|
|
|
target.IsLocked = source.IsLocked;
|
|
|
|
|
target.DisplayMediaType = source.DisplayMediaType;
|
2014-05-01 22:54:33 -04:00
|
|
|
|
|
2014-10-28 19:17:55 -04:00
|
|
|
|
// Grab the value if it's there, but if not then don't overwrite the default
|
|
|
|
|
if (source.DateCreated != default(DateTime))
|
|
|
|
|
{
|
|
|
|
|
target.DateCreated = source.DateCreated;
|
|
|
|
|
}
|
2014-01-30 23:50:09 -05:00
|
|
|
|
|
2015-09-29 13:35:23 -04:00
|
|
|
|
target.PreferredMetadataCountryCode = source.PreferredMetadataCountryCode;
|
|
|
|
|
target.PreferredMetadataLanguage = source.PreferredMetadataLanguage;
|
2014-01-30 23:50:09 -05:00
|
|
|
|
|
2014-10-28 19:17:55 -04:00
|
|
|
|
var sourceHasDisplayOrder = source as IHasDisplayOrder;
|
|
|
|
|
var targetHasDisplayOrder = target as IHasDisplayOrder;
|
|
|
|
|
|
|
|
|
|
if (sourceHasDisplayOrder != null && targetHasDisplayOrder != null)
|
|
|
|
|
{
|
|
|
|
|
targetHasDisplayOrder.DisplayOrder = sourceHasDisplayOrder.DisplayOrder;
|
2014-01-28 13:37:01 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-13 16:49:16 -04:00
|
|
|
|
|
2014-02-02 14:31:56 -05:00
|
|
|
|
private static void MergeAlbumArtist(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
|
|
|
|
|
{
|
|
|
|
|
var sourceHasAlbumArtist = source as IHasAlbumArtist;
|
|
|
|
|
var targetHasAlbumArtist = target as IHasAlbumArtist;
|
|
|
|
|
|
|
|
|
|
if (sourceHasAlbumArtist != null && targetHasAlbumArtist != null)
|
|
|
|
|
{
|
2015-03-12 21:55:22 -04:00
|
|
|
|
if (replaceData || targetHasAlbumArtist.AlbumArtists.Count == 0)
|
2014-02-02 14:31:56 -05:00
|
|
|
|
{
|
2014-06-23 12:05:19 -04:00
|
|
|
|
targetHasAlbumArtist.AlbumArtists = sourceHasAlbumArtist.AlbumArtists;
|
2014-02-02 14:31:56 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-04 15:19:29 -05:00
|
|
|
|
|
2014-02-05 23:39:16 -05:00
|
|
|
|
private static void MergeAwards(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
|
|
|
|
|
{
|
|
|
|
|
var sourceCast = source as IHasAwards;
|
|
|
|
|
var targetCast = target as IHasAwards;
|
|
|
|
|
|
|
|
|
|
if (sourceCast != null && targetCast != null)
|
|
|
|
|
{
|
|
|
|
|
if (replaceData || string.IsNullOrEmpty(targetCast.AwardSummary))
|
|
|
|
|
{
|
|
|
|
|
targetCast.AwardSummary = sourceCast.AwardSummary;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void MergeCriticRating(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
|
|
|
|
|
{
|
2016-10-17 12:35:29 -04:00
|
|
|
|
if (replaceData || !target.CriticRating.HasValue)
|
2014-02-05 23:39:16 -05:00
|
|
|
|
{
|
2016-10-17 12:35:29 -04:00
|
|
|
|
target.CriticRating = source.CriticRating;
|
|
|
|
|
}
|
2014-02-05 23:39:16 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void MergeTrailers(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
|
|
|
|
|
{
|
|
|
|
|
var sourceCast = source as IHasTrailers;
|
|
|
|
|
var targetCast = target as IHasTrailers;
|
|
|
|
|
|
|
|
|
|
if (sourceCast != null && targetCast != null)
|
|
|
|
|
{
|
|
|
|
|
if (replaceData || targetCast.RemoteTrailers.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
targetCast.RemoteTrailers = sourceCast.RemoteTrailers;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-28 13:37:01 -05:00
|
|
|
|
}
|
|
|
|
|
}
|