示例#1
0
        public async Task HappyPathSendSeriesToSonarrAllSeason()
        {
            var seriesResult = new SonarrAddSeries()
            {
                title = "ABC"
            };

            SonarrMock.Setup(x => x.GetSeries(It.IsAny <string>(), It.IsAny <Uri>())).Returns(F.Build <Series>().With(x => x.tvdbId, 1).With(x => x.title, "ABC").CreateMany().ToList());

            Sender = new TvSender(SonarrMock.Object, SickrageMock.Object, Cache.Object);

            var request = new RequestedModel {
                SeasonsRequested = "All", ProviderId = 1, Title = "ABC"
            };

            var result = await Sender.SendToSonarr(GetSonarrSettings(), request);

            Assert.That(result.title, Is.EqualTo("ABC"));
            SonarrMock.Verify(x => x.AddSeries(It.IsAny <int>(),
                                               It.IsAny <string>(),
                                               It.IsAny <int>(),
                                               It.IsAny <bool>(),
                                               It.IsAny <string>(),
                                               It.IsAny <int>(),
                                               It.IsAny <int[]>(),
                                               It.IsAny <string>(),
                                               It.IsAny <Uri>(),
                                               true, It.IsAny <bool>()), Times.Never);
        }
示例#2
0
        public async Task HappyPathSendEpisodeWithExistingSeriesToSonarr()
        {
            var seriesResult = new SonarrAddSeries {
                monitored = true, title = "TitleReturned"
            };
            var selectedSeries = F.Build <Series>().With(x => x.tvdbId, 1).CreateMany();

            SonarrMock.Setup(x => x.GetSeries(It.IsAny <string>(), It.IsAny <Uri>())).Returns(selectedSeries.ToList());
            SonarrMock.Setup(x => x.AddSeries(
                                 It.IsAny <int>(),
                                 It.IsAny <string>(),
                                 It.IsAny <int>(),
                                 It.IsAny <bool>(),
                                 It.IsAny <string>(),
                                 It.IsAny <int>(),
                                 It.IsAny <int[]>(),
                                 It.IsAny <string>(),
                                 It.IsAny <Uri>(),
                                 It.IsAny <bool>(), It.IsAny <bool>())).Returns(seriesResult);
            var sonarrEpisodes = new SonarrEpisodes()
            {
                title         = "abc",
                seasonNumber  = 2,
                episodeNumber = 1,
                monitored     = false
            };
            var episodesList = F.CreateMany <SonarrEpisodes>().ToList();

            episodesList.Add(sonarrEpisodes);

            SonarrMock.Setup(x => x.GetEpisodes(It.IsAny <string>(), It.IsAny <string>(),
                                                It.IsAny <Uri>())).Returns(F.CreateMany <SonarrEpisodes>());

            Sender = new TvSender(SonarrMock.Object, SickrageMock.Object, Cache.Object);
            var episodes = new List <EpisodesModel>
            {
                new EpisodesModel
                {
                    EpisodeNumber = 1,
                    SeasonNumber  = 2
                }
            };
            var model = F.Build <RequestedModel>().With(x => x.ProviderId, 1)
                        .With(x => x.Episodes, episodes).Create();

            var result = await Sender.SendToSonarr(GetSonarrSettings(), model, "2");

            Assert.That(result, Is.EqualTo(seriesResult));
            SonarrMock.Verify(x => x.AddSeries(It.IsAny <int>(),
                                               It.IsAny <string>(),
                                               It.IsAny <int>(),
                                               It.IsAny <bool>(),
                                               It.IsAny <string>(),
                                               It.IsAny <int>(),
                                               It.IsAny <int[]>(),
                                               It.IsAny <string>(),
                                               It.IsAny <Uri>(),
                                               true, It.IsAny <bool>()), Times.Once);
        }
示例#3
0
        public SonarrAddSeries AddSeries(int tvdbId, string title, int qualityId, bool seasonFolders, string rootPath, int seasonCount, int[] seasons, string apiKey, Uri baseUrl)
        {
            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
            };

            for (var i = 1; i <= seasonCount; i++)
            {
                var season = new Season
                {
                    seasonNumber = i,
                    monitored    = seasons.Length == 0 || seasons.Any(x => x == i)
                };
                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
            {
                result = 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);
        }
示例#4
0
        private async Task <SonarrAddSeries> ProcessSonarrEpisodeRequest(SonarrSettings sonarrSettings, RequestedModel model, int qualityId, string rootFolderPath)
        {
            // Does the series exist?

            var series = await GetSonarrSeries(sonarrSettings, model.ProviderId);

            if (series == null)
            {
                var seriesToAdd = new SonarrAddSeries
                {
                    seasonFolder     = sonarrSettings.SeasonFolders,
                    title            = model.Title,
                    qualityProfileId = qualityId,
                    tvdbId           = model.ProviderId,
                    titleSlug        = model.Title,
                    seasons          = new List <Season>(),
                    rootFolderPath   = rootFolderPath,
                    monitored        = true, // Montior the series
                    images           = new List <SonarrImage>(),
                    addOptions       = new AddOptions
                    {
                        ignoreEpisodesWithFiles    = true, // We don't really care about these
                        ignoreEpisodesWithoutFiles = true, // We do not want to grab random episodes missing
                        searchForMissingEpisodes   = false // we want don't want to search for the missing episodes either
                    }
                };

                for (var i = 1; i <= model.SeasonCount; i++)
                {
                    var season = new Season
                    {
                        seasonNumber = i,
                        monitored    = false // Do not monitor any seasons
                    };
                    seriesToAdd.seasons.Add(season);
                }

                // Add the series now
                var result = SonarrApi.AddSeries(seriesToAdd, sonarrSettings.ApiKey, sonarrSettings.FullUri);

                await RequestEpisodesForSonarr(model, result.id, sonarrSettings);
            }
            else
            {
                await RequestEpisodesForSonarr(model, series.id, sonarrSettings);
            }

            return(new SonarrAddSeries()
            {
                title = model.Title
            });
        }
        public async Task HappyPathSendSeriesToSonarr()
        {
            var seriesResult = new SonarrAddSeries()
            {
                monitored = true
            };

            SonarrMock.Setup(x => x.GetSeries(It.IsAny <string>(), It.IsAny <Uri>())).Returns(new List <Series>());
            SonarrMock.Setup(
                x =>
                x.AddSeries(
                    It.IsAny <int>(),
                    It.IsAny <string>(),
                    It.IsAny <int>(),
                    It.IsAny <bool>(),
                    It.IsAny <string>(),
                    It.IsAny <int>(),
                    It.IsAny <int[]>(),
                    It.IsAny <string>(),
                    It.IsAny <Uri>(),
                    It.IsAny <bool>(), It.IsAny <bool>())).Returns(seriesResult);
            Sender = new TvSender(SonarrMock.Object, SickrageMock.Object);

            var request = new RequestedModel();

            var result = await Sender.SendToSonarr(GetSonarrSettings(), request);

            Assert.That(result, Is.EqualTo(seriesResult));
            SonarrMock.Verify(x => x.AddSeries(It.IsAny <int>(),
                                               It.IsAny <string>(),
                                               It.IsAny <int>(),
                                               It.IsAny <bool>(),
                                               It.IsAny <string>(),
                                               It.IsAny <int>(),
                                               It.IsAny <int[]>(),
                                               It.IsAny <string>(),
                                               It.IsAny <Uri>(),
                                               true, It.IsAny <bool>()), Times.Once);
        }
示例#6
0
        public SonarrAddSeries AddSeries(SonarrAddSeries series, string apiKey, Uri baseUrl)
        {
            var request = new RestRequest
            {
                Resource = "/api/Series?",
                Method   = Method.POST
            };

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

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

            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(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);
        }
示例#7
0
        public SonarrAddSeries AddSeries(SonarrSettings sonarrSettings, RequestedModel model, string rootFolderPath, int qualityId)
        {
            //WORKS
            // Add the series
            var seriesToAdd = new SonarrAddSeries
            {
                seasonFolder     = sonarrSettings.SeasonFolders,
                title            = model.Title,
                qualityProfileId = qualityId,
                tvdbId           = model.ProviderId,
                titleSlug        = model.Title,
                seasons          = new List <Season>(),
                rootFolderPath   = rootFolderPath,
                monitored        = true, // Montior the series
                images           = new List <SonarrImage>(),
                addOptions       = new AddOptions
                {
                    ignoreEpisodesWithFiles    = true,  // We don't really care about these
                    ignoreEpisodesWithoutFiles = false, // We want to get the whole season
                    searchForMissingEpisodes   = true   // we want to search for missing
                }
            };

            for (var i = 1; i <= model.SeasonCount; i++)
            {
                var season = new Season
                {
                    seasonNumber = i,
                    // The model.SeasonList.Lenth is 0 when this is a "request all"
                    monitored = model.SeasonList.Length == 0 || model.SeasonList.Any(x => x == i)
                };
                seriesToAdd.seasons.Add(season);
            }

            return(SonarrApi.AddSeries(seriesToAdd, sonarrSettings.ApiKey, sonarrSettings.FullUri));
        }
示例#8
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);
        }