ProviderUtils.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. var sourceHasTags = source as IHasTags;
  135. var targetHasTags = target as IHasTags;
  136. if (sourceHasTags != null && targetHasTags != null)
  137. {
  138. if (replaceData || targetHasTags.Tags.Count == 0)
  139. {
  140. targetHasTags.Tags = sourceHasTags.Tags;
  141. }
  142. }
  143. }
  144. if (!lockedFields.Contains(MetadataFields.Keywords))
  145. {
  146. if (replaceData || target.Keywords.Count == 0)
  147. {
  148. target.Keywords = source.Keywords;
  149. }
  150. }
  151. if (!lockedFields.Contains(MetadataFields.ProductionLocations))
  152. {
  153. var sourceHasProductionLocations = source as IHasProductionLocations;
  154. var targetHasProductionLocations = target as IHasProductionLocations;
  155. if (sourceHasProductionLocations != null && targetHasProductionLocations != null)
  156. {
  157. if (replaceData || targetHasProductionLocations.ProductionLocations.Count == 0)
  158. {
  159. targetHasProductionLocations.ProductionLocations = sourceHasProductionLocations.ProductionLocations;
  160. }
  161. }
  162. }
  163. if (replaceData || !target.VoteCount.HasValue)
  164. {
  165. target.VoteCount = source.VoteCount;
  166. }
  167. foreach (var id in source.ProviderIds)
  168. {
  169. var key = id.Key;
  170. // Don't replace existing Id's.
  171. if (replaceData || !target.ProviderIds.ContainsKey(key))
  172. {
  173. target.ProviderIds[key] = id.Value;
  174. }
  175. }
  176. MergeAlbumArtist(source, target, lockedFields, replaceData);
  177. MergeBudget(source, target, lockedFields, replaceData);
  178. MergeMetascore(source, target, lockedFields, replaceData);
  179. MergeCriticRating(source, target, lockedFields, replaceData);
  180. MergeAwards(source, target, lockedFields, replaceData);
  181. MergeTaglines(source, target, lockedFields, replaceData);
  182. MergeTrailers(source, target, lockedFields, replaceData);
  183. MergeShortOverview(source, target, lockedFields, replaceData);
  184. if (mergeMetadataSettings)
  185. {
  186. MergeMetadataSettings(source, target);
  187. }
  188. }
  189. public static void MergeMetadataSettings(BaseItem source,
  190. BaseItem target)
  191. {
  192. target.ForcedSortName = source.ForcedSortName;
  193. target.LockedFields = source.LockedFields;
  194. target.IsLocked = source.IsLocked;
  195. target.DisplayMediaType = source.DisplayMediaType;
  196. // Grab the value if it's there, but if not then don't overwrite the default
  197. if (source.DateCreated != default(DateTime))
  198. {
  199. target.DateCreated = source.DateCreated;
  200. }
  201. target.PreferredMetadataCountryCode = source.PreferredMetadataCountryCode;
  202. target.PreferredMetadataLanguage = source.PreferredMetadataLanguage;
  203. var sourceHasDisplayOrder = source as IHasDisplayOrder;
  204. var targetHasDisplayOrder = target as IHasDisplayOrder;
  205. if (sourceHasDisplayOrder != null && targetHasDisplayOrder != null)
  206. {
  207. targetHasDisplayOrder.DisplayOrder = sourceHasDisplayOrder.DisplayOrder;
  208. }
  209. }
  210. private static void MergeShortOverview(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  211. {
  212. var sourceHasShortOverview = source as IHasShortOverview;
  213. var targetHasShortOverview = target as IHasShortOverview;
  214. if (sourceHasShortOverview != null && targetHasShortOverview != null)
  215. {
  216. if (replaceData || string.IsNullOrEmpty(targetHasShortOverview.ShortOverview))
  217. {
  218. targetHasShortOverview.ShortOverview = sourceHasShortOverview.ShortOverview;
  219. }
  220. }
  221. }
  222. private static void MergeAlbumArtist(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  223. {
  224. var sourceHasAlbumArtist = source as IHasAlbumArtist;
  225. var targetHasAlbumArtist = target as IHasAlbumArtist;
  226. if (sourceHasAlbumArtist != null && targetHasAlbumArtist != null)
  227. {
  228. if (replaceData || targetHasAlbumArtist.AlbumArtists.Count == 0)
  229. {
  230. targetHasAlbumArtist.AlbumArtists = sourceHasAlbumArtist.AlbumArtists;
  231. }
  232. }
  233. }
  234. private static void MergeBudget(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  235. {
  236. var sourceHasBudget = source as IHasBudget;
  237. var targetHasBudget = target as IHasBudget;
  238. if (sourceHasBudget != null && targetHasBudget != null)
  239. {
  240. if (replaceData || !targetHasBudget.Budget.HasValue)
  241. {
  242. targetHasBudget.Budget = sourceHasBudget.Budget;
  243. }
  244. if (replaceData || !targetHasBudget.Revenue.HasValue)
  245. {
  246. targetHasBudget.Revenue = sourceHasBudget.Revenue;
  247. }
  248. }
  249. }
  250. private static void MergeMetascore(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  251. {
  252. var sourceCast = source as IHasMetascore;
  253. var targetCast = target as IHasMetascore;
  254. if (sourceCast != null && targetCast != null)
  255. {
  256. if (replaceData || !targetCast.Metascore.HasValue)
  257. {
  258. targetCast.Metascore = sourceCast.Metascore;
  259. }
  260. }
  261. }
  262. private static void MergeAwards(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  263. {
  264. var sourceCast = source as IHasAwards;
  265. var targetCast = target as IHasAwards;
  266. if (sourceCast != null && targetCast != null)
  267. {
  268. if (replaceData || string.IsNullOrEmpty(targetCast.AwardSummary))
  269. {
  270. targetCast.AwardSummary = sourceCast.AwardSummary;
  271. }
  272. }
  273. }
  274. private static void MergeCriticRating(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  275. {
  276. var sourceCast = source as IHasCriticRating;
  277. var targetCast = target as IHasCriticRating;
  278. if (sourceCast != null && targetCast != null)
  279. {
  280. if (replaceData || !targetCast.CriticRating.HasValue)
  281. {
  282. targetCast.CriticRating = sourceCast.CriticRating;
  283. }
  284. if (replaceData || string.IsNullOrEmpty(targetCast.CriticRatingSummary))
  285. {
  286. targetCast.CriticRatingSummary = sourceCast.CriticRatingSummary;
  287. }
  288. }
  289. }
  290. private static void MergeTaglines(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  291. {
  292. var sourceCast = source as IHasTaglines;
  293. var targetCast = target as IHasTaglines;
  294. if (sourceCast != null && targetCast != null)
  295. {
  296. if (replaceData || targetCast.Taglines.Count == 0)
  297. {
  298. targetCast.Taglines = sourceCast.Taglines;
  299. }
  300. }
  301. }
  302. private static void MergeTrailers(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)
  303. {
  304. var sourceCast = source as IHasTrailers;
  305. var targetCast = target as IHasTrailers;
  306. if (sourceCast != null && targetCast != null)
  307. {
  308. if (replaceData || targetCast.RemoteTrailers.Count == 0)
  309. {
  310. targetCast.RemoteTrailers = sourceCast.RemoteTrailers;
  311. }
  312. }
  313. }
  314. }
  315. }