Skip to content

Commit

Permalink
fix: use series title then episode title for video title in playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
revam committed Oct 22, 2024
1 parent b0779a7 commit e1abd9f
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions Shoko.Server/Services/GeneratedPlaylistService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
using System.Web;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Shoko.Commons.Extensions;
using Shoko.Plugin.Abstractions.DataModels;
using Shoko.Plugin.Abstractions.DataModels.Shoko;
using Shoko.Plugin.Abstractions.Enums;
using Shoko.Server.Utilities;

Expand All @@ -25,17 +25,15 @@ public GeneratedPlaylistService(IHttpContextAccessor contentAccessor)

public FileStreamResult GeneratePlaylistForVideo(IVideo video)
{
var episode = video.CrossReferences
.Select(xref => xref.AnidbEpisode)
.WhereNotNull()
var episode = video.Episodes
.OrderBy(episode => episode.Type)
.ThenBy(episode => episode.EpisodeNumber)
.FirstOrDefault();
return GeneratePlaylistForEpisodeList(episode is not null ? [(episode, [video])] : []);
}

private FileStreamResult GeneratePlaylistForEpisodeList(
IEnumerable<(IEpisode ep, IReadOnlyList<IVideo> videos)> episodeList,
IEnumerable<(IShokoEpisode ep, IReadOnlyList<IVideo> videos)> episodeList,
string name = "Playlist"
)
{
Expand All @@ -50,13 +48,13 @@ private FileStreamResult GeneratePlaylistForEpisodeList(
);
foreach (var (episode, videos) in episodeList)
{
var anime = episode.Series;
if (anime is null)
var series = episode.Series;
if (series is null)
continue;

var index = 0;
foreach (var video in videos)
m3U8.Append(GetEpisodeEntry(new UriBuilder(uri.ToString()), anime, episode, video, ++index, videos.Count));
m3U8.Append(GetEpisodeEntry(new UriBuilder(uri.ToString()), series, episode, video, ++index, videos.Count));
}

var bytes = Encoding.UTF8.GetBytes(m3U8.ToString());
Expand All @@ -67,9 +65,9 @@ private FileStreamResult GeneratePlaylistForEpisodeList(
};
}

private static string GetEpisodeEntry(UriBuilder uri, ISeries anime, IEpisode episode, IVideo video, int part, int totalParts)
private static string GetEpisodeEntry(UriBuilder uri, IShokoSeries series, IShokoEpisode episode, IVideo video, int part, int totalParts)
{
var poster = anime.GetPreferredImageForType(ImageEntityType.Poster) ?? anime.DefaultPoster;
var poster = series.GetPreferredImageForType(ImageEntityType.Poster) ?? series.DefaultPoster;
var parts = totalParts > 1 ? $" ({part}/{totalParts})" : string.Empty;
var episodeNumber = episode.Type is EpisodeType.Episode
? episode.EpisodeNumber.ToString()
Expand All @@ -81,16 +79,16 @@ private static string GetEpisodeEntry(UriBuilder uri, ISeries anime, IEpisode ep
if (poster is not null && !string.IsNullOrEmpty(poster.RemoteURL))
queryString.Add("posterUrl", poster.RemoteURL);
queryString.Add("appId", "07a58b50-5109-5aa3-abbc-782fed0df04f"); // plugin id
queryString.Add("animeId", anime.ID.ToString());
queryString.Add("animeName", anime.PreferredTitle);
queryString.Add("epId", episode.ID.ToString());
queryString.Add("animeId", series.AnidbAnimeID.ToString());
queryString.Add("animeName", series.PreferredTitle);
queryString.Add("epId", episode.AnidbEpisodeID.ToString());
queryString.Add("episodeName", episode.PreferredTitle);
queryString.Add("epNo", episodeNumber);
queryString.Add("epCount", anime.EpisodeCounts.Episodes.ToString());
queryString.Add("restricted", anime.Restricted ? "true" : "false");
queryString.Add("epCount", series.EpisodeCounts.Episodes.ToString());
queryString.Add("restricted", series.Restricted ? "true" : "false");

uri.Path = $"{(uri.Path.Length > 1 ? uri.Path + "/" : "/")}api/v3/File/{video.ID}/Stream";
uri.Query = queryString.ToString();
return $"#EXTINF:-1,{episode.PreferredTitle} {episodeNumber}{parts}\n{uri}\n";
return $"#EXTINF:-1,{series.PreferredTitle} - {episodeNumber} - {episode.PreferredTitle}{parts}\n{uri}\n";
}
}

0 comments on commit e1abd9f

Please sign in to comment.