Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix plex webhook to work with TMDB season orderings #1217

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions Shoko.Server/API/v2/Modules/PlexWebhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ await watchedService.SetWatchedStatus(episode, true, true, FromUnixTime(metadata
}

var animeId = int.Parse(guid.Authority);
var series = int.Parse(guid.AbsolutePath.Split('/')[1]);
var season = int.Parse(guid.AbsolutePath.Split('/')[1]);
var episodeNumber = int.Parse(guid.AbsolutePath.Split('/')[2]);

//if (!metadata.Guid.StartsWith("com.plexapp.agents.shoko://")) return (null, null);
Expand All @@ -166,7 +166,7 @@ await watchedService.SetWatchedStatus(episode, true, true, FromUnixTime(metadata

EpisodeType episodeType;
switch
(series) //I hate magic number's but this is just about how I can do this, also the rest of this is for later.
(season) //I hate magic number's but this is just about how I can do this, also the rest of this is for later.
{
case -4:
episodeType = EpisodeType.Parody;
Expand Down Expand Up @@ -194,20 +194,18 @@ await watchedService.SetWatchedStatus(episode, true, true, FromUnixTime(metadata
return (null, anime); //right now no clean way to detect the episode. I could do by title.
}


var animeEps = anime
.AnimeEpisodes.Where(a => a.EpisodeTypeEnum == episodeType && a.AniDB_Episode?.EpisodeNumber == episodeNumber).ToList();

//if only one possible match
if (animeEps.Count == 1) return (animeEps.First(), anime);

// Check for Tmdb matches
SVR_AnimeEpisode result;
if ((result = animeEps.FirstOrDefault(a => a.TmdbEpisodes.Any(e => e.SeasonNumber == series))) != null)
// If season is 1, assume Single Season Ordering or linked to season 1 on TMDB
// Barring edge cases, the anidb ordering should be the same as the TMDB ordering
if (season == 1)
{
return (result, anime);
var animeEp = anime.AnimeEpisodes.FirstOrDefault(a => a.EpisodeTypeEnum == episodeType && a.AniDB_Episode?.EpisodeNumber == episodeNumber);
if (animeEp != null) return (animeEp, anime);
}

// Check for Tmdb matches
var result = anime.AnimeEpisodes.FirstOrDefault(a => a.TmdbEpisodes.Any(e => e.SeasonNumber == season && e.EpisodeNumber == episodeNumber));
if (result != null) return (result, anime);

//catch all
_logger.LogInformation($"Unable to work out the metadata for {metadata.Guid}, this might be a clash of multiple episodes linked, but no tmdb link.");
return (null, anime);
Expand Down