Movie.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using MediaBrowser.Model.Entities;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace MediaBrowser.Controller.Entities.Movies
  9. {
  10. /// <summary>
  11. /// Class Movie
  12. /// </summary>
  13. public class Movie : Video, IHasCriticRating, IHasSoundtracks, IHasBudget, IHasTrailers, IHasThemeMedia, IHasTaglines, IHasTags
  14. {
  15. public List<Guid> SpecialFeatureIds { get; set; }
  16. public List<Guid> SoundtrackIds { get; set; }
  17. public List<Guid> ThemeSongIds { get; set; }
  18. public List<Guid> ThemeVideoIds { get; set; }
  19. public Movie()
  20. {
  21. SpecialFeatureIds = new List<Guid>();
  22. SoundtrackIds = new List<Guid>();
  23. RemoteTrailers = new List<MediaUrl>();
  24. LocalTrailerIds = new List<Guid>();
  25. ThemeSongIds = new List<Guid>();
  26. ThemeVideoIds = new List<Guid>();
  27. Taglines = new List<string>();
  28. Tags = new List<string>();
  29. }
  30. public List<Guid> LocalTrailerIds { get; set; }
  31. public List<MediaUrl> RemoteTrailers { get; set; }
  32. /// <summary>
  33. /// Gets or sets the tags.
  34. /// </summary>
  35. /// <value>The tags.</value>
  36. public List<string> Tags { get; set; }
  37. /// <summary>
  38. /// Gets or sets the taglines.
  39. /// </summary>
  40. /// <value>The taglines.</value>
  41. public List<string> Taglines { get; set; }
  42. /// <summary>
  43. /// Gets or sets the budget.
  44. /// </summary>
  45. /// <value>The budget.</value>
  46. public double? Budget { get; set; }
  47. /// <summary>
  48. /// Gets or sets the revenue.
  49. /// </summary>
  50. /// <value>The revenue.</value>
  51. public double? Revenue { get; set; }
  52. /// <summary>
  53. /// Gets or sets the critic rating.
  54. /// </summary>
  55. /// <value>The critic rating.</value>
  56. public float? CriticRating { get; set; }
  57. /// <summary>
  58. /// Gets or sets the critic rating summary.
  59. /// </summary>
  60. /// <value>The critic rating summary.</value>
  61. public string CriticRatingSummary { get; set; }
  62. /// <summary>
  63. /// Gets or sets the name of the TMDB collection.
  64. /// </summary>
  65. /// <value>The name of the TMDB collection.</value>
  66. public string TmdbCollectionName { get; set; }
  67. /// <summary>
  68. /// Gets the user data key.
  69. /// </summary>
  70. /// <returns>System.String.</returns>
  71. public override string GetUserDataKey()
  72. {
  73. return this.GetProviderId(MetadataProviders.Tmdb) ?? this.GetProviderId(MetadataProviders.Imdb) ?? base.GetUserDataKey();
  74. }
  75. /// <summary>
  76. /// Overrides the base implementation to refresh metadata for special features
  77. /// </summary>
  78. /// <param name="cancellationToken">The cancellation token.</param>
  79. /// <param name="forceSave">if set to <c>true</c> [is new item].</param>
  80. /// <param name="forceRefresh">if set to <c>true</c> [force].</param>
  81. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  82. /// <param name="resetResolveArgs">The reset resolve args.</param>
  83. /// <returns>Task{System.Boolean}.</returns>
  84. public override async Task<bool> RefreshMetadata(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true, bool resetResolveArgs = true)
  85. {
  86. // Kick off a task to refresh the main item
  87. var result = await base.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders, resetResolveArgs).ConfigureAwait(false);
  88. var specialFeaturesChanged = false;
  89. // Must have a parent to have special features
  90. // In other words, it must be part of the Parent/Child tree
  91. if (LocationType == LocationType.FileSystem && Parent != null && !IsInMixedFolder)
  92. {
  93. specialFeaturesChanged = await RefreshSpecialFeatures(cancellationToken, forceSave, forceRefresh, allowSlowProviders).ConfigureAwait(false);
  94. }
  95. return specialFeaturesChanged || result;
  96. }
  97. private async Task<bool> RefreshSpecialFeatures(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true)
  98. {
  99. var newItems = LoadSpecialFeatures().ToList();
  100. var newItemIds = newItems.Select(i => i.Id).ToList();
  101. var itemsChanged = !SpecialFeatureIds.SequenceEqual(newItemIds);
  102. var tasks = newItems.Select(i => i.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders, resetResolveArgs: false));
  103. var results = await Task.WhenAll(tasks).ConfigureAwait(false);
  104. SpecialFeatureIds = newItemIds;
  105. return itemsChanged || results.Contains(true);
  106. }
  107. /// <summary>
  108. /// Loads the special features.
  109. /// </summary>
  110. /// <returns>IEnumerable{Video}.</returns>
  111. private IEnumerable<Video> LoadSpecialFeatures()
  112. {
  113. FileSystemInfo folder;
  114. try
  115. {
  116. folder = ResolveArgs.GetFileSystemEntryByName("specials");
  117. }
  118. catch (IOException ex)
  119. {
  120. Logger.ErrorException("Error getting ResolveArgs for {0}", ex, Path);
  121. return new List<Video>();
  122. }
  123. // Path doesn't exist. No biggie
  124. if (folder == null)
  125. {
  126. return new List<Video>();
  127. }
  128. IEnumerable<FileSystemInfo> files;
  129. try
  130. {
  131. files = new DirectoryInfo(folder.FullName).EnumerateFiles();
  132. }
  133. catch (IOException ex)
  134. {
  135. Logger.ErrorException("Error loading special features for {0}", ex, Name);
  136. return new List<Video>();
  137. }
  138. return LibraryManager.ResolvePaths<Video>(files, null).Select(video =>
  139. {
  140. // Try to retrieve it from the db. If we don't find it, use the resolved version
  141. var dbItem = LibraryManager.RetrieveItem(video.Id) as Video;
  142. if (dbItem != null)
  143. {
  144. dbItem.ResetResolveArgs(video.ResolveArgs);
  145. video = dbItem;
  146. }
  147. return video;
  148. });
  149. }
  150. }
  151. }