diff --git a/src/Ombi.Api.Sonarr/Models/Episode.cs b/src/Ombi.Api.Sonarr/Models/Episode.cs index 0e25242c4..c17f5486c 100644 --- a/src/Ombi.Api.Sonarr/Models/Episode.cs +++ b/src/Ombi.Api.Sonarr/Models/Episode.cs @@ -31,7 +31,7 @@ namespace Ombi.Api.Sonarr.Models public int seasonNumber { get; set; } public string relativePath { get; set; } public string path { get; set; } - public int size { get; set; } + public long size { get; set; } public DateTime dateAdded { get; set; } public string sceneName { get; set; } public EpisodeQuality quality { get; set; } diff --git a/src/Ombi.Api.Sonarr/SonarrApi.cs b/src/Ombi.Api.Sonarr/SonarrApi.cs index b4856b347..b4e3e3302 100644 --- a/src/Ombi.Api.Sonarr/SonarrApi.cs +++ b/src/Ombi.Api.Sonarr/SonarrApi.cs @@ -150,7 +150,7 @@ namespace Ombi.Api.Sonarr /// public async Task EpisodeSearch(int[] episodeIds, string apiKey, string baseUrl) { - var result = await Command("EpisodeSearch", apiKey, baseUrl, episodeIds); + var result = await Command(apiKey, baseUrl, new { name = "EpisodeSearch", episodeIds }); return result != null; } @@ -164,7 +164,7 @@ namespace Ombi.Api.Sonarr /// public async Task SeasonSearch(int seriesId, int seasonNumber, string apiKey, string baseUrl) { - var result = await Command("SeasonSearch", apiKey, baseUrl, new { seriesId, seasonNumber }); + var result = await Command(apiKey, baseUrl, new { name = "SeasonSearch", seriesId, seasonNumber }); return result != null; } @@ -177,15 +177,15 @@ namespace Ombi.Api.Sonarr /// public async Task SeriesSearch(int seriesId, string apiKey, string baseUrl) { - var result = await Command("SeasonSearch", apiKey, baseUrl, seriesId); + var result = await Command(apiKey, baseUrl, new { name = "SeriesSearch", seriesId }); return result != null; } - private async Task Command(string commandName, string apiKey, string baseUrl, object body = null) + private async Task Command(string apiKey, string baseUrl, object body) { - var request = new Request($"/api/Command/{commandName}", baseUrl, HttpMethod.Post); + var request = new Request($"/api/Command/", baseUrl, HttpMethod.Post); request.AddHeader("X-Api-Key", apiKey); - if(body != null) request.AddJsonBody(body); + request.AddJsonBody(body); return await Api.Request(request); } } diff --git a/src/Ombi.Core/TvSender.cs b/src/Ombi.Core/TvSender.cs index 95058d0b2..2bd6edc4c 100644 --- a/src/Ombi.Core/TvSender.cs +++ b/src/Ombi.Core/TvSender.cs @@ -3,7 +3,9 @@ using Ombi.Api.Sonarr; using Ombi.Api.Sonarr.Models; using Ombi.Core.Settings; using Ombi.Core.Settings.Models.External; +using Ombi.Helpers; using Ombi.Store.Entities.Requests; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -50,14 +52,10 @@ namespace Ombi.Core var rootFolderPath = await GetSonarrRootPath(model.ParentRequest.RootFolder ?? 0, s); try { - - // Does the series actually exist? var allSeries = await SonarrApi.GetSeries(s.ApiKey, s.FullUri); var existingSeries = allSeries.FirstOrDefault(x => x.tvdbId == model.ParentRequest.TvDbId); - - if (existingSeries == null) { // Time to add a new one @@ -83,7 +81,7 @@ namespace Ombi.Core // Montitor the correct seasons, // If we have that season in the model then it's monitored! var seasonsToAdd = new List(); - for (int i = 1; i < model.ParentRequest.TotalSeasons +1 ; i++) + for (int i = 1; i < model.ParentRequest.TotalSeasons + 1; i++) { var season = new Season { @@ -97,8 +95,9 @@ namespace Ombi.Core // Ok, now let's sort out the episodes. var sonarrEpisodes = await SonarrApi.GetEpisodes(result.id, s.ApiKey, s.FullUri); - while(sonarrEpisodes.Count() == 0) + while (sonarrEpisodes.Count() == 0) { + // It could be that the series metadata is not ready yet. So wait sonarrEpisodes = await SonarrApi.GetEpisodes(result.id, s.ApiKey, s.FullUri); await Task.Delay(300); } @@ -109,7 +108,7 @@ namespace Ombi.Core foreach (var ep in req.Episodes) { var sonarrEp = sonarrEpisodes.FirstOrDefault(x => x.episodeNumber == ep.EpisodeNumber && x.seasonNumber == ep.Season.SeasonNumber); - if(sonarrEp != null) + if (sonarrEp != null) { sonarrEp.monitored = true; episodesToUpdate.Add(sonarrEp); @@ -123,7 +122,9 @@ namespace Ombi.Core await SonarrApi.UpdateEpisode(epToUpdate, s.ApiKey, s.FullUri); } - if(!s.AddOnly) + // TODO possibly update the season as it might be unmonitored due to the clash with the AddOptions + + if (!s.AddOnly) { foreach (var season in model.SeasonRequests) { @@ -131,7 +132,7 @@ namespace Ombi.Core var sonarrEpCount = sonarrSeason.Count(); var ourRequestCount = season.Episodes.Count(); - if(sonarrEpCount == ourRequestCount) + if (sonarrEpCount == ourRequestCount) { // We have the same amount of requests as all of the episodes in the season. // Do a season search @@ -149,19 +150,68 @@ namespace Ombi.Core } else { - // Let's update the existing + + var sonarrEpisodes = await SonarrApi.GetEpisodes(existingSeries.id, s.ApiKey, s.FullUri); + + var episodesToUpdate = new List(); + foreach (var req in model.SeasonRequests) + { + foreach (var ep in req.Episodes) + { + var sonarrEp = sonarrEpisodes.FirstOrDefault(x => x.episodeNumber == ep.EpisodeNumber && x.seasonNumber == ep.Season.SeasonNumber); + if (sonarrEp != null) + { + sonarrEp.monitored = true; + episodesToUpdate.Add(sonarrEp); + } + } + } + + // Now update the episodes that need updating + foreach (var epToUpdate in episodesToUpdate) + { + await SonarrApi.UpdateEpisode(epToUpdate, s.ApiKey, s.FullUri); + } + + // TODO possibly update the season as it might be unmonitored due to the clash with the AddOptions + + if (!s.AddOnly) + { + foreach (var season in model.SeasonRequests) + { + var sonarrSeason = sonarrEpisodes.Where(x => x.seasonNumber == season.SeasonNumber); + var sonarrEpCount = sonarrSeason.Count(); + var ourRequestCount = season.Episodes.Count(); + + if (sonarrEpCount == ourRequestCount) + { + // We have the same amount of requests as all of the episodes in the season. + // Do a season search + await SonarrApi.SeasonSearch(existingSeries.id, season.SeasonNumber, s.ApiKey, s.FullUri); + } + else + { + // There is a miss-match, let's search the episodes indiviaully + await SonarrApi.EpisodeSearch(episodesToUpdate.Select(x => x.id).ToArray(), s.ApiKey, s.FullUri); + } + } + } } + return new NewSeries + { + id = existingSeries.id, + seasons = existingSeries.seasons.ToList(), + cleanTitle = existingSeries.cleanTitle, + title = existingSeries.title, + tvdbId = existingSeries.tvdbId + }; } - catch (System.Exception e) + catch (Exception e) { - + Logger.LogError(LoggingEvents.SonarrSender, e, "Exception thrown when attempting to send series over to Sonarr"); throw; } - - - - return null; } private async Task GetSonarrRootPath(int pathId, SonarrSettings sonarrSettings) diff --git a/src/Ombi.Helpers/LoggingEvents.cs b/src/Ombi.Helpers/LoggingEvents.cs index 525035c98..9c694a2cf 100644 --- a/src/Ombi.Helpers/LoggingEvents.cs +++ b/src/Ombi.Helpers/LoggingEvents.cs @@ -14,5 +14,9 @@ namespace Ombi.Helpers public static EventId Notification => new EventId(4000); public static EventId DiscordNotification => new EventId(4001); + + public static EventId TvSender => new EventId(5000); + public static EventId SonarrSender => new EventId(5001); + } } \ No newline at end of file