ProviderUtils.cs 13 KB

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