ProviderUtils.cs 13 KB

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