Series.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using MediaBrowser.Controller.Library;
  2. using MediaBrowser.Controller.Localization;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.Querying;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Runtime.Serialization;
  10. namespace MediaBrowser.Controller.Entities.TV
  11. {
  12. /// <summary>
  13. /// Class Series
  14. /// </summary>
  15. public class Series : Folder, IHasSoundtracks, IHasTrailers, IHasTags
  16. {
  17. public List<Guid> SpecialFeatureIds { get; set; }
  18. public List<Guid> SoundtrackIds { get; set; }
  19. public int SeasonCount { get; set; }
  20. public Series()
  21. {
  22. AirDays = new List<DayOfWeek>();
  23. SpecialFeatureIds = new List<Guid>();
  24. SoundtrackIds = new List<Guid>();
  25. RemoteTrailers = new List<MediaUrl>();
  26. LocalTrailerIds = new List<Guid>();
  27. Tags = new List<string>();
  28. DisplaySpecialsWithSeasons = true;
  29. }
  30. public bool DisplaySpecialsWithSeasons { get; set; }
  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 status.
  40. /// </summary>
  41. /// <value>The status.</value>
  42. public SeriesStatus? Status { get; set; }
  43. /// <summary>
  44. /// Gets or sets the air days.
  45. /// </summary>
  46. /// <value>The air days.</value>
  47. public List<DayOfWeek> AirDays { get; set; }
  48. /// <summary>
  49. /// Gets or sets the air time.
  50. /// </summary>
  51. /// <value>The air time.</value>
  52. public string AirTime { get; set; }
  53. /// <summary>
  54. /// Gets or sets the date last episode added.
  55. /// </summary>
  56. /// <value>The date last episode added.</value>
  57. public DateTime DateLastEpisodeAdded { get; set; }
  58. /// <summary>
  59. /// Series aren't included directly in indices - Their Episodes will roll up to them
  60. /// </summary>
  61. /// <value><c>true</c> if [include in index]; otherwise, <c>false</c>.</value>
  62. [IgnoreDataMember]
  63. public override bool IncludeInIndex
  64. {
  65. get
  66. {
  67. return false;
  68. }
  69. }
  70. /// <summary>
  71. /// Gets the user data key.
  72. /// </summary>
  73. /// <returns>System.String.</returns>
  74. public override string GetUserDataKey()
  75. {
  76. return this.GetProviderId(MetadataProviders.Tvdb) ?? this.GetProviderId(MetadataProviders.Tvcom) ?? base.GetUserDataKey();
  77. }
  78. // Studio, Genre and Rating will all be the same so makes no sense to index by these
  79. protected override IEnumerable<string> GetIndexByOptions()
  80. {
  81. return new List<string> {
  82. {LocalizedStrings.Instance.GetString("NoneDispPref")},
  83. {LocalizedStrings.Instance.GetString("PerformerDispPref")},
  84. {LocalizedStrings.Instance.GetString("DirectorDispPref")},
  85. {LocalizedStrings.Instance.GetString("YearDispPref")},
  86. };
  87. }
  88. /// <summary>
  89. /// Creates ResolveArgs on demand
  90. /// </summary>
  91. /// <param name="pathInfo">The path info.</param>
  92. /// <returns>ItemResolveArgs.</returns>
  93. protected internal override ItemResolveArgs CreateResolveArgs(FileSystemInfo pathInfo = null)
  94. {
  95. var args = base.CreateResolveArgs(pathInfo);
  96. Season.AddMetadataFiles(args);
  97. return args;
  98. }
  99. [IgnoreDataMember]
  100. public bool ContainsEpisodesWithoutSeasonFolders
  101. {
  102. get
  103. {
  104. return Children.OfType<Video>().Any();
  105. }
  106. }
  107. public override IEnumerable<BaseItem> GetChildren(User user, bool includeLinkedChildren)
  108. {
  109. return GetSeasons(user);
  110. }
  111. public IEnumerable<Season> GetSeasons(User user)
  112. {
  113. var config = user.Configuration;
  114. return GetSeasons(user, config.DisplayMissingEpisodes, config.DisplayUnairedEpisodes);
  115. }
  116. public IEnumerable<Season> GetSeasons(User user, bool includeMissingSeasons, bool includeVirtualUnaired)
  117. {
  118. var seasons = base.GetChildren(user, true)
  119. .OfType<Season>();
  120. if (!includeMissingSeasons && !includeVirtualUnaired)
  121. {
  122. seasons = seasons.Where(i => !i.IsMissingOrVirtualUnaired);
  123. }
  124. else
  125. {
  126. if (!includeMissingSeasons)
  127. {
  128. seasons = seasons.Where(i => !i.IsMissingSeason);
  129. }
  130. if (!includeVirtualUnaired)
  131. {
  132. seasons = seasons.Where(i => !i.IsVirtualUnaired);
  133. }
  134. }
  135. return LibraryManager
  136. .Sort(seasons, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending)
  137. .Cast<Season>();
  138. }
  139. public IEnumerable<Episode> GetEpisodes(User user, int seasonNumber)
  140. {
  141. var config = user.Configuration;
  142. return GetEpisodes(user, seasonNumber, config.DisplayMissingEpisodes, config.DisplayUnairedEpisodes);
  143. }
  144. public IEnumerable<Episode> GetEpisodes(User user, int seasonNumber, bool includeMissingEpisodes, bool includeVirtualUnairedEpisodes)
  145. {
  146. var episodes = GetRecursiveChildren(user)
  147. .OfType<Episode>();
  148. episodes = FilterEpisodesBySeason(episodes, seasonNumber, DisplaySpecialsWithSeasons);
  149. if (!includeMissingEpisodes)
  150. {
  151. episodes = episodes.Where(i => !i.IsMissingEpisode);
  152. }
  153. if (!includeVirtualUnairedEpisodes)
  154. {
  155. episodes = episodes.Where(i => !i.IsVirtualUnaired);
  156. }
  157. var sortBy = seasonNumber == 0 ? ItemSortBy.SortName : ItemSortBy.AiredEpisodeOrder;
  158. return LibraryManager.Sort(episodes, user, new[] { sortBy }, SortOrder.Ascending)
  159. .Cast<Episode>();
  160. }
  161. /// <summary>
  162. /// Filters the episodes by season.
  163. /// </summary>
  164. /// <param name="episodes">The episodes.</param>
  165. /// <param name="seasonNumber">The season number.</param>
  166. /// <param name="includeSpecials">if set to <c>true</c> [include specials].</param>
  167. /// <returns>IEnumerable{Episode}.</returns>
  168. public static IEnumerable<Episode> FilterEpisodesBySeason(IEnumerable<Episode> episodes, int seasonNumber, bool includeSpecials)
  169. {
  170. if (!includeSpecials || seasonNumber < 1)
  171. {
  172. return episodes.Where(i => (i.PhysicalSeasonNumber ?? -1) == seasonNumber);
  173. }
  174. return episodes.Where(i =>
  175. {
  176. var episode = i;
  177. if (episode != null)
  178. {
  179. var currentSeasonNumber = episode.AiredSeasonNumber;
  180. return currentSeasonNumber.HasValue && currentSeasonNumber.Value == seasonNumber;
  181. }
  182. return false;
  183. });
  184. }
  185. }
  186. }