2
0

ProviderUtils.cs 9.9 KB

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