From 3b0b0c521e5750233cc2c6d56708f88fb5ec91e4 Mon Sep 17 00:00:00 2001 From: Jamie Rees Date: Sun, 6 Mar 2016 01:25:54 +0000 Subject: [PATCH] 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 --- PlexRequests.Core/IRequestService.cs | 42 +++++++ PlexRequests.Core/PlexRequests.Core.csproj | 4 +- PlexRequests.Core/RequestService.cs | 44 ++++--- PlexRequests.Core/SettingsService.cs | 83 ------------- PlexRequests.Store/RequestedModel.cs | 1 + PlexRequests.Store/SqlTables.sql | 2 +- PlexRequests.UI/Bootstrapper.cs | 8 ++ PlexRequests.UI/Jobs/IAvailabilityChecker.cs | 34 ++++++ .../Jobs/PlexAvailabilityChecker.cs | 109 ++++++++++++++++++ PlexRequests.UI/Jobs/PlexRegistry.cs | 39 +++++++ PlexRequests.UI/Jobs/TaskFactory.cs | 17 +++ PlexRequests.UI/Modules/AdminModule.cs | 4 +- PlexRequests.UI/Modules/RequestsModule.cs | 41 +++---- PlexRequests.UI/Modules/SearchModule.cs | 24 ++-- PlexRequests.UI/PlexRequests.UI.csproj | 16 +-- PlexRequests.UI/Startup.cs | 10 +- PlexRequests.UI/app.config | 4 +- PlexRequests.UI/packages.config | 5 +- PlexRequests.UI/web.config | 2 +- 19 files changed, 329 insertions(+), 160 deletions(-) create mode 100644 PlexRequests.Core/IRequestService.cs delete mode 100644 PlexRequests.Core/SettingsService.cs create mode 100644 PlexRequests.UI/Jobs/IAvailabilityChecker.cs create mode 100644 PlexRequests.UI/Jobs/PlexAvailabilityChecker.cs create mode 100644 PlexRequests.UI/Jobs/PlexRegistry.cs create mode 100644 PlexRequests.UI/Jobs/TaskFactory.cs diff --git a/PlexRequests.Core/IRequestService.cs b/PlexRequests.Core/IRequestService.cs new file mode 100644 index 000000000..c2751ea21 --- /dev/null +++ b/PlexRequests.Core/IRequestService.cs @@ -0,0 +1,42 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: IRequestService.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.Collections.Generic; +using PlexRequests.Store; + +namespace PlexRequests.Core +{ + public interface IRequestService + { + long AddRequest(int providerId, RequestedModel model); + bool CheckRequest(int providerId); + void DeleteRequest(int tmdbId); + void UpdateRequest(int originalId, RequestedModel model); + RequestedModel Get(int id); + IEnumerable GetAll(); + } +} \ No newline at end of file diff --git a/PlexRequests.Core/PlexRequests.Core.csproj b/PlexRequests.Core/PlexRequests.Core.csproj index 0f4d11b1d..d121724b1 100644 --- a/PlexRequests.Core/PlexRequests.Core.csproj +++ b/PlexRequests.Core/PlexRequests.Core.csproj @@ -64,8 +64,8 @@ + - @@ -73,7 +73,7 @@ - + diff --git a/PlexRequests.Core/RequestService.cs b/PlexRequests.Core/RequestService.cs index e8e2ca049..3c158e5c1 100644 --- a/PlexRequests.Core/RequestService.cs +++ b/PlexRequests.Core/RequestService.cs @@ -24,35 +24,30 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // ************************************************************************/ #endregion -using System.Linq; +using System.Collections.Generic; +using System.Linq; using PlexRequests.Store; namespace PlexRequests.Core { - public class RequestService + public class RequestService : IRequestService { - public RequestService(ISqliteConfiguration db, IRepository repo) + public RequestService(IRepository db) { - Db = db; - Repo = repo; + Repo = db; } - private ISqliteConfiguration Db { get; set; } + private IRepository Repo { get; set; } - public void AddRequest(int tmdbid, RequestType type) - { - var model = new RequestedModel - { - ProviderId = tmdbid, - Type = type - }; - Repo.Insert(model); + public long AddRequest(int providerId, RequestedModel model) + { + return Repo.Insert(model); } - public bool CheckRequest(int tmdbid) + public bool CheckRequest(int providerId) { - return Repo.GetAll().Any(x => x.ProviderId == tmdbid); + return Repo.GetAll().Any(x => x.ProviderId == providerId); } public void DeleteRequest(int tmdbId) @@ -61,5 +56,20 @@ namespace PlexRequests.Core Repo.Delete(entity); } + public void UpdateRequest(int originalId, RequestedModel model) + { + model.Id = originalId; + Repo.Update(model); + } + + public RequestedModel Get(int id) + { + return Repo.Get(id); + } + + public IEnumerable GetAll() + { + return Repo.GetAll(); + } } -} \ No newline at end of file +} diff --git a/PlexRequests.Core/SettingsService.cs b/PlexRequests.Core/SettingsService.cs deleted file mode 100644 index f9ba1a2a1..000000000 --- a/PlexRequests.Core/SettingsService.cs +++ /dev/null @@ -1,83 +0,0 @@ -#region Copyright -// /************************************************************************ -// Copyright (c) 2016 Jamie Rees -// File: SettingsService.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 Mono.Data.Sqlite; - -using PlexRequests.Api; -using PlexRequests.Helpers; -using PlexRequests.Store; - -namespace PlexRequests.Core -{ - public class SettingsService - { - public SettingsService(ICacheProvider cache) - { - Cache = cache; - } - - public SettingsModel GetSettings() - { - - var db = new DbConfiguration(new SqliteFactory()); - var repo = new GenericRepository(db); - - var settings = repo.GetAll().FirstOrDefault(); - - return settings; - } - private ICacheProvider Cache { get; set; } - - public void AddRequest(int providerId, RequestedModel model) - { - var db = new DbConfiguration(new SqliteFactory()); - var repo = new GenericRepository(db); - - repo.Insert(model); - } - - public bool CheckRequest(int providerId) - { - var db = new DbConfiguration(new SqliteFactory()); - var repo = new GenericRepository(db); - - return repo.GetAll().Any(x => x.ProviderId == providerId); - } - - public void DeleteRequest(int tmdbId) - { - var db = new DbConfiguration(new SqliteFactory()); - var repo = new GenericRepository(db); - var entity = repo.GetAll().FirstOrDefault(x => x.ProviderId == tmdbId); - repo.Delete(entity); - } - - - } -} diff --git a/PlexRequests.Store/RequestedModel.cs b/PlexRequests.Store/RequestedModel.cs index 415baf7d5..7c8ca18dc 100644 --- a/PlexRequests.Store/RequestedModel.cs +++ b/PlexRequests.Store/RequestedModel.cs @@ -19,6 +19,7 @@ namespace PlexRequests.Store public bool Approved { get; set; } public string RequestedBy { get; set; } public DateTime RequestedDate { get; set; } + public bool Available { get; set; } } diff --git a/PlexRequests.Store/SqlTables.sql b/PlexRequests.Store/SqlTables.sql index 89992a700..4b2555a1e 100644 --- a/PlexRequests.Store/SqlTables.sql +++ b/PlexRequests.Store/SqlTables.sql @@ -30,7 +30,7 @@ CREATE TABLE IF NOT EXISTS Requested Approved INTEGER NOT NULL, RequestedBy varchar(50), RequestedDate varchar(50) NOT NULL, - Available varchar(50) + Available INTEGER(50) ); diff --git a/PlexRequests.UI/Bootstrapper.cs b/PlexRequests.UI/Bootstrapper.cs index b1b1971ec..f4111730a 100644 --- a/PlexRequests.UI/Bootstrapper.cs +++ b/PlexRequests.UI/Bootstrapper.cs @@ -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, SettingsServiceV2>(); container.Register, SettingsServiceV2>(); container.Register, GenericRepository>(); + container.Register(); + container.Register(); 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); diff --git a/PlexRequests.UI/Jobs/IAvailabilityChecker.cs b/PlexRequests.UI/Jobs/IAvailabilityChecker.cs new file mode 100644 index 000000000..ba75a5b43 --- /dev/null +++ b/PlexRequests.UI/Jobs/IAvailabilityChecker.cs @@ -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(); + } +} \ No newline at end of file diff --git a/PlexRequests.UI/Jobs/PlexAvailabilityChecker.cs b/PlexRequests.UI/Jobs/PlexAvailabilityChecker.cs new file mode 100644 index 000000000..a443a4d00 --- /dev/null +++ b/PlexRequests.UI/Jobs/PlexAvailabilityChecker.cs @@ -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, ISettingsService auth, IRequestService request) + { + Plex = plexSettings; + Auth = auth; + RequestService = request; + HostingEnvironment.RegisterObject(this); + } + private readonly object _lock = new object(); + + private bool _shuttingDown; + private ISettingsService Plex { get; } + private ISettingsService 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); + } + } +} \ No newline at end of file diff --git a/PlexRequests.UI/Jobs/PlexRegistry.cs b/PlexRequests.UI/Jobs/PlexRegistry.cs new file mode 100644 index 000000000..650f020b5 --- /dev/null +++ b/PlexRequests.UI/Jobs/PlexRegistry.cs @@ -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().ToRunNow().AndEvery(2).Minutes(); + } + } +} \ No newline at end of file diff --git a/PlexRequests.UI/Jobs/TaskFactory.cs b/PlexRequests.UI/Jobs/TaskFactory.cs new file mode 100644 index 000000000..d9e112098 --- /dev/null +++ b/PlexRequests.UI/Jobs/TaskFactory.cs @@ -0,0 +1,17 @@ +using FluentScheduler; +using Nancy.TinyIoc; + +namespace PlexRequests.UI.Jobs +{ + public class TaskFactory : ITaskFactory + { + public ITask GetTaskInstance() where T : ITask + { + var container = TinyIoCContainer.Current; + object outT; + container.TryResolve(typeof(T), out outT); + + return (T)outT; + } + } +} \ No newline at end of file diff --git a/PlexRequests.UI/Modules/AdminModule.cs b/PlexRequests.UI/Modules/AdminModule.cs index 3758d6f22..484106d52 100644 --- a/PlexRequests.UI/Modules/AdminModule.cs +++ b/PlexRequests.UI/Modules/AdminModule.cs @@ -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() diff --git a/PlexRequests.UI/Modules/RequestsModule.cs b/PlexRequests.UI/Modules/RequestsModule.cs index 2bd408a3d..90ff48a7e 100644 --- a/PlexRequests.UI/Modules/RequestsModule.cs +++ b/PlexRequests.UI/Modules/RequestsModule.cs @@ -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); } diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index 3d8c30d67..3b2b33957 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -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 cpSettings, ISettingsService prSettings) : base("search") + public SearchModule(ICacheProvider cache, ISettingsService cpSettings, + ISettingsService 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 CpService { get; } private ISettingsService 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) diff --git a/PlexRequests.UI/PlexRequests.UI.csproj b/PlexRequests.UI/PlexRequests.UI.csproj index 759db02f6..95dbc8111 100644 --- a/PlexRequests.UI/PlexRequests.UI.csproj +++ b/PlexRequests.UI/PlexRequests.UI.csproj @@ -61,16 +61,8 @@ ..\packages\Dapper.1.42\lib\net45\Dapper.dll True - - ..\packages\Hangfire.Core.1.5.3\lib\net45\Hangfire.Core.dll - True - - - ..\packages\Hangfire.SQLite.1.1.0.0\lib\net45\Hangfire.SQLite.dll - True - - - ..\packages\Hangfire.SqlServer.1.5.3\lib\net45\Hangfire.SqlServer.dll + + ..\packages\FluentScheduler.3.1.46\lib\net40\FluentScheduler.dll True @@ -169,6 +161,10 @@ PreserveNewest + + + + diff --git a/PlexRequests.UI/Startup.cs b/PlexRequests.UI/Startup.cs index deb73d07b..28a918fd7 100644 --- a/PlexRequests.UI/Startup.cs +++ b/PlexRequests.UI/Startup.cs @@ -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) diff --git a/PlexRequests.UI/app.config b/PlexRequests.UI/app.config index 3319d3f20..4a1adc68b 100644 --- a/PlexRequests.UI/app.config +++ b/PlexRequests.UI/app.config @@ -10,7 +10,9 @@ - + + + diff --git a/PlexRequests.UI/packages.config b/PlexRequests.UI/packages.config index 0106da0e1..cc8667954 100644 --- a/PlexRequests.UI/packages.config +++ b/PlexRequests.UI/packages.config @@ -1,10 +1,7 @@  - - - - + diff --git a/PlexRequests.UI/web.config b/PlexRequests.UI/web.config index 662ac4697..e5b751f70 100644 --- a/PlexRequests.UI/web.config +++ b/PlexRequests.UI/web.config @@ -27,7 +27,7 @@ - +