Made a start on the new Sonarr integration.

the Latest series option works.
pull/1334/head
tidusjar 8 years ago
parent 8a55de2b71
commit ddefbf31db

@ -56,5 +56,8 @@ namespace Ombi.Api.Interfaces
Series UpdateSeries(Series series, string apiKey, Uri baseUrl);
SonarrSeasonSearchResult SearchForSeason(int seriesId, int seasonNumber, string apiKey, Uri baseUrl);
SonarrSeriesSearchResult SearchForSeries(int seriesId, string apiKey, Uri baseUrl);
SonarrAddSeries AddSeries(SonarrAddSeries series, string apiKey, Uri baseUrl);
}
}

@ -148,6 +148,42 @@ namespace Ombi.Api
return result;
}
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;
}
public SonarrAddSeries AddSeriesNew(int tvdbId, string title, int qualityId, bool seasonFolders, string rootPath, int[] seasons, string apiKey, Uri baseUrl, bool monitor = true, bool searchForMissingEpisodes = false)
{
var request = new RestRequest
@ -244,7 +280,18 @@ namespace Ombi.Api
TimeSpan.FromSeconds(5)
});
return policy.Execute(() => Api.ExecuteJson<List<Series>>(request, baseUrl));
var series = policy.Execute(() => Api.ExecuteJson<List<Series>>(request, baseUrl));
// Remove the 'specials from the object'
foreach (var s in series)
{
var seasonToRemove = s.seasons.FirstOrDefault(x => x.seasonNumber == 0);
if (seasonToRemove != null)
{
s.seasons.Remove(seasonToRemove);
}
}
return series;
}
catch (Exception e)
{
@ -266,7 +313,15 @@ namespace Ombi.Api
Log.Error(exception, "Exception when calling GetSeries by ID for Sonarr, Retrying {0}",
timespan));
return policy.Execute(() => Api.ExecuteJson<Series>(request, baseUrl));
var series = policy.Execute(() => Api.ExecuteJson<Series>(request, baseUrl));
// Remove the specials season
var toRemove = series.seasons.FirstOrDefault(x => x.seasonNumber == 0);
if (toRemove != null)
{
series.seasons.Remove(toRemove);
}
return series;
}
catch (Exception e)
{

@ -155,8 +155,9 @@
<Compile Include="StatusChecker\AppveyorArtifactResult.cs" />
<Compile Include="StatusChecker\StatusChecker.cs" />
<Compile Include="StatusChecker\AppveyorBranchResult.cs" />
<Compile Include="TvSender.cs" />
<Compile Include="TvSenderOld.cs" />
<Compile Include="Tv\TvSender.cs" />
<Compile Include="Tv\TvSenderOld.cs" />
<Compile Include="Tv\TvSenderV2.cs" />
<Compile Include="Users\IUserHelper.cs" />
<Compile Include="Users\UserHelper.cs" />
<Compile Include="UserIdentity.cs" />

@ -87,6 +87,8 @@ namespace Ombi.Core
var rootFolderPath = model.RootFolderSelected <= 0 ? sonarrSettings.FullRootPath : await GetRootPath(model.RootFolderSelected, sonarrSettings);
if (episodeRequest)
{
// Does series exist?

@ -0,0 +1,244 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2017 Jamie Rees
// File: TvSenderV2.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NLog;
using Ombi.Api.Interfaces;
using Ombi.Api.Models.SickRage;
using Ombi.Api.Models.Sonarr;
using Ombi.Core.SettingModels;
using Ombi.Helpers;
using Ombi.Store;
namespace Ombi.Core.Tv
{
public class TvSenderV2
{
public TvSenderV2(ISonarrApi sonarrApi, ISickRageApi srApi, ICacheProvider cache)
{
SonarrApi = sonarrApi;
SickrageApi = srApi;
Cache = cache;
}
private ISonarrApi SonarrApi { get; }
private ISickRageApi SickrageApi { get; }
private ICacheProvider Cache { get; }
private static Logger _log = LogManager.GetCurrentClassLogger();
public async Task<SonarrAddSeries> SendToSonarr(SonarrSettings sonarrSettings, RequestedModel model)
{
return await SendToSonarr(sonarrSettings, model, string.Empty);
}
public async Task<SonarrAddSeries> SendToSonarr(SonarrSettings sonarrSettings, RequestedModel model,
string qualityId)
{
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(sonarrSettings.QualityProfile, out qualityProfile);
}
var rootFolderPath = model.RootFolderSelected <= 0 ? sonarrSettings.FullRootPath : await GetSonarrRootPath(model.RootFolderSelected, sonarrSettings);
var episodeRequest = model.Episodes.Any();
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();
if (episodeRequest)
{
return await ProcessSonarrEpisodeRequest(sonarrSettings, model, qualityProfile, rootFolderPath);
}
if (requestAll ?? false)
{
return await ProcessSonarrRequestAll(sonarrSettings, model, qualityProfile, rootFolderPath);
}
if (first ?? false)
{
return await ProcessSonarrRequestFirstSeason(sonarrSettings, model, qualityProfile, rootFolderPath);
}
if (latest ?? false)
{
return await ProcessSonarrRequestLatestSeason(sonarrSettings, model, qualityProfile, rootFolderPath);
}
if (specificSeasonRequest ?? false)
{
return await ProcessSonarrRequestSpecificSeason(sonarrSettings, model, qualityProfile, rootFolderPath);
}
return null;
}
private async Task<SonarrAddSeries> ProcessSonarrRequestSpecificSeason(SonarrSettings sonarrSettings, RequestedModel model, int qualityId, string rootFolderPath)
{
throw new NotImplementedException();
}
private async Task<SonarrAddSeries> ProcessSonarrRequestLatestSeason(SonarrSettings sonarrSettings, RequestedModel model, int qualityId, string rootFolderPath)
{
// Does the series exist?
var series = await GetSonarrSeries(sonarrSettings, model.ProviderId);
if (series == null)
{
//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,
// ReSharper disable once SimplifyConditionalTernaryExpression
monitored = true ? model.SeasonList.Length == 0 || model.SeasonList.Any(x => x == i) : false
};
seriesToAdd.seasons.Add(season);
}
return SonarrApi.AddSeries(seriesToAdd, sonarrSettings.ApiKey, sonarrSettings.FullUri);
}
else
{
// Mark the latest as monitored and search
// Also make sure the series is now monitored otherwise we won't search for it
series.monitored = true;
foreach (var seasons in series.seasons)
{
if (model.SeasonList.Any(x => x == seasons.seasonNumber))
{
seasons.monitored = true;
}
}
// Send the update command
series = SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri);
SonarrApi.SearchForSeries(series.id, sonarrSettings.ApiKey, sonarrSettings.FullUri);
return new SonarrAddSeries{title = series.title};
}
}
private async Task<SonarrAddSeries> ProcessSonarrRequestFirstSeason(SonarrSettings sonarrSettings, RequestedModel model, int qualityId, string rootFolderPath)
{
throw new NotImplementedException();
}
private async Task<SonarrAddSeries> ProcessSonarrRequestAll(SonarrSettings sonarrSettings, RequestedModel model, int qualityId, string rootFolderPath)
{
throw new NotImplementedException();
}
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)
{
// Add the series
}
return null;
}
public SickRageTvAdd SendToSickRage(SickRageSettings sickRageSettings, RequestedModel model)
{
return SendToSickRage(sickRageSettings, model, sickRageSettings.QualityProfile);
}
public SickRageTvAdd SendToSickRage(SickRageSettings sickRageSettings, RequestedModel model, string qualityId)
{
_log.Info("Sending to SickRage {0}", model.Title);
if (sickRageSettings.Qualities.All(x => x.Key != qualityId))
{
qualityId = sickRageSettings.QualityProfile;
}
var apiResult = SickrageApi.AddSeries(model.ProviderId, model.SeasonCount, model.SeasonList, qualityId,
sickRageSettings.ApiKey, sickRageSettings.FullUri);
var result = apiResult.Result;
return result;
}
private async Task<Series> GetSonarrSeries(SonarrSettings sonarrSettings, int showId)
{
var task = await Task.Run(() => SonarrApi.GetSeries(sonarrSettings.ApiKey, sonarrSettings.FullUri)).ConfigureAwait(false);
var selectedSeries = task.FirstOrDefault(series => series.tvdbId == showId);
return selectedSeries;
}
private async Task<string> GetSonarrRootPath(int pathId, SonarrSettings sonarrSettings)
{
var rootFoldersResult = await Cache.GetOrSetAsync(CacheKeys.SonarrRootFolders, async () =>
{
return await Task.Run(() => SonarrApi.GetRootFolders(sonarrSettings.ApiKey, sonarrSettings.FullUri));
});
foreach (var r in rootFoldersResult.Where(r => r.id == pathId))
{
return r.path;
}
return string.Empty;
}
}
}

@ -152,15 +152,7 @@ namespace Ombi.Services.Jobs
return;
}
var jobs = Job.GetJobs();
var job = jobs.FirstOrDefault(x => x.Name.Equals(JobNames.EpisodeCacher, StringComparison.CurrentCultureIgnoreCase));
if (job != null)
{
if (job.LastRun > DateTime.Now.AddHours(-11)) // If it's been run in the last 11 hours
{
return;
}
}
Job.SetRunning(true, JobNames.EpisodeCacher);
CacheEpisodes(s);
}

@ -46,6 +46,7 @@ using Ombi.Core;
using Ombi.Core.Models;
using Ombi.Core.Queue;
using Ombi.Core.SettingModels;
using Ombi.Core.Tv;
using Ombi.Helpers;
using Ombi.Helpers.Analytics;
using Ombi.Helpers.Permissions;
@ -1119,10 +1120,11 @@ namespace Ombi.UI.Modules
RequestedUsers = new List<string> { Username },
Issues = IssueState.None,
ImdbId = showInfo.externals?.imdb ?? string.Empty,
SeasonCount = showInfo.Season.Count,
TvDbId = showId.ToString()
};
var totalSeasons = showInfo.Season.GroupBy(x => x.SeasonNumber);
model.SeasonCount = totalSeasons.Count();
var seasonsList = new List<int>();
switch (seasons)
{
@ -1884,7 +1886,8 @@ namespace Ombi.UI.Modules
{
model.Approved = true;
var s = await sonarrSettings;
var sender = new TvSenderOld(SonarrApi, SickrageApi, Cache); // TODO put back
//var sender = new TvSenderV2(SonarrApi, SickrageApi, Cache);
var sender = new TvSenderOld(SonarrApi, SickrageApi, Cache);
if (s.Enabled)
{
var result = await sender.SendToSonarr(s, model);

Loading…
Cancel
Save