TvShowsController.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Linq;
  3. using Jellyfin.Api.Constants;
  4. using Jellyfin.Api.Extensions;
  5. using MediaBrowser.Controller.Dto;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.Entities.TV;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.Net;
  10. using MediaBrowser.Controller.TV;
  11. using MediaBrowser.Model.Dto;
  12. using MediaBrowser.Model.Entities;
  13. using MediaBrowser.Model.Querying;
  14. using Microsoft.AspNetCore.Authorization;
  15. using Microsoft.AspNetCore.Mvc;
  16. namespace Jellyfin.Api.Controllers
  17. {
  18. /// <summary>
  19. /// The tv shows controller.
  20. /// </summary>
  21. [Route("/Shows")]
  22. [Authorize(Policy = Policies.DefaultAuthorization)]
  23. public class TvShowsController : BaseJellyfinApiController
  24. {
  25. private readonly IUserManager _userManager;
  26. private readonly ILibraryManager _libraryManager;
  27. private readonly IDtoService _dtoService;
  28. private readonly ITVSeriesManager _tvSeriesManager;
  29. private readonly IAuthorizationContext _authContext;
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="TvShowsController"/> class.
  32. /// </summary>
  33. /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
  34. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  35. /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
  36. /// <param name="tvSeriesManager">Instance of the <see cref="ITVSeriesManager"/> interface.</param>
  37. /// <param name="authContext">Instance of the <see cref="IAuthorizationContext"/> interface.</param>
  38. public TvShowsController(
  39. IUserManager userManager,
  40. ILibraryManager libraryManager,
  41. IDtoService dtoService,
  42. ITVSeriesManager tvSeriesManager,
  43. IAuthorizationContext authContext)
  44. {
  45. _userManager = userManager;
  46. _libraryManager = libraryManager;
  47. _dtoService = dtoService;
  48. _tvSeriesManager = tvSeriesManager;
  49. _authContext = authContext;
  50. }
  51. /// <summary>
  52. /// Gets a list of next up episodes.
  53. /// </summary>
  54. /// <param name="userId">The user id of the user to get the next up episodes for.</param>
  55. /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped from the results.</param>
  56. /// <param name="limit">Optional. The maximum number of records to return.</param>
  57. /// <param name="fields">Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines, TrailerUrls.</param>
  58. /// <param name="seriesId">Optional. Filter by series id.</param>
  59. /// <param name="parentId">Optional. Specify this to localize the search to a specific item or folder. Omit to use the root.</param>
  60. /// <param name="enableImges">Optional. Include image information in output.</param>
  61. /// <param name="imageTypeLimit">Optional. The max number of images to return, per image type.</param>
  62. /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
  63. /// <param name="enableUserData">Optional. Include user data.</param>
  64. /// <param name="enableTotalRecordCount">Whether to enable the total records count. Defaults to true.</param>
  65. /// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the next up episodes.</returns>
  66. [HttpGet("NextUp")]
  67. public ActionResult<QueryResult<BaseItemDto>> GetNextUp(
  68. [FromQuery] Guid userId,
  69. [FromQuery] int? startIndex,
  70. [FromQuery] int? limit,
  71. [FromQuery] string? fields,
  72. [FromQuery] string? seriesId,
  73. [FromQuery] string? parentId,
  74. [FromQuery] bool? enableImges,
  75. [FromQuery] int? imageTypeLimit,
  76. [FromQuery] string enableImageTypes,
  77. [FromQuery] bool? enableUserData,
  78. [FromQuery] bool enableTotalRecordCount = true)
  79. {
  80. var options = new DtoOptions()
  81. .AddItemFields(fields)
  82. .AddClientFields(Request)
  83. .AddAdditionalDtoOptions(enableImges, enableUserData, imageTypeLimit, enableImageTypes);
  84. var result = _tvSeriesManager.GetNextUp(
  85. new NextUpQuery
  86. {
  87. Limit = limit,
  88. ParentId = parentId,
  89. SeriesId = seriesId,
  90. StartIndex = startIndex,
  91. UserId = userId,
  92. EnableTotalRecordCount = enableTotalRecordCount
  93. },
  94. options);
  95. var user = _userManager.GetUserById(userId);
  96. var returnItems = _dtoService.GetBaseItemDtos(result.Items, options, user);
  97. return new QueryResult<BaseItemDto>
  98. {
  99. TotalRecordCount = result.TotalRecordCount,
  100. Items = returnItems
  101. };
  102. }
  103. /// <summary>
  104. /// Gets a list of upcoming episodes.
  105. /// </summary>
  106. /// <param name="userId">The user id of the user to get the upcoming episodes for.</param>
  107. /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped from the results.</param>
  108. /// <param name="limit">Optional. The maximum number of records to return.</param>
  109. /// <param name="fields">Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines, TrailerUrls.</param>
  110. /// <param name="seriesId">Optional. Filter by series id.</param>
  111. /// <param name="parentId">Optional. Specify this to localize the search to a specific item or folder. Omit to use the root.</param>
  112. /// <param name="enableImges">Optional. Include image information in output.</param>
  113. /// <param name="imageTypeLimit">Optional. The max number of images to return, per image type.</param>
  114. /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
  115. /// <param name="enableUserData">Optional. Include user data.</param>
  116. /// <param name="enableTotalRecordCount">Whether to enable the total records count. Defaults to true.</param>
  117. /// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the next up episodes.</returns>
  118. [HttpGet("Upcoming")]
  119. public ActionResult<QueryResult<BaseItemDto>> GetUpcomingEpisodes(
  120. [FromQuery] Guid userId,
  121. [FromQuery] int? startIndex,
  122. [FromQuery] int? limit,
  123. [FromQuery] string? fields,
  124. [FromQuery] string? seriesId,
  125. [FromQuery] string? parentId,
  126. [FromQuery] bool? enableImges,
  127. [FromQuery] int? imageTypeLimit,
  128. [FromQuery] string enableImageTypes,
  129. [FromQuery] bool? enableUserData,
  130. [FromQuery] bool enableTotalRecordCount = true)
  131. {
  132. var user = _userManager.GetUserById(userId);
  133. var minPremiereDate = DateTime.Now.Date.ToUniversalTime().AddDays(-1);
  134. var parentIdGuid = string.IsNullOrWhiteSpace(parentId) ? Guid.Empty : new Guid(parentId);
  135. var options = new DtoOptions()
  136. .AddItemFields(fields)
  137. .AddClientFields(Request)
  138. .AddAdditionalDtoOptions(enableImges, enableUserData, imageTypeLimit, enableImageTypes);
  139. var itemsResult = _libraryManager.GetItemList(new InternalItemsQuery(user)
  140. {
  141. IncludeItemTypes = new[] { nameof(Episode) },
  142. OrderBy = new[] { ItemSortBy.PremiereDate, ItemSortBy.SortName }.Select(i => new ValueTuple<string, SortOrder>(i, SortOrder.Ascending)).ToArray(),
  143. MinPremiereDate = minPremiereDate,
  144. StartIndex = startIndex,
  145. Limit = limit,
  146. ParentId = parentIdGuid,
  147. Recursive = true,
  148. DtoOptions = options
  149. });
  150. var returnItems = _dtoService.GetBaseItemDtos(itemsResult, options, user);
  151. return new QueryResult<BaseItemDto>
  152. {
  153. TotalRecordCount = itemsResult.Count,
  154. Items = returnItems
  155. };
  156. }
  157. }
  158. }