ProviderUtils.cs 12 KB

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