|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using Microsoft.AspNetCore.Authorization; |
| 4 | +using Microsoft.AspNetCore.Mvc; |
| 5 | +using Shoko.Plugin.Abstractions.DataModels; |
| 6 | +using Shoko.Plugin.Abstractions.DataModels.Shoko; |
| 7 | +using Shoko.Server.API.Annotations; |
| 8 | +using Shoko.Server.API.ModelBinders; |
| 9 | +using Shoko.Server.API.v3.Models.Common; |
| 10 | +using Shoko.Server.API.v3.Models.Shoko; |
| 11 | +using Shoko.Server.Models; |
| 12 | +using Shoko.Server.Repositories.Cached; |
| 13 | +using Shoko.Server.Services; |
| 14 | +using Shoko.Server.Settings; |
| 15 | + |
| 16 | +#nullable enable |
| 17 | +namespace Shoko.Server.API.v3.Controllers; |
| 18 | + |
| 19 | +[ApiController, Route("/api/v{version:apiVersion}/[controller]"), ApiV3, Authorize] |
| 20 | +public class PlaylistController : BaseController |
| 21 | +{ |
| 22 | + private readonly GeneratedPlaylistService _playlistService; |
| 23 | + |
| 24 | + private readonly AnimeSeriesRepository _seriesRepository; |
| 25 | + |
| 26 | + private readonly AnimeEpisodeRepository _episodeRepository; |
| 27 | + |
| 28 | + private readonly VideoLocalRepository _videoRepository; |
| 29 | + |
| 30 | + public PlaylistController(ISettingsProvider settingsProvider, GeneratedPlaylistService playlistService, AnimeSeriesRepository animeSeriesRepository, AnimeEpisodeRepository animeEpisodeRepository, VideoLocalRepository videoRepository) : base(settingsProvider) |
| 31 | + { |
| 32 | + _playlistService = playlistService; |
| 33 | + _seriesRepository = animeSeriesRepository; |
| 34 | + _episodeRepository = animeEpisodeRepository; |
| 35 | + _videoRepository = videoRepository; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Generate an on-demand playlist for the specified list of items. |
| 40 | + /// </summary> |
| 41 | + /// <param name="items">The list of item IDs to include in the playlist. If no prefix is provided for an id then it will be assumed to be a series id.</param> |
| 42 | + /// <param name="releaseGroupID">The preferred release group ID if available.</param> |
| 43 | + /// <param name="onlyUnwatched">Only show the next unwatched episode.</param> |
| 44 | + /// <param name="includeSpecials">Include specials in the search.</param> |
| 45 | + /// <param name="includeOthers">Include other type episodes in the search.</param> |
| 46 | + /// <param name="includeRewatching">Include already watched episodes in the |
| 47 | + /// search if we determine the user is "re-watching" the series.</param> |
| 48 | + /// <param name="includeMediaInfo">Include media info data.</param> |
| 49 | + /// <param name="includeAbsolutePaths">Include absolute paths for the file locations.</param> |
| 50 | + /// <param name="includeXRefs">Include file/episode cross-references with the episodes.</param> |
| 51 | + /// <param name="includeDataFrom">Include data from selected <see cref="DataSource"/>s.</param> |
| 52 | + /// <returns></returns> |
| 53 | + [HttpGet("Generate")] |
| 54 | + public ActionResult<IReadOnlyList<(Episode, List<File>)>> GetGeneratedPlaylistJson( |
| 55 | + [FromQuery, ModelBinder(typeof(CommaDelimitedModelBinder))] string[]? items = null, |
| 56 | + [FromQuery] int? releaseGroupID = null, |
| 57 | + [FromQuery] bool onlyUnwatched = false, |
| 58 | + [FromQuery] bool includeSpecials = false, |
| 59 | + [FromQuery] bool includeOthers = false, |
| 60 | + [FromQuery] bool includeRewatching = false, |
| 61 | + [FromQuery] bool includeMediaInfo = false, |
| 62 | + [FromQuery] bool includeAbsolutePaths = false, |
| 63 | + [FromQuery] bool includeXRefs = false, |
| 64 | + [FromQuery, ModelBinder(typeof(CommaDelimitedModelBinder))] HashSet<DataSource>? includeDataFrom = null |
| 65 | + ) |
| 66 | + { |
| 67 | + var playlist = GetGeneratedPlaylistInternal(items, releaseGroupID, onlyUnwatched, includeSpecials, includeOthers, includeRewatching); |
| 68 | + if (!ModelState.IsValid) |
| 69 | + return ValidationProblem(ModelState); |
| 70 | + |
| 71 | + return playlist |
| 72 | + .Select(tuple => ( |
| 73 | + new Episode(HttpContext, (tuple.episode as SVR_AnimeEpisode)!, includeDataFrom, withXRefs: includeXRefs), |
| 74 | + tuple.videos |
| 75 | + .Select(video => new File(HttpContext, (video as SVR_VideoLocal)!, withXRefs: includeXRefs, includeDataFrom, includeMediaInfo, includeAbsolutePaths)) |
| 76 | + .ToList() |
| 77 | + )) |
| 78 | + .ToList(); |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Generate an on-demand playlist for the specified list of items, as a .m3u8 file. |
| 83 | + /// </summary> |
| 84 | + /// <param name="items">The list of item IDs to include in the playlist. If no prefix is provided for an id then it will be assumed to be a series id.</param> |
| 85 | + /// <param name="releaseGroupID">The preferred release group ID if available.</param> |
| 86 | + /// <param name="onlyUnwatched">Only show the next unwatched episode.</param> |
| 87 | + /// <param name="includeSpecials">Include specials in the search.</param> |
| 88 | + /// <param name="includeOthers">Include other type episodes in the search.</param> |
| 89 | + /// <param name="includeRewatching">Include already watched episodes in the |
| 90 | + /// search if we determine the user is "re-watching" the series.</param> |
| 91 | + /// <returns></returns> |
| 92 | + [ProducesResponseType(typeof(FileStreamResult), 200)] |
| 93 | + [ProducesResponseType(404)] |
| 94 | + [Produces("application/x-mpegURL")] |
| 95 | + [HttpGet("Generate.m3u8")] |
| 96 | + [HttpHead("Generate.m3u8")] |
| 97 | + public ActionResult GetGeneratedPlaylistM3U8( |
| 98 | + [FromQuery, ModelBinder(typeof(CommaDelimitedModelBinder))] string[]? items = null, |
| 99 | + [FromQuery] int? releaseGroupID = null, |
| 100 | + [FromQuery] bool onlyUnwatched = false, |
| 101 | + [FromQuery] bool includeSpecials = false, |
| 102 | + [FromQuery] bool includeOthers = false, |
| 103 | + [FromQuery] bool includeRewatching = false |
| 104 | + ) |
| 105 | + { |
| 106 | + var playlist = GetGeneratedPlaylistInternal(items, releaseGroupID, onlyUnwatched, includeSpecials, includeOthers, includeRewatching); |
| 107 | + if (!ModelState.IsValid) |
| 108 | + return ValidationProblem(ModelState); |
| 109 | + return _playlistService.GeneratePlaylist(playlist, "Mixed"); |
| 110 | + } |
| 111 | + |
| 112 | + private IReadOnlyList<(IShokoEpisode episode, IReadOnlyList<IVideo> videos)> GetGeneratedPlaylistInternal( |
| 113 | + string[]? items, |
| 114 | + int? releaseGroupID, |
| 115 | + bool onlyUnwatched = true, |
| 116 | + bool includeSpecials = true, |
| 117 | + bool includeOthers = false, |
| 118 | + bool includeRewatching = false |
| 119 | + ) |
| 120 | + { |
| 121 | + items ??= []; |
| 122 | + var playlist = new List<(IShokoEpisode, IReadOnlyList<IVideo>)>(); |
| 123 | + var index = -1; |
| 124 | + foreach (var id in items) |
| 125 | + { |
| 126 | + index++; |
| 127 | + if (string.IsNullOrEmpty(id)) |
| 128 | + continue; |
| 129 | + |
| 130 | + switch (id[0]) { |
| 131 | + case 'f': |
| 132 | + { |
| 133 | + if (!int.TryParse(id[1..], out var fileID) || fileID <= 0 || _videoRepository.GetByID(fileID) is not { } video) |
| 134 | + { |
| 135 | + ModelState.AddModelError(index.ToString(), $"Invalid file ID \"{id}\" at index {index}"); |
| 136 | + continue; |
| 137 | + } |
| 138 | + |
| 139 | + foreach (var tuple in _playlistService.GetListForVideo(video)) |
| 140 | + playlist.Add(tuple); |
| 141 | + break; |
| 142 | + } |
| 143 | + |
| 144 | + case 'e': |
| 145 | + { |
| 146 | + if (!int.TryParse(id[1..], out var episodeID) || episodeID <= 0 || _episodeRepository.GetByID(episodeID) is not { } episode) |
| 147 | + { |
| 148 | + ModelState.AddModelError(index.ToString(), $"Invalid episode ID \"{id}\" at index {index}"); |
| 149 | + continue; |
| 150 | + } |
| 151 | + |
| 152 | + foreach (var tuple in _playlistService.GetListForEpisode(episode, releaseGroupID)) |
| 153 | + playlist.Add(tuple); |
| 154 | + break; |
| 155 | + } |
| 156 | + |
| 157 | + case 's': |
| 158 | + { |
| 159 | + if (!int.TryParse(id[1..], out var seriesID) || seriesID <= 0 || _seriesRepository.GetByID(seriesID) is not { } series) |
| 160 | + { |
| 161 | + ModelState.AddModelError(index.ToString(), $"Invalid series ID \"{id}\" at index {index}"); |
| 162 | + continue; |
| 163 | + } |
| 164 | + |
| 165 | + foreach (var tuple in _playlistService.GetListForSeries(series, releaseGroupID, new() |
| 166 | + { |
| 167 | + IncludeCurrentlyWatching = !onlyUnwatched, |
| 168 | + IncludeSpecials = includeSpecials, |
| 169 | + IncludeOthers = includeOthers, |
| 170 | + IncludeRewatching = includeRewatching, |
| 171 | + })) |
| 172 | + playlist.Add(tuple); |
| 173 | + break; |
| 174 | + } |
| 175 | + |
| 176 | + default: |
| 177 | + { |
| 178 | + if (!int.TryParse(id, out var seriesID) || seriesID <= 0 || _seriesRepository.GetByID(seriesID) is not { } series) |
| 179 | + { |
| 180 | + ModelState.AddModelError(index.ToString(), $"Invalid series ID \"{id}\" at index {index}"); |
| 181 | + continue; |
| 182 | + } |
| 183 | + |
| 184 | + foreach (var tuple in _playlistService.GetListForSeries(series, releaseGroupID, new() |
| 185 | + { |
| 186 | + IncludeCurrentlyWatching = !onlyUnwatched, |
| 187 | + IncludeSpecials = includeSpecials, |
| 188 | + IncludeOthers = includeOthers, |
| 189 | + IncludeRewatching = includeRewatching, |
| 190 | + })) |
| 191 | + playlist.Add(tuple); |
| 192 | + break; |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + return playlist; |
| 197 | + } |
| 198 | +} |
0 commit comments