Movie.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using MediaBrowser.Controller.Providers;
  2. using MediaBrowser.Model.Configuration;
  3. using MediaBrowser.Model.Entities;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.Serialization;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.Controller.Entities.Movies
  12. {
  13. /// <summary>
  14. /// Class Movie
  15. /// </summary>
  16. public class Movie : Video, IHasCriticRating, IHasSoundtracks, IHasBudget, IHasKeywords, IHasTrailers, IHasThemeMedia, IHasTaglines, IHasPreferredMetadataLanguage, IHasAwards, IHasMetascore, IHasLookupInfo<MovieInfo>, ISupportsBoxSetGrouping
  17. {
  18. public List<Guid> SpecialFeatureIds { get; set; }
  19. public List<Guid> SoundtrackIds { get; set; }
  20. public List<Guid> ThemeSongIds { get; set; }
  21. public List<Guid> ThemeVideoIds { get; set; }
  22. /// <summary>
  23. /// This is just a cache to enable quick access by Id
  24. /// </summary>
  25. [IgnoreDataMember]
  26. public List<Guid> BoxSetIdList { get; set; }
  27. /// <summary>
  28. /// Gets or sets the preferred metadata country code.
  29. /// </summary>
  30. /// <value>The preferred metadata country code.</value>
  31. public string PreferredMetadataCountryCode { get; set; }
  32. public string PreferredMetadataLanguage { get; set; }
  33. public Movie()
  34. {
  35. SpecialFeatureIds = new List<Guid>();
  36. SoundtrackIds = new List<Guid>();
  37. RemoteTrailers = new List<MediaUrl>();
  38. LocalTrailerIds = new List<Guid>();
  39. ThemeSongIds = new List<Guid>();
  40. ThemeVideoIds = new List<Guid>();
  41. BoxSetIdList = new List<Guid>();
  42. Taglines = new List<string>();
  43. Keywords = new List<string>();
  44. }
  45. public string AwardSummary { get; set; }
  46. public float? Metascore { get; set; }
  47. public List<Guid> LocalTrailerIds { get; set; }
  48. public List<string> Keywords { get; set; }
  49. public List<MediaUrl> RemoteTrailers { get; set; }
  50. /// <summary>
  51. /// Gets or sets the taglines.
  52. /// </summary>
  53. /// <value>The taglines.</value>
  54. public List<string> Taglines { get; set; }
  55. /// <summary>
  56. /// Gets or sets the budget.
  57. /// </summary>
  58. /// <value>The budget.</value>
  59. public double? Budget { get; set; }
  60. /// <summary>
  61. /// Gets or sets the revenue.
  62. /// </summary>
  63. /// <value>The revenue.</value>
  64. public double? Revenue { get; set; }
  65. /// <summary>
  66. /// Gets or sets the critic rating.
  67. /// </summary>
  68. /// <value>The critic rating.</value>
  69. public float? CriticRating { get; set; }
  70. /// <summary>
  71. /// Gets or sets the critic rating summary.
  72. /// </summary>
  73. /// <value>The critic rating summary.</value>
  74. public string CriticRatingSummary { get; set; }
  75. /// <summary>
  76. /// Gets or sets the name of the TMDB collection.
  77. /// </summary>
  78. /// <value>The name of the TMDB collection.</value>
  79. public string TmdbCollectionName { get; set; }
  80. /// <summary>
  81. /// Gets the user data key.
  82. /// </summary>
  83. /// <returns>System.String.</returns>
  84. public override string GetUserDataKey()
  85. {
  86. return this.GetProviderId(MetadataProviders.Tmdb) ?? this.GetProviderId(MetadataProviders.Imdb) ?? base.GetUserDataKey();
  87. }
  88. protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, List<FileSystemInfo> fileSystemChildren, CancellationToken cancellationToken)
  89. {
  90. var hasChanges = await base.RefreshedOwnedItems(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  91. // Must have a parent to have special features
  92. // In other words, it must be part of the Parent/Child tree
  93. if (LocationType == LocationType.FileSystem && Parent != null && !IsInMixedFolder)
  94. {
  95. var specialFeaturesChanged = await RefreshSpecialFeatures(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  96. if (specialFeaturesChanged)
  97. {
  98. hasChanges = true;
  99. }
  100. }
  101. return hasChanges;
  102. }
  103. private async Task<bool> RefreshSpecialFeatures(MetadataRefreshOptions options, IEnumerable<FileSystemInfo> fileSystemChildren, CancellationToken cancellationToken)
  104. {
  105. var newItems = LoadSpecialFeatures(fileSystemChildren, options.DirectoryService).ToList();
  106. var newItemIds = newItems.Select(i => i.Id).ToList();
  107. var itemsChanged = !SpecialFeatureIds.SequenceEqual(newItemIds);
  108. var tasks = newItems.Select(i => i.RefreshMetadata(options, cancellationToken));
  109. await Task.WhenAll(tasks).ConfigureAwait(false);
  110. SpecialFeatureIds = newItemIds;
  111. return itemsChanged;
  112. }
  113. /// <summary>
  114. /// Loads the special features.
  115. /// </summary>
  116. /// <returns>IEnumerable{Video}.</returns>
  117. private IEnumerable<Video> LoadSpecialFeatures(IEnumerable<FileSystemInfo> fileSystemChildren, IDirectoryService directoryService)
  118. {
  119. var files = fileSystemChildren.OfType<DirectoryInfo>()
  120. .Where(i => string.Equals(i.Name, "extras", StringComparison.OrdinalIgnoreCase) || string.Equals(i.Name, "specials", StringComparison.OrdinalIgnoreCase))
  121. .SelectMany(i => i.EnumerateFiles("*", SearchOption.TopDirectoryOnly));
  122. return LibraryManager.ResolvePaths<Video>(files, directoryService, null).Select(video =>
  123. {
  124. // Try to retrieve it from the db. If we don't find it, use the resolved version
  125. var dbItem = LibraryManager.GetItemById(video.Id) as Video;
  126. if (dbItem != null)
  127. {
  128. video = dbItem;
  129. }
  130. return video;
  131. // Sort them so that the list can be easily compared for changes
  132. }).OrderBy(i => i.Path).ToList();
  133. }
  134. protected override bool GetBlockUnratedValue(UserConfiguration config)
  135. {
  136. return config.BlockUnratedItems.Contains(UnratedItem.Movie);
  137. }
  138. public MovieInfo GetLookupInfo()
  139. {
  140. return GetItemLookupInfo<MovieInfo>();
  141. }
  142. public override bool BeforeMetadataRefresh()
  143. {
  144. var hasChanges = base.BeforeMetadataRefresh();
  145. if (!ProductionYear.HasValue)
  146. {
  147. int? yearInName = null;
  148. string name;
  149. NameParser.ParseName(Name, out name, out yearInName);
  150. if (yearInName.HasValue)
  151. {
  152. ProductionYear = yearInName;
  153. hasChanges = true;
  154. }
  155. }
  156. return hasChanges;
  157. }
  158. }
  159. }