Series.cs 8.2 KB

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