ProviderUtils.cs 9.9 KB

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