Series.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. using System.Threading;
  2. using System.Threading.Tasks;
  3. using MediaBrowser.Controller.Localization;
  4. using MediaBrowser.Controller.Providers;
  5. using MediaBrowser.Model.Configuration;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Querying;
  8. using MediaBrowser.Model.Users;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Runtime.Serialization;
  13. namespace MediaBrowser.Controller.Entities.TV
  14. {
  15. /// <summary>
  16. /// Class Series
  17. /// </summary>
  18. public class Series : Folder, IHasSoundtracks, IHasTrailers, IHasDisplayOrder, IHasLookupInfo<SeriesInfo>, IHasSpecialFeatures, IMetadataContainer
  19. {
  20. public List<Guid> SpecialFeatureIds { get; set; }
  21. public List<Guid> SoundtrackIds { get; set; }
  22. public int SeasonCount { get; set; }
  23. public int? AnimeSeriesIndex { get; set; }
  24. public Series()
  25. {
  26. AirDays = new List<DayOfWeek>();
  27. SpecialFeatureIds = new List<Guid>();
  28. SoundtrackIds = new List<Guid>();
  29. RemoteTrailers = new List<MediaUrl>();
  30. LocalTrailerIds = new List<Guid>();
  31. RemoteTrailerIds = new List<Guid>();
  32. DisplaySpecialsWithSeasons = true;
  33. }
  34. [IgnoreDataMember]
  35. public override bool SupportsAddingToPlaylist
  36. {
  37. get { return true; }
  38. }
  39. [IgnoreDataMember]
  40. public override bool IsPreSorted
  41. {
  42. get
  43. {
  44. return true;
  45. }
  46. }
  47. public bool DisplaySpecialsWithSeasons { get; set; }
  48. public List<Guid> LocalTrailerIds { get; set; }
  49. public List<Guid> RemoteTrailerIds { get; set; }
  50. public List<MediaUrl> RemoteTrailers { get; set; }
  51. /// <summary>
  52. /// airdate, dvd or absolute
  53. /// </summary>
  54. public string DisplayOrder { get; set; }
  55. /// <summary>
  56. /// Gets or sets the status.
  57. /// </summary>
  58. /// <value>The status.</value>
  59. public SeriesStatus? Status { get; set; }
  60. /// <summary>
  61. /// Gets or sets the air days.
  62. /// </summary>
  63. /// <value>The air days.</value>
  64. public List<DayOfWeek> AirDays { get; set; }
  65. /// <summary>
  66. /// Gets or sets the air time.
  67. /// </summary>
  68. /// <value>The air time.</value>
  69. public string AirTime { get; set; }
  70. /// <summary>
  71. /// Gets or sets the date last episode added.
  72. /// </summary>
  73. /// <value>The date last episode added.</value>
  74. [IgnoreDataMember]
  75. public DateTime DateLastEpisodeAdded
  76. {
  77. get
  78. {
  79. return GetRecursiveChildren(i => i is Episode)
  80. .Select(i => i.DateCreated)
  81. .OrderByDescending(i => i)
  82. .FirstOrDefault();
  83. }
  84. }
  85. /// <summary>
  86. /// Series aren't included directly in indices - Their Episodes will roll up to them
  87. /// </summary>
  88. /// <value><c>true</c> if [include in index]; otherwise, <c>false</c>.</value>
  89. [IgnoreDataMember]
  90. public override bool IncludeInIndex
  91. {
  92. get
  93. {
  94. return false;
  95. }
  96. }
  97. /// <summary>
  98. /// Gets the user data key.
  99. /// </summary>
  100. /// <returns>System.String.</returns>
  101. protected override string CreateUserDataKey()
  102. {
  103. return this.GetProviderId(MetadataProviders.Tvdb) ?? this.GetProviderId(MetadataProviders.Tvcom) ?? base.CreateUserDataKey();
  104. }
  105. /// <summary>
  106. /// Gets the trailer ids.
  107. /// </summary>
  108. /// <returns>List&lt;Guid&gt;.</returns>
  109. public List<Guid> GetTrailerIds()
  110. {
  111. var list = LocalTrailerIds.ToList();
  112. list.AddRange(RemoteTrailerIds);
  113. return list;
  114. }
  115. // Studio, Genre and Rating will all be the same so makes no sense to index by these
  116. protected override IEnumerable<string> GetIndexByOptions()
  117. {
  118. return new List<string> {
  119. {LocalizedStrings.Instance.GetString("NoneDispPref")},
  120. {LocalizedStrings.Instance.GetString("PerformerDispPref")},
  121. {LocalizedStrings.Instance.GetString("DirectorDispPref")},
  122. {LocalizedStrings.Instance.GetString("YearDispPref")},
  123. };
  124. }
  125. [IgnoreDataMember]
  126. public bool ContainsEpisodesWithoutSeasonFolders
  127. {
  128. get
  129. {
  130. return Children.OfType<Video>().Any();
  131. }
  132. }
  133. public override IEnumerable<BaseItem> GetChildren(User user, bool includeLinkedChildren)
  134. {
  135. return GetSeasons(user);
  136. }
  137. public IEnumerable<Season> GetSeasons(User user)
  138. {
  139. var config = user.Configuration;
  140. return GetSeasons(user, config.DisplayMissingEpisodes, config.DisplayUnairedEpisodes);
  141. }
  142. public IEnumerable<Season> GetSeasons(User user, bool includeMissingSeasons, bool includeVirtualUnaired)
  143. {
  144. var seasons = base.GetChildren(user, true)
  145. .OfType<Season>();
  146. if (!includeMissingSeasons && !includeVirtualUnaired)
  147. {
  148. seasons = seasons.Where(i => !i.IsMissingOrVirtualUnaired);
  149. }
  150. else
  151. {
  152. if (!includeMissingSeasons)
  153. {
  154. seasons = seasons.Where(i => !i.IsMissingSeason);
  155. }
  156. if (!includeVirtualUnaired)
  157. {
  158. seasons = seasons.Where(i => !i.IsVirtualUnaired);
  159. }
  160. }
  161. return LibraryManager
  162. .Sort(seasons, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending)
  163. .Cast<Season>();
  164. }
  165. public IEnumerable<Episode> GetEpisodes(User user)
  166. {
  167. var config = user.Configuration;
  168. var allEpisodes = GetSeasons(user, true, true)
  169. .SelectMany(i => i.GetEpisodes(user, config.DisplayMissingEpisodes, config.DisplayUnairedEpisodes))
  170. .Reverse()
  171. .ToList();
  172. // Specials could appear twice based on above - once in season 0, once in the aired season
  173. // This depends on settings for that series
  174. // When this happens, remove the duplicate from season 0
  175. var returnList = new List<Episode>();
  176. foreach (var episode in allEpisodes)
  177. {
  178. if (!returnList.Contains(episode))
  179. {
  180. returnList.Insert(0, episode);
  181. }
  182. }
  183. return returnList;
  184. }
  185. public async Task RefreshAllMetadata(MetadataRefreshOptions refreshOptions, IProgress<double> progress, CancellationToken cancellationToken)
  186. {
  187. // Refresh bottom up, children first, then the boxset
  188. // By then hopefully the movies within will have Tmdb collection values
  189. var items = GetRecursiveChildren().ToList();
  190. var seasons = items.OfType<Season>().ToList();
  191. var otherItems = items.Except(seasons).ToList();
  192. var totalItems = seasons.Count + otherItems.Count;
  193. var numComplete = 0;
  194. refreshOptions = new MetadataRefreshOptions(refreshOptions);
  195. refreshOptions.IsPostRecursiveRefresh = true;
  196. // Refresh songs
  197. foreach (var item in seasons)
  198. {
  199. cancellationToken.ThrowIfCancellationRequested();
  200. await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  201. numComplete++;
  202. double percent = numComplete;
  203. percent /= totalItems;
  204. progress.Report(percent * 100);
  205. }
  206. // Refresh current item
  207. await RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  208. // Refresh all non-songs
  209. foreach (var item in otherItems)
  210. {
  211. cancellationToken.ThrowIfCancellationRequested();
  212. await item.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  213. numComplete++;
  214. double percent = numComplete;
  215. percent /= totalItems;
  216. progress.Report(percent * 100);
  217. }
  218. await ProviderManager.RefreshMetadata(this, refreshOptions, cancellationToken).ConfigureAwait(false);
  219. progress.Report(100);
  220. }
  221. public IEnumerable<Episode> GetEpisodes(User user, int seasonNumber)
  222. {
  223. var config = user.Configuration;
  224. return GetEpisodes(user, seasonNumber, config.DisplayMissingEpisodes, config.DisplayUnairedEpisodes);
  225. }
  226. public IEnumerable<Episode> GetEpisodes(User user, int seasonNumber, bool includeMissingEpisodes, bool includeVirtualUnairedEpisodes)
  227. {
  228. return GetEpisodes(user, seasonNumber, includeMissingEpisodes, includeVirtualUnairedEpisodes,
  229. new List<Episode>());
  230. }
  231. internal IEnumerable<Episode> GetEpisodes(User user, int seasonNumber, bool includeMissingEpisodes, bool includeVirtualUnairedEpisodes, IEnumerable<Episode> additionalEpisodes)
  232. {
  233. var episodes = GetRecursiveChildren(user, i => i is Episode)
  234. .Cast<Episode>();
  235. episodes = FilterEpisodesBySeason(episodes, seasonNumber, DisplaySpecialsWithSeasons);
  236. episodes = episodes.Concat(additionalEpisodes).Distinct();
  237. if (!includeMissingEpisodes)
  238. {
  239. episodes = episodes.Where(i => !i.IsMissingEpisode);
  240. }
  241. if (!includeVirtualUnairedEpisodes)
  242. {
  243. episodes = episodes.Where(i => !i.IsVirtualUnaired);
  244. }
  245. var sortBy = seasonNumber == 0 ? ItemSortBy.SortName : ItemSortBy.AiredEpisodeOrder;
  246. return LibraryManager.Sort(episodes, user, new[] { sortBy }, SortOrder.Ascending)
  247. .Cast<Episode>();
  248. }
  249. /// <summary>
  250. /// Filters the episodes by season.
  251. /// </summary>
  252. /// <param name="episodes">The episodes.</param>
  253. /// <param name="seasonNumber">The season number.</param>
  254. /// <param name="includeSpecials">if set to <c>true</c> [include specials].</param>
  255. /// <returns>IEnumerable{Episode}.</returns>
  256. public static IEnumerable<Episode> FilterEpisodesBySeason(IEnumerable<Episode> episodes, int seasonNumber, bool includeSpecials)
  257. {
  258. if (!includeSpecials || seasonNumber < 1)
  259. {
  260. return episodes.Where(i => (i.PhysicalSeasonNumber ?? -1) == seasonNumber);
  261. }
  262. return episodes.Where(i =>
  263. {
  264. var episode = i;
  265. if (episode != null)
  266. {
  267. var currentSeasonNumber = episode.AiredSeasonNumber;
  268. return currentSeasonNumber.HasValue && currentSeasonNumber.Value == seasonNumber;
  269. }
  270. return false;
  271. });
  272. }
  273. protected override bool GetBlockUnratedValue(UserPolicy config)
  274. {
  275. return config.BlockUnratedItems.Contains(UnratedItem.Series);
  276. }
  277. public SeriesInfo GetLookupInfo()
  278. {
  279. var info = GetItemLookupInfo<SeriesInfo>();
  280. info.AnimeSeriesIndex = AnimeSeriesIndex;
  281. return info;
  282. }
  283. public override bool BeforeMetadataRefresh()
  284. {
  285. var hasChanges = base.BeforeMetadataRefresh();
  286. if (!ProductionYear.HasValue)
  287. {
  288. var info = LibraryManager.ParseName(Name);
  289. var yearInName = info.Year;
  290. if (yearInName.HasValue)
  291. {
  292. ProductionYear = yearInName;
  293. hasChanges = true;
  294. }
  295. }
  296. return hasChanges;
  297. }
  298. }
  299. }