ProviderUtils.cs 11 KB

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