ProviderUtils.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #nullable disable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Diacritics.Extensions;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.Entities.Audio;
  8. using MediaBrowser.Controller.Providers;
  9. using MediaBrowser.Model.Entities;
  10. namespace MediaBrowser.Providers.Manager
  11. {
  12. /// <summary>
  13. /// Class ProviderUtils.
  14. /// </summary>
  15. public static class ProviderUtils
  16. {
  17. /// <summary>
  18. /// Merges metadata from source into target.
  19. /// </summary>
  20. /// <param name="sourceResult">The source for new metadata.</param>
  21. /// <param name="targetResult">The target to insert new metadata into.</param>
  22. /// <param name="lockedFields">The fields that are locked and should not be updated.</param>
  23. /// <param name="replaceData"><c>true</c> if existing data should be replaced.</param>
  24. /// <param name="mergeMetadataSettings"><c>true</c> if the metadata settings in target should be updated to match source.</param>
  25. /// <typeparam name="T">The type being acted upon.</typeparam>
  26. /// <exception cref="ArgumentException">Thrown if source or target are null.</exception>
  27. public static void MergeBaseItemData<T>(
  28. MetadataResult<T> sourceResult,
  29. MetadataResult<T> targetResult,
  30. MetadataField[] lockedFields,
  31. bool replaceData,
  32. bool mergeMetadataSettings)
  33. where T : BaseItem
  34. {
  35. var source = sourceResult.Item;
  36. var target = targetResult.Item;
  37. if (source == null)
  38. {
  39. throw new ArgumentException("Item cannot be null.", nameof(sourceResult));
  40. }
  41. if (target == null)
  42. {
  43. throw new ArgumentException("Item cannot be null.", nameof(targetResult));
  44. }
  45. if (!lockedFields.Contains(MetadataField.Name))
  46. {
  47. if (replaceData || string.IsNullOrEmpty(target.Name))
  48. {
  49. // Safeguard against incoming data having an empty name
  50. if (!string.IsNullOrWhiteSpace(source.Name))
  51. {
  52. target.Name = source.Name;
  53. }
  54. }
  55. }
  56. if (replaceData || string.IsNullOrEmpty(target.OriginalTitle))
  57. {
  58. // Safeguard against incoming data having an empty name
  59. if (!string.IsNullOrWhiteSpace(source.OriginalTitle))
  60. {
  61. target.OriginalTitle = source.OriginalTitle;
  62. }
  63. }
  64. if (replaceData || !target.CommunityRating.HasValue)
  65. {
  66. target.CommunityRating = source.CommunityRating;
  67. }
  68. if (replaceData || !target.EndDate.HasValue)
  69. {
  70. target.EndDate = source.EndDate;
  71. }
  72. if (!lockedFields.Contains(MetadataField.Genres))
  73. {
  74. if (replaceData || target.Genres.Length == 0)
  75. {
  76. target.Genres = source.Genres;
  77. }
  78. }
  79. if (replaceData || !target.IndexNumber.HasValue)
  80. {
  81. target.IndexNumber = source.IndexNumber;
  82. }
  83. if (!lockedFields.Contains(MetadataField.OfficialRating))
  84. {
  85. if (replaceData || string.IsNullOrEmpty(target.OfficialRating))
  86. {
  87. target.OfficialRating = source.OfficialRating;
  88. }
  89. }
  90. if (replaceData || string.IsNullOrEmpty(target.CustomRating))
  91. {
  92. target.CustomRating = source.CustomRating;
  93. }
  94. if (replaceData || string.IsNullOrEmpty(target.Tagline))
  95. {
  96. target.Tagline = source.Tagline;
  97. }
  98. if (!lockedFields.Contains(MetadataField.Overview))
  99. {
  100. if (replaceData || string.IsNullOrEmpty(target.Overview))
  101. {
  102. target.Overview = source.Overview;
  103. }
  104. }
  105. if (replaceData || !target.ParentIndexNumber.HasValue)
  106. {
  107. target.ParentIndexNumber = source.ParentIndexNumber;
  108. }
  109. if (!lockedFields.Contains(MetadataField.Cast))
  110. {
  111. if (replaceData || targetResult.People == null || targetResult.People.Count == 0)
  112. {
  113. targetResult.People = sourceResult.People;
  114. }
  115. else if (targetResult.People != null && sourceResult.People != null)
  116. {
  117. MergePeople(sourceResult.People, targetResult.People);
  118. }
  119. }
  120. if (replaceData || !target.PremiereDate.HasValue)
  121. {
  122. target.PremiereDate = source.PremiereDate;
  123. }
  124. if (replaceData || !target.ProductionYear.HasValue)
  125. {
  126. target.ProductionYear = source.ProductionYear;
  127. }
  128. if (!lockedFields.Contains(MetadataField.Runtime))
  129. {
  130. if (replaceData || !target.RunTimeTicks.HasValue)
  131. {
  132. if (target is not Audio && target is not Video)
  133. {
  134. target.RunTimeTicks = source.RunTimeTicks;
  135. }
  136. }
  137. }
  138. if (!lockedFields.Contains(MetadataField.Studios))
  139. {
  140. if (replaceData || target.Studios.Length == 0)
  141. {
  142. target.Studios = source.Studios;
  143. }
  144. }
  145. if (!lockedFields.Contains(MetadataField.Tags))
  146. {
  147. if (replaceData || target.Tags.Length == 0)
  148. {
  149. target.Tags = source.Tags;
  150. }
  151. }
  152. if (!lockedFields.Contains(MetadataField.ProductionLocations))
  153. {
  154. if (replaceData || target.ProductionLocations.Length == 0)
  155. {
  156. target.ProductionLocations = source.ProductionLocations;
  157. }
  158. }
  159. foreach (var id in source.ProviderIds)
  160. {
  161. var key = id.Key;
  162. // Don't replace existing Id's.
  163. if (replaceData || !target.ProviderIds.ContainsKey(key))
  164. {
  165. target.ProviderIds[key] = id.Value;
  166. }
  167. }
  168. MergeAlbumArtist(source, target, replaceData);
  169. MergeCriticRating(source, target, replaceData);
  170. MergeTrailers(source, target, replaceData);
  171. MergeVideoInfo(source, target, replaceData);
  172. MergeDisplayOrder(source, target, replaceData);
  173. if (replaceData || string.IsNullOrEmpty(target.ForcedSortName))
  174. {
  175. var forcedSortName = source.ForcedSortName;
  176. if (!string.IsNullOrWhiteSpace(forcedSortName))
  177. {
  178. target.ForcedSortName = forcedSortName;
  179. }
  180. }
  181. if (mergeMetadataSettings)
  182. {
  183. target.LockedFields = source.LockedFields;
  184. target.IsLocked = source.IsLocked;
  185. // Grab the value if it's there, but if not then don't overwrite with the default
  186. if (source.DateCreated != default)
  187. {
  188. target.DateCreated = source.DateCreated;
  189. }
  190. target.PreferredMetadataCountryCode = source.PreferredMetadataCountryCode;
  191. target.PreferredMetadataLanguage = source.PreferredMetadataLanguage;
  192. }
  193. }
  194. private static void MergePeople(List<PersonInfo> source, List<PersonInfo> target)
  195. {
  196. foreach (var person in target)
  197. {
  198. var normalizedName = person.Name.RemoveDiacritics();
  199. var personInSource = source.FirstOrDefault(i => string.Equals(i.Name.RemoveDiacritics(), normalizedName, StringComparison.OrdinalIgnoreCase));
  200. if (personInSource != null)
  201. {
  202. foreach (var providerId in personInSource.ProviderIds)
  203. {
  204. if (!person.ProviderIds.ContainsKey(providerId.Key))
  205. {
  206. person.ProviderIds[providerId.Key] = providerId.Value;
  207. }
  208. }
  209. if (string.IsNullOrWhiteSpace(person.ImageUrl))
  210. {
  211. person.ImageUrl = personInSource.ImageUrl;
  212. }
  213. }
  214. }
  215. }
  216. private static void MergeDisplayOrder(BaseItem source, BaseItem target, bool replaceData)
  217. {
  218. if (source is IHasDisplayOrder sourceHasDisplayOrder
  219. && target is IHasDisplayOrder targetHasDisplayOrder)
  220. {
  221. if (replaceData || string.IsNullOrEmpty(targetHasDisplayOrder.DisplayOrder))
  222. {
  223. var displayOrder = sourceHasDisplayOrder.DisplayOrder;
  224. if (!string.IsNullOrWhiteSpace(displayOrder))
  225. {
  226. targetHasDisplayOrder.DisplayOrder = displayOrder;
  227. }
  228. }
  229. }
  230. }
  231. private static void MergeAlbumArtist(BaseItem source, BaseItem target, bool replaceData)
  232. {
  233. if (source is IHasAlbumArtist sourceHasAlbumArtist
  234. && target is IHasAlbumArtist targetHasAlbumArtist)
  235. {
  236. if (replaceData || targetHasAlbumArtist.AlbumArtists.Count == 0)
  237. {
  238. targetHasAlbumArtist.AlbumArtists = sourceHasAlbumArtist.AlbumArtists;
  239. }
  240. }
  241. }
  242. private static void MergeCriticRating(BaseItem source, BaseItem target, bool replaceData)
  243. {
  244. if (replaceData || !target.CriticRating.HasValue)
  245. {
  246. target.CriticRating = source.CriticRating;
  247. }
  248. }
  249. private static void MergeTrailers(BaseItem source, BaseItem target, bool replaceData)
  250. {
  251. if (replaceData || target.RemoteTrailers.Count == 0)
  252. {
  253. target.RemoteTrailers = source.RemoteTrailers;
  254. }
  255. }
  256. private static void MergeVideoInfo(BaseItem source, BaseItem target, bool replaceData)
  257. {
  258. if (source is Video sourceCast && target is Video targetCast)
  259. {
  260. if (replaceData || targetCast.Video3DFormat == null)
  261. {
  262. targetCast.Video3DFormat = sourceCast.Video3DFormat;
  263. }
  264. }
  265. }
  266. }
  267. }