ProviderUtils.cs 10 KB

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