ProviderUtils.cs 13 KB

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