ProviderUtils.cs 13 KB

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