Skip to content

Commit

Permalink
feat: add runtime length to webui series data
Browse files Browse the repository at this point in the history
  • Loading branch information
revam committed Nov 21, 2023
1 parent 2094167 commit d61324a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
30 changes: 29 additions & 1 deletion Shoko.Server/API/v3/Helpers/WebUIFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Shoko.Models.Enums;
using Shoko.Models.Server;
using Shoko.Server.API.v3.Models.Common;
using Shoko.Server.Models;

Expand All @@ -19,18 +22,43 @@ public WebUIFactory(FilterFactory filterFactory, SeriesFactory seriesFactory)
public Models.Shoko.WebUI.WebUISeriesExtra GetWebUISeriesExtra(SVR_AnimeSeries series)
{
var anime = series.GetAnime();
var animeEpisodes = anime.GetAniDBEpisodes();
var runtimeLength = GuessCorrectRuntimeLength(animeEpisodes);
var cast = _seriesFactory.GetCast(anime.AnimeID, new () { Role.CreatorRoleType.Studio, Role.CreatorRoleType.Producer });

var result = new Models.Shoko.WebUI.WebUISeriesExtra
{
RuntimeLength = runtimeLength,
FirstAirSeason = _filterFactory.GetFirstAiringSeasonGroupFilter(anime),
Studios = cast.Where(role => role.RoleName == Role.CreatorRoleType.Studio).Select(role => role.Staff).ToList(),
Producers = cast.Where(role => role.RoleName == Role.CreatorRoleType.Producer).Select(role => role.Staff).ToList(),
SourceMaterial = _seriesFactory.GetTags(anime, TagFilter.Filter.Invert | TagFilter.Filter.Source, excludeDescriptions: true).FirstOrDefault()?.Name ?? "Original Work",
};
return result;
}


private static TimeSpan? GuessCorrectRuntimeLength(IReadOnlyList<AniDB_Episode> episodes)
{
// Return early if empty.
if (episodes == null || episodes.Count == 0)
return null;

// Filter the list and return if empty.
episodes = episodes
.Where(episode => episode.EpisodeType == (int)EpisodeType.Episode)
.ToList();
if (episodes.Count == 0)
return null;

// Get the runtime length of the only episode.
if (episodes.Count == 1)
return TimeSpan.FromSeconds(episodes[0].LengthSeconds);

// Get the runtime length of the episode in the middle of the stack.
var index = (int)Math.Round(episodes.Count / 2d);
return TimeSpan.FromSeconds(episodes[index].LengthSeconds);
}

public Models.Shoko.WebUI.WebUIGroupExtra GetWebUIGroupExtra(SVR_AnimeGroup group, SVR_AnimeSeries series, SVR_AniDB_Anime anime,
TagFilter.Filter filter = TagFilter.Filter.None, bool orderByName = false, int tagLimit = 30)
{
Expand Down
6 changes: 6 additions & 0 deletions Shoko.Server/API/v3/Models/Shoko/WebUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ public class WebUIGroupExtra

public class WebUISeriesExtra
{
/// <summary>
/// Common runtime length for the episodes, if the series have any
/// episodes.
/// </summary>
public TimeSpan? RuntimeLength { get; set; }

/// <summary>
/// The first season this show was aired in.
/// </summary>
Expand Down

0 comments on commit d61324a

Please sign in to comment.