Skip to content

Commit b1eb37b

Browse files
committed
RIP Trakt Scrobble
1 parent 805f23d commit b1eb37b

File tree

7 files changed

+6
-225
lines changed

7 files changed

+6
-225
lines changed

Shoko.Server/API/v0/Controllers/PlexWebhook.cs

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
using Shoko.Server.Extensions;
1919
using Shoko.Server.Models;
2020
using Shoko.Server.Plex;
21-
using Shoko.Server.Providers.TraktTV;
2221
using Shoko.Server.Repositories;
2322
using Shoko.Server.Scheduling;
2423
using Shoko.Server.Scheduling.Jobs.Plex;
@@ -38,14 +37,12 @@ namespace Shoko.Server.API.v0.Controllers;
3837
public class PlexWebhook : BaseController
3938
{
4039
private readonly ILogger<PlexWebhook> _logger;
41-
private readonly TraktTVHelper _traktHelper;
4240
private readonly ISchedulerFactory _schedulerFactory;
4341
private readonly IUserDataService _userDataService;
4442

45-
public PlexWebhook(ILogger<PlexWebhook> logger, TraktTVHelper traktHelper, ISettingsProvider settingsProvider, ISchedulerFactory schedulerFactory, IUserDataService userDataService) : base(settingsProvider)
43+
public PlexWebhook(ILogger<PlexWebhook> logger, ISettingsProvider settingsProvider, ISchedulerFactory schedulerFactory, IUserDataService userDataService) : base(settingsProvider)
4644
{
4745
_logger = logger;
48-
_traktHelper = traktHelper;
4946
_schedulerFactory = schedulerFactory;
5047
_userDataService = userDataService;
5148
}
@@ -64,47 +61,16 @@ public async Task<ActionResult> WebhookPost([FromForm, ModelBinder(BinderType =
6461
}
6562

6663
_logger.LogTrace($"{payload.Event}: {payload.Metadata?.Guid}");
67-
switch (payload.Event)
68-
{
69-
case "media.scrobble":
70-
await Scrobble(payload, User);
71-
break;
72-
case "media.resume":
73-
case "media.play":
74-
TraktScrobble(payload, ScrobblePlayingStatus.Start);
75-
break;
76-
case "media.pause":
77-
TraktScrobble(payload, ScrobblePlayingStatus.Pause);
78-
break;
79-
case "media.stop":
80-
TraktScrobble(payload, ScrobblePlayingStatus.Stop);
81-
break;
82-
}
64+
65+
if (payload.Event.EqualsInvariantIgnoreCase("media.scrobble"))
66+
await Scrobble(payload, User);
8367

8468
return Ok(); //doesn't need to be an ApiStatus.OK() since really, all I take is plex.
8569
}
8670

8771

8872
#region Plex events
8973

90-
[NonAction]
91-
private void TraktScrobble(PlexEvent evt, ScrobblePlayingStatus type)
92-
{
93-
var metadata = evt.Metadata;
94-
var (episode, _) = GetEpisode(metadata);
95-
96-
if (episode == null) return;
97-
98-
var vl = RepoFactory.VideoLocal.GetByAniDBEpisodeID(episode.AniDB_EpisodeID).FirstOrDefault();
99-
if (vl == null || vl.Duration == 0) return;
100-
101-
var per = 100 *
102-
(metadata.ViewOffset /
103-
(float)vl.Duration); //this will be nice if plex would ever give me the duration, so I don't have to guess it.
104-
105-
_traktHelper.Scrobble(episode, type, per);
106-
}
107-
10874
[NonAction]
10975
private async Task Scrobble(PlexEvent data, SVR_JMMUser user)
11076
{

Shoko.Server/API/v1/Implementations/ShokoServiceImplementation/ShokoServiceImplementation_Providers.cs

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -364,38 +364,7 @@ public CL_Response<bool> TraktFriendRequestApprove(string friendUsername)
364364
[HttpPost("Trakt/Scrobble/{animeId}/{type}/{progress}/{status}")]
365365
public int TraktScrobble(int animeId, int type, int progress, int status)
366366
{
367-
try
368-
{
369-
var statusTraktV2 = ScrobblePlayingStatus.Start;
370-
371-
switch (status)
372-
{
373-
case (int)ScrobblePlayingStatus.Start:
374-
statusTraktV2 = ScrobblePlayingStatus.Start;
375-
break;
376-
case (int)ScrobblePlayingStatus.Pause:
377-
statusTraktV2 = ScrobblePlayingStatus.Pause;
378-
break;
379-
case (int)ScrobblePlayingStatus.Stop:
380-
statusTraktV2 = ScrobblePlayingStatus.Stop;
381-
break;
382-
}
383-
384-
var isValidProgress = float.TryParse(progress.ToString(), out var progressTrakt);
385-
386-
if (isValidProgress)
387-
{
388-
var animeEpisode = RepoFactory.AnimeEpisode.GetByID(animeId);
389-
return _traktHelper.Scrobble(animeEpisode, statusTraktV2, progressTrakt);
390-
}
391-
392-
return 500;
393-
}
394-
catch (Exception ex)
395-
{
396-
_logger.LogError(ex, "{ex}", ex.ToString());
397-
return 500;
398-
}
367+
return 200;
399368
}
400369

401370
[HttpPost("Trakt/Refresh/{traktID}")]

Shoko.Server/API/v3/Controllers/FileController.cs

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
using Shoko.Server.API.v3.Models.Shoko;
2222
using Shoko.Server.Extensions;
2323
using Shoko.Server.Models;
24-
using Shoko.Server.Providers.TraktTV;
2524
using Shoko.Server.Repositories;
2625
using Shoko.Server.Repositories.Cached;
2726
using Shoko.Server.Scheduling;
@@ -57,15 +56,13 @@ public class FileController : BaseController
5756

5857
internal const string FileLocationNotFoundWithLocationID = "No File.Location entry for the given locationID.";
5958

60-
private readonly TraktTVHelper _traktHelper;
6159
private readonly ISchedulerFactory _schedulerFactory;
6260
private readonly VideoLocalService _vlService;
6361
private readonly VideoLocal_PlaceService _vlPlaceService;
6462
private readonly VideoLocal_UserRepository _vlUsers;
6563
private readonly IUserDataService _userDataService;
6664

6765
public FileController(
68-
TraktTVHelper traktHelper,
6966
ISchedulerFactory schedulerFactory,
7067
ISettingsProvider settingsProvider,
7168
VideoLocal_PlaceService vlPlaceService,
@@ -74,7 +71,6 @@ public FileController(
7471
VideoLocalService vlService
7572
) : base(settingsProvider)
7673
{
77-
_traktHelper = traktHelper;
7874
_vlPlaceService = vlPlaceService;
7975
_vlUsers = vlUsers;
8076
_userDataService = watchedService;
@@ -765,7 +761,7 @@ public async Task<ActionResult> SetWatchedStatusOnFile([FromRoute, Range(1, int.
765761
/// </summary>
766762
/// <param name="fileID">VideoLocal ID. Watch status and resume position is kept per file, regardless of how many duplicates the file has.</param>
767763
/// <param name="eventName">The name of the event that triggered the scrobble.</param>
768-
/// <param name="episodeID">The episode id to scrobble to trakt.</param>
764+
/// <param name="episodeID">The episode id to scrobble.</param>
769765
/// <param name="watched">True if file should be marked as watched, false if file should be unmarked, or null if it shall not be updated.</param>
770766
/// <param name="resumePosition">Number of ticks into the video to resume from, or null if it shall not be updated.</param>
771767
/// <returns></returns>
@@ -789,30 +785,6 @@ public async Task<ActionResult> ScrobbleFileAndEpisode([FromRoute, Range(1, int.
789785
playbackPositionTicks = TimeSpan.Zero;
790786
}
791787

792-
switch (eventName)
793-
{
794-
// The playback was started.
795-
case "play":
796-
// The playback was resumed after a pause.
797-
case "resume":
798-
ScrobbleToTrakt(episode, (float)(playbackPositionTicks / totalDurationTicks), ScrobblePlayingStatus.Start);
799-
break;
800-
// The playback was paused.
801-
case "pause":
802-
ScrobbleToTrakt(episode, (float)(playbackPositionTicks / totalDurationTicks), ScrobblePlayingStatus.Pause);
803-
break;
804-
// The playback was ended.
805-
case "stop":
806-
ScrobbleToTrakt(episode, (float)(playbackPositionTicks / totalDurationTicks), ScrobblePlayingStatus.Stop);
807-
break;
808-
// The playback is still active, but the playback position changed.
809-
case "scrobble":
810-
break;
811-
// A user interaction caused the watch state to change.
812-
case "user-interaction":
813-
break;
814-
}
815-
816788
var reason = eventName switch
817789
{
818790
"play" => UserDataSaveReason.PlaybackStart,
@@ -839,15 +811,6 @@ public async Task<ActionResult> ScrobbleFileAndEpisode([FromRoute, Range(1, int.
839811
return NoContent();
840812
}
841813

842-
[NonAction]
843-
private void ScrobbleToTrakt(SVR_AnimeEpisode episode, float percentage, ScrobblePlayingStatus status)
844-
{
845-
if (User.IsTraktUser == 0)
846-
return;
847-
848-
_traktHelper.Scrobble(episode, status, percentage);
849-
}
850-
851814
/// <summary>
852815
/// Mark or unmark a file as ignored.
853816
/// </summary>

Shoko.Server/Providers/TraktTV/Contracts/Scrobble/TraktScrobble.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

Shoko.Server/Providers/TraktTV/Contracts/Scrobble/TraktScrobbleItem.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

Shoko.Server/Providers/TraktTV/TraktConstants.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,6 @@ public enum TraktSyncType
1414
HistoryRemove = 2
1515
}
1616

17-
public enum ScrobblePlayingStatus
18-
{
19-
Start = 1,
20-
Pause = 2,
21-
Stop = 3
22-
}
23-
24-
public enum ScrobblePlayingType
25-
{
26-
movie = 1,
27-
episode = 2
28-
}
29-
3017
public static class TraktStatusCodes
3118
{
3219
// http://docs.trakt.apiary.io/#introduction/status-codes

Shoko.Server/Providers/TraktTV/TraktTVHelper.cs

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Shoko.Server.Extensions;
1414
using Shoko.Server.Models;
1515
using Shoko.Server.Providers.TraktTV.Contracts;
16-
using Shoko.Server.Providers.TraktTV.Contracts.Scrobble;
1716
using Shoko.Server.Providers.TraktTV.Contracts.Sync;
1817
using Shoko.Server.Repositories;
1918
using Shoko.Server.Settings;
@@ -460,84 +459,6 @@ private void SyncHistory(TraktSyncType syncType, TraktSync syncItems)
460459
}
461460
}
462461

463-
public int Scrobble(SVR_AnimeEpisode episode, ScrobblePlayingStatus scrobbleStatus, float progress)
464-
{
465-
try
466-
{
467-
var settings = _settingsProvider.GetSettings();
468-
if (!settings.TraktTv.Enabled || string.IsNullOrEmpty(settings.TraktTv.AuthToken))
469-
{
470-
return 401;
471-
}
472-
473-
string url;
474-
switch (scrobbleStatus)
475-
{
476-
case ScrobblePlayingStatus.Start:
477-
url = TraktURIs.SetScrobbleStart;
478-
break;
479-
case ScrobblePlayingStatus.Pause:
480-
url = TraktURIs.SetScrobblePause;
481-
break;
482-
case ScrobblePlayingStatus.Stop:
483-
url = TraktURIs.SetScrobbleStop;
484-
break;
485-
default:
486-
return 400;
487-
}
488-
489-
// TODO: Figure out scrobbling if the episode has multiple links
490-
var tmdbEpisodeIds = GetTmdbEpisodeIdsFromEpisode(episode);
491-
var tmdbMovieIds = GetTmdbMovieIdsFromEpisode(episode);
492-
493-
if (tmdbEpisodeIds.Count == 0 && tmdbMovieIds.Count == 0)
494-
{
495-
_logger.LogWarning(
496-
"TraktTVHelper.Scrobble: No TMDB IDs found for: Anime Episode ID: {ID} Anime: {Title}", episode.AnimeEpisodeID,
497-
episode.PreferredTitle);
498-
return 404;
499-
}
500-
501-
var retData = string.Empty;
502-
var scrobble = new TraktScrobble
503-
{
504-
Progress = progress
505-
};
506-
507-
if (tmdbEpisodeIds.Count > 0)
508-
{
509-
scrobble.Episode = new TraktScrobbleItem
510-
{
511-
Ids = new TraktIds
512-
{
513-
TmdbID = tmdbEpisodeIds.First()
514-
}
515-
};
516-
}
517-
else
518-
{
519-
scrobble.Movie = new TraktScrobbleItem
520-
{
521-
Ids = new TraktIds
522-
{
523-
TmdbID = tmdbEpisodeIds.First()
524-
}
525-
};
526-
}
527-
528-
var json = JsonConvert.SerializeObject(scrobble);
529-
TraktTVRateLimiter.Instance.EnsureRate();
530-
SendData(url, json, "POST", BuildRequestHeaders(), ref retData);
531-
532-
return 200;
533-
}
534-
catch (Exception ex)
535-
{
536-
_logger.LogError(ex, "Error in TraktTVHelper.Scrobble");
537-
return 500;
538-
}
539-
}
540-
541462
#endregion
542463

543464
#region Get Data From Trakt

0 commit comments

Comments
 (0)