Skip to content

Commit

Permalink
Add UpdateTraktShowJob, trakt series sync/refresh endpoints (#1211)
Browse files Browse the repository at this point in the history
- Add endpoint to sync Trakt series 

- Add endpoint to update series Trakt data
  • Loading branch information
harshithmohan authored Dec 20, 2024
1 parent e1abf14 commit a6a5c54
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
84 changes: 84 additions & 0 deletions Shoko.Server/API/v3/Controllers/SeriesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
using Shoko.Server.Repositories.Cached;
using Shoko.Server.Scheduling;
using Shoko.Server.Scheduling.Jobs.Shoko;
using Shoko.Server.Scheduling.Jobs.Trakt;
using Shoko.Server.Services;
using Shoko.Server.Settings;
using Shoko.Server.Utilities;
Expand Down Expand Up @@ -114,6 +115,8 @@ WatchedStatusService watchedService
internal const string TmdbNotFoundForSeriesID = "No TMDB.Show entry for the given seriesID";

internal const string TmdbForbiddenForUser = "Accessing TMDB.Show is not allowed for the current user";

internal const string TraktShowNotFound = "No Trakt_Show entry for the given showID";

#endregion

Expand Down Expand Up @@ -1779,6 +1782,87 @@ public ActionResult<List<TmdbSeason>> GetTMDBSeasonsBySeriesID(

#endregion

#endregion

#region Trakt

/// <summary>
/// Queue a job for refreshing series data from Trakt
/// </summary>
/// <param name="seriesID">Shoko ID</param>
/// <returns></returns>
[HttpPost("{seriesID}/Trakt/Refresh")]
public async Task<ActionResult> RefreshTraktBySeriesID([FromRoute, Range(1, int.MaxValue)] int seriesID)
{
var series = RepoFactory.AnimeSeries.GetByID(seriesID);
if (series == null)
{
return NotFound(SeriesNotFoundWithSeriesID);
}

if (!User.AllowedSeries(series))
{
return Forbid(SeriesForbiddenForUser);
}

var anidb = series.AniDB_Anime;
if (anidb == null)
{
return InternalError(AnidbNotFoundForSeriesID);
}

var traktShows = series.TraktShow;
if (traktShows.Count == 0)
{
return ValidationProblem(TraktShowNotFound);
}

var scheduler = await _schedulerFactory.GetScheduler();

foreach(var show in traktShows)
{
await scheduler.StartJob<UpdateTraktShowJob>(c => c.TraktShowID = show.TraktID);
}

return Ok();
}

/// <summary>
/// Queue a job for syncing series status to Trakt
/// </summary>
/// <param name="seriesID">Shoko ID</param>
/// <returns></returns>
[HttpPost("{seriesID}/Trakt/Sync")]
public async Task<ActionResult> SyncTraktBySeriesID([FromRoute, Range(1, int.MaxValue)] int seriesID)
{
var series = RepoFactory.AnimeSeries.GetByID(seriesID);
if (series == null)
{
return NotFound(SeriesNotFoundWithSeriesID);
}

if (!User.AllowedSeries(series))
{
return Forbid(SeriesForbiddenForUser);
}

var anidb = series.AniDB_Anime;
if (anidb == null)
{
return InternalError(AnidbNotFoundForSeriesID);
}

var traktShows = series.TraktShow;
if (traktShows.Count == 0)
{
return ValidationProblem(TraktShowNotFound);
}

var scheduler = await _schedulerFactory.GetScheduler();
await scheduler.StartJob<SyncTraktCollectionSeriesJob>(c => c.AnimeSeriesID = seriesID);
return Ok();
}

#endregion

#endregion
Expand Down
59 changes: 59 additions & 0 deletions Shoko.Server/Scheduling/Jobs/Trakt/UpdateTraktShowJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Shoko.Server.Providers.TraktTV;
using Shoko.Server.Repositories;
using Shoko.Server.Scheduling.Acquisition.Attributes;
using Shoko.Server.Scheduling.Attributes;
using Shoko.Server.Scheduling.Concurrency;
using Shoko.Server.Settings;

namespace Shoko.Server.Scheduling.Jobs.Trakt;

[DatabaseRequired]
[NetworkRequired]
[DisallowConcurrencyGroup(ConcurrencyGroups.Trakt)]
[JobKeyGroup(JobKeyGroup.Trakt)]
public class UpdateTraktShowJob : BaseJob
{
private readonly ISettingsProvider _settingsProvider;
private readonly TraktTVHelper _helper;
private string _showName;
public string TraktShowID { get; set; }

public override string TypeName => "Update Trakt Show data";
public override string Title => "Updating Trakt Show data";

public override void PostInit()
{
_showName = RepoFactory.Trakt_Show?.GetByTraktSlug(TraktShowID)?.Title ?? TraktShowID;
}

public override Dictionary<string, object> Details => new() { { "Show", _showName } };

public override Task Process()
{
_logger.LogInformation("Processing {Job} -> Show: {Name}", nameof(UpdateTraktShowJob), _showName);
var settings = _settingsProvider.GetSettings();
if (!settings.TraktTv.Enabled || string.IsNullOrEmpty(settings.TraktTv.AuthToken)) return Task.CompletedTask;

var show = RepoFactory.Trakt_Show.GetByTraktSlug(TraktShowID);
if (show == null)
{
_logger.LogError("Could not find trakt show: {TraktShowID}", TraktShowID);
return Task.CompletedTask;
}

_helper.UpdateAllInfo(TraktShowID);

return Task.CompletedTask;
}

public UpdateTraktShowJob(TraktTVHelper helper, ISettingsProvider settingsProvider)
{
_helper = helper;
_settingsProvider = settingsProvider;
}

protected UpdateTraktShowJob() { }
}

0 comments on commit a6a5c54

Please sign in to comment.