TrailerFromJsonProvider.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using MediaBrowser.Common.Serialization;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Providers;
  4. using MediaBrowser.Plugins.Trailers.Entities;
  5. using System;
  6. using System.ComponentModel.Composition;
  7. using System.IO;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Plugins.Trailers.Providers
  11. {
  12. /// <summary>
  13. /// Class TrailerFromJsonProvider
  14. /// </summary>
  15. [Export(typeof(BaseMetadataProvider))]
  16. class TrailerFromJsonProvider : BaseMetadataProvider
  17. {
  18. /// <summary>
  19. /// Supportses the specified item.
  20. /// </summary>
  21. /// <param name="item">The item.</param>
  22. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  23. public override bool Supports(BaseItem item)
  24. {
  25. var trailer = item as Trailer;
  26. return trailer != null && trailer.Parent is TrailerCollectionFolder;
  27. }
  28. /// <summary>
  29. /// Override this to return the date that should be compared to the last refresh date
  30. /// to determine if this provider should be re-fetched.
  31. /// </summary>
  32. /// <param name="item">The item.</param>
  33. /// <returns>DateTime.</returns>
  34. protected override DateTime CompareDate(BaseItem item)
  35. {
  36. var entry = item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.MetaLocation, "trailer.json"));
  37. return entry != null ? entry.Value.LastWriteTimeUtc : DateTime.MinValue;
  38. }
  39. /// <summary>
  40. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  41. /// </summary>
  42. /// <param name="item">The item.</param>
  43. /// <param name="force">if set to <c>true</c> [force].</param>
  44. /// <returns>Task{System.Boolean}.</returns>
  45. protected override Task<bool> FetchAsyncInternal(BaseItem item, bool force, CancellationToken cancellationToken)
  46. {
  47. return Task.Run(() => Fetch((Trailer)item));
  48. }
  49. /// <summary>
  50. /// Fetches the specified item.
  51. /// </summary>
  52. /// <param name="item">The item.</param>
  53. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  54. private bool Fetch(Trailer item)
  55. {
  56. var metadataFile = item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.MetaLocation, "trailer.json"));
  57. if (metadataFile.HasValue)
  58. {
  59. var tempTrailer = JsonSerializer.DeserializeFromFile<Trailer>(metadataFile.Value.Path);
  60. ImportMetdata(tempTrailer, item);
  61. SetLastRefreshed(item, DateTime.UtcNow);
  62. return true;
  63. }
  64. return false;
  65. }
  66. /// <summary>
  67. /// Gets the priority.
  68. /// </summary>
  69. /// <value>The priority.</value>
  70. public override MetadataProviderPriority Priority
  71. {
  72. get { return MetadataProviderPriority.First; }
  73. }
  74. /// <summary>
  75. /// Imports the metdata.
  76. /// </summary>
  77. /// <param name="source">The source.</param>
  78. /// <param name="target">The target.</param>
  79. private void ImportMetdata(Trailer source, Trailer target)
  80. {
  81. if (!string.IsNullOrWhiteSpace(source.Name))
  82. {
  83. target.Name = source.Name;
  84. }
  85. if (source.RunTimeTicks.HasValue)
  86. {
  87. target.RunTimeTicks = source.RunTimeTicks;
  88. }
  89. if (source.Genres != null)
  90. {
  91. foreach (var entry in source.Genres)
  92. {
  93. target.AddGenre(entry);
  94. }
  95. }
  96. if (!string.IsNullOrWhiteSpace(source.OfficialRating))
  97. {
  98. target.OfficialRating = source.OfficialRating;
  99. }
  100. if (!string.IsNullOrWhiteSpace(source.Overview))
  101. {
  102. target.Overview = source.Overview;
  103. }
  104. if (source.People != null)
  105. {
  106. target.AddPeople(source.People);
  107. }
  108. if (source.PremiereDate.HasValue)
  109. {
  110. target.PremiereDate = source.PremiereDate;
  111. }
  112. if (source.ProductionYear.HasValue)
  113. {
  114. target.ProductionYear = source.ProductionYear;
  115. }
  116. if (source.Studios != null)
  117. {
  118. foreach (var entry in source.Studios)
  119. {
  120. target.AddStudio(entry);
  121. }
  122. }
  123. }
  124. }
  125. }