Series.cs 8.8 KB

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