ProviderUtils.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. using System.Linq;
  8. using MediaBrowser.Controller.Extensions;
  9. namespace MediaBrowser.Providers.Manager
  10. {
  11. public static class ProviderUtils
  12. {
  13. public static void MergeBaseItemData<T>(MetadataResult<T> sourceResult,
  14. MetadataResult<T> targetResult,
  15. MetadataFields[] lockedFields,
  16. bool replaceData,
  17. bool mergeMetadataSettings)
  18. where T : BaseItem
  19. {
  20. var source = sourceResult.Item;
  21. var target = targetResult.Item;
  22. if (source == null)
  23. {
  24. throw new ArgumentNullException("source");
  25. }
  26. if (target == null)
  27. {
  28. throw new ArgumentNullException("target");
  29. }
  30. if (!lockedFields.Contains(MetadataFields.Name))
  31. {
  32. if (replaceData || string.IsNullOrEmpty(target.Name))
  33. {
  34. // Safeguard against incoming data having an emtpy name
  35. if (!string.IsNullOrWhiteSpace(source.Name))
  36. {
  37. target.Name = source.Name;
  38. }
  39. }
  40. }
  41. if (replaceData || string.IsNullOrEmpty(target.OriginalTitle))
  42. {
  43. // Safeguard against incoming data having an emtpy name
  44. if (!string.IsNullOrWhiteSpace(source.OriginalTitle))
  45. {
  46. target.OriginalTitle = source.OriginalTitle;
  47. }
  48. }
  49. if (replaceData || !target.CommunityRating.HasValue)
  50. {
  51. target.CommunityRating = source.CommunityRating;
  52. }
  53. if (replaceData || !target.EndDate.HasValue)
  54. {
  55. target.EndDate = source.EndDate;
  56. }
  57. if (!lockedFields.Contains(MetadataFields.Genres))
  58. {
  59. if (replaceData || target.Genres.Count == 0)
  60. {
  61. target.Genres = source.Genres;
  62. }
  63. }
  64. if (replaceData || string.IsNullOrEmpty(target.HomePageUrl))
  65. {
  66. target.HomePageUrl = source.HomePageUrl;
  67. if (!string.IsNullOrWhiteSpace(target.HomePageUrl) && target.HomePageUrl.IndexOf("http", StringComparison.OrdinalIgnoreCase) != 0)
  68. {
  69. target.HomePageUrl = "http://" + target.HomePageUrl;
  70. }
  71. }
  72. if (replaceData || !target.IndexNumber.HasValue)
  73. {
  74. target.IndexNumber = source.IndexNumber;
  75. }
  76. if (!lockedFields.Contains(MetadataFields.OfficialRating))
  77. {
  78. if (replaceData || string.IsNullOrEmpty(target.OfficialRating))
  79. {
  80. target.OfficialRating = source.OfficialRating;
  81. }
  82. }
  83. if (replaceData || string.IsNullOrEmpty(target.CustomRating))
  84. {
  85. target.CustomRating = source.CustomRating;
  86. }
  87. if (replaceData || string.IsNullOrEmpty(target.Tagline))
  88. {
  89. target.Tagline = source.Tagline;
  90. }
  91. if (!lockedFields.Contains(MetadataFields.Overview))
  92. {
  93. if (replaceData || string.IsNullOrEmpty(target.Overview))
  94. {
  95. target.Overview = source.Overview;
  96. }
  97. }
  98. if (replaceData || !target.ParentIndexNumber.HasValue)
  99. {
  100. target.ParentIndexNumber = source.ParentIndexNumber;
  101. }
  102. if (!lockedFields.Contains(MetadataFields.Cast))
  103. {
  104. if (replaceData || targetResult.People == null || targetResult.People.Count == 0)
  105. {
  106. targetResult.People = sourceResult.People;
  107. }
  108. else if (targetResult.People != null && sourceResult.People != null)
  109. {
  110. MergePeople(sourceResult.People, targetResult.People);
  111. }
  112. }
  113. if (replaceData || !target.PremiereDate.HasValue)
  114. {
  115. target.PremiereDate = source.PremiereDate;
  116. }
  117. if (replaceData || !target.ProductionYear.HasValue)
  118. {
  119. target.ProductionYear = source.ProductionYear;
  120. }
  121. if (!lockedFields.Contains(MetadataFields.Runtime))
  122. {
  123. if (replaceData || !target.RunTimeTicks.HasValue)
  124. {
  125. if (!(target is Audio) && !(target is Video))
  126. {
  127. target.RunTimeTicks = source.RunTimeTicks;
  128. }
  129. }
  130. }
  131. if (!lockedFields.Contains(MetadataFields.Studios))
  132. {
  133. if (replaceData || target.Studios.Length == 0)
  134. {
  135. target.Studios = source.Studios;
  136. }
  137. }
  138. if (!lockedFields.Contains(MetadataFields.Tags))
  139. {
  140. if (replaceData || target.Tags.Length == 0)
  141. {
  142. target.Tags = source.Tags;
  143. }
  144. }
  145. if (!lockedFields.Contains(MetadataFields.ProductionLocations))
  146. {
  147. if (replaceData || target.ProductionLocations.Length == 0)
  148. {
  149. target.ProductionLocations = source.ProductionLocations;
  150. }
  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. MergeCriticRating(source, target, lockedFields, replaceData);
  163. MergeTrailers(source, target, lockedFields, replaceData);
  164. MergeVideoInfo(source, target, lockedFields, replaceData);
  165. MergeDisplayOrder(source, target, lockedFields, replaceData);
  166. //if (!lockedFields.Contains(MetadataFields.SortName))
  167. {
  168. if (replaceData || string.IsNullOrEmpty(target.ForcedSortName))
  169. {
  170. var forcedSortName = source.ForcedSortName;
  171. if (!string.IsNullOrWhiteSpace(forcedSortName))
  172. {
  173. target.ForcedSortName = forcedSortName;
  174. }
  175. }
  176. }
  177. //if (!lockedFields.Contains(MetadataFields.DisplayMediaType))
  178. {
  179. var targetVideo = target as Video;
  180. var sourceVideo = source as Video;
  181. if (sourceVideo != null && targetVideo != null)
  182. {
  183. if (replaceData || string.IsNullOrEmpty(targetVideo.DisplayMediaType))
  184. {
  185. // Safeguard against incoming data having an emtpy name
  186. if (!string.IsNullOrWhiteSpace(sourceVideo.DisplayMediaType))
  187. {
  188. targetVideo.DisplayMediaType = sourceVideo.DisplayMediaType;
  189. }
  190. }
  191. }
  192. }
  193. if (mergeMetadataSettings)
  194. {
  195. MergeMetadataSettings(source, target);
  196. }
  197. }
  198. private static void MergePeople(List<PersonInfo> source, List<PersonInfo> target)
  199. {
  200. foreach (var person in target)
  201. {
  202. var normalizedName = person.Name.RemoveDiacritics();
  203. var personInSource = source.FirstOrDefault(i => string.Equals(i.Name.RemoveDiacritics(), normalizedName, StringComparison.OrdinalIgnoreCase));
  204. if (personInSource != null)
  205. {
  206. foreach (var providerId in personInSource.ProviderIds)
  207. {
  208. if (!person.ProviderIds.ContainsKey(providerId.Key))
  209. {
  210. person.ProviderIds[providerId.Key] = providerId.Value;
  211. }
  212. }
  213. if (string.IsNullOrWhiteSpace(person.ImageUrl))
  214. {
  215. person.ImageUrl = personInSource.ImageUrl;
  216. }
  217. }
  218. }
  219. }
  220. public static void MergeMetadataSettings(BaseItem source,
  221. BaseItem target)
  222. {
  223. target.LockedFields = source.LockedFields;
  224. target.IsLocked = source.IsLocked;
  225. // Grab the value if it's there, but if not then don't overwrite the default
  226. if (source.DateCreated != default(DateTime))
  227. {
  228. target.DateCreated = source.DateCreated;
  229. }
  230. target.PreferredMetadataCountryCode = source.PreferredMetadataCountryCode;
  231. target.PreferredMetadataLanguage = source.PreferredMetadataLanguage;
  232. }
  233. private static void MergeDisplayOrder(BaseItem source, BaseItem target, MetadataFields[] lockedFields, bool replaceData)
  234. {
  235. var sourceHasDisplayOrder = source as IHasDisplayOrder;
  236. var targetHasDisplayOrder = target as IHasDisplayOrder;
  237. if (sourceHasDisplayOrder != null && targetHasDisplayOrder != null)
  238. {
  239. targetHasDisplayOrder.DisplayOrder = sourceHasDisplayOrder.DisplayOrder;
  240. }
  241. }
  242. private static void MergeAlbumArtist(BaseItem source, BaseItem target, MetadataFields[] lockedFields, bool replaceData)
  243. {
  244. var sourceHasAlbumArtist = source as IHasAlbumArtist;
  245. var targetHasAlbumArtist = target as IHasAlbumArtist;
  246. if (sourceHasAlbumArtist != null && targetHasAlbumArtist != null)
  247. {
  248. if (replaceData || targetHasAlbumArtist.AlbumArtists.Length == 0)
  249. {
  250. targetHasAlbumArtist.AlbumArtists = sourceHasAlbumArtist.AlbumArtists;
  251. }
  252. }
  253. }
  254. private static void MergeCriticRating(BaseItem source, BaseItem target, MetadataFields[] lockedFields, bool replaceData)
  255. {
  256. if (replaceData || !target.CriticRating.HasValue)
  257. {
  258. target.CriticRating = source.CriticRating;
  259. }
  260. }
  261. private static void MergeTrailers(BaseItem source, BaseItem target, MetadataFields[] lockedFields, bool replaceData)
  262. {
  263. var sourceCast = source as IHasTrailers;
  264. var targetCast = target as IHasTrailers;
  265. if (sourceCast != null && targetCast != null)
  266. {
  267. if (replaceData || targetCast.RemoteTrailers.Length == 0)
  268. {
  269. targetCast.RemoteTrailers = sourceCast.RemoteTrailers;
  270. }
  271. }
  272. }
  273. private static void MergeVideoInfo(BaseItem source, BaseItem target, MetadataFields[] lockedFields, bool replaceData)
  274. {
  275. var sourceCast = source as Video;
  276. var targetCast = target as Video;
  277. if (sourceCast != null && targetCast != null)
  278. {
  279. if (replaceData || targetCast.Video3DFormat == null)
  280. {
  281. targetCast.Video3DFormat = sourceCast.Video3DFormat;
  282. }
  283. }
  284. }
  285. }
  286. }