Skip to content

Commit

Permalink
fix: better episode selection logic in playlist for videos
Browse files Browse the repository at this point in the history
  • Loading branch information
revam committed Nov 1, 2024
1 parent 94045ad commit 31e56e9
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions Shoko.Server/Services/GeneratedPlaylistService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,28 @@ public bool TryParsePlaylist(string[] items, out IReadOnlyList<(IReadOnlyList<IS
yield return ([episode], videos);
}

private static readonly EpisodeType[] _videoToEpisodeGroupPreference = [EpisodeType.Episode, EpisodeType.Special, EpisodeType.Other, EpisodeType.Credits, EpisodeType.Trailer, EpisodeType.Parody];

private IEnumerable<(IReadOnlyList<IShokoEpisode> episodes, IReadOnlyList<IVideo> videos)> GetListForVideo(IVideo video)
{
var episode = video.Episodes
.OrderBy(episode => episode.Type)
.ThenBy(episode => episode.EpisodeNumber)
.FirstOrDefault();
return episode is not null ? [([episode], [video])] : [];
var crossReferences = video.CrossReferences;
var seriesOrder = crossReferences
.OrderBy(xref => xref.Order)
.Select(xref => xref.AnidbAnimeID)
.Distinct()
.ToArray();
var episodes = crossReferences
.DistinctBy(xref => xref.AnidbEpisodeID)
.Select(xref => (xref, episode: xref.ShokoEpisode!))
.Where(tuple => tuple.episode is not null)
.GroupBy(tuple => tuple.episode.Type)
.OrderBy(groupBy => Array.IndexOf(_videoToEpisodeGroupPreference, groupBy.Key))
.First()
.OrderBy(tuple => Array.IndexOf(seriesOrder, tuple.xref.AnidbAnimeID))
.ThenBy(tuple => tuple.episode.EpisodeNumber)
.Select(tuple => tuple.episode)
.ToList() as IReadOnlyList<IShokoEpisode>;
return episodes is { Count: > 0 } ? [(episodes, [video])] : [];
}

public FileStreamResult GeneratePlaylist(
Expand Down

0 comments on commit 31e56e9

Please sign in to comment.