Series.cs 12 KB

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