ProviderUtils.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. MergeShortOverview(source, target, lockedFields, replaceData);
  161. if (mergeMetadataSettings)
  162. {
  163. target.ForcedSortName = source.ForcedSortName;
  164. target.LockedFields = source.LockedFields;
  165. target.IsLocked = source.IsLocked;
  166. target.DisplayMediaType = source.DisplayMediaType;
  167. // Grab the value if it's there, but if not then don't overwrite the default
  168. if (source.DateCreated != default(DateTime))
  169. {
  170. target.DateCreated = source.DateCreated;
  171. }
  172. var sourceHasLanguageSettings = source as IHasPreferredMetadataLanguage;
  173. var targetHasLanguageSettings = target as IHasPreferredMetadataLanguage;
  174. if (sourceHasLanguageSettings != null && targetHasLanguageSettings != null)
  175. {
  176. targetHasLanguageSettings.PreferredMetadataCountryCode = sourceHasLanguageSettings.PreferredMetadataCountryCode;
  177. targetHasLanguageSettings.PreferredMetadataLanguage = sourceHasLanguageSettings.PreferredMetadataLanguage;
  178. }
  179. var sourceHasDisplayOrder = source as IHasDisplayOrder;
  180. var targetHasDisplayOrder = target as IHasDisplayOrder;
  181. if (sourceHasDisplayOrder != null && targetHasDisplayOrder != null)
  182. {
  183. targetHasDisplayOrder.DisplayOrder = sourceHasDisplayOrder.DisplayOrder;
  184. }
  185. }
  186. }
  187. private static void MergeShortOverview(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  188. {
  189. var sourceHasShortOverview = source as IHasShortOverview;
  190. var targetHasShortOverview = target as IHasShortOverview;
  191. if (sourceHasShortOverview != null && targetHasShortOverview != null)
  192. {
  193. if (replaceData || string.IsNullOrEmpty(targetHasShortOverview.ShortOverview))
  194. {
  195. targetHasShortOverview.ShortOverview = sourceHasShortOverview.ShortOverview;
  196. }
  197. }
  198. }
  199. private static void MergeAlbumArtist(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  200. {
  201. var sourceHasAlbumArtist = source as IHasAlbumArtist;
  202. var targetHasAlbumArtist = target as IHasAlbumArtist;
  203. if (sourceHasAlbumArtist != null && targetHasAlbumArtist != null)
  204. {
  205. if (replaceData || targetHasAlbumArtist.AlbumArtists.Count > 0)
  206. {
  207. targetHasAlbumArtist.AlbumArtists = sourceHasAlbumArtist.AlbumArtists;
  208. }
  209. }
  210. }
  211. private static void MergeBudget(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  212. {
  213. var sourceHasBudget = source as IHasBudget;
  214. var targetHasBudget = target as IHasBudget;
  215. if (sourceHasBudget != null && targetHasBudget != null)
  216. {
  217. if (replaceData || !targetHasBudget.Budget.HasValue)
  218. {
  219. targetHasBudget.Budget = sourceHasBudget.Budget;
  220. }
  221. if (replaceData || !targetHasBudget.Revenue.HasValue)
  222. {
  223. targetHasBudget.Revenue = sourceHasBudget.Revenue;
  224. }
  225. }
  226. }
  227. private static void MergeMetascore(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  228. {
  229. var sourceCast = source as IHasMetascore;
  230. var targetCast = target as IHasMetascore;
  231. if (sourceCast != null && targetCast != null)
  232. {
  233. if (replaceData || !targetCast.Metascore.HasValue)
  234. {
  235. targetCast.Metascore = sourceCast.Metascore;
  236. }
  237. }
  238. }
  239. private static void MergeAwards(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  240. {
  241. var sourceCast = source as IHasAwards;
  242. var targetCast = target as IHasAwards;
  243. if (sourceCast != null && targetCast != null)
  244. {
  245. if (replaceData || string.IsNullOrEmpty(targetCast.AwardSummary))
  246. {
  247. targetCast.AwardSummary = sourceCast.AwardSummary;
  248. }
  249. }
  250. }
  251. private static void MergeCriticRating(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  252. {
  253. var sourceCast = source as IHasCriticRating;
  254. var targetCast = target as IHasCriticRating;
  255. if (sourceCast != null && targetCast != null)
  256. {
  257. if (replaceData || !targetCast.CriticRating.HasValue)
  258. {
  259. targetCast.CriticRating = sourceCast.CriticRating;
  260. }
  261. if (replaceData || string.IsNullOrEmpty(targetCast.CriticRatingSummary))
  262. {
  263. targetCast.CriticRatingSummary = sourceCast.CriticRatingSummary;
  264. }
  265. }
  266. }
  267. private static void MergeTaglines(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  268. {
  269. var sourceCast = source as IHasTaglines;
  270. var targetCast = target as IHasTaglines;
  271. if (sourceCast != null && targetCast != null)
  272. {
  273. if (replaceData || targetCast.Taglines.Count == 0)
  274. {
  275. targetCast.Taglines = sourceCast.Taglines;
  276. }
  277. }
  278. }
  279. private static void MergeTrailers(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  280. {
  281. var sourceCast = source as IHasTrailers;
  282. var targetCast = target as IHasTrailers;
  283. if (sourceCast != null && targetCast != null)
  284. {
  285. if (replaceData || targetCast.RemoteTrailers.Count == 0)
  286. {
  287. targetCast.RemoteTrailers = sourceCast.RemoteTrailers;
  288. }
  289. }
  290. }
  291. }
  292. }