예제 #1
0
        public List <SonarrProfile> GetProfiles(string apiKey, Uri baseUrl)
        {
            var request = new RestRequest {
                Resource = "/api/profile", Method = Method.GET
            };

            request.AddHeader("X-Api-Key", apiKey);
            var policy = RetryHandler.RetryAndWaitPolicy(new TimeSpan[] {
                TimeSpan.FromSeconds(2),
                TimeSpan.FromSeconds(5),
                TimeSpan.FromSeconds(10)
            }, (exception, timespan) => Log.Error(exception, "Exception when calling GetProfiles for Sonarr, Retrying {0}", timespan));

            var obj = policy.Execute(() => Api.ExecuteJson <List <SonarrProfile> >(request, baseUrl));

            return(obj);
        }
예제 #2
0
        public SystemStatus SystemStatus(string apiKey, Uri baseUrl)
        {
            var request = new RestRequest {
                Resource = "/api/system/status", Method = Method.GET
            };

            request.AddHeader("X-Api-Key", apiKey);

            var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling SystemStatus for Sonarr, Retrying {0}", timespan), new TimeSpan[] {
                TimeSpan.FromSeconds(2),
                TimeSpan.FromSeconds(5),
                TimeSpan.FromSeconds(10)
            });

            var obj = policy.Execute(() => Api.ExecuteJson <SystemStatus>(request, baseUrl));

            return(obj);
        }
예제 #3
0
        public SonarrEpisodes UpdateEpisode(SonarrEpisodes episodeInfo, string apiKey, Uri baseUrl)
        {
            var request = new RestRequest {
                Resource = "/api/Episode", Method = Method.PUT
            };

            request.AddHeader("X-Api-Key", apiKey);
            request.AddJsonBody(episodeInfo);
            try
            {
                var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) =>
                                                             Log.Error(exception, "Exception when calling UpdateEpisode for Sonarr, Retrying {0}", timespan));

                return(policy.Execute(() => Api.ExecuteJson <SonarrEpisodes>(request, baseUrl)));
            }
            catch (Exception e)
            {
                Log.Error(e, "There has been an API exception when put the Sonarr UpdateEpisode");
                return(null);
            }
        }
예제 #4
0
        /// <summary>
        /// Returns the episode with the matching id.
        /// </summary>
        /// <param name="episodeId">The episode identifier.</param>
        /// <param name="apiKey">The API key.</param>
        /// <param name="baseUrl">The base URL.</param>
        /// <returns></returns>
        public SonarrEpisode GetEpisode(string episodeId, string apiKey, Uri baseUrl)
        {
            var request = new RestRequest {
                Resource = "/api/Episode/{episodeId}", Method = Method.GET
            };

            request.AddHeader("X-Api-Key", apiKey);
            request.AddUrlSegment("episodeId", episodeId);
            try
            {
                var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) =>
                                                             Log.Error(exception, "Exception when calling GetEpisode by ID for Sonarr, Retrying {0}", timespan));

                return(policy.Execute(() => Api.ExecuteJson <SonarrEpisode>(request, baseUrl)));
            }
            catch (Exception e)
            {
                Log.Error(e, "There has been an API exception when getting the Sonarr GetEpisode by ID");
                return(null);
            }
        }
예제 #5
0
        /// <summary>
        /// Returns all episodes for the given series.
        /// </summary>
        /// <param name="seriesId">The series identifier.</param>
        /// <param name="apiKey">The API key.</param>
        /// <param name="baseUrl">The base URL.</param>
        /// <returns></returns>
        public IEnumerable <SonarrEpisodes> GetEpisodes(string seriesId, string apiKey, Uri baseUrl)
        {
            var request = new RestRequest {
                Resource = "/api/Episode", Method = Method.GET
            };

            request.AddHeader("X-Api-Key", apiKey);
            request.AddQueryParameter("seriesId", seriesId);
            try
            {
                var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) =>
                                                             Log.Error(exception, "Exception when calling GetEpisodes for Sonarr, Retrying {0}", timespan));

                return(policy.Execute(() => Api.ExecuteJson <List <SonarrEpisodes> >(request, baseUrl)));
            }
            catch (Exception e)
            {
                Log.Error(e, "There has been an API exception when getting the Sonarr GetEpisodes");
                return(null);
            }
        }
예제 #6
0
        public List <Series> GetSeries(string apiKey, Uri baseUrl)
        {
            var request = new RestRequest {
                Resource = "/api/series", Method = Method.GET
            };

            request.AddHeader("X-Api-Key", apiKey);
            try
            {
                var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling GetSeries for Sonarr, Retrying {0}", timespan), new TimeSpan[] {
                    TimeSpan.FromSeconds(5),
                    TimeSpan.FromSeconds(10),
                    TimeSpan.FromSeconds(30)
                });

                return(policy.Execute(() => Api.ExecuteJson <List <Series> >(request, baseUrl)));
            }
            catch (Exception e)
            {
                Log.Error(e, "There has been an API exception when getting the Sonarr Series");
                return(null);
            }
        }
예제 #7
0
        public SickRageSeasonList VerifyShowHasLoaded(int tvdbId, string apiKey, Uri baseUrl)
        {
            Log.Trace("Entered `VerifyShowHasLoaded({0} <- id)`", tvdbId);
            var request = new RestRequest {
                Resource = "/api/{apiKey}/?cmd=show.seasonlist", Method = Method.GET
            };

            request.AddUrlSegment("apiKey", apiKey);
            request.AddQueryParameter("tvdbid", tvdbId.ToString());

            try
            {
                var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling VerifyShowHasLoaded for SR, Retrying {0}", timespan), null);

                var obj = policy.Execute(() => Api.ExecuteJson <SickRageSeasonList>(request, baseUrl));
                return(obj);
            }
            catch (Exception e)
            {
                Log.Error(e);
                return(new SickRageSeasonList());
            }
        }
예제 #8
0
        public async Task <SickRageTvAdd> AddSeason(int tvdbId, int season, string apiKey, Uri baseUrl)
        {
            var request = new RestRequest {
                Resource = "/api/{apiKey}/?cmd=episode.setstatus", Method = Method.GET
            };

            request.AddUrlSegment("apiKey", apiKey);
            request.AddQueryParameter("tvdbid", tvdbId.ToString());
            request.AddQueryParameter("season", season.ToString());
            request.AddQueryParameter("status", SickRageStatus.Wanted);

            await Task.Run(() => Thread.Sleep(2000));

            return(await Task.Run(
                       () =>
            {
                var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling AddSeason for SR, Retrying {0}", timespan), null);

                var result = policy.Execute(() => Api.Execute <SickRageTvAdd>(request, baseUrl));

                return result;
            }).ConfigureAwait(false));
        }
예제 #9
0
        public async Task <SickrageShows> GetShows(string apiKey, Uri baseUrl)
        {
            var request = new RestRequest {
                Resource = "/api/{apiKey}/?cmd=shows", Method = Method.GET
            };

            request.AddUrlSegment("apiKey", apiKey);

            return(await Task.Run(
                       () =>
            {
                try
                {
                    var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling GetShows for SR, Retrying {0}", timespan), new[] { TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(30) });

                    return policy.Execute(() => Api.Execute <SickrageShows>(request, baseUrl));
                }
                catch (ApiRequestException)
                {
                    Log.Error("There has been a API exception when Getting the Sickrage shows");
                    return null;
                }
            }).ConfigureAwait(false));
        }
예제 #10
0
        public SonarrAddSeries AddSeries(int tvdbId, string title, int qualityId, bool seasonFolders, string rootPath, int seasonCount, int[] seasons, string apiKey, Uri baseUrl, bool monitor = true, bool searchForMissingEpisodes = false)
        {
            Log.Debug("Adding series {0}", title);
            Log.Debug("Seasons = {0}, out of {1} seasons", seasons.DumpJson(), seasonCount);
            var request = new RestRequest
            {
                Resource = "/api/Series?",
                Method   = Method.POST
            };

            var options = new SonarrAddSeries
            {
                seasonFolder     = seasonFolders,
                title            = title,
                qualityProfileId = qualityId,
                tvdbId           = tvdbId,
                titleSlug        = title,
                seasons          = new List <Season>(),
                rootFolderPath   = rootPath,
                monitored        = monitor
            };

            if (!searchForMissingEpisodes)
            {
                options.addOptions = new AddOptions
                {
                    searchForMissingEpisodes   = false,
                    ignoreEpisodesWithFiles    = true,
                    ignoreEpisodesWithoutFiles = true
                };
            }

            for (var i = 1; i <= seasonCount; i++)
            {
                var season = new Season
                {
                    seasonNumber = i,
                    // ReSharper disable once SimplifyConditionalTernaryExpression
                    monitored = monitor ? seasons.Length == 0 || seasons.Any(x => x == i) : false
                };
                options.seasons.Add(season);
            }

            Log.Debug("Sonarr API Options:");
            Log.Debug(options.DumpJson());

            request.AddHeader("X-Api-Key", apiKey);
            request.AddJsonBody(options);

            SonarrAddSeries result;

            try
            {
                var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling AddSeries for Sonarr, Retrying {0}", timespan), new TimeSpan[] {
                    TimeSpan.FromSeconds(1),
                    TimeSpan.FromSeconds(2),
                });

                result = policy.Execute(() => Api.ExecuteJson <SonarrAddSeries>(request, baseUrl));
            }
            catch (JsonSerializationException jse)
            {
                Log.Error(jse);
                var error    = Api.ExecuteJson <List <SonarrError> >(request, baseUrl);
                var messages = error?.Select(x => x.errorMessage).ToList();
                messages?.ForEach(x => Log.Error(x));
                result = new SonarrAddSeries {
                    ErrorMessages = messages
                };
            }

            return(result);
        }
예제 #11
0
        public async Task <SickRageTvAdd> AddSeries(int tvdbId, int seasonCount, int[] seasons, string quality, string apiKey, Uri baseUrl)
        {
            var futureStatus = seasons.Length > 0 && !seasons.Any(x => x == seasonCount) ? SickRageStatus.Skipped : SickRageStatus.Wanted;
            var status       = seasons.Length > 0 ? SickRageStatus.Skipped : SickRageStatus.Wanted;

            Log.Trace("Future Status: {0}", futureStatus);
            Log.Trace("Current Status: {0}", status);

            var request = new RestRequest {
                Resource = "/api/{apiKey}/?cmd=show.addnew", Method = Method.GET
            };

            request.AddUrlSegment("apiKey", apiKey);
            request.AddQueryParameter("tvdbid", tvdbId.ToString());
            request.AddQueryParameter("status", status);
            request.AddQueryParameter("future_status", futureStatus);
            if (!quality.Equals("default", StringComparison.CurrentCultureIgnoreCase))
            {
                Log.Trace("Settings quality to {0}", quality);
                request.AddQueryParameter("initial", quality);
            }

            var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling AddSeries for SR, Retrying {0}", timespan), null);

            var obj = policy.Execute(() => Api.Execute <SickRageTvAdd>(request, baseUrl));

            Log.Trace("obj Result:");
            Log.Trace(obj.DumpJson());

            if (obj.result != "failure")
            {
                var sw = new Stopwatch();
                sw.Start();

                var seasonIncrement = 0;
                var seasonList      = new SickRageSeasonList();
                try
                {
                    while (seasonIncrement < seasonCount)
                    {
                        seasonList = VerifyShowHasLoaded(tvdbId, apiKey, baseUrl);
                        if (seasonList.result.Equals("failure"))
                        {
                            Thread.Sleep(3000);
                            continue;
                        }
                        seasonIncrement = seasonList.Data?.Length ?? 0;
                        Log.Trace("New seasonIncrement -> {0}", seasonIncrement);

                        if (sw.ElapsedMilliseconds > 30000) // Break out after 30 seconds, it's not going to get added
                        {
                            Log.Warn("Couldn't find out if the show had been added after 10 seconds. I doubt we can change the status to wanted.");
                            break;
                        }
                    }
                    sw.Stop();
                }
                catch (Exception e)
                {
                    Log.Error("Exception thrown when getting the seasonList");
                    Log.Error(e);
                }
            }
            Log.Trace("seasons.Length > 0 where seasons.Len -> {0}", seasons.Length);
            try
            {
                if (seasons.Length > 0)
                {
                    //handle the seasons requested
                    foreach (var s in seasons)
                    {
                        Log.Trace("Adding season {0}", s);

                        var result = await AddSeason(tvdbId, s, apiKey, baseUrl);

                        Log.Trace("SickRage adding season results: ");
                        Log.Trace(result.DumpJson());
                    }
                }
            }
            catch (Exception e)
            {
                Log.Trace("Exception when adding seasons:");
                Log.Error(e);
                throw;
            }

            Log.Trace("Finished with the API, returning the obj");
            return(obj);
        }