ProviderUtils.cs 11 KB

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