Skip to content

Commit

Permalink
feat: add "fake" tmdb season xrefs for series
Browse files Browse the repository at this point in the history
- Added "fake" tmdb season cross-references inferred from the linked episodes.

- Added the images linked to the seasons to the shoko series entry, because apparently there isn't a 100% overlap between the show images and season images.
  • Loading branch information
revam committed Oct 13, 2024
1 parent 1ddec53 commit 0cfc5d4
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public CrossRef_AniDB_TMDB_Episode(int anidbEpisodeId, int anidbAnimeId, int tmd
public TMDB_Episode? TmdbEpisode =>
TmdbEpisodeID == 0 ? null : RepoFactory.TMDB_Episode.GetByTmdbEpisodeID(TmdbEpisodeID);

public CrossRef_AniDB_TMDB_Season? TmdbSeasonCrossReference =>
TmdbEpisode is { } tmdbEpisode
? new(AnidbAnimeID, tmdbEpisode.TmdbSeasonID, TmdbShowID, tmdbEpisode.SeasonNumber)
: null;

public TMDB_Season? TmdbSeason =>
TmdbEpisode?.TmdbSeason;

Expand Down
97 changes: 97 additions & 0 deletions Shoko.Server/Models/CrossReference/CrossRef_AniDB_TMDB_Season.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Shoko.Plugin.Abstractions.DataModels;
using Shoko.Plugin.Abstractions.Enums;
using Shoko.Server.Models.TMDB;
using Shoko.Server.Repositories;

#nullable enable
namespace Shoko.Server.Models.CrossReference;

/// <summary>
/// Not actually stored in the database, but made from the episode cross-reference.
/// </summary>
public class CrossRef_AniDB_TMDB_Season : IEquatable<CrossRef_AniDB_TMDB_Season>
{
#region Columns

public int AnidbAnimeID { get; set; }

public int TmdbShowID { get; set; }

public int TmdbSeasonID { get; set; }

public int SeasonNumber { get; set; }

#endregion
#region Constructors

public CrossRef_AniDB_TMDB_Season(int anidbAnimeId, int tmdbSeasonId, int tmdbShowId, int seasonNumber = 1)
{
AnidbAnimeID = anidbAnimeId;
TmdbSeasonID = tmdbSeasonId;
TmdbShowID = tmdbShowId;
SeasonNumber = seasonNumber;
}

#endregion
#region Methods

public SVR_AniDB_Anime? AnidbAnime =>
RepoFactory.AniDB_Anime.GetByAnimeID(AnidbAnimeID);

public SVR_AnimeSeries? AnimeSeries =>
RepoFactory.AnimeSeries.GetByAnimeID(AnidbAnimeID);

public TMDB_Season? TmdbSeason =>
TmdbSeasonID == 0 ? null : RepoFactory.TMDB_Season.GetByTmdbSeasonID(TmdbSeasonID);

public TMDB_Show? TmdbShow =>
TmdbShowID == 0 ? null : RepoFactory.TMDB_Show.GetByTmdbShowID(TmdbShowID);

/// <summary>
/// Get all images for the episode, or all images for the given
/// <paramref name="entityType"/> provided for the episode.
/// </summary>
/// <param name="entityType">If set, will restrict the returned list to only
/// containing the images of the given entity type.</param>
/// <returns>A read-only list of images that are linked to the episode.
/// </returns>
public IReadOnlyList<TMDB_Image> GetImages(ImageEntityType? entityType = null) => entityType.HasValue
? RepoFactory.TMDB_Image.GetByTmdbSeasonIDAndType(TmdbSeasonID, entityType.Value)
: RepoFactory.TMDB_Image.GetByTmdbSeasonID(TmdbSeasonID);

/// <summary>
/// Get all images for the episode, or all images for the given
/// <paramref name="entityType"/> provided for the episode.
/// </summary>
/// <param name="entityType">If set, will restrict the returned list to only
/// containing the images of the given entity type.</param>
/// <param name="preferredImages">The preferred images.</param>
/// <returns>A read-only list of images that are linked to the episode.
/// </returns>
public IReadOnlyList<IImageMetadata> GetImages(ImageEntityType? entityType, IReadOnlyDictionary<ImageEntityType, IImageMetadata> preferredImages) =>
GetImages(entityType)
.GroupBy(i => i.ImageType)
.SelectMany(gB => preferredImages.TryGetValue(gB.Key, out var pI) ? gB.Select(i => i.Equals(pI) ? pI : i) : gB)
.ToList();

public bool Equals(CrossRef_AniDB_TMDB_Season? other)
{
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return AnidbAnimeID == other.AnidbAnimeID
&& TmdbSeasonID == other.TmdbSeasonID
&& TmdbShowID == other.TmdbShowID
&& SeasonNumber == other.SeasonNumber;
}

public override bool Equals(object? obj)
=> Equals(obj as CrossRef_AniDB_TMDB_Season);

public override int GetHashCode()
=> HashCode.Combine(AnidbAnimeID, TmdbSeasonID, TmdbShowID, SeasonNumber);

#endregion
}
20 changes: 20 additions & 0 deletions Shoko.Server/Models/SVR_AnimeSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ public IReadOnlyList<IImageMetadata> GetImages(ImageEntityType? entityType = nul
}
foreach (var xref in TmdbShowCrossReferences)
images.AddRange(xref.GetImages(entityType, preferredImages));
foreach (var xref in TmdbSeasonCrossReferences)
images.AddRange(xref.GetImages(entityType, preferredImages));
foreach (var xref in TmdbMovieCrossReferences.DistinctBy(xref => xref.TmdbMovieID))
images.AddRange(xref.GetImages(entityType, preferredImages));

Expand Down Expand Up @@ -389,6 +391,24 @@ public IReadOnlyList<CrossRef_AniDB_TMDB_Episode> GetTmdbEpisodeCrossReferences(
? RepoFactory.CrossRef_AniDB_TMDB_Episode.GetOnlyByAnidbAnimeAndTmdbShowIDs(AniDB_ID, tmdbShowId.Value)
: RepoFactory.CrossRef_AniDB_TMDB_Episode.GetByAnidbAnimeID(AniDB_ID);

public IReadOnlyList<CrossRef_AniDB_TMDB_Season> TmdbSeasonCrossReferences =>
TmdbEpisodeCrossReferences
.Select(xref => xref.TmdbSeasonCrossReference)
.WhereNotNull()
.DistinctBy(xref => xref.TmdbSeasonID)
.ToList();

public IReadOnlyList<TMDB_Season> TmdbSeasons => TmdbSeasonCrossReferences
.Select(xref => xref.TmdbSeason)
.WhereNotNull()
.ToList();

public IReadOnlyList<CrossRef_AniDB_TMDB_Season> GetTmdbSeasonCrossReferences(int? tmdbShowId = null) =>
GetTmdbEpisodeCrossReferences(tmdbShowId)
.Select(xref => xref.TmdbSeasonCrossReference)
.WhereNotNull().Distinct()
.ToList();

#endregion

#region Trakt
Expand Down

0 comments on commit 0cfc5d4

Please sign in to comment.