Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
Fix bug with SonarrV3 notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
darkalfx committed Aug 23, 2020
1 parent 8d86ba8 commit 5622fc3
Showing 1 changed file with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,18 @@ public async Task<SearchedTvShow> 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)
{
Expand Down Expand Up @@ -235,11 +238,18 @@ public async Task<IReadOnlyList<TvShow>> GetTvShowDetailsAsync(HashSet<int> 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);
}
}

Expand Down Expand Up @@ -349,7 +359,7 @@ private async Task UpdateSonarrTvSeriesAsync(TvShow tvShow, IReadOnlyList<TvSeas
{
var sonarrTvShow = await FindSeriesInSonarrAsync(tvShow.TheTvDbId);

if (sonarrTvShow.id.HasValue)
if (sonarrTvShow != null && sonarrTvShow.id.HasValue)
{
await UpdateSonarrTvSeriesAsync(tvShow, seasons);
return;
Expand Down Expand Up @@ -431,7 +441,7 @@ private async Task<JSONTvShow> 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}");

Expand All @@ -441,7 +451,7 @@ private async Task<JSONTvShow> FindSeriesInSonarrAsync(int tvDbId)
return JsonConvert.DeserializeObject<JSONTvShow>(jsonResponse);
}
}

return series;
}

Expand All @@ -451,9 +461,8 @@ private async Task<JSONTvShow> SearchSerieByTvDbIdAsync(int tvDbId)
await response.ThrowIfNotSuccessfulAsync("SonarrSeriesLookupByTvDbId failed", x => x.error);

var jsonResponse = await response.Content.ReadAsStringAsync();
var tvShow = JsonConvert.DeserializeObject<IEnumerable<JSONTvShow>>(jsonResponse).First();

return tvShow;

return JsonConvert.DeserializeObject<IEnumerable<JSONTvShow>>(jsonResponse).FirstOrDefault();
}

private async Task<IDictionary<int, JSONEpisode[]>> GetSonarrEpisodesAsync(int sonarrId)
Expand Down Expand Up @@ -533,9 +542,9 @@ private string GetPosterImageUrl(List<JSONImage> 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;
}
Expand Down

0 comments on commit 5622fc3

Please sign in to comment.