Movie.cs 6.7 KB

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