private async Task CreateSonarrTvSeriesAsync(TvShowRequest request, TvShow tvShow, IReadOnlyList <TvSeason> seasons) { SonarrCategory category = null; try { category = SonarrSettings.Categories.Single(x => x.Id == request.CategoryId); } catch { _logger.LogError($"An error occurred while requesting a tv show \"{tvShow.Title}\" from Sonarr, could not find category with id {request.CategoryId}"); throw new System.Exception($"An error occurred while requesting tv show \"{tvShow.Title}\" from Sonarr, could not find category with id {request.CategoryId}"); } var response = await HttpGetAsync($"{BaseURL}/series/lookup?term=tvdb:{tvShow.TheTvDbId}"); await response.ThrowIfNotSuccessfulAsync("SonarrSeriesLookup failed", x => x.error); var jsonResponse = await response.Content.ReadAsStringAsync(); var jsonTvShow = JsonConvert.DeserializeObject <IEnumerable <JSONTvShow> >(jsonResponse).First(); int[] tags = category.Tags; string seriesType = category.SeriesType; if (seriesType == "automatic") { seriesType = await IsAnimeAsync(tvShow.TheTvDbId) ? "anime" : "standard"; } response = await HttpPostAsync($"{BaseURL}/series", JsonConvert.SerializeObject(new { title = jsonTvShow.title, qualityProfileId = category.ProfileId, profileId = category.ProfileId, languageProfileId = category.LanguageId, titleSlug = jsonTvShow.titleSlug, monitored = SonarrSettings.MonitorNewRequests, images = new string[0], tvdbId = tvShow.TheTvDbId, tags = JToken.FromObject(tags), seriesType = seriesType, year = jsonTvShow.year, seasonFolder = category.UseSeasonFolders, rootFolderPath = category.RootFolder, seasons = jsonTvShow.seasons.Select(s => new { seasonNumber = s.seasonNumber, monitored = seasons.Any(rs => rs.SeasonNumber == s.seasonNumber) }), addOptions = new { searchForMissingEpisodes = SonarrSettings.SearchNewRequests } })); await response.ThrowIfNotSuccessfulAsync("SonarrSeriesCreation failed", x => x.error); }
private async Task UpdateSonarrTvSeriesAsync(TvShowRequest request, TvShow tvShow, IReadOnlyList <TvSeason> seasons) { var response = await HttpGetAsync($"{BaseURL}/series/{tvShow.DownloadClientId}"); if (!response.IsSuccessStatusCode) { if (response.StatusCode == System.Net.HttpStatusCode.NotFound) { var sonarrTvShow = await FindSeriesInSonarrAsync(tvShow.TheTvDbId); if (sonarrTvShow != null && sonarrTvShow.id.HasValue) { await UpdateSonarrTvSeriesAsync(request, tvShow, seasons); return; } else { await CreateSonarrTvSeriesAsync(request, tvShow, seasons); return; } } await response.ThrowIfNotSuccessfulAsync("SonarrSerieGet failed", x => x.error); } var jsonResponse = await response.Content.ReadAsStringAsync(); dynamic sonarrSeries = JObject.Parse(jsonResponse); SonarrCategory category = null; try { category = SonarrSettings.Categories.Single(x => x.Id == request.CategoryId); } catch { _logger.LogError($"An error occurred while requesting a tv show \"{tvShow.Title}\" from Sonarr, could not find category with id {request.CategoryId}"); throw new System.Exception($"An error occurred while requesting tv show \"{tvShow.Title}\" from Sonarr, could not find category with id {request.CategoryId}"); } sonarrSeries.tags = JToken.FromObject(category.Tags); sonarrSeries.qualityProfileId = category.ProfileId; sonarrSeries.languageProfileId = category.LanguageId; sonarrSeries.monitored = SonarrSettings.MonitorNewRequests; sonarrSeries.seasonFolder = category.UseSeasonFolders; if (seasons.Any()) { IEnumerable <dynamic> jsonSeasons = sonarrSeries.seasons; foreach (var season in jsonSeasons) { season.monitored = seasons.Any(s => s.SeasonNumber == (int)season.seasonNumber) ? false : (bool)season.monitored; } response = await HttpPutAsync($"{BaseURL}/series/{tvShow.DownloadClientId}", JsonConvert.SerializeObject(sonarrSeries)); await response.ThrowIfNotSuccessfulAsync("SonarrSeriesUpdate failed", x => x.error); foreach (var season in jsonSeasons) { season.monitored = seasons.Any(s => s.SeasonNumber == (int)season.seasonNumber) ? true : (bool)season.monitored; } } response = await HttpPutAsync($"{BaseURL}/series/{tvShow.DownloadClientId}", JsonConvert.SerializeObject(sonarrSeries)); await response.ThrowIfNotSuccessfulAsync("SonarrSeriesUpdate failed", x => x.error); if (SonarrSettings.SearchNewRequests) { var episodes = await GetSonarrEpisodesAsync(int.Parse(tvShow.DownloadClientId)); await Task.WhenAll(seasons.Select(async s => { try { if (episodes[s.SeasonNumber].Any()) { var undownloadedEpisodes = episodes[s.SeasonNumber].Where(x => !x.hasFile).Select(x => x.id).ToArray(); if (undownloadedEpisodes.Length == episodes[s.SeasonNumber].Count()) { response = await HttpPostAsync($"{BaseURL}/command", JsonConvert.SerializeObject(new { name = "SeasonSearch", seasonNumber = s.SeasonNumber, seriesId = tvShow.DownloadClientId })); await response.ThrowIfNotSuccessfulAsync("SonarrSeasonSearchCommand failed", x => x.error); } else { await response.ThrowIfNotSuccessfulAsync("SonarrSearchEpisodeCommand failed", x => x.error); response = await HttpPostAsync($"{BaseURL}/command", JsonConvert.SerializeObject(new { name = "EpisodeSearch", episodeIds = undownloadedEpisodes, })); await response.ThrowIfNotSuccessfulAsync("SonarrSearchEpisodeCommand failed", x => x.error); } } } catch (System.Exception ex) { _logger.LogError(ex.Message); throw; } })); } }