Skip to content

Commit

Permalink
fix: improve show info community rating
Browse files Browse the repository at this point in the history
Improved the show info community rating by pre-caching it instead of calculating it on demand, allowing us to do different methods of calculation per show info type/impl., which allows us to use a direct value for show infos for tmdb shows and movies and anidb anime, while aggregating the value for shoko groups and tmdb movie collections.
  • Loading branch information
revam committed Dec 13, 2024
1 parent 6f23443 commit ce03a05
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions Shokofin/API/Info/ShowInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ public class ShowInfo : IExtendedItemInfo {
/// <summary>
/// Overall community rating of the show.
/// </summary>
public float CommunityRating =>
(float)(SeasonList.Aggregate(0f, (total, seasonInfo) => total + seasonInfo.CommunityRating.ToFloat(10)) / SeasonList.Count);
public float CommunityRating { get; init; }

/// <summary>
/// All tags from across all seasons.
Expand Down Expand Up @@ -182,10 +181,11 @@ public ShowInfo(ShokoApiClient client, SeasonInfo seasonInfo, string? collection
Overview = seasonInfo.Overview;
Overviews = seasonInfo.Overviews;
OriginalLanguageCode = seasonInfo.OriginalLanguageCode;
CommunityRating = seasonInfo.CommunityRating.ToFloat(10);
Tags = seasonInfo.Tags;
Genres = seasonInfo.Genres;
PremiereDate = seasonInfo.PremiereDate;
EndDate = seasonInfo.EndDate;
Genres = seasonInfo.Genres;
ProductionLocations = seasonInfo.ProductionLocations;
ContentRatings = seasonInfo.ContentRatings;
Studios = seasonInfo.Studios;
Expand Down Expand Up @@ -259,6 +259,10 @@ bool useGroupIdForCollection
specialsSet.Add(episodeInfo.Id, episodeInfo.IsAvailable);
}

var communityRatingSeasons = seasonOrderDictionary
.Where(pair => seasonNumberBaseDictionary.TryGetValue(pair.Value.Id, out var seasonNumber) && seasonNumber == pair.Key && pair.Value.CommunityRating is { Value: > 0 })
.Select(pair => pair.Value)
.ToList();
var anidbRating = ContentRatingUtil.GetCombinedAnidbContentRating(seasonOrderDictionary.Values);
var contentRatings = seasonOrderDictionary.Values
.SelectMany(sI => sI.ContentRatings)
Expand Down Expand Up @@ -300,6 +304,9 @@ bool useGroupIdForCollection
EndDate = !seasonList.Any(s => s.PremiereDate.HasValue && s.PremiereDate.Value < DateTime.Now && s.EndDate == null)
? seasonList.Select(s => s.EndDate).Where(s => s.HasValue).Max()
: null;
CommunityRating = communityRatingSeasons.Count > 0
? communityRatingSeasons.Aggregate(0f, (total, seasonInfo) => total + seasonInfo.CommunityRating.ToFloat(10)) / communityRatingSeasons.Count
: 0f;
Genres = seasonList.SelectMany(s => s.Genres).Distinct().ToArray();
Tags = seasonList.SelectMany(s => s.Tags).Distinct().ToArray();
Studios = seasonList.SelectMany(s => s.Studios).Distinct().ToArray();
Expand Down Expand Up @@ -385,6 +392,7 @@ public ShowInfo(ShokoApiClient client, TmdbShow tmdbShow, IReadOnlyList<SeasonIn
OriginalLanguageCode = tmdbShow.OriginalLanguage;
PremiereDate = tmdbShow.FirstAiredAt?.ToDateTime(TimeOnly.Parse("00:00:00", CultureInfo.InvariantCulture), DateTimeKind.Local);
EndDate = tmdbShow.LastAiredAt?.ToDateTime(TimeOnly.Parse("00:00:00", CultureInfo.InvariantCulture), DateTimeKind.Local);
CommunityRating = tmdbShow.UserRating.ToFloat(10);
Genres = seasonList.SelectMany(s => s.Genres).Distinct().ToArray();
Tags = seasonList.SelectMany(s => s.Tags).Distinct().ToArray();
Studios = seasonList.SelectMany(s => s.Studios).Distinct().ToArray();
Expand Down Expand Up @@ -423,6 +431,7 @@ public ShowInfo(ShokoApiClient client, TmdbMovie tmdbMovie, SeasonInfo seasonInf
OriginalLanguageCode = tmdbMovie.OriginalLanguage;
PremiereDate = releasedAt;
EndDate = releasedAt < DateTime.Now ? releasedAt : null;
CommunityRating = tmdbMovie.UserRating.ToFloat(10);
Genres = seasonInfo.Genres;
Tags = seasonInfo.Tags;
Studios = seasonInfo.Studios;
Expand Down Expand Up @@ -453,6 +462,10 @@ public ShowInfo(ShokoApiClient client, TmdbMovieCollection tmdbMovieCollection,
foreach (var episodeInfo in seasonInfo.SpecialsList)
specialsSet.Add(episodeInfo.Id, episodeInfo.IsAvailable);
}
var communityRatingSeasons = seasonOrderDictionary
.Where(pair => seasonNumberBaseDictionary.TryGetValue(pair.Value.Id, out var seasonNumber) && seasonNumber == pair.Key && pair.Value.CommunityRating is { Value: > 0 })
.Select(pair => pair.Value)
.ToList();

if (seasonList.All(seasonInfo => !string.IsNullOrEmpty(seasonInfo.AnidbId))) {
var anidbIdList = seasonList
Expand Down Expand Up @@ -501,6 +514,11 @@ public ShowInfo(ShokoApiClient client, TmdbMovieCollection tmdbMovieCollection,
Overview = tmdbMovieCollection.Overview;
Overviews = tmdbMovieCollection.Overviews;
OriginalLanguageCode = defaultSeason.OriginalLanguageCode;
PremiereDate = seasonList[0].PremiereDate;
EndDate = seasonList[^1].EndDate;
CommunityRating = communityRatingSeasons.Count > 0
? communityRatingSeasons.Aggregate(0f, (total, seasonInfo) => total + seasonInfo.CommunityRating.ToFloat(10)) / communityRatingSeasons.Count
: 0f;
Tags = seasonList.SelectMany(s => s.Tags).Distinct().ToArray();
Genres = seasonList.SelectMany(s => s.Genres).Distinct().ToArray();
ProductionLocations = seasonList
Expand Down

0 comments on commit ce03a05

Please sign in to comment.