Skip to content

Commit

Permalink
fix: re-implement the logic to use the existing links for other shows
Browse files Browse the repository at this point in the history
but this time, make it off by default, with a setting to turn it on by default, and add a query parameter to each endpoint where it matter to override it as needed, if needed.
  • Loading branch information
revam committed Nov 29, 2024
1 parent ecd308b commit 10467fe
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
8 changes: 5 additions & 3 deletions Shoko.Server/API/v3/Controllers/SeriesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,8 @@ public async Task<ActionResult> OverrideTMDBEpisodeMappingsBySeriesID(
/// <param name="seriesID">Shoko Series ID.</param>
/// <param name="tmdbShowID">The specified TMDB Show ID to search for links. This parameter is used to select a specific show.</param>
/// <param name="tmdbSeasonID">The specified TMDB Season ID to search for links. If not provided, links are searched for any season of the selected or first linked show.</param>
/// <param name="keepExisting">Determines whether to retain any and all existing links.</param>
/// <param name="keepExisting">Determines whether to retain existing links when picking episodes.</param>
/// <param name="considerExistingOtherLinks">Determines whether to consider existing links for other series when picking episodes.</param>
/// <param name="pageSize">The page size.</param>
/// <param name="page">The page index.</param>
/// <returns>A preview of the automagically matched episodes.</returns>
Expand All @@ -1581,6 +1582,7 @@ public async Task<ActionResult> OverrideTMDBEpisodeMappingsBySeriesID(
[FromQuery] int? tmdbShowID,
[FromQuery] int? tmdbSeasonID,
[FromQuery] bool keepExisting = true,
[FromQuery] bool? considerExistingOtherLinks = null,
[FromQuery, Range(0, 1000)] int pageSize = 50,
[FromQuery, Range(1, int.MaxValue)] int page = 1
)
Expand Down Expand Up @@ -1612,7 +1614,7 @@ public async Task<ActionResult> OverrideTMDBEpisodeMappingsBySeriesID(
return ValidationProblem("The selected tmdbSeasonID does not belong to the selected tmdbShowID", "tmdbSeasonID");
}

return _tmdbLinkingService.MatchAnidbToTmdbEpisodes(series.AniDB_ID, tmdbShowID.Value, tmdbSeasonID, keepExisting, saveToDatabase: false)
return _tmdbLinkingService.MatchAnidbToTmdbEpisodes(series.AniDB_ID, tmdbShowID.Value, tmdbSeasonID, useExisting: keepExisting, useExistingOtherShows: considerExistingOtherLinks, saveToDatabase: false)
.ToListResult(x => new TmdbEpisode.CrossReference(x), page, pageSize);
}

Expand Down Expand Up @@ -1674,7 +1676,7 @@ public async Task<ActionResult> AutoTMDBEpisodeMappingsBySeriesID(
if (isMissing)
await _tmdbLinkingService.AddShowLink(series.AniDB_ID, body.TmdbShowID.Value, additiveLink: true);
else
_tmdbLinkingService.MatchAnidbToTmdbEpisodes(series.AniDB_ID, body.TmdbShowID.Value, body.TmdbSeasonID, body.KeepExisting, saveToDatabase: true);
_tmdbLinkingService.MatchAnidbToTmdbEpisodes(series.AniDB_ID, body.TmdbShowID.Value, body.TmdbSeasonID, useExisting: body.KeepExisting, useExistingOtherShows: body.ConsiderExistingOtherLinks, saveToDatabase: true);

if (tmdbShow.CreatedAt == tmdbShow.LastUpdatedAt)
{
Expand Down
7 changes: 6 additions & 1 deletion Shoko.Server/API/v3/Models/Shoko/Series.cs
Original file line number Diff line number Diff line change
Expand Up @@ -711,10 +711,15 @@ public class AutoMatchTmdbEpisodesBody
public int? TmdbSeasonID { get; set; }

/// <summary>
/// Determines whether to retain any and all existing links.
/// Determines whether to retain existing links for the current series.
/// </summary>
[DefaultValue(true)]
public bool KeepExisting { get; set; } = true;

/// <summary>
/// Determines whether to consider existing links for other series when picking episodes.
/// </summary>
public bool? ConsiderExistingOtherLinks { get; set; }
}

public class OverrideTmdbEpisodeMappingBody
Expand Down
22 changes: 21 additions & 1 deletion Shoko.Server/Providers/TMDB/TmdbLinkingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Shoko.Server.Scheduling;
using Shoko.Server.Scheduling.Jobs.TMDB;
using Shoko.Server.Server;
using Shoko.Server.Settings;
using Shoko.Server.Utilities;

using CrossRefSource = Shoko.Models.Enums.CrossRefSource;
Expand All @@ -32,6 +33,8 @@ public class TmdbLinkingService

private readonly ISchedulerFactory _schedulerFactory;

private readonly ISettingsProvider _settingsProvider;

private readonly TmdbImageService _imageService;

private readonly AnimeSeriesRepository _animeSeries;
Expand All @@ -55,6 +58,7 @@ public class TmdbLinkingService
public TmdbLinkingService(
ILogger<TmdbLinkingService> logger,
ISchedulerFactory schedulerFactory,
ISettingsProvider settingsProvider,
TmdbImageService imageService,
AnimeSeriesRepository animeSeries,
AniDB_AnimeRepository anidbAnime,
Expand All @@ -69,6 +73,7 @@ CrossRef_AniDB_TMDB_EpisodeRepository xrefAnidbTmdbEpisodes
{
_logger = logger;
_schedulerFactory = schedulerFactory;
_settingsProvider = settingsProvider;
_imageService = imageService;
_animeSeries = animeSeries;
_anidbAnime = anidbAnime;
Expand Down Expand Up @@ -382,7 +387,7 @@ public bool SetEpisodeLink(int anidbEpisodeId, int tmdbEpisodeId, bool additiveL
return true;
}

public IReadOnlyList<CrossRef_AniDB_TMDB_Episode> MatchAnidbToTmdbEpisodes(int anidbAnimeId, int tmdbShowId, int? tmdbSeasonId, bool useExisting = false, bool saveToDatabase = false, bool useExistingOtherShows = true)
public IReadOnlyList<CrossRef_AniDB_TMDB_Episode> MatchAnidbToTmdbEpisodes(int anidbAnimeId, int tmdbShowId, int? tmdbSeasonId, bool useExisting = false, bool saveToDatabase = false, bool? useExistingOtherShows = null)
{
var anime = _anidbAnime.GetByAnimeID(anidbAnimeId);
if (anime == null)
Expand Down Expand Up @@ -416,6 +421,21 @@ public IReadOnlyList<CrossRef_AniDB_TMDB_Episode> MatchAnidbToTmdbEpisodes(int a
var tmdbEpisodes = tmdbEpisodeDict.Values
.Where(episode => episode.SeasonNumber == 0 || !tmdbSeasonId.HasValue || episode.TmdbSeasonID == tmdbSeasonId.Value)
.ToList();
var considerExistingOtherLinks = useExistingOtherShows ?? _settingsProvider.GetSettings().TMDB.ConsiderExistingOtherLinks;
if (considerExistingOtherLinks)
{
var otherShowsExisting = existing.Values.SelectMany(xref => xref).ExceptBy(anidbEpisodes.Keys.Append(0), xref => xref.AnidbEpisodeID).ToList();
foreach (var link in otherShowsExisting)
{
_logger.LogTrace("Skipping existing episode link: AniDB episode (EpisodeID={EpisodeID},AnimeID={AnimeID}) → TMDB episode (EpisodeID={TmdbID})", link.AnidbEpisodeID, link.AnidbAnimeID, link.TmdbEpisodeID);

// Exclude the linked episodes from the auto-match candidates.
var index = tmdbEpisodes.FindIndex(episode => episode.TmdbEpisodeID == link.TmdbEpisodeID);
if (index >= 0)
tmdbEpisodes.RemoveAt(index);
}
}

var tmdbNormalEpisodes = isOVA ? tmdbEpisodes : tmdbEpisodes
.Where(episode => episode.SeasonNumber != 0)
.OrderBy(episode => episode.SeasonNumber)
Expand Down
11 changes: 11 additions & 0 deletions Shoko.Server/Settings/TMDBSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ public class TMDBSettings
/// </summary>
public bool AutoLinkRestricted { get; set; } = false;

/// <summary>
/// Determines whether to consider existing cross-reference links to other
/// AniDB anime when linking an AniDB anime to a TMDB show.
/// </summary>
/// <remarks>
/// This setting also applies to the auto-matching process and can be
/// overridden on a per request basis for the API when previewing or
/// linking.
/// </remarks>
public bool ConsiderExistingOtherLinks { get; set; } = false;

/// <summary>
/// Indicates that all titles should be stored locally for the TMDB entity,
/// otherwise it will use
Expand Down

0 comments on commit 10467fe

Please sign in to comment.