ProviderUtils.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.CustomRating))
  84. {
  85. target.CustomRating = source.CustomRating;
  86. }
  87. if (replaceData || string.IsNullOrEmpty(target.Tagline))
  88. {
  89. target.Tagline = source.Tagline;
  90. }
  91. if (!lockedFields.Contains(MetadataFields.Overview))
  92. {
  93. if (replaceData || string.IsNullOrEmpty(target.Overview))
  94. {
  95. target.Overview = source.Overview;
  96. }
  97. }
  98. if (replaceData || !target.ParentIndexNumber.HasValue)
  99. {
  100. target.ParentIndexNumber = source.ParentIndexNumber;
  101. }
  102. if (!lockedFields.Contains(MetadataFields.Cast))
  103. {
  104. if (replaceData || targetResult.People == null || targetResult.People.Count == 0)
  105. {
  106. targetResult.People = sourceResult.People;
  107. }
  108. else if (targetResult.People != null && sourceResult.People != null)
  109. {
  110. MergePeople(sourceResult.People, targetResult.People);
  111. }
  112. }
  113. if (replaceData || !target.PremiereDate.HasValue)
  114. {
  115. target.PremiereDate = source.PremiereDate;
  116. }
  117. if (replaceData || !target.ProductionYear.HasValue)
  118. {
  119. target.ProductionYear = source.ProductionYear;
  120. }
  121. if (!lockedFields.Contains(MetadataFields.Runtime))
  122. {
  123. if (replaceData || !target.RunTimeTicks.HasValue)
  124. {
  125. if (!(target is Audio) && !(target is Video))
  126. {
  127. target.RunTimeTicks = source.RunTimeTicks;
  128. }
  129. }
  130. }
  131. if (!lockedFields.Contains(MetadataFields.Studios))
  132. {
  133. if (replaceData || target.Studios.Count == 0)
  134. {
  135. target.Studios = source.Studios;
  136. }
  137. }
  138. if (!lockedFields.Contains(MetadataFields.Tags))
  139. {
  140. if (replaceData || target.Tags.Count == 0)
  141. {
  142. target.Tags = source.Tags;
  143. }
  144. }
  145. if (!lockedFields.Contains(MetadataFields.Keywords))
  146. {
  147. if (replaceData || target.Keywords.Count == 0)
  148. {
  149. target.Keywords = source.Keywords;
  150. }
  151. }
  152. if (!lockedFields.Contains(MetadataFields.ProductionLocations))
  153. {
  154. if (replaceData || target.ProductionLocations.Count == 0)
  155. {
  156. target.ProductionLocations = source.ProductionLocations;
  157. }
  158. }
  159. if (replaceData || !target.VoteCount.HasValue)
  160. {
  161. target.VoteCount = source.VoteCount;
  162. }
  163. foreach (var id in source.ProviderIds)
  164. {
  165. var key = id.Key;
  166. // Don't replace existing Id's.
  167. if (replaceData || !target.ProviderIds.ContainsKey(key))
  168. {
  169. target.ProviderIds[key] = id.Value;
  170. }
  171. }
  172. MergeAlbumArtist(source, target, lockedFields, replaceData);
  173. MergeCriticRating(source, target, lockedFields, replaceData);
  174. MergeTrailers(source, target, lockedFields, replaceData);
  175. MergeVideoInfo(source, target, lockedFields, replaceData);
  176. if (mergeMetadataSettings)
  177. {
  178. MergeMetadataSettings(source, target);
  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. public static void MergeMetadataSettings(BaseItem source,
  204. BaseItem target)
  205. {
  206. target.ForcedSortName = source.ForcedSortName;
  207. target.LockedFields = source.LockedFields;
  208. target.IsLocked = source.IsLocked;
  209. target.DisplayMediaType = source.DisplayMediaType;
  210. // Grab the value if it's there, but if not then don't overwrite the default
  211. if (source.DateCreated != default(DateTime))
  212. {
  213. target.DateCreated = source.DateCreated;
  214. }
  215. target.PreferredMetadataCountryCode = source.PreferredMetadataCountryCode;
  216. target.PreferredMetadataLanguage = source.PreferredMetadataLanguage;
  217. var sourceHasDisplayOrder = source as IHasDisplayOrder;
  218. var targetHasDisplayOrder = target as IHasDisplayOrder;
  219. if (sourceHasDisplayOrder != null && targetHasDisplayOrder != null)
  220. {
  221. targetHasDisplayOrder.DisplayOrder = sourceHasDisplayOrder.DisplayOrder;
  222. }
  223. }
  224. private static void MergeAlbumArtist(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  225. {
  226. var sourceHasAlbumArtist = source as IHasAlbumArtist;
  227. var targetHasAlbumArtist = target as IHasAlbumArtist;
  228. if (sourceHasAlbumArtist != null && targetHasAlbumArtist != null)
  229. {
  230. if (replaceData || targetHasAlbumArtist.AlbumArtists.Count == 0)
  231. {
  232. targetHasAlbumArtist.AlbumArtists = sourceHasAlbumArtist.AlbumArtists;
  233. }
  234. }
  235. }
  236. private static void MergeCriticRating(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  237. {
  238. if (replaceData || !target.CriticRating.HasValue)
  239. {
  240. target.CriticRating = source.CriticRating;
  241. }
  242. }
  243. private static void MergeTrailers(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  244. {
  245. var sourceCast = source as IHasTrailers;
  246. var targetCast = target as IHasTrailers;
  247. if (sourceCast != null && targetCast != null)
  248. {
  249. if (replaceData || targetCast.RemoteTrailers.Count == 0)
  250. {
  251. targetCast.RemoteTrailers = sourceCast.RemoteTrailers;
  252. }
  253. }
  254. }
  255. private static void MergeVideoInfo(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  256. {
  257. var sourceCast = source as Video;
  258. var targetCast = target as Video;
  259. if (sourceCast != null && targetCast != null)
  260. {
  261. if (replaceData || targetCast.Video3DFormat == null)
  262. {
  263. targetCast.Video3DFormat = sourceCast.Video3DFormat;
  264. }
  265. }
  266. }
  267. }
  268. }