| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | #pragma warning disable CS1591 |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Linq; |
| | | 8 | | using System.Net.Http; |
| | | 9 | | using System.Threading; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | using Jellyfin.Extensions; |
| | | 12 | | using MediaBrowser.Controller.Configuration; |
| | | 13 | | using MediaBrowser.Controller.Entities; |
| | | 14 | | using MediaBrowser.Controller.Entities.Audio; |
| | | 15 | | using MediaBrowser.Controller.IO; |
| | | 16 | | using MediaBrowser.Controller.Library; |
| | | 17 | | using MediaBrowser.Controller.Persistence; |
| | | 18 | | using MediaBrowser.Controller.Providers; |
| | | 19 | | using MediaBrowser.Model.Configuration; |
| | | 20 | | using MediaBrowser.Model.Entities; |
| | | 21 | | using MediaBrowser.Model.IO; |
| | | 22 | | using MediaBrowser.Model.Providers; |
| | | 23 | | using Microsoft.Extensions.Logging; |
| | | 24 | | |
| | | 25 | | namespace MediaBrowser.Providers.Manager |
| | | 26 | | { |
| | | 27 | | public abstract class MetadataService<TItemType, TIdType> : IMetadataService |
| | | 28 | | where TItemType : BaseItem, IHasLookupInfo<TIdType>, new() |
| | | 29 | | where TIdType : ItemLookupInfo, new() |
| | | 30 | | { |
| | | 31 | | protected MetadataService( |
| | | 32 | | IServerConfigurationManager serverConfigurationManager, |
| | | 33 | | ILogger<MetadataService<TItemType, TIdType>> logger, |
| | | 34 | | IProviderManager providerManager, |
| | | 35 | | IFileSystem fileSystem, |
| | | 36 | | ILibraryManager libraryManager, |
| | | 37 | | IExternalDataManager externalDataManager, |
| | | 38 | | IItemRepository itemRepository) |
| | | 39 | | { |
| | 725 | 40 | | ServerConfigurationManager = serverConfigurationManager; |
| | 725 | 41 | | Logger = logger; |
| | 725 | 42 | | ProviderManager = providerManager; |
| | 725 | 43 | | FileSystem = fileSystem; |
| | 725 | 44 | | LibraryManager = libraryManager; |
| | 725 | 45 | | ExternalDataManager = externalDataManager; |
| | 725 | 46 | | ItemRepository = itemRepository; |
| | 725 | 47 | | ImageProvider = new ItemImageProvider(Logger, ProviderManager, FileSystem); |
| | 725 | 48 | | } |
| | | 49 | | |
| | | 50 | | protected ItemImageProvider ImageProvider { get; } |
| | | 51 | | |
| | | 52 | | protected IServerConfigurationManager ServerConfigurationManager { get; } |
| | | 53 | | |
| | | 54 | | protected ILogger<MetadataService<TItemType, TIdType>> Logger { get; } |
| | | 55 | | |
| | | 56 | | protected IProviderManager ProviderManager { get; } |
| | | 57 | | |
| | | 58 | | protected IFileSystem FileSystem { get; } |
| | | 59 | | |
| | | 60 | | protected ILibraryManager LibraryManager { get; } |
| | | 61 | | |
| | | 62 | | protected IExternalDataManager ExternalDataManager { get; } |
| | | 63 | | |
| | | 64 | | protected IItemRepository ItemRepository { get; } |
| | | 65 | | |
| | 47 | 66 | | protected virtual bool EnableUpdatingPremiereDateFromChildren => false; |
| | | 67 | | |
| | 47 | 68 | | protected virtual bool EnableUpdatingGenresFromChildren => false; |
| | | 69 | | |
| | 47 | 70 | | protected virtual bool EnableUpdatingStudiosFromChildren => false; |
| | | 71 | | |
| | 47 | 72 | | protected virtual bool EnableUpdatingOfficialRatingFromChildren => false; |
| | | 73 | | |
| | 648 | 74 | | public virtual int Order => 0; |
| | | 75 | | |
| | | 76 | | private FileSystemMetadata TryGetFileSystemMetadata(string path, IDirectoryService directoryService) |
| | | 77 | | { |
| | | 78 | | try |
| | | 79 | | { |
| | 47 | 80 | | return directoryService.GetFileSystemEntry(path); |
| | | 81 | | } |
| | 0 | 82 | | catch (Exception ex) |
| | | 83 | | { |
| | 0 | 84 | | Logger.LogError(ex, "Error getting file {Path}", path); |
| | 0 | 85 | | return null; |
| | | 86 | | } |
| | 47 | 87 | | } |
| | | 88 | | |
| | | 89 | | public virtual async Task<ItemUpdateType> RefreshMetadata(BaseItem item, MetadataRefreshOptions refreshOptions, |
| | | 90 | | { |
| | 114 | 91 | | var itemOfType = (TItemType)item; |
| | 114 | 92 | | var updateType = ItemUpdateType.None; |
| | | 93 | | |
| | 114 | 94 | | var libraryOptions = LibraryManager.GetLibraryOptions(item); |
| | 114 | 95 | | var isFirstRefresh = item.DateLastRefreshed == DateTime.MinValue; |
| | 114 | 96 | | var hasRefreshedMetadata = true; |
| | 114 | 97 | | var hasRefreshedImages = true; |
| | | 98 | | |
| | 114 | 99 | | var requiresRefresh = libraryOptions.AutomaticRefreshIntervalDays > 0 && (DateTime.UtcNow - item.DateLastRef |
| | | 100 | | |
| | 114 | 101 | | if (!requiresRefresh && refreshOptions.MetadataRefreshMode != MetadataRefreshMode.None) |
| | | 102 | | { |
| | | 103 | | // TODO: If this returns true, should we instead just change metadata refresh mode to Full? |
| | 114 | 104 | | requiresRefresh = item.RequiresRefresh(); |
| | | 105 | | |
| | 114 | 106 | | if (requiresRefresh) |
| | | 107 | | { |
| | 23 | 108 | | Logger.LogDebug("Refreshing {Type} {Item} because item.RequiresRefresh() returned true", typeof(TIte |
| | | 109 | | } |
| | | 110 | | } |
| | | 111 | | |
| | 114 | 112 | | if (refreshOptions.RemoveOldMetadata && refreshOptions.ReplaceAllImages) |
| | | 113 | | { |
| | 0 | 114 | | if (ImageProvider.RemoveImages(item)) |
| | | 115 | | { |
| | 0 | 116 | | updateType |= ItemUpdateType.ImageUpdate; |
| | | 117 | | } |
| | | 118 | | } |
| | | 119 | | |
| | 114 | 120 | | var localImagesFailed = false; |
| | 114 | 121 | | var allImageProviders = ProviderManager.GetImageProviders(item, refreshOptions).ToList(); |
| | | 122 | | |
| | | 123 | | // Only validate already registered images if we are replacing and saving locally |
| | 114 | 124 | | if (item.IsSaveLocalMetadataEnabled() && refreshOptions.ReplaceAllImages) |
| | | 125 | | { |
| | 0 | 126 | | item.ValidateImages(); |
| | | 127 | | } |
| | | 128 | | else |
| | | 129 | | { |
| | | 130 | | // Run full image validation and register new local images |
| | | 131 | | try |
| | | 132 | | { |
| | 114 | 133 | | if (ImageProvider.ValidateImages(item, allImageProviders.OfType<ILocalImageProvider>(), refreshOptio |
| | | 134 | | { |
| | 0 | 135 | | updateType |= ItemUpdateType.ImageUpdate; |
| | | 136 | | } |
| | 113 | 137 | | } |
| | 1 | 138 | | catch (Exception ex) |
| | | 139 | | { |
| | 1 | 140 | | localImagesFailed = true; |
| | 1 | 141 | | Logger.LogError(ex, "Error validating images for {Item}", item.Path ?? item.Name ?? "Unknown name"); |
| | 1 | 142 | | } |
| | | 143 | | } |
| | | 144 | | |
| | 114 | 145 | | var metadataResult = new MetadataResult<TItemType> |
| | 114 | 146 | | { |
| | 114 | 147 | | Item = itemOfType |
| | 114 | 148 | | }; |
| | | 149 | | |
| | 114 | 150 | | var beforeSaveResult = await BeforeSave(itemOfType, isFirstRefresh || refreshOptions.ReplaceAllMetadata || r |
| | 114 | 151 | | .ConfigureAwait(false); |
| | 114 | 152 | | updateType |= beforeSaveResult; |
| | | 153 | | |
| | 114 | 154 | | if (isFirstRefresh) |
| | | 155 | | { |
| | 46 | 156 | | await SaveItemAsync(metadataResult, ItemUpdateType.MetadataImport, false, cancellationToken).ConfigureAw |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | // Next run metadata providers |
| | 114 | 160 | | if (refreshOptions.MetadataRefreshMode != MetadataRefreshMode.None) |
| | | 161 | | { |
| | 114 | 162 | | var providers = GetProviders(item, libraryOptions, refreshOptions, isFirstRefresh, requiresRefresh) |
| | 114 | 163 | | .ToList(); |
| | | 164 | | |
| | 114 | 165 | | if (providers.Count > 0 || isFirstRefresh || requiresRefresh) |
| | | 166 | | { |
| | 50 | 167 | | if (item.BeforeMetadataRefresh(refreshOptions.ReplaceAllMetadata)) |
| | | 168 | | { |
| | 35 | 169 | | updateType |= ItemUpdateType.MetadataImport; |
| | | 170 | | } |
| | | 171 | | } |
| | | 172 | | |
| | 114 | 173 | | if (providers.Count > 0) |
| | | 174 | | { |
| | 50 | 175 | | var id = itemOfType.GetLookupInfo(); |
| | | 176 | | |
| | 50 | 177 | | if (refreshOptions.SearchResult is not null) |
| | | 178 | | { |
| | 0 | 179 | | ApplySearchResult(id, refreshOptions.SearchResult); |
| | | 180 | | } |
| | | 181 | | |
| | 50 | 182 | | id.IsAutomated = refreshOptions.IsAutomated; |
| | | 183 | | |
| | 50 | 184 | | var hasMetadataSavers = ProviderManager.GetMetadataSavers(item, libraryOptions).Any(); |
| | 50 | 185 | | var result = await RefreshWithProviders(metadataResult, id, refreshOptions, providers, ImageProvider |
| | | 186 | | |
| | 50 | 187 | | updateType |= result.UpdateType; |
| | 50 | 188 | | if (result.Failures > 0) |
| | | 189 | | { |
| | 3 | 190 | | hasRefreshedMetadata = false; |
| | | 191 | | } |
| | | 192 | | } |
| | | 193 | | } |
| | | 194 | | |
| | 114 | 195 | | if (localImagesFailed) |
| | | 196 | | { |
| | 1 | 197 | | hasRefreshedImages = false; |
| | | 198 | | } |
| | 113 | 199 | | else if (refreshOptions.ImageRefreshMode > MetadataRefreshMode.ValidationOnly) |
| | | 200 | | { |
| | | 201 | | // Next run remote image providers, now that local image providers didn't throw |
| | 113 | 202 | | var providers = GetNonLocalImageProviders(item, allImageProviders, refreshOptions).ToList(); |
| | | 203 | | |
| | 113 | 204 | | if (providers.Count > 0) |
| | | 205 | | { |
| | 2 | 206 | | var result = await ImageProvider.RefreshImages(itemOfType, libraryOptions, providers, refreshOptions |
| | | 207 | | |
| | 2 | 208 | | updateType |= result.UpdateType; |
| | 2 | 209 | | if (result.Failures > 0) |
| | | 210 | | { |
| | 1 | 211 | | hasRefreshedImages = false; |
| | | 212 | | } |
| | | 213 | | } |
| | | 214 | | } |
| | | 215 | | |
| | 114 | 216 | | var attemptedFetch = refreshOptions.MetadataRefreshMode > MetadataRefreshMode.ValidationOnly |
| | 114 | 217 | | || refreshOptions.ImageRefreshMode > MetadataRefreshMode.ValidationOnly; |
| | | 218 | | |
| | 114 | 219 | | var refreshStampNeedsSaving = false; |
| | | 220 | | |
| | 114 | 221 | | if (hasRefreshedMetadata && hasRefreshedImages && attemptedFetch) |
| | | 222 | | { |
| | 109 | 223 | | item.DateLastRefreshed = DateTime.UtcNow; |
| | 109 | 224 | | updateType |= item.OnMetadataChanged(); |
| | | 225 | | |
| | | 226 | | // A full refresh queries every provider whether or not anything looks stale. When they all |
| | | 227 | | // come back empty the stamp is the only thing that changed, and without it nothing records |
| | | 228 | | // that the lookup happened, so the next pass repeats the same fruitless queries forever. |
| | 109 | 229 | | refreshStampNeedsSaving = refreshOptions.MetadataRefreshMode == MetadataRefreshMode.FullRefresh |
| | 109 | 230 | | || refreshOptions.ImageRefreshMode == MetadataRefreshMode.FullRefresh; |
| | | 231 | | } |
| | | 232 | | |
| | 114 | 233 | | updateType = await SaveInternal(item, refreshOptions, updateType, isFirstRefresh, requiresRefresh, refreshSt |
| | | 234 | | |
| | 114 | 235 | | await AfterMetadataRefresh(itemOfType, refreshOptions, cancellationToken).ConfigureAwait(false); |
| | | 236 | | |
| | 114 | 237 | | return updateType; |
| | | 238 | | |
| | | 239 | | async Task<ItemUpdateType> SaveInternal(BaseItem item, MetadataRefreshOptions refreshOptions, ItemUpdateType |
| | | 240 | | { |
| | | 241 | | // Save if changes were made, or it's never been saved before |
| | | 242 | | if (refreshOptions.ForceSave || updateType > ItemUpdateType.None || isFirstRefresh || refreshOptions.Rep |
| | | 243 | | { |
| | | 244 | | if (item.IsFileProtocol) |
| | | 245 | | { |
| | | 246 | | var file = TryGetFileSystemMetadata(item.Path, refreshOptions.DirectoryService); |
| | | 247 | | if (file is not null) |
| | | 248 | | { |
| | | 249 | | item.DateModified = file.LastWriteTimeUtc; |
| | | 250 | | |
| | | 251 | | if (!file.IsDirectory) |
| | | 252 | | { |
| | | 253 | | item.Size = file.Length; |
| | | 254 | | } |
| | | 255 | | } |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | // If any of these properties are set then make sure the updateType is not None, just to force every |
| | | 259 | | if (refreshOptions.ForceSave || refreshOptions.ReplaceAllMetadata) |
| | | 260 | | { |
| | | 261 | | updateType |= ItemUpdateType.MetadataDownload; |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | // Save to database |
| | | 265 | | await SaveItemAsync(metadataResult, updateType, isFirstRefresh, cancellationToken).ConfigureAwait(fa |
| | | 266 | | } |
| | | 267 | | |
| | | 268 | | return updateType; |
| | | 269 | | } |
| | 114 | 270 | | } |
| | | 271 | | |
| | | 272 | | private void ApplySearchResult(ItemLookupInfo lookupInfo, RemoteSearchResult result) |
| | | 273 | | { |
| | | 274 | | // Episode and Season do not support Identify, so the search results are the Series' |
| | | 275 | | switch (lookupInfo) |
| | | 276 | | { |
| | | 277 | | case EpisodeInfo episodeInfo: |
| | 0 | 278 | | episodeInfo.SeriesProviderIds = GetValidProviderIds(result.ProviderIds); |
| | 0 | 279 | | episodeInfo.ProviderIds.Clear(); |
| | 0 | 280 | | break; |
| | | 281 | | case SeasonInfo seasonInfo: |
| | 0 | 282 | | seasonInfo.SeriesProviderIds = GetValidProviderIds(result.ProviderIds); |
| | 0 | 283 | | seasonInfo.ProviderIds.Clear(); |
| | 0 | 284 | | break; |
| | | 285 | | default: |
| | 0 | 286 | | lookupInfo.SetProviderIds(result.ProviderIds); |
| | 0 | 287 | | lookupInfo.Name = result.Name; |
| | 0 | 288 | | lookupInfo.Year = result.ProductionYear; |
| | | 289 | | break; |
| | | 290 | | } |
| | 0 | 291 | | } |
| | | 292 | | |
| | | 293 | | private static Dictionary<string, string> GetValidProviderIds(IReadOnlyDictionary<string, string> providerIds) |
| | | 294 | | { |
| | 0 | 295 | | var validProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| | 0 | 296 | | if (providerIds is null) |
| | | 297 | | { |
| | 0 | 298 | | return validProviderIds; |
| | | 299 | | } |
| | | 300 | | |
| | 0 | 301 | | foreach (var (name, value) in providerIds) |
| | | 302 | | { |
| | 0 | 303 | | if (ProviderIdsExtensions.IsValidProviderId(name, value)) |
| | | 304 | | { |
| | 0 | 305 | | validProviderIds[name] = value; |
| | | 306 | | } |
| | | 307 | | } |
| | | 308 | | |
| | 0 | 309 | | return validProviderIds; |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | protected async Task SaveItemAsync(MetadataResult<TItemType> result, ItemUpdateType reason, bool reattachUserDat |
| | | 313 | | { |
| | 96 | 314 | | await result.Item.UpdateToRepositoryAsync(reason, cancellationToken).ConfigureAwait(false); |
| | 96 | 315 | | if (reattachUserData) |
| | | 316 | | { |
| | 46 | 317 | | await result.Item.ReattachUserDataAsync(cancellationToken).ConfigureAwait(false); |
| | | 318 | | } |
| | | 319 | | |
| | 96 | 320 | | if (result.Item.SupportsPeople && result.People is not null) |
| | | 321 | | { |
| | 0 | 322 | | var baseItem = result.Item; |
| | | 323 | | |
| | 0 | 324 | | await LibraryManager.UpdatePeopleAsync(baseItem, result.People, cancellationToken).ConfigureAwait(false) |
| | | 325 | | } |
| | 96 | 326 | | } |
| | | 327 | | |
| | | 328 | | protected virtual Task AfterMetadataRefresh(TItemType item, MetadataRefreshOptions refreshOptions, CancellationT |
| | | 329 | | { |
| | 114 | 330 | | item.AfterMetadataRefresh(); |
| | 114 | 331 | | return Task.CompletedTask; |
| | | 332 | | } |
| | | 333 | | |
| | | 334 | | /// <summary> |
| | | 335 | | /// Before the save. |
| | | 336 | | /// </summary> |
| | | 337 | | /// <param name="item">The item.</param> |
| | | 338 | | /// <param name="isFullRefresh">if set to <c>true</c> [is full refresh].</param> |
| | | 339 | | /// <param name="currentUpdateType">Type of the current update.</param> |
| | | 340 | | /// <returns>ItemUpdateType.</returns> |
| | | 341 | | private async Task<ItemUpdateType> BeforeSave(TItemType item, bool isFullRefresh, ItemUpdateType currentUpdateTy |
| | | 342 | | { |
| | 114 | 343 | | var updateType = BeforeSaveInternal(item, isFullRefresh, currentUpdateType); |
| | | 344 | | |
| | 114 | 345 | | updateType |= item.OnMetadataChanged(); |
| | | 346 | | |
| | 114 | 347 | | if (updateType == ItemUpdateType.None) |
| | | 348 | | { |
| | 68 | 349 | | if (!await ItemRepository.ItemExistsAsync(item.Id).ConfigureAwait(false)) |
| | | 350 | | { |
| | 0 | 351 | | return ItemUpdateType.MetadataImport; |
| | | 352 | | } |
| | | 353 | | } |
| | | 354 | | |
| | 114 | 355 | | return updateType; |
| | 114 | 356 | | } |
| | | 357 | | |
| | | 358 | | protected virtual ItemUpdateType BeforeSaveInternal(TItemType item, bool isFullRefresh, ItemUpdateType updateTyp |
| | | 359 | | { |
| | 120 | 360 | | if (EnableUpdateMetadataFromChildren(item, isFullRefresh, updateType)) |
| | | 361 | | { |
| | 0 | 362 | | var children = GetChildrenForMetadataUpdates(item); |
| | 0 | 363 | | updateType = UpdateMetadataFromChildren(item, children, isFullRefresh, updateType); |
| | | 364 | | } |
| | | 365 | | |
| | 120 | 366 | | var presentationUniqueKey = item.CreatePresentationUniqueKey(); |
| | 120 | 367 | | if (!string.Equals(item.PresentationUniqueKey, presentationUniqueKey, StringComparison.Ordinal)) |
| | | 368 | | { |
| | 52 | 369 | | item.PresentationUniqueKey = presentationUniqueKey; |
| | 52 | 370 | | updateType |= ItemUpdateType.MetadataImport; |
| | | 371 | | } |
| | | 372 | | |
| | | 373 | | // Cleanup extracted files if source file was modified |
| | 120 | 374 | | var itemPath = item.Path; |
| | 120 | 375 | | if (!string.IsNullOrEmpty(itemPath)) |
| | | 376 | | { |
| | 107 | 377 | | var info = FileSystem.GetFileSystemInfo(itemPath); |
| | 107 | 378 | | if (info.Exists && item.HasChanged(info.LastWriteTimeUtc)) |
| | | 379 | | { |
| | 5 | 380 | | Logger.LogDebug("File modification time changed from {Then} to {Now}: {Path}", item.DateModified, in |
| | | 381 | | |
| | 5 | 382 | | item.DateModified = info.LastWriteTimeUtc; |
| | 5 | 383 | | if (ServerConfigurationManager.GetMetadataConfiguration().UseFileCreationTimeForDateAdded) |
| | | 384 | | { |
| | 5 | 385 | | if (info.CreationTimeUtc > DateTime.MinValue) |
| | | 386 | | { |
| | 0 | 387 | | item.DateCreated = info.CreationTimeUtc; |
| | | 388 | | } |
| | | 389 | | } |
| | | 390 | | |
| | 5 | 391 | | if (item is Video video) |
| | | 392 | | { |
| | 0 | 393 | | Logger.LogInformation("File changed, pruning extracted data: {Path}", item.Path); |
| | 0 | 394 | | ExternalDataManager.DeleteExternalItemDataAsync(video, CancellationToken.None).GetAwaiter().GetR |
| | | 395 | | } |
| | | 396 | | |
| | 5 | 397 | | updateType |= ItemUpdateType.MetadataImport; |
| | | 398 | | } |
| | | 399 | | } |
| | | 400 | | |
| | 120 | 401 | | return updateType; |
| | | 402 | | } |
| | | 403 | | |
| | | 404 | | protected virtual bool EnableUpdateMetadataFromChildren(TItemType item, bool isFullRefresh, ItemUpdateType curre |
| | | 405 | | { |
| | 120 | 406 | | if (item is Folder folder) |
| | | 407 | | { |
| | 107 | 408 | | if (!isFullRefresh && currentUpdateType == ItemUpdateType.None) |
| | | 409 | | { |
| | 60 | 410 | | return folder.SupportsDateLastMediaAdded; |
| | | 411 | | } |
| | | 412 | | |
| | 47 | 413 | | if (isFullRefresh || currentUpdateType > ItemUpdateType.None) |
| | | 414 | | { |
| | 47 | 415 | | if (EnableUpdatingPremiereDateFromChildren || EnableUpdatingGenresFromChildren || EnableUpdatingStud |
| | | 416 | | { |
| | 0 | 417 | | return true; |
| | | 418 | | } |
| | | 419 | | |
| | 47 | 420 | | if (folder.SupportsDateLastMediaAdded || folder.SupportsCumulativeRunTimeTicks) |
| | | 421 | | { |
| | 0 | 422 | | return true; |
| | | 423 | | } |
| | | 424 | | } |
| | | 425 | | } |
| | | 426 | | |
| | 60 | 427 | | return false; |
| | | 428 | | } |
| | | 429 | | |
| | | 430 | | protected virtual IReadOnlyList<BaseItem> GetChildrenForMetadataUpdates(TItemType item) |
| | | 431 | | { |
| | 0 | 432 | | if (item is Folder folder) |
| | | 433 | | { |
| | 0 | 434 | | return folder.GetRecursiveChildren(); |
| | | 435 | | } |
| | | 436 | | |
| | 0 | 437 | | return []; |
| | | 438 | | } |
| | | 439 | | |
| | | 440 | | protected virtual ItemUpdateType UpdateMetadataFromChildren(TItemType item, IReadOnlyList<BaseItem> children, bo |
| | | 441 | | { |
| | 0 | 442 | | var updateType = ItemUpdateType.None; |
| | | 443 | | |
| | 0 | 444 | | if (item is Folder folder) |
| | | 445 | | { |
| | 0 | 446 | | if (folder.SupportsDateLastMediaAdded) |
| | | 447 | | { |
| | 0 | 448 | | updateType |= UpdateDateLastMediaAdded(item, children); |
| | | 449 | | } |
| | | 450 | | |
| | 0 | 451 | | if ((isFullRefresh || currentUpdateType > ItemUpdateType.None) && folder.SupportsCumulativeRunTimeTicks) |
| | | 452 | | { |
| | 0 | 453 | | updateType |= UpdateCumulativeRunTimeTicks(item, children); |
| | | 454 | | } |
| | | 455 | | } |
| | | 456 | | |
| | 0 | 457 | | if (!(isFullRefresh || currentUpdateType > ItemUpdateType.None) || item.IsLocked) |
| | | 458 | | { |
| | 0 | 459 | | return updateType; |
| | | 460 | | } |
| | | 461 | | |
| | 0 | 462 | | if (EnableUpdatingPremiereDateFromChildren) |
| | | 463 | | { |
| | 0 | 464 | | updateType |= UpdatePremiereDate(item, children); |
| | | 465 | | } |
| | | 466 | | |
| | 0 | 467 | | if (EnableUpdatingGenresFromChildren) |
| | | 468 | | { |
| | 0 | 469 | | updateType |= UpdateGenres(item, children); |
| | | 470 | | } |
| | | 471 | | |
| | 0 | 472 | | if (EnableUpdatingStudiosFromChildren) |
| | | 473 | | { |
| | 0 | 474 | | updateType |= UpdateStudios(item, children); |
| | | 475 | | } |
| | | 476 | | |
| | 0 | 477 | | if (EnableUpdatingOfficialRatingFromChildren) |
| | | 478 | | { |
| | 0 | 479 | | updateType |= UpdateOfficialRating(item, children); |
| | | 480 | | } |
| | | 481 | | |
| | 0 | 482 | | return updateType; |
| | | 483 | | } |
| | | 484 | | |
| | | 485 | | private ItemUpdateType UpdateCumulativeRunTimeTicks(TItemType item, IReadOnlyList<BaseItem> children) |
| | | 486 | | { |
| | 0 | 487 | | if (item is Folder folder && folder.SupportsCumulativeRunTimeTicks) |
| | | 488 | | { |
| | 0 | 489 | | long ticks = 0; |
| | | 490 | | |
| | 0 | 491 | | foreach (var child in children) |
| | | 492 | | { |
| | 0 | 493 | | if (!child.IsFolder) |
| | | 494 | | { |
| | 0 | 495 | | ticks += child.RunTimeTicks ?? 0; |
| | | 496 | | } |
| | | 497 | | } |
| | | 498 | | |
| | 0 | 499 | | if (!folder.RunTimeTicks.HasValue || folder.RunTimeTicks.Value != ticks) |
| | | 500 | | { |
| | 0 | 501 | | folder.RunTimeTicks = ticks; |
| | 0 | 502 | | return ItemUpdateType.MetadataImport; |
| | | 503 | | } |
| | | 504 | | } |
| | | 505 | | |
| | 0 | 506 | | return ItemUpdateType.None; |
| | | 507 | | } |
| | | 508 | | |
| | | 509 | | private ItemUpdateType UpdateDateLastMediaAdded(TItemType item, IReadOnlyList<BaseItem> children) |
| | | 510 | | { |
| | 0 | 511 | | var updateType = ItemUpdateType.None; |
| | | 512 | | |
| | 0 | 513 | | if (item is Folder folder && folder.SupportsDateLastMediaAdded) |
| | | 514 | | { |
| | 0 | 515 | | var dateLastMediaAdded = DateTime.MinValue; |
| | 0 | 516 | | var any = false; |
| | | 517 | | |
| | 0 | 518 | | foreach (var child in children) |
| | | 519 | | { |
| | | 520 | | // Exclude any folders and virtual items since they are only placeholders |
| | 0 | 521 | | if (!child.IsFolder && !child.IsVirtualItem) |
| | | 522 | | { |
| | 0 | 523 | | var childDateCreated = child.DateCreated; |
| | 0 | 524 | | if (childDateCreated > dateLastMediaAdded) |
| | | 525 | | { |
| | 0 | 526 | | dateLastMediaAdded = childDateCreated; |
| | | 527 | | } |
| | | 528 | | |
| | 0 | 529 | | any = true; |
| | | 530 | | } |
| | | 531 | | } |
| | | 532 | | |
| | 0 | 533 | | if ((!folder.DateLastMediaAdded.HasValue && any) || folder.DateLastMediaAdded != dateLastMediaAdded) |
| | | 534 | | { |
| | 0 | 535 | | folder.DateLastMediaAdded = dateLastMediaAdded; |
| | 0 | 536 | | updateType = ItemUpdateType.MetadataImport; |
| | | 537 | | } |
| | | 538 | | } |
| | | 539 | | |
| | 0 | 540 | | return updateType; |
| | | 541 | | } |
| | | 542 | | |
| | | 543 | | private ItemUpdateType UpdatePremiereDate(TItemType item, IReadOnlyList<BaseItem> children) |
| | | 544 | | { |
| | 0 | 545 | | var updateType = ItemUpdateType.None; |
| | | 546 | | |
| | 0 | 547 | | if (children.Count == 0) |
| | | 548 | | { |
| | 0 | 549 | | return updateType; |
| | | 550 | | } |
| | | 551 | | |
| | 0 | 552 | | var date = children.Select(i => i.PremiereDate ?? DateTime.MaxValue).Min(); |
| | | 553 | | |
| | 0 | 554 | | var originalPremiereDate = item.PremiereDate; |
| | 0 | 555 | | var originalProductionYear = item.ProductionYear; |
| | | 556 | | |
| | 0 | 557 | | if (date > DateTime.MinValue && date < DateTime.MaxValue) |
| | | 558 | | { |
| | 0 | 559 | | item.PremiereDate = date; |
| | 0 | 560 | | item.ProductionYear = date.Year; |
| | | 561 | | } |
| | | 562 | | else |
| | | 563 | | { |
| | 0 | 564 | | var year = children.Select(i => i.ProductionYear ?? 0).Min(); |
| | | 565 | | |
| | 0 | 566 | | if (year > 0) |
| | | 567 | | { |
| | 0 | 568 | | item.ProductionYear = year; |
| | | 569 | | } |
| | | 570 | | } |
| | | 571 | | |
| | 0 | 572 | | if ((originalPremiereDate ?? DateTime.MinValue) != (item.PremiereDate ?? DateTime.MinValue) |
| | 0 | 573 | | || (originalProductionYear ?? -1) != (item.ProductionYear ?? -1)) |
| | | 574 | | { |
| | 0 | 575 | | updateType |= ItemUpdateType.MetadataEdit; |
| | | 576 | | } |
| | | 577 | | |
| | 0 | 578 | | return updateType; |
| | | 579 | | } |
| | | 580 | | |
| | | 581 | | private ItemUpdateType UpdateGenres(TItemType item, IReadOnlyList<BaseItem> children) |
| | | 582 | | { |
| | 0 | 583 | | var updateType = ItemUpdateType.None; |
| | | 584 | | |
| | 0 | 585 | | if (!item.LockedFields.Contains(MetadataField.Genres)) |
| | | 586 | | { |
| | 0 | 587 | | var currentList = item.Genres; |
| | | 588 | | |
| | 0 | 589 | | item.Genres = children.SelectMany(i => i.Genres) |
| | 0 | 590 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 0 | 591 | | .ToArray(); |
| | | 592 | | |
| | 0 | 593 | | if (currentList.Length != item.Genres.Length || !currentList.Order().SequenceEqual(item.Genres.Order(), |
| | | 594 | | { |
| | 0 | 595 | | updateType |= ItemUpdateType.MetadataEdit; |
| | | 596 | | } |
| | | 597 | | } |
| | | 598 | | |
| | 0 | 599 | | return updateType; |
| | | 600 | | } |
| | | 601 | | |
| | | 602 | | private ItemUpdateType UpdateStudios(TItemType item, IReadOnlyList<BaseItem> children) |
| | | 603 | | { |
| | 0 | 604 | | var updateType = ItemUpdateType.None; |
| | | 605 | | |
| | 0 | 606 | | if (!item.LockedFields.Contains(MetadataField.Studios)) |
| | | 607 | | { |
| | 0 | 608 | | var currentList = item.Studios; |
| | | 609 | | |
| | 0 | 610 | | item.Studios = children.SelectMany(i => i.Studios) |
| | 0 | 611 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 0 | 612 | | .ToArray(); |
| | | 613 | | |
| | 0 | 614 | | if (currentList.Length != item.Studios.Length || !currentList.Order().SequenceEqual(item.Studios.Order() |
| | | 615 | | { |
| | 0 | 616 | | updateType |= ItemUpdateType.MetadataEdit; |
| | | 617 | | } |
| | | 618 | | } |
| | | 619 | | |
| | 0 | 620 | | return updateType; |
| | | 621 | | } |
| | | 622 | | |
| | | 623 | | private ItemUpdateType UpdateOfficialRating(TItemType item, IReadOnlyList<BaseItem> children) |
| | | 624 | | { |
| | 0 | 625 | | var updateType = ItemUpdateType.None; |
| | | 626 | | |
| | 0 | 627 | | if (!item.LockedFields.Contains(MetadataField.OfficialRating)) |
| | | 628 | | { |
| | 0 | 629 | | if (item.UpdateRatingToItems(children)) |
| | | 630 | | { |
| | 0 | 631 | | updateType |= ItemUpdateType.MetadataEdit; |
| | | 632 | | } |
| | | 633 | | } |
| | | 634 | | |
| | 0 | 635 | | return updateType; |
| | | 636 | | } |
| | | 637 | | |
| | | 638 | | /// <summary> |
| | | 639 | | /// Gets the providers. |
| | | 640 | | /// </summary> |
| | | 641 | | /// <param name="item">A media item.</param> |
| | | 642 | | /// <param name="libraryOptions">The LibraryOptions to use.</param> |
| | | 643 | | /// <param name="options">The MetadataRefreshOptions to use.</param> |
| | | 644 | | /// <param name="isFirstRefresh">Specifies first refresh mode.</param> |
| | | 645 | | /// <param name="requiresRefresh">Specifies refresh mode.</param> |
| | | 646 | | /// <returns>IEnumerable{`0}.</returns> |
| | | 647 | | protected IEnumerable<IMetadataProvider> GetProviders(BaseItem item, LibraryOptions libraryOptions, MetadataRefr |
| | | 648 | | { |
| | | 649 | | // Get providers to refresh |
| | 114 | 650 | | var providers = ProviderManager.GetMetadataProviders<TItemType>(item, libraryOptions).ToList(); |
| | | 651 | | |
| | 114 | 652 | | var metadataRefreshMode = options.MetadataRefreshMode; |
| | | 653 | | |
| | | 654 | | // Run all if either of these flags are true |
| | 114 | 655 | | var runAllProviders = options.ReplaceAllMetadata || |
| | 114 | 656 | | metadataRefreshMode == MetadataRefreshMode.FullRefresh || |
| | 114 | 657 | | (isFirstRefresh && metadataRefreshMode >= MetadataRefreshMode.Default) || |
| | 114 | 658 | | (requiresRefresh && metadataRefreshMode >= MetadataRefreshMode.Default); |
| | | 659 | | |
| | 114 | 660 | | if (!runAllProviders) |
| | | 661 | | { |
| | 61 | 662 | | var providersWithChanges = providers |
| | 61 | 663 | | .Where(i => |
| | 61 | 664 | | { |
| | 61 | 665 | | if (i is IHasItemChangeMonitor hasFileChangeMonitor) |
| | 61 | 666 | | { |
| | 61 | 667 | | return HasChanged(item, hasFileChangeMonitor, options.DirectoryService); |
| | 61 | 668 | | } |
| | 61 | 669 | | |
| | 61 | 670 | | return false; |
| | 61 | 671 | | }) |
| | 61 | 672 | | .ToList(); |
| | | 673 | | |
| | 61 | 674 | | if (providersWithChanges.Count == 0) |
| | | 675 | | { |
| | 61 | 676 | | providers = new List<IMetadataProvider<TItemType>>(); |
| | | 677 | | } |
| | | 678 | | else |
| | | 679 | | { |
| | 0 | 680 | | var anyRemoteProvidersChanged = providersWithChanges.OfType<IRemoteMetadataProvider>() |
| | 0 | 681 | | .Any(); |
| | | 682 | | |
| | 0 | 683 | | var anyLocalProvidersChanged = providersWithChanges.OfType<ILocalMetadataProvider>() |
| | 0 | 684 | | .Any(); |
| | | 685 | | |
| | 0 | 686 | | var anyLocalPreRefreshProvidersChanged = providersWithChanges.OfType<IPreRefreshProvider>() |
| | 0 | 687 | | .Any(); |
| | | 688 | | |
| | 0 | 689 | | providers = providers.Where(i => |
| | 0 | 690 | | { |
| | 0 | 691 | | // If any provider reports a change, always run local ones as well |
| | 0 | 692 | | if (i is ILocalMetadataProvider) |
| | 0 | 693 | | { |
| | 0 | 694 | | return anyRemoteProvidersChanged || anyLocalProvidersChanged || anyLocalPreRefreshProvidersC |
| | 0 | 695 | | } |
| | 0 | 696 | | |
| | 0 | 697 | | // If any remote providers changed, run them all so that priorities can be honored |
| | 0 | 698 | | if (i is IRemoteMetadataProvider) |
| | 0 | 699 | | { |
| | 0 | 700 | | if (options.MetadataRefreshMode == MetadataRefreshMode.ValidationOnly) |
| | 0 | 701 | | { |
| | 0 | 702 | | return false; |
| | 0 | 703 | | } |
| | 0 | 704 | | |
| | 0 | 705 | | return anyRemoteProvidersChanged; |
| | 0 | 706 | | } |
| | 0 | 707 | | |
| | 0 | 708 | | // Run custom refresh providers if they report a change or any remote providers change |
| | 0 | 709 | | return anyRemoteProvidersChanged || providersWithChanges.Contains(i); |
| | 0 | 710 | | }).ToList(); |
| | | 711 | | } |
| | | 712 | | } |
| | | 713 | | |
| | 114 | 714 | | return providers; |
| | | 715 | | } |
| | | 716 | | |
| | | 717 | | protected virtual IEnumerable<IImageProvider> GetNonLocalImageProviders(BaseItem item, IEnumerable<IImageProvide |
| | | 718 | | { |
| | | 719 | | // Get providers to refresh |
| | 113 | 720 | | var providers = allImageProviders.Where(i => i is not ILocalImageProvider); |
| | | 721 | | |
| | | 722 | | // When identifying, run the provider the user picked first so the correct image is used. |
| | 113 | 723 | | if (!string.IsNullOrEmpty(options.SearchResult?.SearchProviderName)) |
| | | 724 | | { |
| | 0 | 725 | | providers = providers |
| | 0 | 726 | | .OrderBy(i => string.Equals(i.Name, options.SearchResult.SearchProviderName, StringComparison.Ordina |
| | | 727 | | } |
| | | 728 | | |
| | 113 | 729 | | var dateLastImageRefresh = item.DateLastRefreshed; |
| | | 730 | | |
| | | 731 | | // Run all if either of these flags are true |
| | 113 | 732 | | var runAllProviders = options.ImageRefreshMode == MetadataRefreshMode.FullRefresh || dateLastImageRefresh.Da |
| | | 733 | | |
| | 113 | 734 | | if (!runAllProviders) |
| | | 735 | | { |
| | 62 | 736 | | providers = providers |
| | 62 | 737 | | .Where(i => |
| | 62 | 738 | | { |
| | 62 | 739 | | if (i is IHasItemChangeMonitor hasFileChangeMonitor) |
| | 62 | 740 | | { |
| | 62 | 741 | | return HasChanged(item, hasFileChangeMonitor, options.DirectoryService); |
| | 62 | 742 | | } |
| | 62 | 743 | | |
| | 62 | 744 | | return false; |
| | 62 | 745 | | }); |
| | | 746 | | } |
| | | 747 | | |
| | 113 | 748 | | return providers; |
| | | 749 | | } |
| | | 750 | | |
| | | 751 | | public bool CanRefresh(BaseItem item) |
| | | 752 | | { |
| | 1508 | 753 | | return item is TItemType; |
| | | 754 | | } |
| | | 755 | | |
| | | 756 | | public bool CanRefreshPrimary(Type type) |
| | | 757 | | { |
| | 2444 | 758 | | return type == typeof(TItemType); |
| | | 759 | | } |
| | | 760 | | |
| | | 761 | | protected virtual async Task<RefreshResult> RefreshWithProviders( |
| | | 762 | | MetadataResult<TItemType> metadata, |
| | | 763 | | TIdType id, |
| | | 764 | | MetadataRefreshOptions options, |
| | | 765 | | ICollection<IMetadataProvider> providers, |
| | | 766 | | ItemImageProvider imageService, |
| | | 767 | | bool isSavingMetadata, |
| | | 768 | | CancellationToken cancellationToken) |
| | | 769 | | { |
| | 57 | 770 | | var refreshResult = new RefreshResult |
| | 57 | 771 | | { |
| | 57 | 772 | | UpdateType = ItemUpdateType.None |
| | 57 | 773 | | }; |
| | | 774 | | |
| | 57 | 775 | | var item = metadata.Item; |
| | | 776 | | |
| | 57 | 777 | | var customProviders = providers.OfType<ICustomMetadataProvider<TItemType>>().ToList(); |
| | 57 | 778 | | var logName = !item.IsFileProtocol ? item.Name ?? item.Path : item.Path ?? item.Name; |
| | | 779 | | |
| | 114 | 780 | | foreach (var provider in customProviders.Where(i => i is IPreRefreshProvider)) |
| | | 781 | | { |
| | 0 | 782 | | await RunCustomProvider(provider, item, logName, options, refreshResult, cancellationToken).ConfigureAwa |
| | | 783 | | } |
| | | 784 | | |
| | 57 | 785 | | if (item.IsLocked) |
| | | 786 | | { |
| | 0 | 787 | | return refreshResult; |
| | | 788 | | } |
| | | 789 | | |
| | 57 | 790 | | var temp = new MetadataResult<TItemType> |
| | 57 | 791 | | { |
| | 57 | 792 | | Item = CreateNew() |
| | 57 | 793 | | }; |
| | 57 | 794 | | temp.Item.Path = item.Path; |
| | 57 | 795 | | temp.Item.Id = item.Id; |
| | 57 | 796 | | temp.Item.ParentIndexNumber = item.ParentIndexNumber; |
| | 57 | 797 | | temp.Item.PreferredMetadataCountryCode = item.PreferredMetadataCountryCode; |
| | 57 | 798 | | temp.Item.PreferredMetadataLanguage = item.PreferredMetadataLanguage; |
| | | 799 | | |
| | 57 | 800 | | var foundImageTypes = new List<ImageType>(); |
| | | 801 | | |
| | | 802 | | // Do not execute local providers if we are identifying or replacing with local metadata saving enabled |
| | 57 | 803 | | if (options.SearchResult is null && !(isSavingMetadata && options.ReplaceAllMetadata)) |
| | | 804 | | { |
| | 115 | 805 | | foreach (var provider in providers.OfType<ILocalMetadataProvider<TItemType>>()) |
| | | 806 | | { |
| | 1 | 807 | | var providerName = provider.GetType().Name; |
| | 1 | 808 | | Logger.LogDebug("Running {Provider} for {Item}", providerName, logName); |
| | | 809 | | |
| | 1 | 810 | | var itemInfo = new ItemInfo(item); |
| | | 811 | | |
| | | 812 | | try |
| | | 813 | | { |
| | 1 | 814 | | var localItem = await provider.GetMetadata(itemInfo, options.DirectoryService, cancellationToken |
| | | 815 | | |
| | 1 | 816 | | if (localItem.HasMetadata) |
| | | 817 | | { |
| | 2 | 818 | | foreach (var remoteImage in localItem.RemoteImages) |
| | | 819 | | { |
| | | 820 | | try |
| | | 821 | | { |
| | 0 | 822 | | if (item.ImageInfos.Any(x => x.Type == remoteImage.Type) |
| | 0 | 823 | | && !options.IsReplacingImage(remoteImage.Type)) |
| | | 824 | | { |
| | 0 | 825 | | continue; |
| | | 826 | | } |
| | | 827 | | |
| | 0 | 828 | | await ProviderManager.SaveImage(item, remoteImage.Url, remoteImage.Type, null, cance |
| | 0 | 829 | | refreshResult.UpdateType |= ItemUpdateType.ImageUpdate; |
| | | 830 | | |
| | | 831 | | // remember imagetype that has just been downloaded |
| | 0 | 832 | | foundImageTypes.Add(remoteImage.Type); |
| | 0 | 833 | | } |
| | 0 | 834 | | catch (HttpRequestException ex) |
| | | 835 | | { |
| | 0 | 836 | | Logger.LogError(ex, "Could not save {ImageType} image: {Url}", Enum.GetName(remoteIm |
| | 0 | 837 | | } |
| | 0 | 838 | | } |
| | | 839 | | |
| | 1 | 840 | | if (foundImageTypes.Count > 0) |
| | | 841 | | { |
| | 0 | 842 | | imageService.UpdateReplaceImages(options, foundImageTypes); |
| | | 843 | | } |
| | | 844 | | |
| | 1 | 845 | | if (imageService.MergeImages(item, localItem.Images, options)) |
| | | 846 | | { |
| | 0 | 847 | | refreshResult.UpdateType |= ItemUpdateType.ImageUpdate; |
| | | 848 | | } |
| | | 849 | | |
| | 1 | 850 | | MergeData(localItem, temp, [], false, true); |
| | 1 | 851 | | refreshResult.UpdateType |= ItemUpdateType.MetadataImport; |
| | | 852 | | |
| | 1 | 853 | | break; |
| | | 854 | | } |
| | | 855 | | |
| | 0 | 856 | | Logger.LogDebug("{Provider} returned no metadata for {Item}", providerName, logName); |
| | 0 | 857 | | } |
| | 0 | 858 | | catch (OperationCanceledException) |
| | | 859 | | { |
| | 0 | 860 | | throw; |
| | | 861 | | } |
| | 0 | 862 | | catch (Exception ex) |
| | | 863 | | { |
| | 0 | 864 | | Logger.LogError(ex, "Error in {Provider} for {Item}", provider.Name, logName); |
| | | 865 | | |
| | | 866 | | // If a local provider fails, consider that a failure |
| | 0 | 867 | | refreshResult.ErrorMessage = ex.Message; |
| | 0 | 868 | | } |
| | 0 | 869 | | } |
| | | 870 | | } |
| | | 871 | | |
| | 57 | 872 | | var hasRemoteMetadata = false; |
| | 57 | 873 | | var isLocalLocked = temp.Item.IsLocked; |
| | 57 | 874 | | if (!isLocalLocked && (options.ReplaceAllMetadata || options.MetadataRefreshMode > MetadataRefreshMode.Valid |
| | | 875 | | { |
| | 57 | 876 | | var remoteProviders = providers.OfType<IRemoteMetadataProvider<TItemType, TIdType>>(); |
| | | 877 | | |
| | | 878 | | // When identifying, run the provider the user picked first so the correct IDs are used. |
| | 57 | 879 | | if (!string.IsNullOrEmpty(options.SearchResult?.SearchProviderName)) |
| | | 880 | | { |
| | 0 | 881 | | remoteProviders = remoteProviders |
| | 0 | 882 | | .OrderBy(i => string.Equals(i.Name, options.SearchResult.SearchProviderName, StringComparison.Or |
| | | 883 | | } |
| | | 884 | | |
| | 57 | 885 | | var remoteResult = await ExecuteRemoteProviders(temp, logName, false, id, remoteProviders, cancellationT |
| | | 886 | | |
| | 57 | 887 | | hasRemoteMetadata = remoteResult.UpdateType.HasFlag(ItemUpdateType.MetadataDownload); |
| | 57 | 888 | | refreshResult.UpdateType |= remoteResult.UpdateType; |
| | 57 | 889 | | refreshResult.ErrorMessage = remoteResult.ErrorMessage; |
| | 57 | 890 | | refreshResult.Failures += remoteResult.Failures; |
| | | 891 | | } |
| | | 892 | | |
| | 57 | 893 | | if (providers.Any(i => i is not ICustomMetadataProvider)) |
| | | 894 | | { |
| | 8 | 895 | | if (refreshResult.UpdateType > ItemUpdateType.None) |
| | | 896 | | { |
| | | 897 | | // Erasing the old values is only safe when a remote provider returned something to |
| | | 898 | | // replace them with. If every one of them failed there is no replacement, and wiping the |
| | | 899 | | // item would turn a provider being temporarily unreachable into permanent data loss. |
| | | 900 | | // A single failure is not enough: Identify asks for the erasure precisely because the |
| | | 901 | | // previous match was wrong, and an unrelated provider throwing must not undo that. |
| | 7 | 902 | | if (!options.RemoveOldMetadata || (refreshResult.Failures > 0 && !hasRemoteMetadata)) |
| | | 903 | | { |
| | | 904 | | // Add existing metadata to provider result if it does not exist there |
| | 5 | 905 | | MergeData(metadata, temp, [], false, false); |
| | | 906 | | } |
| | | 907 | | |
| | 7 | 908 | | if (isLocalLocked) |
| | | 909 | | { |
| | 0 | 910 | | MergeData(temp, metadata, item.LockedFields, true, true); |
| | | 911 | | } |
| | | 912 | | else |
| | | 913 | | { |
| | 7 | 914 | | var shouldReplace = (options.MetadataRefreshMode > MetadataRefreshMode.ValidationOnly && options |
| | 7 | 915 | | // Case for Scan for new and updated files |
| | 7 | 916 | | || (options.MetadataRefreshMode == MetadataRefreshMode.Default && !options.ReplaceAllMetadat |
| | 7 | 917 | | MergeData(temp, metadata, item.LockedFields, shouldReplace, true); |
| | | 918 | | } |
| | | 919 | | } |
| | | 920 | | } |
| | | 921 | | |
| | 212 | 922 | | foreach (var provider in customProviders.Where(i => i is not IPreRefreshProvider)) |
| | | 923 | | { |
| | 49 | 924 | | await RunCustomProvider(provider, item, logName, options, refreshResult, cancellationToken).ConfigureAwa |
| | | 925 | | } |
| | | 926 | | |
| | 57 | 927 | | return refreshResult; |
| | 57 | 928 | | } |
| | | 929 | | |
| | | 930 | | private async Task RunCustomProvider(ICustomMetadataProvider<TItemType> provider, TItemType item, string logName |
| | | 931 | | { |
| | 49 | 932 | | Logger.LogDebug("Running {Provider} for {Item}", provider.GetType().Name, logName); |
| | | 933 | | |
| | | 934 | | try |
| | | 935 | | { |
| | 49 | 936 | | refreshResult.UpdateType |= await provider.FetchAsync(item, options, cancellationToken).ConfigureAwait(f |
| | 46 | 937 | | } |
| | 0 | 938 | | catch (OperationCanceledException) |
| | | 939 | | { |
| | 0 | 940 | | throw; |
| | | 941 | | } |
| | 3 | 942 | | catch (Exception ex) |
| | | 943 | | { |
| | 3 | 944 | | refreshResult.Failures++; |
| | 3 | 945 | | refreshResult.ErrorMessage = ex.Message; |
| | 3 | 946 | | Logger.LogError(ex, "Error in {Provider} for {Item}", provider.Name, logName); |
| | 3 | 947 | | } |
| | 49 | 948 | | } |
| | | 949 | | |
| | | 950 | | protected virtual TItemType CreateNew() |
| | | 951 | | { |
| | 57 | 952 | | return new TItemType(); |
| | | 953 | | } |
| | | 954 | | |
| | | 955 | | private async Task<RefreshResult> ExecuteRemoteProviders(MetadataResult<TItemType> temp, string logName, bool re |
| | | 956 | | { |
| | 57 | 957 | | var refreshResult = new RefreshResult(); |
| | 57 | 958 | | var preferredLanguage = id?.MetadataLanguage; |
| | | 959 | | |
| | 57 | 960 | | var overviewIsFallback = false; |
| | 57 | 961 | | var taglineIsFallback = false; |
| | | 962 | | |
| | 57 | 963 | | if (id is not null) |
| | | 964 | | { |
| | 57 | 965 | | MergeNewData(temp.Item, id); |
| | | 966 | | } |
| | | 967 | | |
| | 136 | 968 | | foreach (var provider in providers) |
| | | 969 | | { |
| | 11 | 970 | | var providerName = provider.GetType().Name; |
| | 11 | 971 | | Logger.LogDebug("Running {Provider} for {Item}", providerName, logName); |
| | | 972 | | |
| | | 973 | | try |
| | | 974 | | { |
| | 11 | 975 | | var result = await provider.GetMetadata(id, cancellationToken).ConfigureAwait(false); |
| | | 976 | | |
| | 9 | 977 | | if (result.HasMetadata) |
| | | 978 | | { |
| | 7 | 979 | | result.Provider = provider.Name; |
| | | 980 | | |
| | 7 | 981 | | if (MetadataLanguageUtils.MatchesPreferredLanguage(result.ResultLanguage, preferredLanguage)) |
| | | 982 | | { |
| | 7 | 983 | | if (overviewIsFallback && !string.IsNullOrEmpty(result.Item.Overview)) |
| | | 984 | | { |
| | 0 | 985 | | temp.Item.Overview = null; |
| | 0 | 986 | | overviewIsFallback = false; |
| | | 987 | | } |
| | | 988 | | |
| | 7 | 989 | | if (taglineIsFallback && !string.IsNullOrEmpty(result.Item.Tagline)) |
| | | 990 | | { |
| | 0 | 991 | | temp.Item.Tagline = null; |
| | 0 | 992 | | taglineIsFallback = false; |
| | | 993 | | } |
| | | 994 | | } |
| | | 995 | | else |
| | | 996 | | { |
| | 0 | 997 | | overviewIsFallback |= string.IsNullOrEmpty(temp.Item.Overview) && !string.IsNullOrEmpty(resu |
| | 0 | 998 | | taglineIsFallback |= string.IsNullOrEmpty(temp.Item.Tagline) && !string.IsNullOrEmpty(result |
| | | 999 | | } |
| | | 1000 | | |
| | 7 | 1001 | | LogInvalidProviderIds(result, providerName, logName); |
| | | 1002 | | |
| | 7 | 1003 | | MergeData(result, temp, [], replaceData, false); |
| | 7 | 1004 | | MergeNewData(temp.Item, id); |
| | | 1005 | | |
| | 7 | 1006 | | refreshResult.UpdateType |= ItemUpdateType.MetadataDownload; |
| | | 1007 | | } |
| | | 1008 | | else |
| | | 1009 | | { |
| | 2 | 1010 | | Logger.LogDebug("{Provider} returned no metadata for {Item}", providerName, logName); |
| | | 1011 | | } |
| | 9 | 1012 | | } |
| | 0 | 1013 | | catch (OperationCanceledException) |
| | | 1014 | | { |
| | 0 | 1015 | | throw; |
| | | 1016 | | } |
| | 2 | 1017 | | catch (Exception ex) |
| | | 1018 | | { |
| | 2 | 1019 | | refreshResult.Failures++; |
| | 2 | 1020 | | refreshResult.ErrorMessage = ex.Message; |
| | 2 | 1021 | | Logger.LogError(ex, "Error in {Provider} for {Item}", provider.Name, logName); |
| | 2 | 1022 | | } |
| | 11 | 1023 | | } |
| | | 1024 | | |
| | 57 | 1025 | | return refreshResult; |
| | 57 | 1026 | | } |
| | | 1027 | | |
| | | 1028 | | /// <summary> |
| | | 1029 | | /// Reports the ids a provider returned that cannot belong to the provider they are filed under. |
| | | 1030 | | /// </summary> |
| | | 1031 | | /// <remarks> |
| | | 1032 | | /// The ids are dropped when merging, this names the provider that produced them so the source of a |
| | | 1033 | | /// recurring bad id can be found. |
| | | 1034 | | /// </remarks> |
| | | 1035 | | private void LogInvalidProviderIds(MetadataResult<TItemType> result, string providerName, string logName) |
| | | 1036 | | { |
| | 7 | 1037 | | if (!Logger.IsEnabled(LogLevel.Debug)) |
| | | 1038 | | { |
| | 7 | 1039 | | return; |
| | | 1040 | | } |
| | | 1041 | | |
| | 0 | 1042 | | LogInvalidProviderIds(result.Item?.ProviderIds, providerName, logName, null); |
| | | 1043 | | |
| | 0 | 1044 | | if (result.People is null) |
| | | 1045 | | { |
| | 0 | 1046 | | return; |
| | | 1047 | | } |
| | | 1048 | | |
| | 0 | 1049 | | foreach (var person in result.People) |
| | | 1050 | | { |
| | 0 | 1051 | | LogInvalidProviderIds(person.ProviderIds, providerName, logName, person.Name); |
| | | 1052 | | } |
| | 0 | 1053 | | } |
| | | 1054 | | |
| | | 1055 | | private void LogInvalidProviderIds(IReadOnlyDictionary<string, string> providerIds, string providerName, string |
| | | 1056 | | { |
| | 0 | 1057 | | if (providerIds is null) |
| | | 1058 | | { |
| | 0 | 1059 | | return; |
| | | 1060 | | } |
| | | 1061 | | |
| | 0 | 1062 | | foreach (var (key, value) in providerIds) |
| | | 1063 | | { |
| | 0 | 1064 | | if (ProviderIdsExtensions.IsValidProviderId(key, value)) |
| | | 1065 | | { |
| | | 1066 | | continue; |
| | | 1067 | | } |
| | | 1068 | | |
| | 0 | 1069 | | if (personName is null) |
| | | 1070 | | { |
| | 0 | 1071 | | Logger.LogDebug("Discarding {Key} id '{Value}' returned by {Provider} for {Item}", key, value, provi |
| | | 1072 | | } |
| | | 1073 | | else |
| | | 1074 | | { |
| | 0 | 1075 | | Logger.LogDebug("Discarding {Key} id '{Value}' returned by {Provider} for {Person} of {Item}", key, |
| | | 1076 | | } |
| | | 1077 | | } |
| | 0 | 1078 | | } |
| | | 1079 | | |
| | | 1080 | | private void MergeNewData(TItemType source, TIdType lookupInfo) |
| | | 1081 | | { |
| | | 1082 | | // Copy new provider id's that may have been obtained |
| | 132 | 1083 | | foreach (var providerId in source.ProviderIds) |
| | | 1084 | | { |
| | 2 | 1085 | | var key = providerId.Key; |
| | | 1086 | | |
| | 2 | 1087 | | if (!ProviderIdsExtensions.IsValidProviderId(key, providerId.Value)) |
| | | 1088 | | { |
| | | 1089 | | continue; |
| | | 1090 | | } |
| | | 1091 | | |
| | | 1092 | | // Don't replace existing Id's, unless the one already there is unusable - handing that |
| | | 1093 | | // one to the providers that have yet to run is what makes them fail. |
| | 2 | 1094 | | if (!lookupInfo.ProviderIds.TryGetValue(key, out var existingId) |
| | 2 | 1095 | | || !ProviderIdsExtensions.IsValidProviderId(key, existingId)) |
| | | 1096 | | { |
| | 2 | 1097 | | lookupInfo.ProviderIds[key] = providerId.Value; |
| | | 1098 | | } |
| | | 1099 | | } |
| | 64 | 1100 | | } |
| | | 1101 | | |
| | | 1102 | | private bool HasChanged(BaseItem item, IHasItemChangeMonitor changeMonitor, IDirectoryService directoryService) |
| | | 1103 | | { |
| | | 1104 | | try |
| | | 1105 | | { |
| | 60 | 1106 | | var hasChanged = changeMonitor.HasChanged(item, directoryService); |
| | | 1107 | | |
| | 60 | 1108 | | if (hasChanged) |
| | | 1109 | | { |
| | 0 | 1110 | | Logger.LogDebug("{Monitor} reports change to {Item}", changeMonitor.GetType().Name, item.Path ?? ite |
| | | 1111 | | } |
| | | 1112 | | |
| | 60 | 1113 | | return hasChanged; |
| | | 1114 | | } |
| | 0 | 1115 | | catch (Exception ex) |
| | | 1116 | | { |
| | 0 | 1117 | | Logger.LogError(ex, "Error in {Monitor}.HasChanged", changeMonitor.GetType().Name); |
| | 0 | 1118 | | return false; |
| | | 1119 | | } |
| | 60 | 1120 | | } |
| | | 1121 | | |
| | | 1122 | | /// <summary> |
| | | 1123 | | /// Merges metadata from source into target. |
| | | 1124 | | /// </summary> |
| | | 1125 | | /// <param name="source">The source for new metadata.</param> |
| | | 1126 | | /// <param name="target">The target to insert new metadata into.</param> |
| | | 1127 | | /// <param name="lockedFields">The fields that are locked and should not be updated.</param> |
| | | 1128 | | /// <param name="replaceData"><c>true</c> if existing data should be replaced.</param> |
| | | 1129 | | /// <param name="mergeMetadataSettings"><c>true</c> if the metadata settings in target should be updated to matc |
| | | 1130 | | /// <exception cref="ArgumentException">Thrown if source or target are null.</exception> |
| | | 1131 | | protected virtual void MergeData( |
| | | 1132 | | MetadataResult<TItemType> source, |
| | | 1133 | | MetadataResult<TItemType> target, |
| | | 1134 | | MetadataField[] lockedFields, |
| | | 1135 | | bool replaceData, |
| | | 1136 | | bool mergeMetadataSettings) |
| | | 1137 | | { |
| | 23 | 1138 | | MergeBaseItemData(source, target, lockedFields, replaceData, mergeMetadataSettings); |
| | 23 | 1139 | | } |
| | | 1140 | | |
| | | 1141 | | internal static void MergeBaseItemData( |
| | | 1142 | | MetadataResult<TItemType> sourceResult, |
| | | 1143 | | MetadataResult<TItemType> targetResult, |
| | | 1144 | | MetadataField[] lockedFields, |
| | | 1145 | | bool replaceData, |
| | | 1146 | | bool mergeMetadataSettings) |
| | | 1147 | | { |
| | 152 | 1148 | | var source = sourceResult.Item; |
| | 152 | 1149 | | var target = targetResult.Item; |
| | | 1150 | | |
| | 152 | 1151 | | ArgumentNullException.ThrowIfNull(sourceResult); |
| | 152 | 1152 | | ArgumentNullException.ThrowIfNull(targetResult); |
| | | 1153 | | |
| | 152 | 1154 | | if (!lockedFields.Contains(MetadataField.Name)) |
| | | 1155 | | { |
| | 149 | 1156 | | if (replaceData || string.IsNullOrEmpty(target.Name)) |
| | | 1157 | | { |
| | | 1158 | | // Safeguard against incoming data having an empty name |
| | 142 | 1159 | | if (!string.IsNullOrWhiteSpace(source.Name)) |
| | | 1160 | | { |
| | 16 | 1161 | | target.Name = source.Name; |
| | | 1162 | | } |
| | | 1163 | | } |
| | | 1164 | | } |
| | | 1165 | | |
| | 152 | 1166 | | if (replaceData || string.IsNullOrEmpty(target.OriginalTitle)) |
| | | 1167 | | { |
| | 151 | 1168 | | target.OriginalTitle = source.OriginalTitle; |
| | | 1169 | | } |
| | | 1170 | | |
| | 152 | 1171 | | if (replaceData || string.IsNullOrEmpty(target.HomePageUrl)) |
| | | 1172 | | { |
| | 152 | 1173 | | target.HomePageUrl = source.HomePageUrl; |
| | | 1174 | | } |
| | | 1175 | | |
| | 152 | 1176 | | if (replaceData || string.IsNullOrEmpty(target.OriginalLanguage)) |
| | | 1177 | | { |
| | 152 | 1178 | | target.OriginalLanguage = source.OriginalLanguage; |
| | | 1179 | | } |
| | | 1180 | | |
| | 152 | 1181 | | if (replaceData || !target.CommunityRating.HasValue) |
| | | 1182 | | { |
| | 151 | 1183 | | target.CommunityRating = source.CommunityRating; |
| | | 1184 | | } |
| | | 1185 | | |
| | 152 | 1186 | | if (replaceData || !target.EndDate.HasValue) |
| | | 1187 | | { |
| | 151 | 1188 | | target.EndDate = source.EndDate; |
| | | 1189 | | } |
| | | 1190 | | |
| | 152 | 1191 | | if (!lockedFields.Contains(MetadataField.Genres)) |
| | | 1192 | | { |
| | 150 | 1193 | | if (replaceData || target.Genres.Length == 0) |
| | | 1194 | | { |
| | 149 | 1195 | | target.Genres = source.Genres; |
| | | 1196 | | } |
| | | 1197 | | } |
| | | 1198 | | |
| | 152 | 1199 | | if (replaceData || !target.IndexNumber.HasValue) |
| | | 1200 | | { |
| | 151 | 1201 | | target.IndexNumber = source.IndexNumber; |
| | | 1202 | | } |
| | | 1203 | | |
| | 152 | 1204 | | if (!lockedFields.Contains(MetadataField.OfficialRating)) |
| | | 1205 | | { |
| | 149 | 1206 | | if (replaceData || string.IsNullOrEmpty(target.OfficialRating)) |
| | | 1207 | | { |
| | 148 | 1208 | | target.OfficialRating = source.OfficialRating; |
| | | 1209 | | } |
| | | 1210 | | } |
| | | 1211 | | |
| | 152 | 1212 | | if (replaceData || string.IsNullOrEmpty(target.CustomRating)) |
| | | 1213 | | { |
| | 151 | 1214 | | target.CustomRating = source.CustomRating; |
| | | 1215 | | } |
| | | 1216 | | |
| | 152 | 1217 | | if (replaceData || string.IsNullOrEmpty(target.Tagline)) |
| | | 1218 | | { |
| | 150 | 1219 | | target.Tagline = source.Tagline; |
| | | 1220 | | } |
| | | 1221 | | |
| | 152 | 1222 | | if (!lockedFields.Contains(MetadataField.Overview)) |
| | | 1223 | | { |
| | 149 | 1224 | | if (replaceData || string.IsNullOrEmpty(target.Overview)) |
| | | 1225 | | { |
| | 148 | 1226 | | target.Overview = source.Overview; |
| | | 1227 | | } |
| | | 1228 | | } |
| | | 1229 | | |
| | 152 | 1230 | | if (replaceData || !target.ParentIndexNumber.HasValue) |
| | | 1231 | | { |
| | 148 | 1232 | | target.ParentIndexNumber = source.ParentIndexNumber; |
| | | 1233 | | } |
| | | 1234 | | |
| | 152 | 1235 | | if (!lockedFields.Contains(MetadataField.Cast)) |
| | | 1236 | | { |
| | 151 | 1237 | | RemoveInvalidProviderIds(sourceResult.People); |
| | 151 | 1238 | | RemoveInvalidProviderIds(targetResult.People); |
| | | 1239 | | |
| | 151 | 1240 | | if (replaceData || targetResult.People is null || targetResult.People.Count == 0) |
| | | 1241 | | { |
| | 144 | 1242 | | targetResult.People = sourceResult.People; |
| | | 1243 | | } |
| | 7 | 1244 | | else if (sourceResult.People is not null && sourceResult.People.Count > 0) |
| | | 1245 | | { |
| | 7 | 1246 | | MergePeople(sourceResult.People, targetResult.People); |
| | | 1247 | | } |
| | | 1248 | | } |
| | | 1249 | | |
| | 152 | 1250 | | if (replaceData || !target.PremiereDate.HasValue) |
| | | 1251 | | { |
| | 151 | 1252 | | target.PremiereDate = source.PremiereDate; |
| | | 1253 | | } |
| | | 1254 | | |
| | 152 | 1255 | | if (replaceData || target.ProductionYear is null) |
| | | 1256 | | { |
| | 151 | 1257 | | target.ProductionYear = source.ProductionYear; |
| | | 1258 | | } |
| | | 1259 | | |
| | 152 | 1260 | | if (!lockedFields.Contains(MetadataField.Runtime)) |
| | | 1261 | | { |
| | 152 | 1262 | | if (replaceData || !target.RunTimeTicks.HasValue) |
| | | 1263 | | { |
| | 152 | 1264 | | if (target is not Audio && target is not Video && target is not Book) |
| | | 1265 | | { |
| | 49 | 1266 | | target.RunTimeTicks = source.RunTimeTicks; |
| | | 1267 | | } |
| | | 1268 | | } |
| | | 1269 | | } |
| | | 1270 | | |
| | 152 | 1271 | | if (!lockedFields.Contains(MetadataField.Studios)) |
| | | 1272 | | { |
| | 150 | 1273 | | if (replaceData || target.Studios.Length == 0) |
| | | 1274 | | { |
| | 149 | 1275 | | target.Studios = source.Studios; |
| | | 1276 | | } |
| | | 1277 | | else |
| | | 1278 | | { |
| | 1 | 1279 | | target.Studios = target.Studios.Concat(source.Studios).Distinct(StringComparer.OrdinalIgnoreCase).To |
| | | 1280 | | } |
| | | 1281 | | } |
| | | 1282 | | |
| | 152 | 1283 | | if (!lockedFields.Contains(MetadataField.Tags)) |
| | | 1284 | | { |
| | 150 | 1285 | | if (replaceData || target.Tags.Length == 0) |
| | | 1286 | | { |
| | 149 | 1287 | | target.Tags = source.Tags; |
| | | 1288 | | } |
| | | 1289 | | else |
| | | 1290 | | { |
| | 1 | 1291 | | target.Tags = target.Tags.Concat(source.Tags).Distinct(StringComparer.OrdinalIgnoreCase).ToArray(); |
| | | 1292 | | } |
| | | 1293 | | } |
| | | 1294 | | |
| | 152 | 1295 | | if (!lockedFields.Contains(MetadataField.ProductionLocations)) |
| | | 1296 | | { |
| | 150 | 1297 | | if (replaceData || target.ProductionLocations.Length == 0) |
| | | 1298 | | { |
| | 149 | 1299 | | target.ProductionLocations = source.ProductionLocations; |
| | | 1300 | | } |
| | | 1301 | | else |
| | | 1302 | | { |
| | 1 | 1303 | | target.ProductionLocations = target.ProductionLocations.Concat(source.ProductionLocations).Distinct( |
| | | 1304 | | } |
| | | 1305 | | } |
| | | 1306 | | |
| | 322 | 1307 | | foreach (var id in source.ProviderIds) |
| | | 1308 | | { |
| | 9 | 1309 | | var key = id.Key; |
| | | 1310 | | |
| | | 1311 | | // An id that cannot belong to the provider it is filed under only breaks that provider on |
| | | 1312 | | // the next refresh, so never let one in - not even when replacing all metadata. |
| | 9 | 1313 | | if (!ProviderIdsExtensions.IsValidProviderId(key, id.Value)) |
| | | 1314 | | { |
| | | 1315 | | continue; |
| | | 1316 | | } |
| | | 1317 | | |
| | | 1318 | | // Don't replace existing Id's, unless the stored one is unusable - that one is the bad |
| | | 1319 | | // match the refresh is meant to repair. |
| | 8 | 1320 | | if (replaceData |
| | 8 | 1321 | | || !target.ProviderIds.TryGetValue(key, out var existingId) |
| | 8 | 1322 | | || !ProviderIdsExtensions.IsValidProviderId(key, existingId)) |
| | | 1323 | | { |
| | 6 | 1324 | | target.ProviderIds[key] = id.Value; |
| | | 1325 | | } |
| | | 1326 | | } |
| | | 1327 | | |
| | | 1328 | | // A bad id no provider offered a replacement for still has to go, otherwise the item keeps |
| | | 1329 | | // failing the same way on every refresh. |
| | 304 | 1330 | | foreach (var key in target.ProviderIds |
| | 152 | 1331 | | .Where(id => !ProviderIdsExtensions.IsValidProviderId(id.Key, id.Value)) |
| | 152 | 1332 | | .Select(id => id.Key) |
| | 152 | 1333 | | .ToArray()) |
| | | 1334 | | { |
| | 0 | 1335 | | target.ProviderIds.Remove(key); |
| | | 1336 | | } |
| | | 1337 | | |
| | 152 | 1338 | | if (replaceData || !target.CriticRating.HasValue) |
| | | 1339 | | { |
| | 151 | 1340 | | target.CriticRating = source.CriticRating; |
| | | 1341 | | } |
| | | 1342 | | |
| | 152 | 1343 | | if (replaceData || target.RemoteTrailers.Count == 0) |
| | | 1344 | | { |
| | 151 | 1345 | | target.RemoteTrailers = source.RemoteTrailers; |
| | | 1346 | | } |
| | | 1347 | | else |
| | | 1348 | | { |
| | 1 | 1349 | | target.RemoteTrailers = target.RemoteTrailers.Concat(source.RemoteTrailers).DistinctBy(t => t.Url).ToArr |
| | | 1350 | | } |
| | | 1351 | | |
| | 152 | 1352 | | MergeAlbumArtist(source, target, replaceData); |
| | 152 | 1353 | | MergeVideoInfo(source, target, replaceData); |
| | 152 | 1354 | | MergeDisplayOrder(source, target, replaceData); |
| | | 1355 | | |
| | 152 | 1356 | | if (replaceData || string.IsNullOrEmpty(target.ForcedSortName)) |
| | | 1357 | | { |
| | 151 | 1358 | | var forcedSortName = source.ForcedSortName; |
| | 151 | 1359 | | if (!string.IsNullOrEmpty(forcedSortName)) |
| | | 1360 | | { |
| | 3 | 1361 | | target.ForcedSortName = forcedSortName; |
| | | 1362 | | } |
| | | 1363 | | } |
| | | 1364 | | |
| | 152 | 1365 | | if (mergeMetadataSettings) |
| | | 1366 | | { |
| | 12 | 1367 | | if (replaceData || !target.IsLocked) |
| | | 1368 | | { |
| | 12 | 1369 | | target.IsLocked = target.IsLocked || source.IsLocked; |
| | | 1370 | | } |
| | | 1371 | | |
| | 12 | 1372 | | if (target.LockedFields.Length == 0) |
| | | 1373 | | { |
| | 10 | 1374 | | target.LockedFields = source.LockedFields; |
| | | 1375 | | } |
| | | 1376 | | else |
| | | 1377 | | { |
| | 2 | 1378 | | target.LockedFields = target.LockedFields.Concat(source.LockedFields).Distinct().ToArray(); |
| | | 1379 | | } |
| | | 1380 | | |
| | 12 | 1381 | | if (source.DateCreated != DateTime.MinValue) |
| | | 1382 | | { |
| | 1 | 1383 | | target.DateCreated = source.DateCreated; |
| | | 1384 | | } |
| | | 1385 | | |
| | 12 | 1386 | | if (replaceData || source.DateModified != DateTime.MinValue) |
| | | 1387 | | { |
| | 8 | 1388 | | target.DateModified = source.DateModified; |
| | | 1389 | | } |
| | | 1390 | | |
| | 12 | 1391 | | if (replaceData || string.IsNullOrEmpty(target.PreferredMetadataCountryCode)) |
| | | 1392 | | { |
| | 12 | 1393 | | target.PreferredMetadataCountryCode = source.PreferredMetadataCountryCode; |
| | | 1394 | | } |
| | | 1395 | | |
| | 12 | 1396 | | if (replaceData || string.IsNullOrEmpty(target.PreferredMetadataLanguage)) |
| | | 1397 | | { |
| | 12 | 1398 | | target.PreferredMetadataLanguage = source.PreferredMetadataLanguage; |
| | | 1399 | | } |
| | | 1400 | | } |
| | 152 | 1401 | | } |
| | | 1402 | | |
| | | 1403 | | private static void RemoveInvalidProviderIds(IReadOnlyList<PersonInfo> people) |
| | | 1404 | | { |
| | 302 | 1405 | | if (people is null) |
| | | 1406 | | { |
| | 277 | 1407 | | return; |
| | | 1408 | | } |
| | | 1409 | | |
| | 96 | 1410 | | foreach (var person in people) |
| | | 1411 | | { |
| | 23 | 1412 | | if (person.ProviderIds is null || person.ProviderIds.Count == 0) |
| | | 1413 | | { |
| | | 1414 | | continue; |
| | | 1415 | | } |
| | | 1416 | | |
| | 12 | 1417 | | var invalidKeys = person.ProviderIds |
| | 12 | 1418 | | .Where(id => !ProviderIdsExtensions.IsValidProviderId(id.Key, id.Value)) |
| | 12 | 1419 | | .Select(id => id.Key) |
| | 12 | 1420 | | .ToArray(); |
| | | 1421 | | |
| | 28 | 1422 | | foreach (var key in invalidKeys) |
| | | 1423 | | { |
| | 2 | 1424 | | person.ProviderIds.Remove(key); |
| | | 1425 | | } |
| | | 1426 | | } |
| | 25 | 1427 | | } |
| | | 1428 | | |
| | | 1429 | | private static void MergePeople(IReadOnlyList<PersonInfo> source, IReadOnlyList<PersonInfo> target) |
| | | 1430 | | { |
| | 7 | 1431 | | var sourceByName = source.ToLookup(p => p.Name.RemoveDiacritics(), StringComparer.OrdinalIgnoreCase); |
| | 7 | 1432 | | var targetByName = target.ToLookup(p => p.Name.RemoveDiacritics(), StringComparer.OrdinalIgnoreCase); |
| | | 1433 | | |
| | 28 | 1434 | | foreach (var name in targetByName.Select(g => g.Key)) |
| | | 1435 | | { |
| | 7 | 1436 | | var targetPeople = targetByName[name].ToArray(); |
| | 7 | 1437 | | var sourcePeople = sourceByName[name].ToArray(); |
| | | 1438 | | |
| | 7 | 1439 | | if (sourcePeople.Length == 0) |
| | | 1440 | | { |
| | | 1441 | | continue; |
| | | 1442 | | } |
| | | 1443 | | |
| | 24 | 1444 | | for (int i = 0; i < targetPeople.Length; i++) |
| | | 1445 | | { |
| | 6 | 1446 | | var person = targetPeople[i]; |
| | 6 | 1447 | | var personInSource = i < sourcePeople.Length ? sourcePeople[i] : sourcePeople[0]; |
| | | 1448 | | |
| | 18 | 1449 | | foreach (var providerId in personInSource.ProviderIds) |
| | | 1450 | | { |
| | 3 | 1451 | | person.ProviderIds.TryAdd(providerId.Key, providerId.Value); |
| | | 1452 | | } |
| | | 1453 | | |
| | 6 | 1454 | | if (string.IsNullOrWhiteSpace(person.ImageUrl)) |
| | | 1455 | | { |
| | 5 | 1456 | | person.ImageUrl = personInSource.ImageUrl; |
| | | 1457 | | } |
| | | 1458 | | |
| | 6 | 1459 | | if (!string.IsNullOrWhiteSpace(personInSource.Role) && string.IsNullOrWhiteSpace(person.Role)) |
| | | 1460 | | { |
| | 0 | 1461 | | person.Role = personInSource.Role; |
| | | 1462 | | } |
| | | 1463 | | |
| | 6 | 1464 | | if (personInSource.SortOrder.HasValue && !person.SortOrder.HasValue) |
| | | 1465 | | { |
| | 0 | 1466 | | person.SortOrder = personInSource.SortOrder; |
| | | 1467 | | } |
| | | 1468 | | } |
| | | 1469 | | } |
| | 7 | 1470 | | } |
| | | 1471 | | |
| | | 1472 | | private static void MergeDisplayOrder(BaseItem source, BaseItem target, bool replaceData) |
| | | 1473 | | { |
| | 152 | 1474 | | if (source is IHasDisplayOrder sourceHasDisplayOrder |
| | 152 | 1475 | | && target is IHasDisplayOrder targetHasDisplayOrder) |
| | | 1476 | | { |
| | 49 | 1477 | | if (replaceData || string.IsNullOrEmpty(targetHasDisplayOrder.DisplayOrder)) |
| | | 1478 | | { |
| | 48 | 1479 | | var displayOrder = sourceHasDisplayOrder.DisplayOrder; |
| | 48 | 1480 | | if (!string.IsNullOrWhiteSpace(displayOrder)) |
| | | 1481 | | { |
| | 3 | 1482 | | targetHasDisplayOrder.DisplayOrder = displayOrder; |
| | | 1483 | | } |
| | | 1484 | | } |
| | | 1485 | | } |
| | 152 | 1486 | | } |
| | | 1487 | | |
| | | 1488 | | private static void MergeAlbumArtist(BaseItem source, BaseItem target, bool replaceData) |
| | | 1489 | | { |
| | 152 | 1490 | | if (source is IHasAlbumArtist sourceHasAlbumArtist |
| | 152 | 1491 | | && target is IHasAlbumArtist targetHasAlbumArtist) |
| | | 1492 | | { |
| | 28 | 1493 | | if (replaceData || targetHasAlbumArtist.AlbumArtists.Count == 0) |
| | | 1494 | | { |
| | 27 | 1495 | | targetHasAlbumArtist.AlbumArtists = sourceHasAlbumArtist.AlbumArtists; |
| | | 1496 | | } |
| | 1 | 1497 | | else if (sourceHasAlbumArtist.AlbumArtists.Count > 0) |
| | | 1498 | | { |
| | 1 | 1499 | | targetHasAlbumArtist.AlbumArtists = targetHasAlbumArtist.AlbumArtists.Concat(sourceHasAlbumArtist.Al |
| | | 1500 | | } |
| | | 1501 | | } |
| | 125 | 1502 | | } |
| | | 1503 | | |
| | | 1504 | | private static void MergeVideoInfo(BaseItem source, BaseItem target, bool replaceData) |
| | | 1505 | | { |
| | 152 | 1506 | | if (source is Video sourceCast && target is Video targetCast) |
| | | 1507 | | { |
| | 75 | 1508 | | if (sourceCast.Video3DFormat.HasValue && (replaceData || !targetCast.Video3DFormat.HasValue)) |
| | | 1509 | | { |
| | 2 | 1510 | | targetCast.Video3DFormat = sourceCast.Video3DFormat; |
| | | 1511 | | } |
| | | 1512 | | } |
| | 152 | 1513 | | } |
| | | 1514 | | } |
| | | 1515 | | } |