ProviderUtils.cs 12 KB

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