Skip to content

Commit

Permalink
Finished #535 #445 #170
Browse files Browse the repository at this point in the history
  • Loading branch information
tidusjar committed Jan 23, 2017
1 parent 9121af0 commit 7fc26df
Show file tree
Hide file tree
Showing 19 changed files with 263 additions and 129 deletions.
3 changes: 1 addition & 2 deletions Ombi.Api.Interfaces/ISonarrApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ public interface ISonarrApi
List<SonarrProfile> GetProfiles(string apiKey, Uri baseUrl);

SonarrAddSeries AddSeries(int tvdbId, string title, int qualityId, bool seasonFolders, string rootPath,
int rootFolderId,
int seasonCount, int[] seasons, string apiKey, Uri baseUrl, bool monitor = true,
bool searchForMissingEpisodes = false);

SonarrAddSeries AddSeriesNew(int tvdbId, string title, int qualityId, bool seasonFolders, string rootPath,
int rootFolderId, int[] seasons, string apiKey, Uri baseUrl, bool monitor = true,
int[] seasons, string apiKey, Uri baseUrl, bool monitor = true,
bool searchForMissingEpisodes = false);

SystemStatus SystemStatus(string apiKey, Uri baseUrl);
Expand Down
4 changes: 2 additions & 2 deletions Ombi.Api/SonarrApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public List<SonarrRootFolder> GetRootFolders(string apiKey, Uri baseUrl)
return obj;
}

public SonarrAddSeries AddSeries(int tvdbId, string title, int qualityId, bool seasonFolders, string rootPath, int rootFolderId, int seasonCount, int[] seasons, string apiKey, Uri baseUrl, bool monitor = true, bool searchForMissingEpisodes = false)
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);
Expand Down Expand Up @@ -148,7 +148,7 @@ public SonarrAddSeries AddSeries(int tvdbId, string title, int qualityId, bool s
return result;
}

public SonarrAddSeries AddSeriesNew(int tvdbId, string title, int qualityId, bool seasonFolders, string rootPath, int rootFolderId, int[] seasons, string apiKey, Uri baseUrl, bool monitor = true, bool searchForMissingEpisodes = false)
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
{
Expand Down
2 changes: 0 additions & 2 deletions Ombi.Core/SettingModels/SonarrSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,5 @@ public sealed class SonarrSettings : ExternalSettings
public string QualityProfile { get; set; }
public bool SeasonFolders { get; set; }
public string RootPath { get; set; }
public string RootFolder { get; set; }

}
}
39 changes: 24 additions & 15 deletions Ombi.Core/TvSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,27 @@
using Ombi.Api.Models.SickRage;
using Ombi.Api.Models.Sonarr;
using Ombi.Core.SettingModels;
using Ombi.Helpers;
using Ombi.Store;

namespace Ombi.Core
{
public class TvSender
{
public TvSender(ISonarrApi sonarrApi, ISickRageApi srApi)
public TvSender(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, string.Empty);
return await SendToSonarr(sonarrSettings, model, string.Empty);
}

/// <summary>
Expand All @@ -61,7 +64,7 @@ public async Task<SonarrAddSeries> SendToSonarr(SonarrSettings sonarrSettings, R
/// <param name="model"></param>
/// <param name="qualityId"></param>
/// <returns></returns>
public async Task<SonarrAddSeries> SendToSonarr(SonarrSettings sonarrSettings, RequestedModel model, string qualityId, string rootFolderId)
public async Task<SonarrAddSeries> SendToSonarr(SonarrSettings sonarrSettings, RequestedModel model, string qualityId)
{
var qualityProfile = 0;
var episodeRequest = model.Episodes.Any();
Expand All @@ -82,16 +85,7 @@ public async Task<SonarrAddSeries> SendToSonarr(SonarrSettings sonarrSettings, R
var latest = model.SeasonsRequested?.Equals("Latest", StringComparison.CurrentCultureIgnoreCase);
var specificSeasonRequest = model.SeasonList?.Any();

var rootFolder = 0;
if (!string.IsNullOrEmpty(rootFolderId))
{
int.TryParse(qualityId, out rootFolder);
}

if (rootFolder <= 0)
{
int.TryParse(sonarrSettings.RootFolder, out rootFolder);
}
var rootFolderPath = model.RootFolderSelected <= 0 ? sonarrSettings.RootPath : await GetRootPath(model.RootFolderSelected, sonarrSettings);

if (episodeRequest)
{
Expand All @@ -107,7 +101,7 @@ public async Task<SonarrAddSeries> SendToSonarr(SonarrSettings sonarrSettings, R

// 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, rootFolder, new int[0], sonarrSettings.ApiKey,
sonarrSettings.SeasonFolders, rootFolderPath, 0, new int[0], sonarrSettings.ApiKey,
sonarrSettings.FullUri, false));


Expand Down Expand Up @@ -136,7 +130,7 @@ public async Task<SonarrAddSeries> SendToSonarr(SonarrSettings sonarrSettings, R
{
// 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, sonarrSettings.RootPath, rootFolder, new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13}, sonarrSettings.ApiKey,
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));
Expand Down Expand Up @@ -383,5 +377,20 @@ private async Task<Series> GetSonarrSeries(SonarrSettings sonarrSettings, int sh

return selectedSeries;
}

private async Task<string> GetRootPath(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;
}

}
}
41 changes: 25 additions & 16 deletions Ombi.Core/TvSenderOld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,30 @@
using Ombi.Api.Models.SickRage;
using Ombi.Api.Models.Sonarr;
using Ombi.Core.SettingModels;
using Ombi.Helpers;
using Ombi.Store;

namespace Ombi.Core
{
public class TvSenderOld
{
public TvSenderOld(ISonarrApi sonarrApi, ISickRageApi srApi)
public TvSenderOld(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, string rootFolderId = null)
public async Task<SonarrAddSeries> SendToSonarr(SonarrSettings sonarrSettings, RequestedModel model)
{
return await SendToSonarr(sonarrSettings, model, string.Empty, rootFolderId);
return await SendToSonarr(sonarrSettings, model, string.Empty);
}

public async Task<SonarrAddSeries> SendToSonarr(SonarrSettings sonarrSettings, RequestedModel model, string qualityId, string rootFolderId)
public async Task<SonarrAddSeries> SendToSonarr(SonarrSettings sonarrSettings, RequestedModel model, string qualityId)
{
var qualityProfile = 0;
var episodeRequest = model.Episodes.Any();
Expand All @@ -67,16 +70,7 @@ public async Task<SonarrAddSeries> SendToSonarr(SonarrSettings sonarrSettings, R
{
int.TryParse(sonarrSettings.QualityProfile, out qualityProfile);
}
var rootFolder = 0;
if (!string.IsNullOrEmpty(rootFolderId))
{
int.TryParse(qualityId, out rootFolder);
}

if (rootFolder <= 0)
{
int.TryParse(sonarrSettings.RootFolder, out rootFolder);
}
var rootFolderPath = model.RootFolderSelected <= 0 ? sonarrSettings.RootPath : await GetRootPath(model.RootFolderSelected, sonarrSettings);


var series = await GetSonarrSeries(sonarrSettings, model.ProviderId);
Expand All @@ -95,7 +89,7 @@ public async Task<SonarrAddSeries> SendToSonarr(SonarrSettings sonarrSettings, R

// 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, rootFolder, 0, new int[0], sonarrSettings.ApiKey,
sonarrSettings.SeasonFolders, rootFolderPath, 0, new int[0], sonarrSettings.ApiKey,
sonarrSettings.FullUri, false));


Expand Down Expand Up @@ -167,7 +161,7 @@ public async Task<SonarrAddSeries> SendToSonarr(SonarrSettings sonarrSettings, R


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

return result;
Expand Down Expand Up @@ -309,5 +303,20 @@ private async Task<Series> GetSonarrSeries(SonarrSettings sonarrSettings, int sh

return selectedSeries;
}


private async Task<string> GetRootPath(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;
}
}
}
7 changes: 5 additions & 2 deletions Ombi.Services/Jobs/FaultQueueHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public FaultQueueHandler(IJobRecord record, IRepository<RequestQueue> repo, ISon
ISickRageApi srApi, ISettingsService<SonarrSettings> sonarrSettings, ISettingsService<SickRageSettings> srSettings,
ICouchPotatoApi cpApi, ISettingsService<CouchPotatoSettings> cpsettings, IRequestService requestService,
ISettingsService<HeadphonesSettings> hpSettings, IHeadphonesApi headphonesApi, ISettingsService<PlexRequestSettings> prSettings,
ISecurityExtensions security, IMovieSender movieSender)
ISecurityExtensions security, IMovieSender movieSender, ICacheProvider cache)
{
Record = record;
Repo = repo;
Expand All @@ -71,13 +71,16 @@ public FaultQueueHandler(IJobRecord record, IRepository<RequestQueue> repo, ISon
Security = security;
PrSettings = prSettings.GetSettings();
MovieSender = movieSender;

Cache = cache;
}

private IMovieSender MovieSender { get; }
private IRepository<RequestQueue> Repo { get; }
private IJobRecord Record { get; }
private ISonarrApi SonarrApi { get; }
private ISickRageApi SrApi { get; }
private ICacheProvider Cache { get; }
private ICouchPotatoApi CpApi { get; }
private IHeadphonesApi HpApi { get; }
private IRequestService RequestService { get; }
Expand Down Expand Up @@ -163,7 +166,7 @@ private bool ProcessTvShow(RequestedModel tvModel, SonarrSettings sonarr, SickRa
try
{

var sender = new TvSenderOld(SonarrApi, SrApi);
var sender = new TvSenderOld(SonarrApi, SrApi, Cache);
if (sonarr.Enabled)
{
var task = sender.SendToSonarr(sonarr, tvModel, sonarr.QualityProfile);
Expand Down
1 change: 1 addition & 0 deletions Ombi.Services/Jobs/PlexAvailabilityChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public PlexAvailabilityChecker(ISettingsService<PlexSettings> plexSettings, IReq

public void CheckAndUpdateAll()
{

var plexSettings = Plex.GetSettings();

if (!ValidateSettings(plexSettings))
Expand Down
7 changes: 7 additions & 0 deletions Ombi.Store/RequestedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ public RequestedModel()
public List<EpisodesModel> Episodes { get; set; }
public bool Denied { get; set; }
public string DeniedReason { get; set; }
/// <summary>
/// For TV Shows with a custom root folder
/// </summary>
/// <value>
/// The root folder selected.
/// </value>
public int RootFolderSelected { get; set; }

[JsonIgnore]
public List<string> AllUsers
Expand Down
14 changes: 7 additions & 7 deletions Ombi.UI.Tests/TvSenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
using Ombi.Api.Models.Sonarr;
using Ombi.Core;
using Ombi.Core.SettingModels;
using Ombi.Helpers;
using Ombi.Store;
using Ploeh.AutoFixture;

Expand All @@ -49,14 +50,16 @@ public class TvSenderTests

private TvSender Sender { get; set; }
private Fixture F { get; set; }
private Mock<ICacheProvider> Cache { get; set; }

[SetUp]
public void Setup()
{
F = new Fixture();
SonarrMock = new Mock<ISonarrApi>();
SickrageMock = new Mock<ISickRageApi>();
Sender = new TvSender(SonarrMock.Object, SickrageMock.Object);
Cache = new Mock<ICacheProvider>();
Sender = new TvSender(SonarrMock.Object, SickrageMock.Object, Cache.Object);
}

[Test]
Expand All @@ -66,7 +69,7 @@ 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);
Sender = new TvSender(SonarrMock.Object, SickrageMock.Object, Cache.Object);

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

Expand All @@ -79,7 +82,6 @@ public async Task HappyPathSendSeriesToSonarrAllSeason()
It.IsAny<bool>(),
It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<int>(),
It.IsAny<int[]>(),
It.IsAny<string>(),
It.IsAny<Uri>(),
Expand All @@ -100,7 +102,6 @@ public async Task HappyPathSendEpisodeWithExistingSeriesToSonarr()
It.IsAny<bool>(),
It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<int>(),
It.IsAny<int[]>(),
It.IsAny<string>(),
It.IsAny<Uri>(),
Expand All @@ -118,7 +119,7 @@ public async Task HappyPathSendEpisodeWithExistingSeriesToSonarr()
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);
Sender = new TvSender(SonarrMock.Object, SickrageMock.Object, Cache.Object);
var episodes = new List<EpisodesModel>
{
new EpisodesModel
Expand All @@ -130,7 +131,7 @@ public async Task HappyPathSendEpisodeWithExistingSeriesToSonarr()
var model = F.Build<RequestedModel>().With(x => x.ProviderId, 1)
.With(x => x.Episodes, episodes).Create();

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

Assert.That(result, Is.EqualTo(seriesResult));
SonarrMock.Verify(x => x.AddSeries(It.IsAny<int>(),
Expand All @@ -139,7 +140,6 @@ public async Task HappyPathSendEpisodeWithExistingSeriesToSonarr()
It.IsAny<bool>(),
It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<int>(),
It.IsAny<int[]>(),
It.IsAny<string>(),
It.IsAny<Uri>(),
Expand Down
Loading

0 comments on commit 7fc26df

Please sign in to comment.