Started to impliment the Plex checker. This will check plex every x minutes to see if there is any new content and then update the avalibility of the requests

This commit is contained in:
Jamie Rees 2016-03-06 01:25:54 +00:00
commit 3b0b0c521e
19 changed files with 329 additions and 160 deletions

View file

@ -24,6 +24,8 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using FluentScheduler;
using Mono.Data.Sqlite;
using Nancy;
@ -39,6 +41,8 @@ using PlexRequests.Core.SettingModels;
using PlexRequests.Helpers;
using PlexRequests.Store;
using PlexRequests.Store.Repository;
using PlexRequests.UI.Jobs;
using TaskFactory = FluentScheduler.TaskFactory;
namespace PlexRequests.UI
{
@ -62,12 +66,16 @@ namespace PlexRequests.UI
container.Register<ISettingsService<AuthenticationSettings>, SettingsServiceV2<AuthenticationSettings>>();
container.Register<ISettingsService<PlexSettings>, SettingsServiceV2<PlexSettings>>();
container.Register<IRepository<RequestedModel>, GenericRepository<RequestedModel>>();
container.Register<IAvailabilityChecker, PlexAvailabilityChecker>();
container.Register<IRequestService, RequestService>();
base.ConfigureRequestContainer(container, context);
}
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
TaskManager.TaskFactory = new Jobs.TaskFactory();
TaskManager.Initialize(new PlexRegistry());
CookieBasedSessions.Enable(pipelines, CryptographyConfiguration.Default);

View file

@ -0,0 +1,34 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: IAvailabilityChecker.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
namespace PlexRequests.UI.Jobs
{
public interface IAvailabilityChecker
{
void CheckAndUpdate(string searchTerm, int id);
void CheckAndUpdateAll();
}
}

View file

@ -0,0 +1,109 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: PlexAvailabilityChecker.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.Linq;
using System.Web.Hosting;
using FluentScheduler;
using PlexRequests.Api;
using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
namespace PlexRequests.UI.Jobs
{
public class PlexAvailabilityChecker : IAvailabilityChecker, ITask, IRegisteredObject
{
public PlexAvailabilityChecker(ISettingsService<PlexSettings> plexSettings, ISettingsService<AuthenticationSettings> auth, IRequestService request)
{
Plex = plexSettings;
Auth = auth;
RequestService = request;
HostingEnvironment.RegisterObject(this);
}
private readonly object _lock = new object();
private bool _shuttingDown;
private ISettingsService<PlexSettings> Plex { get; }
private ISettingsService<AuthenticationSettings> Auth { get; }
private IRequestService RequestService { get; set; }
public void CheckAndUpdate(string searchTerm, int id)
{
var plexSettings = Plex.GetSettings();
var authSettings = Auth.GetSettings();
var api = new PlexApi();
var results = api.SearchContent(authSettings.PlexAuthToken, searchTerm, plexSettings.FullUri);
var result = results.Video.FirstOrDefault(x => x.Title == searchTerm);
var originalRequest = RequestService.Get(id);
originalRequest.Available = result != null;
RequestService.UpdateRequest(id, originalRequest);
}
public void CheckAndUpdateAll()
{
var plexSettings = Plex.GetSettings();
var authSettings = Auth.GetSettings();
var requests = RequestService.GetAll();
var api = new PlexApi();
foreach (var r in requests)
{
var results = api.SearchContent(authSettings.PlexAuthToken, r.Title, plexSettings.FullUri);
var result = results.Video.FirstOrDefault(x => x.Title == r.Title);
var originalRequest = RequestService.Get(r.Id);
originalRequest.Available = result != null;
RequestService.UpdateRequest(r.Id, originalRequest);
}
}
public void Execute()
{
lock (_lock)
{
if (_shuttingDown)
return;
CheckAndUpdateAll();
}
}
public void Stop(bool immediate)
{
lock (_lock)
{
_shuttingDown = true;
}
HostingEnvironment.UnregisterObject(this);
}
}
}

View file

@ -0,0 +1,39 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: PlexRegistry.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 FluentScheduler;
namespace PlexRequests.UI.Jobs
{
public class PlexRegistry : Registry
{
public PlexRegistry()
{
Schedule<PlexAvailabilityChecker>().ToRunNow().AndEvery(2).Minutes();
}
}
}

View file

@ -0,0 +1,17 @@
using FluentScheduler;
using Nancy.TinyIoc;
namespace PlexRequests.UI.Jobs
{
public class TaskFactory : ITaskFactory
{
public ITask GetTaskInstance<T>() where T : ITask
{
var container = TinyIoCContainer.Current;
object outT;
container.TryResolve(typeof(T), out outT);
return (T)outT;
}
}
}

View file

@ -104,7 +104,7 @@ namespace PlexRequests.UI.Modules
Log.Trace("Getting Settings:");
Log.Trace(settings.DumpJson());
return View["/Settings", settings];
return View["Settings", settings];
}
private Response SaveAdmin()
@ -166,7 +166,7 @@ namespace PlexRequests.UI.Modules
{ return Response.AsJson(string.Empty); }
var usernames = users.User.Select(x => x.Username);
return Response.AsJson(usernames); //TODO usernames are not populated.
return Response.AsJson(usernames);
}
private Negotiator CouchPotato()

View file

@ -72,31 +72,25 @@ namespace PlexRequests.UI.Modules
private Response GetMovies()
{
// TODO check plex to see the availability
var settings = AuthSettings.GetSettings();
var plexSettings = PlexSettings.GetSettings();
var plex = new PlexApi();
var dbMovies = Service.GetAll().Where(x => x.Type == RequestType.Movie);
var viewModel = dbMovies.Select(tv => new RequestViewModel
var viewModel = dbMovies.Select(movie => new RequestViewModel
{
ProviderId = tv.ProviderId,
Type = tv.Type,
Status = tv.Status,
ImdbId = tv.ImdbId,
Id = tv.Id,
PosterPath = tv.PosterPath,
ReleaseDate = tv.ReleaseDate.Humanize(),
RequestedDate = tv.RequestedDate.Humanize(),
Approved = tv.Approved,
Title = tv.Title,
Overview = tv.Overview,
RequestedBy = tv.RequestedBy,
ReleaseYear = tv.ReleaseDate.Year.ToString()
ProviderId = movie.ProviderId,
Type = movie.Type,
Status = movie.Status,
ImdbId = movie.ImdbId,
Id = movie.Id,
PosterPath = movie.PosterPath,
ReleaseDate = movie.ReleaseDate.Humanize(),
RequestedDate = movie.RequestedDate.Humanize(),
Approved = movie.Approved,
Title = movie.Title,
Overview = movie.Overview,
RequestedBy = movie.RequestedBy,
ReleaseYear = movie.ReleaseDate.Year.ToString(),
Available = movie.Available
}).ToList();
//TODO check if Available in CP
return Response.AsJson(viewModel);
}
@ -117,9 +111,10 @@ namespace PlexRequests.UI.Modules
Title = tv.Title,
Overview = tv.Overview,
RequestedBy = tv.RequestedBy,
ReleaseYear = tv.ReleaseDate.Year.ToString()
ReleaseYear = tv.ReleaseDate.Year.ToString(),
Available = tv.Available
}).ToList();
//TODO check if Available in Sonarr
return Response.AsJson(viewModel);
}

View file

@ -26,7 +26,6 @@
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using Nancy;
using Nancy.Responses.Negotiation;
@ -37,19 +36,24 @@ using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
using PlexRequests.Helpers;
using PlexRequests.Store;
using PlexRequests.UI.Jobs;
using PlexRequests.UI.Models;
namespace PlexRequests.UI.Modules
{
public class SearchModule : BaseModule
{
public SearchModule(ICacheProvider cache, ISettingsService<CouchPotatoSettings> cpSettings, ISettingsService<PlexRequestSettings> prSettings) : base("search")
public SearchModule(ICacheProvider cache, ISettingsService<CouchPotatoSettings> cpSettings,
ISettingsService<PlexRequestSettings> prSettings, IAvailabilityChecker checker,
IRequestService request) : base("search")
{
CpService = cpSettings;
PrService = prSettings;
MovieApi = new TheMovieDbApi();
TvApi = new TheTvDbApi();
Cache = cache;
Checker = checker;
RequestService = request;
Get["/"] = parameters => RequestLoad();
@ -64,9 +68,11 @@ namespace PlexRequests.UI.Modules
}
private TheMovieDbApi MovieApi { get; }
private TheTvDbApi TvApi { get; }
private IRequestService RequestService { get; }
private ICacheProvider Cache { get; }
private ISettingsService<CouchPotatoSettings> CpService { get; }
private ISettingsService<PlexRequestSettings> PrService { get; }
private IAvailabilityChecker Checker { get; }
private static Logger Log = LogManager.GetCurrentClassLogger();
private string AuthToken => Cache.GetOrSet(CacheKeys.TvDbToken, TvApi.Authenticate, 50);
@ -145,8 +151,7 @@ namespace PlexRequests.UI.Modules
private Response RequestMovie(int movieId)
{
Log.Trace("Requesting movie with id {0}", movieId);
var s = new SettingsService(Cache);
if (s.CheckRequest(movieId))
if (RequestService.CheckRequest(movieId))
{
Log.Trace("movie with id {0} exists", movieId);
return Response.AsJson(new { Result = false, Message = "Movie has already been requested!" });
@ -193,7 +198,7 @@ namespace PlexRequests.UI.Modules
{
model.Approved = true;
Log.Trace("Adding movie to database requests (No approval required)");
s.AddRequest(movieId, model);
RequestService.AddRequest(movieId, model);
return Response.AsJson(new { Result = true });
}
@ -203,7 +208,9 @@ namespace PlexRequests.UI.Modules
try
{
Log.Trace("Adding movie to database requests");
s.AddRequest(movieId, model);
var id = RequestService.AddRequest(movieId, model);
//BackgroundJob.Enqueue(() => Checker.CheckAndUpdate(model.Title, (int)id));
return Response.AsJson(new { Result = true });
}
catch (Exception e)
@ -223,8 +230,7 @@ namespace PlexRequests.UI.Modules
private Response RequestTvShow(int showId, bool latest)
{
// Latest send to Sonarr and no need to store in DB
var s = new SettingsService(Cache);
if (s.CheckRequest(showId))
if (RequestService.CheckRequest(showId))
{
return Response.AsJson(new { Result = false, Message = "TV Show has already been requested!" });
}
@ -251,7 +257,7 @@ namespace PlexRequests.UI.Modules
RequestedBy = Session[SessionKeys.UsernameKey].ToString()
};
s.AddRequest(showId, model);
RequestService.AddRequest(showId, model);
return Response.AsJson(new { Result = true });
}
private string GetAuthToken(TheTvDbApi api)

View file

@ -61,16 +61,8 @@
<HintPath>..\packages\Dapper.1.42\lib\net45\Dapper.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Hangfire.Core, Version=1.5.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Hangfire.Core.1.5.3\lib\net45\Hangfire.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Hangfire.SQLite, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Hangfire.SQLite.1.1.0.0\lib\net45\Hangfire.SQLite.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Hangfire.SqlServer, Version=1.5.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Hangfire.SqlServer.1.5.3\lib\net45\Hangfire.SqlServer.dll</HintPath>
<Reference Include="FluentScheduler, Version=3.1.46.0, Culture=neutral, PublicKeyToken=b76503528a14ebd1, processorArchitecture=MSIL">
<HintPath>..\packages\FluentScheduler.3.1.46\lib\net40\FluentScheduler.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Humanizer, Version=2.0.1.0, Culture=neutral, PublicKeyToken=979442b78dfc278e, processorArchitecture=MSIL">
@ -169,6 +161,10 @@
<Content Include="Content\custom.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Compile Include="Jobs\IAvailabilityChecker.cs" />
<Compile Include="Jobs\PlexAvailabilityChecker.cs" />
<Compile Include="Jobs\PlexRegistry.cs" />
<Compile Include="Jobs\TaskFactory.cs" />
<Compile Include="Models\PlexAuth.cs" />
<Compile Include="Models\RequestViewModel.cs" />
<Compile Include="Models\SearchTvShowViewModel.cs" />

View file

@ -25,10 +25,10 @@
// ************************************************************************/
#endregion
using System;
using Hangfire;
using Hangfire.SQLite;
using FluentScheduler;
using Owin;
using PlexRequests.Core;
using PlexRequests.UI.Jobs;
using TaskFactory = FluentScheduler.TaskFactory;
namespace PlexRequests.UI
{
@ -40,10 +40,6 @@ namespace PlexRequests.UI
{
app.UseNancy();
//GlobalConfiguration.Configuration.UseSQLiteStorage("Sqlite");
//app.UseHangfireDashboard();
//app.UseHangfireServer();
}
catch (Exception exception)

View file

@ -10,7 +10,9 @@
<add key="ClientSettingsProvider.ServiceUri" value="" />
<add key="webPages:Enabled" value="false" /></appSettings>
<connectionStrings>
<add name="Sqlite" connectionString="Data Source=RequestPlex.sqlite" providerName="Mono.Data.Sqlite" />
</connectionStrings>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>

View file

@ -1,10 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Dapper" version="1.42" targetFramework="net46" />
<package id="Hangfire" version="1.5.3" targetFramework="net46" />
<package id="Hangfire.Core" version="1.5.3" targetFramework="net46" />
<package id="Hangfire.SQLite" version="1.1.0.0" targetFramework="net46" />
<package id="Hangfire.SqlServer" version="1.5.3" targetFramework="net46" />
<package id="FluentScheduler" version="3.1.46" targetFramework="net46" />
<package id="Humanizer.Core" version="2.0.1" targetFramework="net452" />
<package id="Microsoft.AspNet.Razor" version="2.0.30506.0" targetFramework="net452" />
<package id="Microsoft.Owin" version="3.0.1" targetFramework="net452" />

View file

@ -27,7 +27,7 @@
</httpHandlers>
</system.web>
<connectionStrings>
<add name="Sqlite" connectionString="Data Source=RequestPlex.sqlite"/>
<add name="Sqlite" connectionString="Data Source=RequestPlex.sqlite" providerName="Mono.Data.Sqlite"/>
</connectionStrings>
<appSettings>
<add key="webPages:Enabled" value="false" />