Series.cs 7.8 KB

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