Fixed #5 and also added some tests to the availability checker

This commit is contained in:
tidusjar 2016-03-15 12:21:41 +00:00
parent 3188a720fa
commit ba5ccfec33
11 changed files with 356 additions and 31 deletions

View file

@ -38,7 +38,6 @@ using PlexRequests.Core.SettingModels;
using PlexRequests.Helpers;
using PlexRequests.Services.Interfaces;
using PlexRequests.Store;
using PlexRequests.UI.Jobs;
using PlexRequests.UI.Models;
namespace PlexRequests.UI.Modules
@ -47,7 +46,8 @@ namespace PlexRequests.UI.Modules
{
public SearchModule(ICacheProvider cache, ISettingsService<CouchPotatoSettings> cpSettings,
ISettingsService<PlexRequestSettings> prSettings, IAvailabilityChecker checker,
IRequestService request, ISonarrApi sonarrApi, ISettingsService<SonarrSettings> sonarrSettings) : base("search")
IRequestService request, ISonarrApi sonarrApi, ISettingsService<SonarrSettings> sonarrSettings,
ICouchPotatoApi cpApi) : base("search")
{
CpService = cpSettings;
PrService = prSettings;
@ -58,6 +58,7 @@ namespace PlexRequests.UI.Modules
RequestService = request;
SonarrApi = sonarrApi;
SonarrService = sonarrSettings;
CouchPotatoApi = cpApi;
Get["/"] = parameters => RequestLoad();
@ -71,6 +72,7 @@ namespace PlexRequests.UI.Modules
Post["request/tv"] = parameters => RequestTvShow((int)Request.Form.tvId, (bool)Request.Form.latest);
}
private TheMovieDbApi MovieApi { get; }
private ICouchPotatoApi CouchPotatoApi { get; }
private ISonarrApi SonarrApi { get; }
private TheTvDbApi TvApi { get; }
private IRequestService RequestService { get; }
@ -168,15 +170,17 @@ namespace PlexRequests.UI.Modules
if (RequestService.CheckRequest(movieId))
{
Log.Trace("movie with id {0} exists", movieId);
return Response.AsJson(new { Result = false, Message = "Movie has already been requested!" });
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Movie has already been requested!" });
}
Log.Debug("movie with id {0} doesnt exists", movieId);
var cpSettings = CpService.GetSettings();
if (cpSettings.ApiKey == null)
{
Log.Warn("CP apiKey is null");
return Response.AsJson(new { Result = false, Message = "CouchPotato is not yet configured, If you are the Admin, please log in." });
return Response.AsJson(new JsonResponseModel { Result = false, Message = "CouchPotato is not yet configured, If you are the Admin, please log in." });
}
Log.Trace("Settings: ");
Log.Trace(cpSettings.DumpJson);
@ -185,6 +189,11 @@ namespace PlexRequests.UI.Modules
Log.Trace("Getting movie info from TheMovieDb");
Log.Trace(movieInfo.DumpJson);
if (CheckIfTitleExistsInPlex(movieInfo.Title))
{
return Response.AsJson(new JsonResponseModel { Result = false, Message = $"{movieInfo.Title} is already in Plex!" });
}
var model = new RequestedModel
{
ProviderId = movieInfo.Id,
@ -206,9 +215,8 @@ namespace PlexRequests.UI.Modules
Log.Trace(settings.DumpJson());
if (!settings.RequireApproval)
{
var cp = new CouchPotatoApi();
Log.Info("Adding movie to CP (No approval required)");
var result = cp.AddMovie(model.ImdbId, cpSettings.ApiKey, model.Title, cpSettings.FullUri);
var result = CouchPotatoApi.AddMovie(model.ImdbId, cpSettings.ApiKey, model.Title, cpSettings.FullUri);
Log.Debug("Adding movie to CP result {0}", result);
if (result)
{
@ -216,9 +224,9 @@ namespace PlexRequests.UI.Modules
Log.Debug("Adding movie to database requests (No approval required)");
RequestService.AddRequest(movieId, model);
return Response.AsJson(new { Result = true });
return Response.AsJson(new JsonResponseModel { Result = true });
}
return Response.AsJson(new { Result = false, Message = "Something went wrong adding the movie to CouchPotato! Please check your settings." });
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Something went wrong adding the movie to CouchPotato! Please check your settings." });
}
try
@ -227,13 +235,13 @@ namespace PlexRequests.UI.Modules
var id = RequestService.AddRequest(movieId, model);
//BackgroundJob.Enqueue(() => Checker.CheckAndUpdate(model.Title, (int)id));
return Response.AsJson(new { Result = true });
return Response.AsJson(new JsonResponseModel { Result = true });
}
catch (Exception e)
{
Log.Fatal(e);
return Response.AsJson(new { Result = false, Message = "Something went wrong adding the movie to CouchPotato! Please check your settings." });
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Something went wrong adding the movie to CouchPotato! Please check your settings." });
}
}
@ -247,14 +255,19 @@ namespace PlexRequests.UI.Modules
{
if (RequestService.CheckRequest(showId))
{
return Response.AsJson(new { Result = false, Message = "TV Show has already been requested!" });
return Response.AsJson(new JsonResponseModel { Result = false, Message = "TV Show has already been requested!" });
}
var tvApi = new TheTvDbApi();
var token = GetAuthToken(tvApi);
var token = GetTvDbAuthToken(tvApi);
var showInfo = tvApi.GetInformation(showId, token).data;
if (CheckIfTitleExistsInPlex(showInfo.seriesName))
{
return Response.AsJson(new JsonResponseModel { Result = false, Message = $"{showInfo.seriesName} is already in Plex!" });
}
DateTime firstAir;
DateTime.TryParse(showInfo.firstAired, out firstAir);
@ -271,7 +284,7 @@ namespace PlexRequests.UI.Modules
Approved = false,
RequestedBy = Session[SessionKeys.UsernameKey].ToString(),
Issues = IssueState.None,
LatestTv = latest
LatestTv = latest
};
RequestService.AddRequest(showId, model);
@ -292,9 +305,15 @@ namespace PlexRequests.UI.Modules
return Response.AsJson(new { Result = true });
}
private string GetAuthToken(TheTvDbApi api)
private string GetTvDbAuthToken(TheTvDbApi api)
{
return Cache.GetOrSet(CacheKeys.TvDbToken, api.Authenticate, 50);
}
private bool CheckIfTitleExistsInPlex(string title)
{
var result = Checker.IsAvailable(title);
return result;
}
}
}