2021-10-26 13:49:01 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2020-06-19 18:24:13 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2019-01-13 19:26:31 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2014-01-29 05:17:58 +00:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-02-04 20:19:29 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-01-28 18:37:01 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Model.Configuration;
|
2014-01-28 18:37:01 +00:00
|
|
|
using MediaBrowser.Model.Entities;
|
2016-10-25 19:02:04 +00:00
|
|
|
using MediaBrowser.Model.IO;
|
2016-04-06 02:18:56 +00:00
|
|
|
using MediaBrowser.Model.Providers;
|
2019-01-13 19:26:31 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Manager
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
public abstract class MetadataService<TItemType, TIdType> : IMetadataService
|
2018-09-12 17:26:21 +00:00
|
|
|
where TItemType : BaseItem, IHasLookupInfo<TIdType>, new()
|
2014-02-07 03:10:13 +00:00
|
|
|
where TIdType : ItemLookupInfo, new()
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2020-06-06 00:15:56 +00:00
|
|
|
protected MetadataService(IServerConfigurationManager serverConfigurationManager, ILogger<MetadataService<TItemType, TIdType>> logger, IProviderManager providerManager, IFileSystem fileSystem, ILibraryManager libraryManager)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
ServerConfigurationManager = serverConfigurationManager;
|
|
|
|
Logger = logger;
|
|
|
|
ProviderManager = providerManager;
|
2014-02-02 13:36:31 +00:00
|
|
|
FileSystem = fileSystem;
|
2015-06-29 01:10:45 +00:00
|
|
|
LibraryManager = libraryManager;
|
2021-05-23 22:30:41 +00:00
|
|
|
ImageProvider = new ItemImageProvider(Logger, ProviderManager, FileSystem);
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
2017-11-05 21:51:23 +00:00
|
|
|
|
2021-05-23 22:30:41 +00:00
|
|
|
protected ItemImageProvider ImageProvider { get; }
|
|
|
|
|
2020-09-07 11:20:39 +00:00
|
|
|
protected IServerConfigurationManager ServerConfigurationManager { get; }
|
|
|
|
|
|
|
|
protected ILogger<MetadataService<TItemType, TIdType>> Logger { get; }
|
|
|
|
|
|
|
|
protected IProviderManager ProviderManager { get; }
|
|
|
|
|
|
|
|
protected IFileSystem FileSystem { get; }
|
|
|
|
|
|
|
|
protected ILibraryManager LibraryManager { get; }
|
|
|
|
|
|
|
|
protected virtual bool EnableUpdatingPremiereDateFromChildren => false;
|
|
|
|
|
|
|
|
protected virtual bool EnableUpdatingGenresFromChildren => false;
|
|
|
|
|
|
|
|
protected virtual bool EnableUpdatingStudiosFromChildren => false;
|
|
|
|
|
|
|
|
protected virtual bool EnableUpdatingOfficialRatingFromChildren => false;
|
|
|
|
|
|
|
|
public virtual int Order => 0;
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private FileSystemMetadata TryGetFile(string path, IDirectoryService directoryService)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return directoryService.GetFile(path);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2019-09-10 20:37:53 +00:00
|
|
|
Logger.LogError(ex, "Error getting file {Path}", path);
|
2018-09-12 17:26:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public async Task<ItemUpdateType> RefreshMetadata(BaseItem item, MetadataRefreshOptions refreshOptions, CancellationToken cancellationToken)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
var itemOfType = (TItemType)item;
|
2014-02-02 13:36:31 +00:00
|
|
|
|
2014-02-10 18:39:41 +00:00
|
|
|
var updateType = ItemUpdateType.None;
|
2016-08-18 05:56:10 +00:00
|
|
|
var requiresRefresh = false;
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var libraryOptions = LibraryManager.GetLibraryOptions(item);
|
2017-03-14 19:44:11 +00:00
|
|
|
|
2017-10-19 01:19:48 +00:00
|
|
|
if (!requiresRefresh && libraryOptions.AutomaticRefreshIntervalDays > 0 && (DateTime.UtcNow - item.DateLastRefreshed).TotalDays >= libraryOptions.AutomaticRefreshIntervalDays)
|
|
|
|
{
|
|
|
|
requiresRefresh = true;
|
|
|
|
}
|
|
|
|
|
2017-10-14 06:52:56 +00:00
|
|
|
if (!requiresRefresh && refreshOptions.MetadataRefreshMode != MetadataRefreshMode.None)
|
|
|
|
{
|
|
|
|
// TODO: If this returns true, should we instead just change metadata refresh mode to Full?
|
2017-10-19 01:19:48 +00:00
|
|
|
requiresRefresh = item.RequiresRefresh();
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
if (requiresRefresh)
|
|
|
|
{
|
2018-12-13 13:18:25 +00:00
|
|
|
Logger.LogDebug("Refreshing {0} {1} because item.RequiresRefresh() returned true", typeof(TItemType).Name, item.Path ?? item.Name);
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
2017-10-14 06:52:56 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
var localImagesFailed = false;
|
|
|
|
|
2016-03-05 04:17:02 +00:00
|
|
|
var allImageProviders = ((ProviderManager)ProviderManager).GetImageProviders(item, refreshOptions).ToList();
|
2014-01-31 19:55:21 +00:00
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
// Start by validating images
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Always validate images and check for new locally stored ones.
|
2021-05-23 22:30:41 +00:00
|
|
|
if (ImageProvider.ValidateImages(item, allImageProviders.OfType<ILocalImageProvider>(), refreshOptions.DirectoryService))
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2020-08-07 17:26:28 +00:00
|
|
|
updateType |= ItemUpdateType.ImageUpdate;
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
localImagesFailed = true;
|
2019-02-24 15:16:53 +00:00
|
|
|
Logger.LogError(ex, "Error validating images for {0}", item.Path ?? item.Name ?? "Unknown name");
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 01:10:45 +00:00
|
|
|
var metadataResult = new MetadataResult<TItemType>
|
|
|
|
{
|
|
|
|
Item = itemOfType
|
|
|
|
};
|
|
|
|
|
2016-04-16 02:18:40 +00:00
|
|
|
bool hasRefreshedMetadata = true;
|
|
|
|
bool hasRefreshedImages = true;
|
2020-08-07 17:26:28 +00:00
|
|
|
var isFirstRefresh = item.DateLastRefreshed == default;
|
2016-04-08 18:32:38 +00:00
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
// Next run metadata providers
|
2014-02-02 13:36:31 +00:00
|
|
|
if (refreshOptions.MetadataRefreshMode != MetadataRefreshMode.None)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var providers = GetProviders(item, libraryOptions, refreshOptions, isFirstRefresh, requiresRefresh)
|
2014-03-24 17:54:45 +00:00
|
|
|
.ToList();
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2017-11-16 21:25:18 +00:00
|
|
|
if (providers.Count > 0 || isFirstRefresh || requiresRefresh)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (item.BeforeMetadataRefresh(refreshOptions.ReplaceAllMetadata))
|
2014-02-13 05:11:54 +00:00
|
|
|
{
|
2020-08-07 17:26:28 +00:00
|
|
|
updateType |= ItemUpdateType.MetadataImport;
|
2014-02-13 05:11:54 +00:00
|
|
|
}
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
if (providers.Count > 0)
|
|
|
|
{
|
2015-09-19 21:25:19 +00:00
|
|
|
var id = itemOfType.GetLookupInfo();
|
2015-10-23 16:04:33 +00:00
|
|
|
|
2016-04-06 02:18:56 +00:00
|
|
|
if (refreshOptions.SearchResult != null)
|
|
|
|
{
|
|
|
|
ApplySearchResult(id, refreshOptions.SearchResult);
|
|
|
|
}
|
|
|
|
|
2020-06-14 09:11:11 +00:00
|
|
|
// await FindIdentities(id, cancellationToken).ConfigureAwait(false);
|
2016-01-23 02:32:14 +00:00
|
|
|
id.IsAutomated = refreshOptions.IsAutomated;
|
2014-08-21 16:21:39 +00:00
|
|
|
|
2021-05-23 22:30:41 +00:00
|
|
|
var result = await RefreshWithProviders(metadataResult, id, refreshOptions, providers, ImageProvider, cancellationToken).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2020-08-07 17:26:28 +00:00
|
|
|
updateType |= result.UpdateType;
|
2016-11-27 19:36:56 +00:00
|
|
|
if (result.Failures > 0)
|
2015-09-10 18:28:22 +00:00
|
|
|
{
|
2016-04-16 02:18:40 +00:00
|
|
|
hasRefreshedMetadata = false;
|
2015-09-10 18:28:22 +00:00
|
|
|
}
|
2014-08-21 16:21:39 +00:00
|
|
|
}
|
2014-05-06 02:00:16 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
// Next run remote image providers, but only if local image providers didn't throw an exception
|
2018-09-12 17:26:21 +00:00
|
|
|
if (!localImagesFailed && refreshOptions.ImageRefreshMode != MetadataRefreshMode.ValidationOnly)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2016-05-21 06:12:00 +00:00
|
|
|
var providers = GetNonLocalImageProviders(item, allImageProviders, refreshOptions).ToList();
|
2014-01-29 17:23:38 +00:00
|
|
|
|
|
|
|
if (providers.Count > 0)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2021-05-23 22:30:41 +00:00
|
|
|
var result = await ImageProvider.RefreshImages(itemOfType, libraryOptions, providers, refreshOptions, cancellationToken).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2020-08-07 17:26:28 +00:00
|
|
|
updateType |= result.UpdateType;
|
2016-11-27 19:36:56 +00:00
|
|
|
if (result.Failures > 0)
|
2015-09-10 18:28:22 +00:00
|
|
|
{
|
2016-04-16 02:18:40 +00:00
|
|
|
hasRefreshedImages = false;
|
2015-09-10 18:28:22 +00:00
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var beforeSaveResult = BeforeSave(itemOfType, isFirstRefresh || refreshOptions.ReplaceAllMetadata || refreshOptions.MetadataRefreshMode == MetadataRefreshMode.FullRefresh || requiresRefresh || refreshOptions.ForceSave, updateType);
|
2020-08-07 17:26:28 +00:00
|
|
|
updateType |= beforeSaveResult;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2014-02-11 21:41:01 +00:00
|
|
|
// Save if changes were made, or it's never been saved before
|
2016-05-21 03:58:04 +00:00
|
|
|
if (refreshOptions.ForceSave || updateType > ItemUpdateType.None || isFirstRefresh || refreshOptions.ReplaceAllMetadata || requiresRefresh)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (item.IsFileProtocol)
|
|
|
|
{
|
|
|
|
var file = TryGetFile(item.Path, refreshOptions.DirectoryService);
|
|
|
|
if (file != null)
|
|
|
|
{
|
|
|
|
item.DateModified = file.LastWriteTimeUtc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-17 18:15:09 +00:00
|
|
|
// If any of these properties are set then make sure the updateType is not None, just to force everything to save
|
|
|
|
if (refreshOptions.ForceSave || refreshOptions.ReplaceAllMetadata)
|
2014-09-13 14:10:30 +00:00
|
|
|
{
|
2020-08-07 17:26:28 +00:00
|
|
|
updateType |= ItemUpdateType.MetadataDownload;
|
2014-09-13 14:10:30 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 18:32:38 +00:00
|
|
|
if (hasRefreshedMetadata && hasRefreshedImages)
|
2015-10-14 05:02:30 +00:00
|
|
|
{
|
|
|
|
item.DateLastRefreshed = DateTime.UtcNow;
|
2016-04-08 18:32:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-07 17:26:28 +00:00
|
|
|
item.DateLastRefreshed = default;
|
2015-10-14 05:02:30 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
// Save to database
|
2021-04-24 18:22:23 +00:00
|
|
|
await SaveItemAsync(metadataResult, updateType, cancellationToken).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 21:29:02 +00:00
|
|
|
await AfterMetadataRefresh(itemOfType, refreshOptions, cancellationToken).ConfigureAwait(false);
|
2015-03-11 02:07:07 +00:00
|
|
|
|
|
|
|
return updateType;
|
2015-01-28 21:29:02 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 02:18:56 +00:00
|
|
|
private void ApplySearchResult(ItemLookupInfo lookupInfo, RemoteSearchResult result)
|
|
|
|
{
|
2021-06-11 22:22:55 +00:00
|
|
|
// Episode and Season do not support Identify, so the search results are the Series'
|
2021-05-29 08:56:38 +00:00
|
|
|
switch (lookupInfo)
|
|
|
|
{
|
|
|
|
case EpisodeInfo episodeInfo:
|
|
|
|
episodeInfo.SeriesProviderIds = result.ProviderIds;
|
|
|
|
episodeInfo.ProviderIds.Clear();
|
|
|
|
break;
|
|
|
|
case SeasonInfo seasonInfo:
|
|
|
|
seasonInfo.SeriesProviderIds = result.ProviderIds;
|
|
|
|
seasonInfo.ProviderIds.Clear();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lookupInfo.ProviderIds = result.ProviderIds;
|
|
|
|
lookupInfo.Name = result.Name;
|
|
|
|
lookupInfo.Year = result.ProductionYear;
|
|
|
|
break;
|
|
|
|
}
|
2016-04-06 02:18:56 +00:00
|
|
|
}
|
|
|
|
|
2021-04-24 18:22:23 +00:00
|
|
|
protected async Task SaveItemAsync(MetadataResult<TItemType> result, ItemUpdateType reason, CancellationToken cancellationToken)
|
2015-07-24 02:48:10 +00:00
|
|
|
{
|
|
|
|
if (result.Item.SupportsPeople && result.People != null)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var baseItem = result.Item;
|
2016-09-11 17:09:10 +00:00
|
|
|
|
2021-04-24 18:22:23 +00:00
|
|
|
await LibraryManager.UpdatePeopleAsync(baseItem, result.People, cancellationToken).ConfigureAwait(false);
|
2015-07-24 02:48:10 +00:00
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2020-08-21 20:01:19 +00:00
|
|
|
await result.Item.UpdateToRepositoryAsync(reason, cancellationToken).ConfigureAwait(false);
|
2015-07-24 02:48:10 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 21:29:02 +00:00
|
|
|
protected virtual Task AfterMetadataRefresh(TItemType item, MetadataRefreshOptions refreshOptions, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
item.AfterMetadataRefresh();
|
2018-09-12 17:26:21 +00:00
|
|
|
return Task.CompletedTask;
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
2015-10-14 05:02:30 +00:00
|
|
|
|
2014-02-03 05:35:43 +00:00
|
|
|
/// <summary>
|
2020-09-16 12:16:44 +00:00
|
|
|
/// Before the save.
|
2014-02-03 05:35:43 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item.</param>
|
2015-01-17 18:15:09 +00:00
|
|
|
/// <param name="isFullRefresh">if set to <c>true</c> [is full refresh].</param>
|
|
|
|
/// <param name="currentUpdateType">Type of the current update.</param>
|
2014-02-03 05:35:43 +00:00
|
|
|
/// <returns>ItemUpdateType.</returns>
|
2017-09-13 18:41:48 +00:00
|
|
|
private ItemUpdateType BeforeSave(TItemType item, bool isFullRefresh, ItemUpdateType currentUpdateType)
|
|
|
|
{
|
|
|
|
var updateType = BeforeSaveInternal(item, isFullRefresh, currentUpdateType);
|
|
|
|
|
|
|
|
updateType |= item.OnMetadataChanged();
|
|
|
|
|
|
|
|
return updateType;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
protected virtual ItemUpdateType BeforeSaveInternal(TItemType item, bool isFullRefresh, ItemUpdateType updateType)
|
2016-05-09 03:13:38 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (EnableUpdateMetadataFromChildren(item, isFullRefresh, updateType))
|
|
|
|
{
|
|
|
|
if (isFullRefresh || updateType > ItemUpdateType.None)
|
|
|
|
{
|
|
|
|
var children = GetChildrenForMetadataUpdates(item);
|
2016-05-09 03:13:38 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
updateType = UpdateMetadataFromChildren(item, children, isFullRefresh, updateType);
|
|
|
|
}
|
|
|
|
}
|
2016-05-09 03:13:38 +00:00
|
|
|
|
2016-08-12 19:30:04 +00:00
|
|
|
var presentationUniqueKey = item.CreatePresentationUniqueKey();
|
|
|
|
if (!string.Equals(item.PresentationUniqueKey, presentationUniqueKey, StringComparison.Ordinal))
|
|
|
|
{
|
|
|
|
item.PresentationUniqueKey = presentationUniqueKey;
|
|
|
|
updateType |= ItemUpdateType.MetadataImport;
|
|
|
|
}
|
|
|
|
|
2016-05-09 03:13:38 +00:00
|
|
|
return updateType;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
protected virtual bool EnableUpdateMetadataFromChildren(TItemType item, bool isFullRefresh, ItemUpdateType currentUpdateType)
|
|
|
|
{
|
|
|
|
if (isFullRefresh || currentUpdateType > ItemUpdateType.None)
|
|
|
|
{
|
|
|
|
if (EnableUpdatingPremiereDateFromChildren || EnableUpdatingGenresFromChildren || EnableUpdatingStudiosFromChildren || EnableUpdatingOfficialRatingFromChildren)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2021-05-05 11:37:36 +00:00
|
|
|
if (item is Folder folder)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
return folder.SupportsDateLastMediaAdded || folder.SupportsCumulativeRunTimeTicks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual IList<BaseItem> GetChildrenForMetadataUpdates(TItemType item)
|
|
|
|
{
|
2020-09-16 12:16:44 +00:00
|
|
|
if (item is Folder folder)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
return folder.GetRecursiveChildren();
|
|
|
|
}
|
|
|
|
|
2020-09-16 12:16:44 +00:00
|
|
|
return Array.Empty<BaseItem>();
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual ItemUpdateType UpdateMetadataFromChildren(TItemType item, IList<BaseItem> children, bool isFullRefresh, ItemUpdateType currentUpdateType)
|
2016-05-09 03:13:38 +00:00
|
|
|
{
|
|
|
|
var updateType = ItemUpdateType.None;
|
|
|
|
|
|
|
|
if (isFullRefresh || currentUpdateType > ItemUpdateType.None)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
updateType |= UpdateCumulativeRunTimeTicks(item, children);
|
|
|
|
updateType |= UpdateDateLastMediaAdded(item, children);
|
|
|
|
|
|
|
|
if (EnableUpdatingPremiereDateFromChildren)
|
2016-05-09 03:13:38 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
updateType |= UpdatePremiereDate(item, children);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EnableUpdatingGenresFromChildren)
|
|
|
|
{
|
|
|
|
updateType |= UpdateGenres(item, children);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EnableUpdatingStudiosFromChildren)
|
|
|
|
{
|
|
|
|
updateType |= UpdateStudios(item, children);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EnableUpdatingOfficialRatingFromChildren)
|
|
|
|
{
|
|
|
|
updateType |= UpdateOfficialRating(item, children);
|
|
|
|
}
|
|
|
|
}
|
2016-05-09 03:13:38 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
return updateType;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ItemUpdateType UpdateCumulativeRunTimeTicks(TItemType item, IList<BaseItem> children)
|
|
|
|
{
|
2021-05-05 11:37:36 +00:00
|
|
|
if (item is Folder folder && folder.SupportsCumulativeRunTimeTicks)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
long ticks = 0;
|
|
|
|
|
|
|
|
foreach (var child in children)
|
|
|
|
{
|
|
|
|
if (!child.IsFolder)
|
2016-05-09 03:13:38 +00:00
|
|
|
{
|
2020-06-19 09:57:37 +00:00
|
|
|
ticks += child.RunTimeTicks ?? 0;
|
2016-05-09 03:13:38 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
if (!folder.RunTimeTicks.HasValue || folder.RunTimeTicks.Value != ticks)
|
|
|
|
{
|
|
|
|
folder.RunTimeTicks = ticks;
|
|
|
|
return ItemUpdateType.MetadataEdit;
|
|
|
|
}
|
2016-05-09 03:13:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
return ItemUpdateType.None;
|
2016-05-09 03:13:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private ItemUpdateType UpdateDateLastMediaAdded(TItemType item, IList<BaseItem> children)
|
2014-02-03 05:35:43 +00:00
|
|
|
{
|
2016-05-09 03:13:38 +00:00
|
|
|
var updateType = ItemUpdateType.None;
|
|
|
|
|
2016-10-17 16:35:29 +00:00
|
|
|
var folder = item as Folder;
|
|
|
|
if (folder != null && folder.SupportsDateLastMediaAdded)
|
2016-05-09 03:13:38 +00:00
|
|
|
{
|
2019-01-13 20:37:13 +00:00
|
|
|
var dateLastMediaAdded = DateTime.MinValue;
|
2018-09-12 17:26:21 +00:00
|
|
|
var any = false;
|
|
|
|
|
|
|
|
foreach (var child in children)
|
|
|
|
{
|
|
|
|
if (!child.IsFolder)
|
|
|
|
{
|
|
|
|
var childDateCreated = child.DateCreated;
|
|
|
|
if (childDateCreated > dateLastMediaAdded)
|
|
|
|
{
|
|
|
|
dateLastMediaAdded = childDateCreated;
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
any = true;
|
|
|
|
}
|
|
|
|
}
|
2016-05-09 03:13:38 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if ((!folder.DateLastMediaAdded.HasValue && any) || folder.DateLastMediaAdded != dateLastMediaAdded)
|
2016-10-17 16:35:29 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
folder.DateLastMediaAdded = dateLastMediaAdded;
|
2016-10-17 16:35:29 +00:00
|
|
|
updateType = ItemUpdateType.MetadataImport;
|
2016-05-09 03:13:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return updateType;
|
2014-02-03 05:35:43 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private ItemUpdateType UpdatePremiereDate(TItemType item, IList<BaseItem> children)
|
|
|
|
{
|
|
|
|
var updateType = ItemUpdateType.None;
|
|
|
|
|
|
|
|
if (children.Count == 0)
|
|
|
|
{
|
|
|
|
return updateType;
|
|
|
|
}
|
|
|
|
|
|
|
|
var date = children.Select(i => i.PremiereDate ?? DateTime.MaxValue).Min();
|
|
|
|
|
|
|
|
var originalPremiereDate = item.PremiereDate;
|
|
|
|
var originalProductionYear = item.ProductionYear;
|
|
|
|
|
|
|
|
if (date > DateTime.MinValue && date < DateTime.MaxValue)
|
|
|
|
{
|
|
|
|
item.PremiereDate = date;
|
|
|
|
item.ProductionYear = date.Year;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var year = children.Select(i => i.ProductionYear ?? 0).Min();
|
|
|
|
|
|
|
|
if (year > 0)
|
|
|
|
{
|
|
|
|
item.ProductionYear = year;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((originalPremiereDate ?? DateTime.MinValue) != (item.PremiereDate ?? DateTime.MinValue) ||
|
|
|
|
(originalProductionYear ?? -1) != (item.ProductionYear ?? -1))
|
|
|
|
{
|
2021-03-23 18:25:32 +00:00
|
|
|
updateType |= ItemUpdateType.MetadataEdit;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return updateType;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ItemUpdateType UpdateGenres(TItemType item, IList<BaseItem> children)
|
|
|
|
{
|
|
|
|
var updateType = ItemUpdateType.None;
|
|
|
|
|
2020-06-09 22:12:53 +00:00
|
|
|
if (!item.LockedFields.Contains(MetadataField.Genres))
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
var currentList = item.Genres;
|
|
|
|
|
|
|
|
item.Genres = children.SelectMany(i => i.Genres)
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
if (currentList.Length != item.Genres.Length || !currentList.OrderBy(i => i).SequenceEqual(item.Genres.OrderBy(i => i), StringComparer.OrdinalIgnoreCase))
|
|
|
|
{
|
2021-03-23 18:25:32 +00:00
|
|
|
updateType |= ItemUpdateType.MetadataEdit;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return updateType;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ItemUpdateType UpdateStudios(TItemType item, IList<BaseItem> children)
|
|
|
|
{
|
|
|
|
var updateType = ItemUpdateType.None;
|
|
|
|
|
2020-06-09 22:12:53 +00:00
|
|
|
if (!item.LockedFields.Contains(MetadataField.Studios))
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
var currentList = item.Studios;
|
|
|
|
|
|
|
|
item.Studios = children.SelectMany(i => i.Studios)
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
if (currentList.Length != item.Studios.Length || !currentList.OrderBy(i => i).SequenceEqual(item.Studios.OrderBy(i => i), StringComparer.OrdinalIgnoreCase))
|
|
|
|
{
|
2021-03-23 18:25:32 +00:00
|
|
|
updateType |= ItemUpdateType.MetadataEdit;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return updateType;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ItemUpdateType UpdateOfficialRating(TItemType item, IList<BaseItem> children)
|
|
|
|
{
|
|
|
|
var updateType = ItemUpdateType.None;
|
|
|
|
|
2020-06-09 22:12:53 +00:00
|
|
|
if (!item.LockedFields.Contains(MetadataField.OfficialRating))
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
if (item.UpdateRatingToItems(children))
|
|
|
|
{
|
2021-03-23 18:25:32 +00:00
|
|
|
updateType |= ItemUpdateType.MetadataEdit;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return updateType;
|
|
|
|
}
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the providers.
|
|
|
|
/// </summary>
|
2021-07-23 00:33:19 +00:00
|
|
|
/// <param name="item">A media item.</param>
|
|
|
|
/// <param name="libraryOptions">The LibraryOptions to use.</param>
|
|
|
|
/// <param name="options">The MetadataRefreshOptions to use.</param>
|
|
|
|
/// <param name="isFirstRefresh">Specifies first refresh mode.</param>
|
|
|
|
/// <param name="requiresRefresh">Specifies refresh mode.</param>
|
2014-01-28 18:37:01 +00:00
|
|
|
/// <returns>IEnumerable{`0}.</returns>
|
2018-09-12 17:26:21 +00:00
|
|
|
protected IEnumerable<IMetadataProvider> GetProviders(BaseItem item, LibraryOptions libraryOptions, MetadataRefreshOptions options, bool isFirstRefresh, bool requiresRefresh)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
// Get providers to refresh
|
2018-09-12 17:26:21 +00:00
|
|
|
var providers = ((ProviderManager)ProviderManager).GetMetadataProviders<TItemType>(item, libraryOptions).ToList();
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2016-11-27 19:36:56 +00:00
|
|
|
var metadataRefreshMode = options.MetadataRefreshMode;
|
2016-04-08 18:32:38 +00:00
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
// Run all if either of these flags are true
|
2016-11-27 19:36:56 +00:00
|
|
|
var runAllProviders = options.ReplaceAllMetadata ||
|
2017-05-26 06:48:54 +00:00
|
|
|
metadataRefreshMode == MetadataRefreshMode.FullRefresh ||
|
2016-11-27 19:36:56 +00:00
|
|
|
(isFirstRefresh && metadataRefreshMode >= MetadataRefreshMode.Default) ||
|
|
|
|
(requiresRefresh && metadataRefreshMode >= MetadataRefreshMode.Default);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
|
|
if (!runAllProviders)
|
|
|
|
{
|
2014-09-11 01:57:11 +00:00
|
|
|
var providersWithChanges = providers
|
|
|
|
.Where(i =>
|
|
|
|
{
|
2019-01-27 09:20:05 +00:00
|
|
|
if (i is IHasItemChangeMonitor hasFileChangeMonitor)
|
2014-09-11 01:57:11 +00:00
|
|
|
{
|
2016-04-08 18:32:38 +00:00
|
|
|
return HasChanged(item, hasFileChangeMonitor, options.DirectoryService);
|
2014-09-11 01:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
})
|
2014-01-28 18:37:01 +00:00
|
|
|
.ToList();
|
|
|
|
|
2014-02-10 18:39:41 +00:00
|
|
|
if (providersWithChanges.Count == 0)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2014-02-10 18:39:41 +00:00
|
|
|
providers = new List<IMetadataProvider<TItemType>>();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-27 19:36:56 +00:00
|
|
|
var anyRemoteProvidersChanged = providersWithChanges.OfType<IRemoteMetadataProvider>()
|
|
|
|
.Any();
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var anyLocalProvidersChanged = providersWithChanges.OfType<ILocalMetadataProvider>()
|
|
|
|
.Any();
|
|
|
|
|
|
|
|
var anyLocalPreRefreshProvidersChanged = providersWithChanges.OfType<IPreRefreshProvider>()
|
|
|
|
.Any();
|
|
|
|
|
2014-02-10 18:39:41 +00:00
|
|
|
providers = providers.Where(i =>
|
|
|
|
{
|
|
|
|
// If any provider reports a change, always run local ones as well
|
|
|
|
if (i is ILocalMetadataProvider)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
return anyRemoteProvidersChanged || anyLocalProvidersChanged || anyLocalPreRefreshProvidersChanged;
|
2014-02-10 18:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If any remote providers changed, run them all so that priorities can be honored
|
|
|
|
if (i is IRemoteMetadataProvider)
|
|
|
|
{
|
2016-11-23 06:54:09 +00:00
|
|
|
if (options.MetadataRefreshMode == MetadataRefreshMode.ValidationOnly)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-10 18:39:41 +00:00
|
|
|
return anyRemoteProvidersChanged;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
// Run custom refresh providers if they report a change or any remote providers change
|
2014-02-10 18:39:41 +00:00
|
|
|
return anyRemoteProvidersChanged || providersWithChanges.Contains(i);
|
|
|
|
}).ToList();
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return providers;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
protected virtual IEnumerable<IImageProvider> GetNonLocalImageProviders(BaseItem item, IEnumerable<IImageProvider> allImageProviders, ImageRefreshOptions options)
|
2014-01-29 17:23:38 +00:00
|
|
|
{
|
|
|
|
// Get providers to refresh
|
2021-08-28 22:32:50 +00:00
|
|
|
var providers = allImageProviders.Where(i => i is not ILocalImageProvider);
|
2014-01-29 17:23:38 +00:00
|
|
|
|
2016-05-21 06:12:00 +00:00
|
|
|
var dateLastImageRefresh = item.DateLastRefreshed;
|
2016-04-08 18:32:38 +00:00
|
|
|
|
2014-01-29 17:23:38 +00:00
|
|
|
// Run all if either of these flags are true
|
2018-09-12 17:26:21 +00:00
|
|
|
var runAllProviders = options.ImageRefreshMode == MetadataRefreshMode.FullRefresh || dateLastImageRefresh == default(DateTime);
|
2014-01-29 17:23:38 +00:00
|
|
|
|
|
|
|
if (!runAllProviders)
|
|
|
|
{
|
2014-09-11 01:57:11 +00:00
|
|
|
providers = providers
|
|
|
|
.Where(i =>
|
|
|
|
{
|
2021-05-23 22:30:41 +00:00
|
|
|
if (i is IHasItemChangeMonitor hasFileChangeMonitor)
|
2014-09-11 01:57:11 +00:00
|
|
|
{
|
2016-04-08 18:32:38 +00:00
|
|
|
return HasChanged(item, hasFileChangeMonitor, options.DirectoryService);
|
2014-09-11 01:57:11 +00:00
|
|
|
}
|
2014-09-27 02:28:13 +00:00
|
|
|
|
2014-09-11 01:57:11 +00:00
|
|
|
return false;
|
2021-05-23 22:30:41 +00:00
|
|
|
});
|
2014-01-29 17:23:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return providers;
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public bool CanRefresh(BaseItem item)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
return item is TItemType;
|
|
|
|
}
|
|
|
|
|
2017-10-06 15:49:22 +00:00
|
|
|
public bool CanRefreshPrimary(Type type)
|
|
|
|
{
|
|
|
|
return type == typeof(TItemType);
|
|
|
|
}
|
|
|
|
|
2020-09-07 11:20:39 +00:00
|
|
|
protected virtual async Task<RefreshResult> RefreshWithProviders(
|
|
|
|
MetadataResult<TItemType> metadata,
|
2015-01-28 21:29:02 +00:00
|
|
|
TIdType id,
|
|
|
|
MetadataRefreshOptions options,
|
2021-06-27 00:00:17 +00:00
|
|
|
ICollection<IMetadataProvider> providers,
|
2015-01-28 21:29:02 +00:00
|
|
|
ItemImageProvider imageService,
|
2014-10-25 18:32:58 +00:00
|
|
|
CancellationToken cancellationToken)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2014-01-29 05:17:58 +00:00
|
|
|
var refreshResult = new RefreshResult
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
UpdateType = ItemUpdateType.None
|
2014-01-29 05:17:58 +00:00
|
|
|
};
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2015-06-29 01:10:45 +00:00
|
|
|
var item = metadata.Item;
|
|
|
|
|
2014-03-01 04:13:22 +00:00
|
|
|
var customProviders = providers.OfType<ICustomMetadataProvider<TItemType>>().ToList();
|
2018-09-12 17:26:21 +00:00
|
|
|
var logName = !item.IsFileProtocol ? item.Name ?? item.Path : item.Path ?? item.Name;
|
2014-03-01 04:13:22 +00:00
|
|
|
|
|
|
|
foreach (var provider in customProviders.Where(i => i is IPreRefreshProvider))
|
|
|
|
{
|
2014-09-28 20:49:43 +00:00
|
|
|
await RunCustomProvider(provider, item, logName, options, refreshResult, cancellationToken).ConfigureAwait(false);
|
2014-03-01 04:13:22 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 01:10:45 +00:00
|
|
|
var temp = new MetadataResult<TItemType>
|
|
|
|
{
|
|
|
|
Item = CreateNew()
|
|
|
|
};
|
|
|
|
temp.Item.Path = item.Path;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2015-06-29 01:10:45 +00:00
|
|
|
var userDataList = new List<UserItemData>();
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
// If replacing all metadata, run internet providers first
|
|
|
|
if (options.ReplaceAllMetadata)
|
|
|
|
{
|
2015-04-24 20:06:37 +00:00
|
|
|
var remoteResult = await ExecuteRemoteProviders(temp, logName, id, providers.OfType<IRemoteMetadataProvider<TItemType, TIdType>>(), cancellationToken)
|
2014-07-09 02:53:04 +00:00
|
|
|
.ConfigureAwait(false);
|
2014-10-28 23:17:55 +00:00
|
|
|
|
2021-03-23 18:25:32 +00:00
|
|
|
refreshResult.UpdateType |= remoteResult.UpdateType;
|
2014-07-09 02:53:04 +00:00
|
|
|
refreshResult.ErrorMessage = remoteResult.ErrorMessage;
|
2015-10-05 03:24:24 +00:00
|
|
|
refreshResult.Failures += remoteResult.Failures;
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var hasLocalMetadata = false;
|
|
|
|
|
2015-04-24 20:06:37 +00:00
|
|
|
foreach (var provider in providers.OfType<ILocalMetadataProvider<TItemType>>().ToList())
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2014-02-17 21:35:08 +00:00
|
|
|
var providerName = provider.GetType().Name;
|
2018-12-13 13:18:25 +00:00
|
|
|
Logger.LogDebug("Running {0} for {1}", providerName, logName);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2015-05-15 15:46:20 +00:00
|
|
|
var itemInfo = new ItemInfo(item);
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
try
|
|
|
|
{
|
2014-10-25 18:32:58 +00:00
|
|
|
var localItem = await provider.GetMetadata(itemInfo, options.DirectoryService, cancellationToken).ConfigureAwait(false);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
|
|
if (localItem.HasMetadata)
|
|
|
|
{
|
2021-02-27 21:46:03 +00:00
|
|
|
foreach (var remoteImage in localItem.RemoteImages)
|
|
|
|
{
|
|
|
|
await ProviderManager.SaveImage(item, remoteImage.url, remoteImage.type, null, cancellationToken).ConfigureAwait(false);
|
2021-03-23 18:25:32 +00:00
|
|
|
refreshResult.UpdateType |= ItemUpdateType.ImageUpdate;
|
2021-02-27 21:46:03 +00:00
|
|
|
}
|
|
|
|
|
2014-02-09 04:52:52 +00:00
|
|
|
if (imageService.MergeImages(item, localItem.Images))
|
|
|
|
{
|
2021-03-23 18:25:32 +00:00
|
|
|
refreshResult.UpdateType |= ItemUpdateType.ImageUpdate;
|
2014-02-09 04:52:52 +00:00
|
|
|
}
|
|
|
|
|
2015-08-02 17:31:08 +00:00
|
|
|
if (localItem.UserDataList != null)
|
|
|
|
{
|
|
|
|
userDataList.AddRange(localItem.UserDataList);
|
|
|
|
}
|
2014-09-27 02:28:13 +00:00
|
|
|
|
2020-07-04 15:54:25 +00:00
|
|
|
MergeData(localItem, temp, Array.Empty<MetadataField>(), !options.ReplaceAllMetadata, true);
|
2021-03-23 18:25:32 +00:00
|
|
|
refreshResult.UpdateType |= ItemUpdateType.MetadataImport;
|
2014-03-12 02:11:01 +00:00
|
|
|
|
|
|
|
// Only one local provider allowed per item
|
2016-11-21 08:54:53 +00:00
|
|
|
if (item.IsLocked || localItem.Item.IsLocked || IsFullLocalMetadata(localItem.Item))
|
2015-02-25 18:11:49 +00:00
|
|
|
{
|
|
|
|
hasLocalMetadata = true;
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2014-03-12 02:11:01 +00:00
|
|
|
break;
|
2014-02-17 21:35:08 +00:00
|
|
|
}
|
2014-03-12 02:11:01 +00:00
|
|
|
|
2018-12-13 13:18:25 +00:00
|
|
|
Logger.LogDebug("{0} returned no metadata for {1}", providerName, logName);
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2021-11-09 12:14:31 +00:00
|
|
|
Logger.LogError(ex, "Error in {Provider}", provider.Name);
|
2014-09-27 02:28:13 +00:00
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
// If a local provider fails, consider that a failure
|
|
|
|
refreshResult.ErrorMessage = ex.Message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-08 20:02:35 +00:00
|
|
|
// Local metadata is king - if any is found don't run remote providers
|
2016-08-14 21:29:35 +00:00
|
|
|
if (!options.ReplaceAllMetadata && (!hasLocalMetadata || options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh || !item.StopRefreshIfLocalMetadataFound))
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2015-04-24 20:06:37 +00:00
|
|
|
var remoteResult = await ExecuteRemoteProviders(temp, logName, id, providers.OfType<IRemoteMetadataProvider<TItemType, TIdType>>(), cancellationToken)
|
2014-07-09 02:53:04 +00:00
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
2021-03-23 18:25:32 +00:00
|
|
|
refreshResult.UpdateType |= remoteResult.UpdateType;
|
2015-10-05 03:24:24 +00:00
|
|
|
refreshResult.ErrorMessage = remoteResult.ErrorMessage;
|
|
|
|
refreshResult.Failures += remoteResult.Failures;
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-28 22:32:50 +00:00
|
|
|
if (providers.Any(i => i is not ICustomMetadataProvider))
|
2014-10-28 23:17:55 +00:00
|
|
|
{
|
2015-03-22 23:24:35 +00:00
|
|
|
if (refreshResult.UpdateType > ItemUpdateType.None)
|
|
|
|
{
|
2016-11-21 08:54:53 +00:00
|
|
|
if (hasLocalMetadata)
|
|
|
|
{
|
|
|
|
MergeData(temp, metadata, item.LockedFields, true, true);
|
|
|
|
}
|
|
|
|
else
|
2015-04-24 20:06:37 +00:00
|
|
|
{
|
|
|
|
// TODO: If the new metadata from above has some blank data, this can cause old data to get filled into those empty fields
|
2020-09-07 11:20:39 +00:00
|
|
|
MergeData(metadata, temp, Array.Empty<MetadataField>(), false, false);
|
2016-11-21 08:54:53 +00:00
|
|
|
MergeData(temp, metadata, item.LockedFields, true, false);
|
2015-04-24 20:06:37 +00:00
|
|
|
}
|
2015-03-22 23:24:35 +00:00
|
|
|
}
|
2014-02-01 06:18:16 +00:00
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2020-06-14 09:11:11 +00:00
|
|
|
// var isUnidentified = failedProviderCount > 0 && successfulProviderCount == 0;
|
2014-07-09 02:53:04 +00:00
|
|
|
|
2021-08-28 22:32:50 +00:00
|
|
|
foreach (var provider in customProviders.Where(i => i is not IPreRefreshProvider))
|
2014-02-02 13:36:31 +00:00
|
|
|
{
|
2014-09-28 20:49:43 +00:00
|
|
|
await RunCustomProvider(provider, item, logName, options, refreshResult, cancellationToken).ConfigureAwait(false);
|
2014-02-02 13:36:31 +00:00
|
|
|
}
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2020-06-14 09:11:11 +00:00
|
|
|
// ImportUserData(item, userDataList, cancellationToken);
|
2014-09-27 02:28:13 +00:00
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
return refreshResult;
|
|
|
|
}
|
|
|
|
|
2015-02-25 18:11:49 +00:00
|
|
|
protected virtual bool IsFullLocalMetadata(TItemType item)
|
|
|
|
{
|
2015-04-19 00:05:36 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(item.Name))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-10 18:28:22 +00:00
|
|
|
|
2015-02-25 18:11:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-28 20:49:43 +00:00
|
|
|
private async Task RunCustomProvider(ICustomMetadataProvider<TItemType> provider, TItemType item, string logName, MetadataRefreshOptions options, RefreshResult refreshResult, CancellationToken cancellationToken)
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
2018-12-13 13:18:25 +00:00
|
|
|
Logger.LogDebug("Running {0} for {1}", provider.GetType().Name, logName);
|
2014-02-04 20:19:29 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-09-16 12:16:44 +00:00
|
|
|
refreshResult.UpdateType |= await provider.FetchAsync(item, options, cancellationToken).ConfigureAwait(false);
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
refreshResult.ErrorMessage = ex.Message;
|
2021-11-09 12:14:31 +00:00
|
|
|
Logger.LogError(ex, "Error in {Provider}", provider.Name);
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-02 13:36:31 +00:00
|
|
|
protected virtual TItemType CreateNew()
|
|
|
|
{
|
|
|
|
return new TItemType();
|
|
|
|
}
|
2014-01-30 21:23:54 +00:00
|
|
|
|
2015-06-29 01:10:45 +00:00
|
|
|
private async Task<RefreshResult> ExecuteRemoteProviders(MetadataResult<TItemType> temp, string logName, TIdType id, IEnumerable<IRemoteMetadataProvider<TItemType, TIdType>> providers, CancellationToken cancellationToken)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
2014-07-09 02:53:04 +00:00
|
|
|
var refreshResult = new RefreshResult();
|
2014-03-03 05:11:03 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var tmpDataMerged = false;
|
2016-05-14 01:55:46 +00:00
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
foreach (var provider in providers)
|
|
|
|
{
|
2014-02-17 21:35:08 +00:00
|
|
|
var providerName = provider.GetType().Name;
|
2018-12-13 13:18:25 +00:00
|
|
|
Logger.LogDebug("Running {0} for {1}", providerName, logName);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (id != null && !tmpDataMerged)
|
2014-02-08 20:02:35 +00:00
|
|
|
{
|
2015-06-29 01:10:45 +00:00
|
|
|
MergeNewData(temp.Item, id);
|
2018-09-12 17:26:21 +00:00
|
|
|
tmpDataMerged = true;
|
2014-02-08 20:02:35 +00:00
|
|
|
}
|
2014-02-07 03:10:13 +00:00
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var result = await provider.GetMetadata(id, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
if (result.HasMetadata)
|
|
|
|
{
|
2017-10-05 18:10:07 +00:00
|
|
|
result.Provider = provider.Name;
|
|
|
|
|
2020-07-04 15:54:25 +00:00
|
|
|
MergeData(result, temp, Array.Empty<MetadataField>(), false, false);
|
2018-09-12 17:26:21 +00:00
|
|
|
MergeNewData(temp.Item, id);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2021-03-23 18:25:32 +00:00
|
|
|
refreshResult.UpdateType |= ItemUpdateType.MetadataDownload;
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
2014-02-17 21:35:08 +00:00
|
|
|
else
|
|
|
|
{
|
2018-12-13 13:18:25 +00:00
|
|
|
Logger.LogDebug("{0} returned no metadata for {1}", providerName, logName);
|
2014-02-17 21:35:08 +00:00
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2014-07-09 02:53:04 +00:00
|
|
|
refreshResult.Failures++;
|
2014-01-28 18:37:01 +00:00
|
|
|
refreshResult.ErrorMessage = ex.Message;
|
2021-11-09 12:14:31 +00:00
|
|
|
Logger.LogError(ex, "Error in {Provider}", provider.Name);
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-03 05:11:03 +00:00
|
|
|
|
2014-07-09 02:53:04 +00:00
|
|
|
return refreshResult;
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
|
2014-02-08 20:02:35 +00:00
|
|
|
private void MergeNewData(TItemType source, TIdType lookupInfo)
|
|
|
|
{
|
|
|
|
// Copy new provider id's that may have been obtained
|
|
|
|
foreach (var providerId in source.ProviderIds)
|
|
|
|
{
|
2014-02-13 05:11:54 +00:00
|
|
|
var key = providerId.Key;
|
|
|
|
|
|
|
|
// Don't replace existing Id's.
|
|
|
|
if (!lookupInfo.ProviderIds.ContainsKey(key))
|
|
|
|
{
|
|
|
|
lookupInfo.ProviderIds[key] = providerId.Value;
|
|
|
|
}
|
2014-02-08 20:02:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 11:20:39 +00:00
|
|
|
protected abstract void MergeData(
|
|
|
|
MetadataResult<TItemType> source,
|
2015-06-29 01:10:45 +00:00
|
|
|
MetadataResult<TItemType> target,
|
2020-06-09 22:12:53 +00:00
|
|
|
MetadataField[] lockedFields,
|
2015-01-28 21:29:02 +00:00
|
|
|
bool replaceData,
|
2014-10-28 23:17:55 +00:00
|
|
|
bool mergeMetadataSettings);
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private bool HasChanged(BaseItem item, IHasItemChangeMonitor changeMonitor, IDirectoryService directoryService)
|
2014-09-11 01:57:11 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2016-04-08 18:32:38 +00:00
|
|
|
var hasChanged = changeMonitor.HasChanged(item, directoryService);
|
2015-11-13 02:49:32 +00:00
|
|
|
|
2020-09-07 11:20:39 +00:00
|
|
|
if (hasChanged)
|
|
|
|
{
|
|
|
|
Logger.LogDebug("{0} reports change to {1}", changeMonitor.GetType().Name, item.Path ?? item.Name);
|
|
|
|
}
|
2015-11-13 02:49:32 +00:00
|
|
|
|
|
|
|
return hasChanged;
|
2014-09-11 01:57:11 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
Logger.LogError(ex, "Error in {0}.HasChanged", changeMonitor.GetType().Name);
|
2014-09-11 01:57:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
}
|
|
|
|
}
|