示例#1
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
            });
        }
示例#2
0
        public SonarrAddSeries SendToSonarr(SonarrSettings sonarrSettings, RequestedModel model, string qualityId)
        {
            int qualityProfile;

            if (!string.IsNullOrEmpty(qualityId) || !int.TryParse(qualityId, out qualityProfile)) // try to parse the passed in quality, otherwise use the settings default quality
            {
                int.TryParse(sonarrSettings.QualityProfile, out qualityProfile);
            }

            var result = SonarrApi.AddSeries(model.ProviderId, model.Title, qualityProfile,
                                             sonarrSettings.SeasonFolders, sonarrSettings.RootPath, model.SeasonCount, model.SeasonList, sonarrSettings.ApiKey,
                                             sonarrSettings.FullUri);

            Log.Trace("Sonarr Add Result: ");
            Log.Trace(result.DumpJson());

            return(result);
        }
示例#3
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));
        }
        public async Task <SonarrAddSeries> SendToSonarr(SonarrSettings sonarrSettings, RequestedModel model, string qualityId)
        {
            var qualityProfile = 0;
            var episodeRequest = model.Episodes.Any();

            if (!string.IsNullOrEmpty(qualityId)) // try to parse the passed in quality, otherwise use the settings default quality
            {
                int.TryParse(qualityId, out qualityProfile);
            }

            if (qualityProfile <= 0)
            {
                int.TryParse(sonarrSettings.QualityProfile, out qualityProfile);
            }

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

            if (episodeRequest)
            {
                // Does series exist?
                if (series != null)
                {
                    // Series Exists
                    // Request the episodes in the existing series
                    await RequestEpisodesWithExistingSeries(model, series, sonarrSettings);

                    return(new SonarrAddSeries {
                        title = series.title
                    });
                }


                // Series doesn't exist, need to add it as unmonitored.
                var addResult = await Task.Run(() => SonarrApi.AddSeries(model.ProviderId, model.Title, qualityProfile,
                                                                         sonarrSettings.SeasonFolders, sonarrSettings.RootPath, 0, new int[0], sonarrSettings.ApiKey,
                                                                         sonarrSettings.FullUri, false));


                // Get the series that was just added
                series = await GetSonarrSeries(sonarrSettings, model.ProviderId);

                series.monitored = true; // We want to make sure we are monitoring the series

                // Un-monitor all seasons
                foreach (var season in series.seasons)
                {
                    season.monitored = false;
                }

                // Update the series, Since we cannot add as un-monitored due to the following bug: https://github.com/Sonarr/Sonarr/issues/1404
                SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri);


                // We now have the series in Sonarr, update it to request the episodes.
                await RequestAllEpisodesInASeasonWithExistingSeries(model, series, sonarrSettings);

                return(addResult);
            }

            if (series != null)
            {
                var requestAll = model.SeasonsRequested.Equals("All", StringComparison.CurrentCultureIgnoreCase);
                var first      = model.SeasonsRequested.Equals("First", StringComparison.CurrentCultureIgnoreCase);
                var latest     = model.SeasonsRequested.Equals("Latest", StringComparison.CurrentCultureIgnoreCase);

                if (model.SeasonList.Any())
                {
                    // Monitor the seasons that we have chosen
                    foreach (var season in series.seasons)
                    {
                        if (model.SeasonList.Contains(season.seasonNumber))
                        {
                            season.monitored = true;
                        }
                    }
                }

                if (requestAll)
                {
                    // Monitor all seasons
                    foreach (var season in series.seasons)
                    {
                        season.monitored = true;
                    }
                }

                if (first)
                {
                    var firstSeries = series?.seasons?.OrderBy(x => x.seasonNumber)?.FirstOrDefault() ?? new Season();
                    firstSeries.monitored = true;
                }

                if (latest)
                {
                    var lastSeries = series?.seasons?.OrderByDescending(x => x.seasonNumber)?.FirstOrDefault() ?? new Season();
                    lastSeries.monitored = true;
                }


                // Update the series in sonarr with the new monitored status
                SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri);
                await RequestAllEpisodesInASeasonWithExistingSeries(model, series, sonarrSettings);

                return(new SonarrAddSeries {
                    title = series.title
                });                                                  // We have updated it
            }


            var result = SonarrApi.AddSeries(model.ProviderId, model.Title, qualityProfile,
                                             sonarrSettings.SeasonFolders, sonarrSettings.RootPath, model.SeasonCount, model.SeasonList, sonarrSettings.ApiKey,
                                             sonarrSettings.FullUri, true, true);

            return(result);
        }
示例#5
0
        /// <summary>
        /// Broken Way
        /// </summary>
        /// <param name="sonarrSettings"></param>
        /// <param name="model"></param>
        /// <param name="qualityId"></param>
        /// <returns></returns>
        public async Task <SonarrAddSeries> SendToSonarr(SonarrSettings sonarrSettings, RequestedModel model, string qualityId)
        {
            var qualityProfile = 0;
            var episodeRequest = model.Episodes.Any();

            if (!string.IsNullOrEmpty(qualityId)) // try to parse the passed in quality, otherwise use the settings default quality
            {
                int.TryParse(qualityId, out qualityProfile);
            }

            if (qualityProfile <= 0)
            {
                int.TryParse(sonarrSettings.QualityProfile, out qualityProfile);
            }

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

            var requestAll            = model.SeasonsRequested?.Equals("All", StringComparison.CurrentCultureIgnoreCase);
            var first                 = model.SeasonsRequested?.Equals("First", StringComparison.CurrentCultureIgnoreCase);
            var latest                = model.SeasonsRequested?.Equals("Latest", StringComparison.CurrentCultureIgnoreCase);
            var specificSeasonRequest = model.SeasonList?.Any();

            var rootFolderPath = model.RootFolderSelected <= 0 ? sonarrSettings.FullRootPath : await GetRootPath(model.RootFolderSelected, sonarrSettings);



            if (episodeRequest)
            {
                // Does series exist?
                if (series != null)
                {
                    // Series Exists
                    // Request the episodes in the existing series
                    await RequestEpisodesWithExistingSeries(model, series, sonarrSettings);

                    return(new SonarrAddSeries {
                        title = series.title
                    });
                }


                // Series doesn't exist, need to add it as unmonitored.
                var addResult = await Task.Run(() => SonarrApi.AddSeries(model.ProviderId, model.Title, qualityProfile,
                                                                         sonarrSettings.SeasonFolders, rootFolderPath, 0, new int[0], sonarrSettings.ApiKey,
                                                                         sonarrSettings.FullUri, false));


                // Get the series that was just added
                series = await GetSonarrSeries(sonarrSettings, model.ProviderId);

                series.monitored = true; // We want to make sure we are monitoring the series

                // Un-monitor all seasons
                foreach (var season in series.seasons)
                {
                    season.monitored = false;
                }

                // Update the series, Since we cannot add as un-monitored due to the following bug: https://github.com/Sonarr/Sonarr/issues/1404
                SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri);


                // We now have the series in Sonarr, update it to request the episodes.
                await RequestAllEpisodesInASeasonWithExistingSeries(model, series, sonarrSettings);

                return(addResult);
            }

            // Series exists, don't need to add it
            if (series == null)
            {
                // Set the series as monitored with a season count as 0 so it doesn't search for anything
                SonarrApi.AddSeriesNew(model.ProviderId, model.Title, qualityProfile,
                                       sonarrSettings.SeasonFolders, rootFolderPath, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }, sonarrSettings.ApiKey,
                                       sonarrSettings.FullUri);

                await Task.Delay(TimeSpan.FromSeconds(1));

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


                foreach (var s in series.seasons)
                {
                    s.monitored = false;
                }

                SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri);
            }

            if (requestAll ?? false)
            {
                // Monitor all seasons
                foreach (var season in series.seasons)
                {
                    season.monitored = true;
                }


                SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri);
                SonarrApi.SearchForSeries(series.id, sonarrSettings.ApiKey, sonarrSettings.FullUri); // Search For all episodes!"


                //// This is a work around for this issue: https://github.com/Sonarr/Sonarr/issues/1507
                //// The above is the previous code.
                //SonarrApi.AddSeries(model.ProviderId, model.Title, qualityProfile,
                //    sonarrSettings.SeasonFolders, sonarrSettings.RootPath, 0, model.SeasonList, sonarrSettings.ApiKey,
                //    sonarrSettings.FullUri, true, true);
                return(new SonarrAddSeries {
                    title = series.title
                });                                                  // We have updated it
            }



            if (first ?? false)
            {
                var firstSeries = (series?.seasons?.OrderBy(x => x.seasonNumber)).FirstOrDefault(x => x.seasonNumber > 0) ?? new Season();
                firstSeries.monitored = true;
                var episodes = SonarrApi.GetEpisodes(series.id.ToString(), sonarrSettings.ApiKey, sonarrSettings.FullUri); // Need to get the episodes so we mark them as monitored

                var episodesToUpdate = new List <SonarrEpisodes>();
                foreach (var e in episodes)
                {
                    if (e.hasFile || e.seasonNumber != firstSeries.seasonNumber)
                    {
                        continue;
                    }
                    e.monitored = true; // Mark only the episodes we want as monitored
                    episodesToUpdate.Add(e);
                }
                foreach (var sonarrEpisode in episodesToUpdate)
                {
                    SonarrApi.UpdateEpisode(sonarrEpisode, sonarrSettings.ApiKey, sonarrSettings.FullUri);
                }

                SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri);
                SonarrApi.SearchForSeason(series.id, firstSeries.seasonNumber, sonarrSettings.ApiKey,
                                          sonarrSettings.FullUri);
                return(new SonarrAddSeries {
                    title = series.title
                });                                                  // We have updated it
            }

            if (latest ?? false)
            {
                var lastSeries = series?.seasons?.OrderByDescending(x => x.seasonNumber)?.FirstOrDefault() ?? new Season();
                lastSeries.monitored = true;

                var episodes = SonarrApi.GetEpisodes(series.id.ToString(), sonarrSettings.ApiKey, sonarrSettings.FullUri); // Need to get the episodes so we mark them as monitored

                var episodesToUpdate = new List <SonarrEpisodes>();
                foreach (var e in episodes)
                {
                    if (e.hasFile || e.seasonNumber != lastSeries.seasonNumber)
                    {
                        continue;
                    }
                    e.monitored = true; // Mark only the episodes we want as monitored
                    episodesToUpdate.Add(e);
                }
                foreach (var sonarrEpisode in episodesToUpdate)
                {
                    SonarrApi.UpdateEpisode(sonarrEpisode, sonarrSettings.ApiKey, sonarrSettings.FullUri);
                }
                SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri);
                SonarrApi.SearchForSeason(series.id, lastSeries.seasonNumber, sonarrSettings.ApiKey,
                                          sonarrSettings.FullUri);
                return(new SonarrAddSeries {
                    title = series.title
                });                                                  // We have updated it
            }

            if (specificSeasonRequest ?? false)
            {
                // Monitor the seasons that we have chosen
                foreach (var season in series.seasons)
                {
                    if (model.SeasonList.Contains(season.seasonNumber))
                    {
                        season.monitored = true;
                        SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri);
                        SonarrApi.SearchForSeason(series.id, season.seasonNumber, sonarrSettings.ApiKey, sonarrSettings.FullUri);
                    }
                }
                return(new SonarrAddSeries {
                    title = series.title
                });                                                  // We have updated it
            }

            return(null);
        }
示例#6
0
文件: TvSender.cs 项目: itzfk0/ombi
        /// <summary>
        /// Send the request to Sonarr to process
        /// </summary>
        /// <param name="s"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task <NewSeries> SendToSonarr(ChildRequests model, SonarrSettings s)
        {
            if (string.IsNullOrEmpty(s.ApiKey))
            {
                return(null);
            }

            int    qualityToUse;
            string rootFolderPath;
            string seriesType;

            var profiles = await UserQualityProfiles.GetAll().FirstOrDefaultAsync(x => x.UserId == model.RequestedUserId);

            if (model.SeriesType == SeriesType.Anime)
            {
                // Get the root path from the rootfolder selected.
                // For some reason, if we haven't got one use the first root folder in Sonarr
                rootFolderPath = await GetSonarrRootPath(model.ParentRequest.RootFolder ?? int.Parse(s.RootPathAnime), s);

                int.TryParse(s.QualityProfileAnime, out qualityToUse);
                if (profiles != null)
                {
                    if (profiles.SonarrRootPathAnime > 0)
                    {
                        rootFolderPath = await GetSonarrRootPath(profiles.SonarrRootPathAnime, s);
                    }
                    if (profiles.SonarrQualityProfileAnime > 0)
                    {
                        qualityToUse = profiles.SonarrQualityProfileAnime;
                    }
                }
                seriesType = "anime";
            }
            else
            {
                int.TryParse(s.QualityProfile, out qualityToUse);
                // Get the root path from the rootfolder selected.
                // For some reason, if we haven't got one use the first root folder in Sonarr
                rootFolderPath = await GetSonarrRootPath(model.ParentRequest.RootFolder ?? int.Parse(s.RootPath), s);

                if (profiles != null)
                {
                    if (profiles.SonarrRootPath > 0)
                    {
                        rootFolderPath = await GetSonarrRootPath(profiles.SonarrRootPath, s);
                    }
                    if (profiles.SonarrQualityProfile > 0)
                    {
                        qualityToUse = profiles.SonarrQualityProfile;
                    }
                }
                seriesType = "standard";
            }

            // Overrides on the request take priority
            if (model.ParentRequest.QualityOverride.HasValue)
            {
                qualityToUse = model.ParentRequest.QualityOverride.Value;
            }

            // Are we using v3 sonarr?
            var sonarrV3          = s.V3;
            var languageProfileId = s.LanguageProfile;

            try
            {
                // Does the series actually exist?
                var allSeries = await SonarrApi.GetSeries(s.ApiKey, s.FullUri);

                var existingSeries = allSeries.FirstOrDefault(x => x.tvdbId == model.ParentRequest.TvDbId);

                if (existingSeries == null)
                {
                    // Time to add a new one
                    var newSeries = new NewSeries
                    {
                        title            = model.ParentRequest.Title,
                        imdbId           = model.ParentRequest.ImdbId,
                        tvdbId           = model.ParentRequest.TvDbId,
                        cleanTitle       = model.ParentRequest.Title,
                        monitored        = true,
                        seasonFolder     = s.SeasonFolders,
                        rootFolderPath   = rootFolderPath,
                        qualityProfileId = qualityToUse,
                        titleSlug        = model.ParentRequest.Title,
                        seriesType       = seriesType,
                        addOptions       = new AddOptions
                        {
                            ignoreEpisodesWithFiles    = false, // There shouldn't be any episodes with files, this is a new season
                            ignoreEpisodesWithoutFiles = false, // We want all missing
                            searchForMissingEpisodes   = false  // we want dont want to search yet. We want to make sure everything is unmonitored/monitored correctly.
                        }
                    };

                    if (sonarrV3)
                    {
                        newSeries.languageProfileId = languageProfileId;
                    }

                    // Montitor the correct seasons,
                    // If we have that season in the model then it's monitored!
                    var seasonsToAdd = GetSeasonsToCreate(model);
                    newSeries.seasons = seasonsToAdd;
                    var result = await SonarrApi.AddSeries(newSeries, s.ApiKey, s.FullUri);

                    existingSeries = await SonarrApi.GetSeriesById(result.id, s.ApiKey, s.FullUri);
                    await SendToSonarr(model, existingSeries, s);
                }
                else
                {
                    await SendToSonarr(model, existingSeries, s);
                }

                return(new NewSeries
                {
                    id = existingSeries.id,
                    seasons = existingSeries.seasons.ToList(),
                    cleanTitle = existingSeries.cleanTitle,
                    title = existingSeries.title,
                    tvdbId = existingSeries.tvdbId
                });
            }
            catch (Exception e)
            {
                Logger.LogError(LoggingEvents.SonarrSender, e, "Exception thrown when attempting to send series over to Sonarr");
                throw;
            }
        }
示例#7
0
        /// <summary>
        /// Send the request to Sonarr to process
        /// </summary>
        /// <param name="s"></param>
        /// <param name="model"></param>
        /// <param name="qualityId">This is for any qualities overriden from the UI</param>
        /// <returns></returns>
        public async Task <NewSeries> SendToSonarr(ChildRequests model, string qualityId = null)
        {
            var s = await SonarrSettings.GetSettingsAsync();

            if (!s.Enabled)
            {
                return(null);
            }
            if (string.IsNullOrEmpty(s.ApiKey))
            {
                return(null);
            }
            var qualityProfile = 0;

            if (!string.IsNullOrEmpty(qualityId)) // try to parse the passed in quality, otherwise use the settings default quality
            {
                int.TryParse(qualityId, out qualityProfile);
            }

            if (qualityProfile <= 0)
            {
                int.TryParse(s.QualityProfile, out qualityProfile);
            }

            // Get the root path from the rootfolder selected.
            // For some reason, if we haven't got one use the first root folder in Sonarr
            // TODO make this overrideable via the UI
            var rootFolderPath = await GetSonarrRootPath(model.ParentRequest.RootFolder ?? int.Parse(s.RootPath), s);

            try
            {
                // Does the series actually exist?
                var allSeries = await SonarrApi.GetSeries(s.ApiKey, s.FullUri);

                var existingSeries = allSeries.FirstOrDefault(x => x.tvdbId == model.ParentRequest.TvDbId);

                if (existingSeries == null)
                {
                    // Time to add a new one
                    var newSeries = new NewSeries
                    {
                        title            = model.ParentRequest.Title,
                        imdbId           = model.ParentRequest.ImdbId,
                        tvdbId           = model.ParentRequest.TvDbId,
                        cleanTitle       = model.ParentRequest.Title,
                        monitored        = true,
                        seasonFolder     = s.SeasonFolders,
                        rootFolderPath   = rootFolderPath,
                        qualityProfileId = qualityProfile,
                        titleSlug        = model.ParentRequest.Title,
                        addOptions       = new AddOptions
                        {
                            ignoreEpisodesWithFiles    = true, // There shouldn't be any episodes with files, this is a new season
                            ignoreEpisodesWithoutFiles = true, // We want all missing
                            searchForMissingEpisodes   = false // we want dont want to search yet. We want to make sure everything is unmonitored/monitored correctly.
                        }
                    };

                    // Montitor the correct seasons,
                    // If we have that season in the model then it's monitored!
                    var seasonsToAdd = new List <Season>();
                    for (var i = 1; i < model.ParentRequest.TotalSeasons + 1; i++)
                    {
                        var index  = i;
                        var season = new Season
                        {
                            seasonNumber = i,
                            monitored    = model.SeasonRequests.Any(x => x.SeasonNumber == index)
                        };
                        seasonsToAdd.Add(season);
                    }
                    newSeries.seasons = seasonsToAdd;
                    var result = await SonarrApi.AddSeries(newSeries, s.ApiKey, s.FullUri);

                    existingSeries = await SonarrApi.GetSeriesById(result.id, s.ApiKey, s.FullUri);
                    await SendToSonarr(model, existingSeries, s);
                }
                else
                {
                    await SendToSonarr(model, existingSeries, s);
                }

                return(new NewSeries
                {
                    id = existingSeries.id,
                    seasons = existingSeries.seasons.ToList(),
                    cleanTitle = existingSeries.cleanTitle,
                    title = existingSeries.title,
                    tvdbId = existingSeries.tvdbId
                });
            }
            catch (Exception e)
            {
                Logger.LogError(LoggingEvents.SonarrSender, e, "Exception thrown when attempting to send series over to Sonarr");
                throw;
            }
        }