Skip to content

Commit

Permalink
refactor: split off linking and image logic
Browse files Browse the repository at this point in the history
- Split off linking and image logic from the tmdb metadata service into two new services, one for linking, and one for handling images.
  • Loading branch information
revam committed Aug 24, 2024
1 parent 40a1a37 commit 9b072b7
Show file tree
Hide file tree
Showing 8 changed files with 816 additions and 662 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public partial class ShokoServiceImplementation : Controller, IShokoServer
private readonly JobFactory _jobFactory;
private readonly TvDBApiHelper _tvdbHelper;
private readonly TraktTVHelper _traktHelper;
private readonly TmdbMetadataService _tmdbService;
private readonly TmdbLinkingService _tmdbLinkingService;
private readonly TmdbMetadataService _tmdbMetadataService;
private readonly ISettingsProvider _settingsProvider;
private readonly ISchedulerFactory _schedulerFactory;
private readonly ActionService _actionService;
Expand All @@ -55,22 +56,23 @@ public partial class ShokoServiceImplementation : Controller, IShokoServer
public ShokoServiceImplementation(
TvDBApiHelper tvdbHelper,
TraktTVHelper traktHelper,
TmdbLinkingService tmdbLinkingService,
TmdbMetadataService tmdbService,
ISchedulerFactory schedulerFactory,
ISettingsProvider settingsProvider,
ILogger<ShokoServiceImplementation> logger,
ActionService actionService,
AnimeGroupCreator groupCreator,
JobFactory jobFactory,
AnimeSeriesService seriesService,
AnimeEpisodeService episodeService,
WatchedStatusService watchedService,
VideoLocalService videoLocalService
)
{
_tvdbHelper = tvdbHelper;
_traktHelper = traktHelper;
_tmdbService = tmdbService;
_tmdbLinkingService = tmdbLinkingService;
_tmdbMetadataService = tmdbService;
_schedulerFactory = schedulerFactory;
_settingsProvider = settingsProvider;
_logger = logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1018,8 +1018,8 @@ public string LinkAniDBOther(int animeID, int id, int crossRefType)
switch (xrefType)
{
case CrossRefType.MovieDB:
_tmdbService.AddMovieLink(animeID, id).ConfigureAwait(false).GetAwaiter().GetResult();
_tmdbService.ScheduleUpdateOfMovie(id, downloadImages: true).ConfigureAwait(false).GetAwaiter().GetResult();
_tmdbLinkingService.AddMovieLink(animeID, id).ConfigureAwait(false).GetAwaiter().GetResult();
_tmdbMetadataService.ScheduleUpdateOfMovie(id, downloadImages: true).ConfigureAwait(false).GetAwaiter().GetResult();
break;
}

Expand Down Expand Up @@ -1048,7 +1048,7 @@ public string RemoveLinkAniDBOther(int animeID, int crossRefType)
switch (xrefType)
{
case CrossRefType.MovieDB:
_tmdbService.RemoveAllMovieLinks(animeID).ConfigureAwait(false).GetAwaiter().GetResult();
_tmdbLinkingService.RemoveAllMovieLinksForAnime(animeID).ConfigureAwait(false).GetAwaiter().GetResult();
break;
}

Expand All @@ -1071,7 +1071,7 @@ public List<CL_MovieDBMovieSearch_Response> SearchTheMovieDB(string criteria)
var results = new List<CL_MovieDBMovieSearch_Response>();
try
{
var (movieResults, _) = _tmdbService.SearchMovies(System.Web.HttpUtility.UrlDecode(criteria)).ConfigureAwait(false).GetAwaiter().GetResult();
var (movieResults, _) = _tmdbMetadataService.SearchMovies(System.Web.HttpUtility.UrlDecode(criteria)).ConfigureAwait(false).GetAwaiter().GetResult();

results.AddRange(movieResults.Select(movie => movie.ToContract()));

Expand Down Expand Up @@ -1131,7 +1131,7 @@ public string UpdateMovieDBData(int movieID)
{
try
{
_tmdbService.ScheduleUpdateOfMovie(movieID, downloadImages: true, forceRefresh: true).ConfigureAwait(false).GetAwaiter().GetResult();
_tmdbMetadataService.ScheduleUpdateOfMovie(movieID, downloadImages: true, forceRefresh: true).ConfigureAwait(false).GetAwaiter().GetResult();
}
catch (Exception ex)
{
Expand Down
48 changes: 34 additions & 14 deletions Shoko.Server/API/v3/Controllers/SeriesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,38 @@ namespace Shoko.Server.API.v3.Controllers;
public class SeriesController : BaseController
{
private readonly AnimeSeriesService _seriesService;

private readonly AniDBTitleHelper _titleHelper;

private readonly ISchedulerFactory _schedulerFactory;

private readonly TmdbLinkingService _tmdbLinkingService;

private readonly TmdbMetadataService _tmdbMetadataService;


private readonly TmdbSearchService _tmdbSearchService;

private readonly CrossRef_File_EpisodeRepository _crossRefFileEpisode;

private readonly WatchedStatusService _watchedService;

public SeriesController(ISettingsProvider settingsProvider, AnimeSeriesService seriesService, AniDBTitleHelper titleHelper, ISchedulerFactory schedulerFactory, TmdbMetadataService tmdbMetadataService, TmdbSearchService tmdbSearchService, CrossRef_File_EpisodeRepository crossRefFileEpisode, WatchedStatusService watchedService) : base(settingsProvider)
public SeriesController(
ISettingsProvider settingsProvider,
AnimeSeriesService seriesService,
AniDBTitleHelper titleHelper,
ISchedulerFactory schedulerFactory,
TmdbLinkingService tmdbLinkingService,
TmdbMetadataService tmdbMetadataService,
TmdbSearchService tmdbSearchService,
CrossRef_File_EpisodeRepository crossRefFileEpisode,
WatchedStatusService watchedService
) : base(settingsProvider)
{
_seriesService = seriesService;
_titleHelper = titleHelper;
_schedulerFactory = schedulerFactory;
_tmdbLinkingService = tmdbLinkingService;
_tmdbMetadataService = tmdbMetadataService;
_tmdbSearchService = tmdbSearchService;
_crossRefFileEpisode = crossRefFileEpisode;
Expand Down Expand Up @@ -1217,7 +1237,7 @@ public async Task<ActionResult> AddLinkToTMDBMoviesBySeriesID(
if (!User.AllowedSeries(series))
return Forbid(SeriesForbiddenForUser);

await _tmdbMetadataService.AddMovieLink(series.AniDB_ID, body.ID, body.EpisodeID, additiveLink: !body.Replace);
await _tmdbLinkingService.AddMovieLink(series.AniDB_ID, body.ID, body.EpisodeID, additiveLink: !body.Replace);

var needRefresh = RepoFactory.TMDB_Movie.GetByTmdbMovieID(body.ID) is null || body.Refresh;
if (needRefresh)
Expand Down Expand Up @@ -1247,9 +1267,9 @@ public async Task<ActionResult> RemoveLinkToTMDBMoviesBySeriesID(
return Forbid(SeriesForbiddenForUser);

if (body != null && body.ID > 0)
await _tmdbMetadataService.RemoveMovieLink(series.AniDB_ID, body.ID, body.Purge);
await _tmdbLinkingService.RemoveMovieLink(series.AniDB_ID, body.ID, body.Purge);
else
await _tmdbMetadataService.RemoveAllMovieLinks(series.AniDB_ID, body?.Purge ?? false);
await _tmdbLinkingService.RemoveAllMovieLinksForAnime(series.AniDB_ID, body?.Purge ?? false);

return NoContent();
}
Expand Down Expand Up @@ -1411,7 +1431,7 @@ public async Task<ActionResult> AddLinkToTMDBShowsBySeriesID(
if (!User.AllowedSeries(series))
return Forbid(SeriesForbiddenForUser);

await _tmdbMetadataService.AddShowLink(series.AniDB_ID, body.ID, additiveLink: !body.Replace);
await _tmdbLinkingService.AddShowLink(series.AniDB_ID, body.ID, additiveLink: !body.Replace);

var needRefresh = RepoFactory.TMDB_Show.GetByTmdbShowID(body.ID) is null || body.Refresh;
if (needRefresh)
Expand Down Expand Up @@ -1441,9 +1461,9 @@ public async Task<ActionResult> RemoveLinkToTMDBShowsBySeriesID(
return Forbid(SeriesForbiddenForUser);

if (body != null && body.ID > 0)
await _tmdbMetadataService.RemoveShowLink(series.AniDB_ID, body.ID, body.Purge);
await _tmdbLinkingService.RemoveShowLink(series.AniDB_ID, body.ID, body.Purge);
else
await _tmdbMetadataService.RemoveAllShowLinks(series.AniDB_ID, body?.Purge ?? false);
await _tmdbLinkingService.RemoveAllShowLinksForAnime(series.AniDB_ID, body?.Purge ?? false);

return NoContent();
}
Expand Down Expand Up @@ -1644,15 +1664,15 @@ public async Task<ActionResult> OverrideTMDBEpisodeMappingsBySeriesID(

// Add any missing links if needed.
foreach (var showId in missingIDs)
await _tmdbMetadataService.AddShowLink(series.AniDB_ID, showId, additiveLink: true);
await _tmdbLinkingService.AddShowLink(series.AniDB_ID, showId, additiveLink: true);

// Reset the existing links if we wanted to replace all.
if (body.ResetAll)
_tmdbMetadataService.ResetAllEpisodeLinks(series.AniDB_ID);
_tmdbLinkingService.ResetAllEpisodeLinks(series.AniDB_ID);

// Do the actual linking.
foreach (var link in body.Mapping)
_tmdbMetadataService.SetEpisodeLink(link.AniDBID, link.TmdbID, !link.Replace);
_tmdbLinkingService.SetEpisodeLink(link.AniDBID, link.TmdbID, !link.Replace);

return NoContent();
}
Expand Down Expand Up @@ -1709,7 +1729,7 @@ public async Task<ActionResult> OverrideTMDBEpisodeMappingsBySeriesID(
return ValidationProblem("The selected tmdbSeasonID does not belong to the selected tmdbShowID", "tmdbSeasonID");
}

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

Expand Down Expand Up @@ -1762,7 +1782,7 @@ public async Task<ActionResult> AutoTMDBEpisodeMappingsBySeriesID(

// Add the missing link if needed.
if (isMissing)
await _tmdbMetadataService.AddShowLink(series.AniDB_ID, tmdbShowID.Value, additiveLink: true);
await _tmdbLinkingService.AddShowLink(series.AniDB_ID, tmdbShowID.Value, additiveLink: true);

if (tmdbSeasonID.HasValue)
{
Expand All @@ -1774,7 +1794,7 @@ public async Task<ActionResult> AutoTMDBEpisodeMappingsBySeriesID(
return ValidationProblem("The selected tmdbSeasonID does not belong to the selected tmdbShowID", "tmdbSeasonID");
}

_tmdbMetadataService.MatchAnidbToTmdbEpisodes(series.AniDB_ID, tmdbShowID.Value, tmdbSeasonID, keepExisting, saveToDatabase: true);
_tmdbLinkingService.MatchAnidbToTmdbEpisodes(series.AniDB_ID, tmdbShowID.Value, tmdbSeasonID, keepExisting, saveToDatabase: true);

return NoContent();
}
Expand All @@ -1797,7 +1817,7 @@ public ActionResult RemoveTMDBEpisodeMappingsBySeriesID(
if (!User.AllowedSeries(series))
return Forbid(TvdbForbiddenForUser);

_tmdbMetadataService.ResetAllEpisodeLinks(series.AniDB_ID);
_tmdbLinkingService.ResetAllEpisodeLinks(series.AniDB_ID);

return NoContent();
}
Expand Down
Loading

0 comments on commit 9b072b7

Please sign in to comment.