ProviderUtils.cs 13 KB

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