diff --git a/Requestrr.WebApi/RequestrrBot/DownloadClients/Sonarr/SonarrClientV3.cs b/Requestrr.WebApi/RequestrrBot/DownloadClients/Sonarr/SonarrClientV3.cs index 5ae07442..d91efb7d 100644 --- a/Requestrr.WebApi/RequestrrBot/DownloadClients/Sonarr/SonarrClientV3.cs +++ b/Requestrr.WebApi/RequestrrBot/DownloadClients/Sonarr/SonarrClientV3.cs @@ -163,15 +163,18 @@ public async Task SearchTvShowAsync(int tvDbId) { var jsonTvShow = await SearchSerieByTvDbIdAsync(tvDbId); - var searchedTvShow = new SearchedTvShow + if (jsonTvShow != null && jsonTvShow.tvdbId == tvDbId) { - TheTvDbId = jsonTvShow.tvdbId.Value, - Title = jsonTvShow.title, - FirstAired = jsonTvShow.year > 0 ? jsonTvShow.year.ToString() : string.Empty, - Banner = jsonTvShow.remotePoster - }; + return new SearchedTvShow + { + TheTvDbId = jsonTvShow.tvdbId.Value, + Title = jsonTvShow.title, + FirstAired = jsonTvShow.year > 0 ? jsonTvShow.year.ToString() : string.Empty, + Banner = jsonTvShow.remotePoster + }; + } - return searchedTvShow.TheTvDbId == tvDbId ? searchedTvShow : null; + return null; } catch (System.Exception ex) { @@ -235,11 +238,18 @@ public async Task> GetTvShowDetailsAsync(HashSet theT foreach (var tvDbId in theTvDbIds) { - var series = await FindSeriesInSonarrAsync(tvDbId); + try + { + var series = await FindSeriesInSonarrAsync(tvDbId); - if(series.id != null && series.id > 0) + if (series != null && series.id != null && series.id > 0) + { + convertedTvShows.Add(Convert(series, series.seasons, await GetSonarrEpisodesAsync(series.id.Value))); + } + } + catch (System.Exception ex) { - convertedTvShows.Add(Convert(series, series.seasons, await GetSonarrEpisodesAsync(series.id.Value))); + _logger.LogError(ex, "An error occurred while searching available tv shows with Sonarr: " + ex.Message); } } @@ -349,7 +359,7 @@ private async Task UpdateSonarrTvSeriesAsync(TvShow tvShow, IReadOnlyList FindSeriesInSonarrAsync(int tvDbId) { var series = await SearchSerieByTvDbIdAsync(tvDbId); - if(series.id != null && series.id > 0) + if (series != null && series.id != null && series.id > 0) { var response = await HttpGetAsync($"{BaseURL}/series/{series.id}"); @@ -441,7 +451,7 @@ private async Task FindSeriesInSonarrAsync(int tvDbId) return JsonConvert.DeserializeObject(jsonResponse); } } - + return series; } @@ -451,9 +461,8 @@ private async Task SearchSerieByTvDbIdAsync(int tvDbId) await response.ThrowIfNotSuccessfulAsync("SonarrSeriesLookupByTvDbId failed", x => x.error); var jsonResponse = await response.Content.ReadAsStringAsync(); - var tvShow = JsonConvert.DeserializeObject>(jsonResponse).First(); - - return tvShow; + + return JsonConvert.DeserializeObject>(jsonResponse).FirstOrDefault(); } private async Task> GetSonarrEpisodesAsync(int sonarrId) @@ -533,9 +542,9 @@ private string GetPosterImageUrl(List jsonImages) { var posterImage = jsonImages.Where(x => x.coverType.Equals("poster", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault(); - if(posterImage != null) + if (posterImage != null) { - if(!string.IsNullOrWhiteSpace(posterImage.remoteUrl)) + if (!string.IsNullOrWhiteSpace(posterImage.remoteUrl)) { return posterImage.remoteUrl; }