ProviderUtils.cs 11 KB

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