ProviderUtils.cs 12 KB

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