jellyfin/MediaBrowser.Providers/Manager/ProviderUtils.cs

294 lines
10 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Linq;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Extensions;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
2014-02-08 21:02:35 +01:00
namespace MediaBrowser.Providers.Manager
{
public static class ProviderUtils
{
public static void MergeBaseItemData<T>(
MetadataResult<T> sourceResult,
2016-04-13 22:49:16 +02:00
MetadataResult<T> targetResult,
2020-06-10 00:12:53 +02:00
MetadataField[] lockedFields,
2016-04-13 22:49:16 +02:00
bool replaceData,
2014-10-29 00:17:55 +01:00
bool mergeMetadataSettings)
2015-06-29 03:10:45 +02:00
where T : BaseItem
{
2015-06-29 03:10:45 +02:00
var source = sourceResult.Item;
var target = targetResult.Item;
2015-05-08 18:28:06 +02:00
if (source == null)
{
throw new ArgumentNullException(nameof(source));
2015-05-08 18:28:06 +02:00
}
2020-06-15 23:43:52 +02:00
2015-05-08 18:28:06 +02:00
if (target == null)
{
throw new ArgumentNullException(nameof(target));
2015-05-08 18:28:06 +02:00
}
2020-06-10 00:12:53 +02:00
if (!lockedFields.Contains(MetadataField.Name))
{
if (replaceData || string.IsNullOrEmpty(target.Name))
{
// Safeguard against incoming data having an emtpy name
if (!string.IsNullOrWhiteSpace(source.Name))
{
target.Name = source.Name;
}
}
}
2016-04-20 07:21:40 +02:00
if (replaceData || string.IsNullOrEmpty(target.OriginalTitle))
{
// Safeguard against incoming data having an emtpy name
if (!string.IsNullOrWhiteSpace(source.OriginalTitle))
{
target.OriginalTitle = source.OriginalTitle;
}
}
2017-10-05 20:10:07 +02:00
if (replaceData || !target.CommunityRating.HasValue || (source.CommunityRating.HasValue && string.Equals(sourceResult.Provider, "The Open Movie Database", StringComparison.OrdinalIgnoreCase)))
{
target.CommunityRating = source.CommunityRating;
}
if (replaceData || !target.EndDate.HasValue)
{
target.EndDate = source.EndDate;
}
2020-06-10 00:12:53 +02:00
if (!lockedFields.Contains(MetadataField.Genres))
{
2018-09-12 19:26:21 +02:00
if (replaceData || target.Genres.Length == 0)
{
target.Genres = source.Genres;
}
}
if (replaceData || !target.IndexNumber.HasValue)
{
target.IndexNumber = source.IndexNumber;
}
2020-06-10 00:12:53 +02:00
if (!lockedFields.Contains(MetadataField.OfficialRating))
{
if (replaceData || string.IsNullOrEmpty(target.OfficialRating))
{
target.OfficialRating = source.OfficialRating;
}
}
2014-03-01 18:53:56 +01:00
if (replaceData || string.IsNullOrEmpty(target.CustomRating))
{
target.CustomRating = source.CustomRating;
}
2016-04-13 22:49:16 +02:00
2016-10-08 07:57:38 +02:00
if (replaceData || string.IsNullOrEmpty(target.Tagline))
{
target.Tagline = source.Tagline;
}
2020-06-10 00:12:53 +02:00
if (!lockedFields.Contains(MetadataField.Overview))
{
if (replaceData || string.IsNullOrEmpty(target.Overview))
{
target.Overview = source.Overview;
}
}
if (replaceData || !target.ParentIndexNumber.HasValue)
{
target.ParentIndexNumber = source.ParentIndexNumber;
}
2020-06-10 00:12:53 +02:00
if (!lockedFields.Contains(MetadataField.Cast))
{
2015-07-24 04:48:10 +02:00
if (replaceData || targetResult.People == null || targetResult.People.Count == 0)
{
2016-04-13 22:49:16 +02:00
targetResult.People = sourceResult.People;
}
else if (targetResult.People != null && sourceResult.People != null)
{
MergePeople(sourceResult.People, targetResult.People);
}
}
if (replaceData || !target.PremiereDate.HasValue)
{
target.PremiereDate = source.PremiereDate;
}
if (replaceData || !target.ProductionYear.HasValue)
{
target.ProductionYear = source.ProductionYear;
}
2020-06-10 00:12:53 +02:00
if (!lockedFields.Contains(MetadataField.Runtime))
{
if (replaceData || !target.RunTimeTicks.HasValue)
{
if (!(target is Audio) && !(target is Video))
{
target.RunTimeTicks = source.RunTimeTicks;
}
}
}
2020-06-10 00:12:53 +02:00
if (!lockedFields.Contains(MetadataField.Studios))
{
if (replaceData || target.Studios.Length == 0)
{
target.Studios = source.Studios;
}
}
2020-06-10 00:12:53 +02:00
if (!lockedFields.Contains(MetadataField.Tags))
2014-01-31 05:50:09 +01:00
{
if (replaceData || target.Tags.Length == 0)
2014-01-31 05:50:09 +01:00
{
2016-06-02 19:43:29 +02:00
target.Tags = source.Tags;
2014-01-31 05:50:09 +01:00
}
}
2020-06-10 00:12:53 +02:00
if (!lockedFields.Contains(MetadataField.ProductionLocations))
2016-10-09 09:18:43 +02:00
{
if (replaceData || target.ProductionLocations.Length == 0)
2016-10-09 09:18:43 +02:00
{
target.ProductionLocations = source.ProductionLocations;
}
}
foreach (var id in source.ProviderIds)
{
2014-02-13 06:11:54 +01:00
var key = id.Key;
// Don't replace existing Id's.
2014-07-08 03:41:03 +02:00
if (replaceData || !target.ProviderIds.ContainsKey(key))
2014-02-13 06:11:54 +01:00
{
target.ProviderIds[key] = id.Value;
}
}
MergeAlbumArtist(source, target, replaceData);
MergeCriticRating(source, target, replaceData);
MergeTrailers(source, target, replaceData);
MergeVideoInfo(source, target, replaceData);
MergeDisplayOrder(source, target, replaceData);
2017-06-03 09:36:32 +02:00
2018-09-12 19:26:21 +02:00
if (replaceData || string.IsNullOrEmpty(target.ForcedSortName))
2017-06-03 09:36:32 +02:00
{
2018-09-12 19:26:21 +02:00
var forcedSortName = source.ForcedSortName;
2017-06-03 09:36:32 +02:00
2018-09-12 19:26:21 +02:00
if (!string.IsNullOrWhiteSpace(forcedSortName))
{
target.ForcedSortName = forcedSortName;
2017-06-03 09:36:32 +02:00
}
}
if (mergeMetadataSettings)
{
2018-09-12 19:26:21 +02:00
target.LockedFields = source.LockedFields;
target.IsLocked = source.IsLocked;
// Grab the value if it's there, but if not then don't overwrite the default
if (source.DateCreated != default)
2018-09-12 19:26:21 +02:00
{
target.DateCreated = source.DateCreated;
}
target.PreferredMetadataCountryCode = source.PreferredMetadataCountryCode;
target.PreferredMetadataLanguage = source.PreferredMetadataLanguage;
2014-10-29 00:17:55 +01:00
}
}
2014-01-31 05:50:09 +01: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;
}
}
}
}
private static void MergeDisplayOrder(BaseItem source, BaseItem target, bool replaceData)
2017-06-03 09:36:32 +02:00
{
if (source is IHasDisplayOrder sourceHasDisplayOrder
&& target is IHasDisplayOrder targetHasDisplayOrder)
2014-10-29 00:17:55 +01:00
{
2018-09-12 19:26:21 +02:00
if (replaceData || string.IsNullOrEmpty(targetHasDisplayOrder.DisplayOrder))
{
var displayOrder = sourceHasDisplayOrder.DisplayOrder;
if (!string.IsNullOrWhiteSpace(displayOrder))
{
targetHasDisplayOrder.DisplayOrder = displayOrder;
}
}
}
}
2016-04-13 22:49:16 +02:00
private static void MergeAlbumArtist(BaseItem source, BaseItem target, bool replaceData)
{
2019-08-29 22:28:33 +02:00
if (source is IHasAlbumArtist sourceHasAlbumArtist
&& target is IHasAlbumArtist targetHasAlbumArtist)
{
2019-08-29 22:28:33 +02:00
if (replaceData || targetHasAlbumArtist.AlbumArtists.Count == 0)
{
2014-06-23 18:05:19 +02:00
targetHasAlbumArtist.AlbumArtists = sourceHasAlbumArtist.AlbumArtists;
}
}
}
private static void MergeCriticRating(BaseItem source, BaseItem target, bool replaceData)
{
2016-10-17 18:35:29 +02:00
if (replaceData || !target.CriticRating.HasValue)
{
2016-10-17 18:35:29 +02:00
target.CriticRating = source.CriticRating;
}
}
private static void MergeTrailers(BaseItem source, BaseItem target, bool replaceData)
{
if (replaceData || target.RemoteTrailers.Count == 0)
{
2018-09-12 19:26:21 +02:00
target.RemoteTrailers = source.RemoteTrailers;
}
}
2017-05-09 20:51:26 +02:00
private static void MergeVideoInfo(BaseItem source, BaseItem target, bool replaceData)
2017-05-09 20:51:26 +02:00
{
if (source is Video sourceCast && target is Video targetCast)
2017-05-09 20:51:26 +02:00
{
if (replaceData || targetCast.Video3DFormat == null)
{
targetCast.Video3DFormat = sourceCast.Video3DFormat;
}
}
}
}
}