diff --git a/PlexRequests.Api/MusicBrainzApi.cs b/PlexRequests.Api/MusicBrainzApi.cs index b9c581986..c810df6c3 100644 --- a/PlexRequests.Api/MusicBrainzApi.cs +++ b/PlexRequests.Api/MusicBrainzApi.cs @@ -1,116 +1,116 @@ -#region Copyright -// /************************************************************************ -// Copyright (c) 2016 Jamie Rees -// File: MusicBrainzApi.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 Newtonsoft.Json; - -using NLog; - -using PlexRequests.Api.Interfaces; -using PlexRequests.Api.Models.Music; - -using RestSharp; - -namespace PlexRequests.Api -{ - public class MusicBrainzApi : IMusicBrainzApi - { - public MusicBrainzApi() - { - Api = new ApiRequest(); - } - private ApiRequest Api { get; } - private static readonly Logger Log = LogManager.GetCurrentClassLogger(); - private readonly Uri BaseUri = new Uri("http://musicbrainz.org/ws/2/"); - - public MusicBrainzSearchResults SearchAlbum(string searchTerm) - { - Log.Trace("Searching for album: {0}", searchTerm); - var request = new RestRequest - { - Resource = "release/?query={searchTerm}&fmt=json", - Method = Method.GET - }; - request.AddUrlSegment("searchTerm", searchTerm); - - try - { - return Api.ExecuteJson(request, BaseUri); - } - catch (JsonSerializationException jse) - { - Log.Warn(jse); - return new MusicBrainzSearchResults(); // If there is no matching result we do not get returned a JSON string, it just returns "false". - } - } - - public MusicBrainzReleaseInfo GetAlbum(string releaseId) - { - Log.Trace("Getting album: {0}", releaseId); - var request = new RestRequest - { - Resource = "release/{albumId}", - Method = Method.GET - }; - request.AddUrlSegment("albumId", releaseId); - request.AddQueryParameter("fmt", "json"); - request.AddQueryParameter("inc", "artists"); - - try - { - return Api.ExecuteJson(request, BaseUri); - } - catch (JsonSerializationException jse) - { - Log.Warn(jse); - return new MusicBrainzReleaseInfo(); // If there is no matching result we do not get returned a JSON string, it just returns "false". - } - } - - public MusicBrainzCoverArt GetCoverArt(string releaseId) - { - Log.Trace("Getting cover art for release: {0}", releaseId); - var request = new RestRequest - { - Resource = "release/{releaseId}", - Method = Method.GET - }; - request.AddUrlSegment("releaseId", releaseId); - - try - { - return Api.Execute(request, new Uri("http://coverartarchive.org/")); - } - catch (Exception e) - { - Log.Warn(e); - return new MusicBrainzCoverArt(); // If there is no matching result we do not get returned a JSON string, it just returns "false". - } - } - - } +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: MusicBrainzApi.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 Newtonsoft.Json; + +using NLog; + +using PlexRequests.Api.Interfaces; +using PlexRequests.Api.Models.Music; + +using RestSharp; + +namespace PlexRequests.Api +{ + public class MusicBrainzApi : IMusicBrainzApi + { + public MusicBrainzApi() + { + Api = new ApiRequest(); + } + private ApiRequest Api { get; } + private static readonly Logger Log = LogManager.GetCurrentClassLogger(); + private readonly Uri BaseUri = new Uri("http://musicbrainz.org/ws/2/"); + + public MusicBrainzSearchResults SearchAlbum(string searchTerm) + { + Log.Trace("Searching for album: {0}", searchTerm); + var request = new RestRequest + { + Resource = "release/?query={searchTerm}&fmt=json", + Method = Method.GET + }; + request.AddUrlSegment("searchTerm", searchTerm); + + try + { + return Api.ExecuteJson(request, BaseUri); + } + catch (JsonSerializationException jse) + { + Log.Warn(jse); + return new MusicBrainzSearchResults(); // If there is no matching result we do not get returned a JSON string, it just returns "false". + } + } + + public MusicBrainzReleaseInfo GetAlbum(string releaseId) + { + Log.Trace("Getting album: {0}", releaseId); + var request = new RestRequest + { + Resource = "release/{albumId}", + Method = Method.GET + }; + request.AddUrlSegment("albumId", releaseId); + request.AddQueryParameter("fmt", "json"); + request.AddQueryParameter("inc", "artists"); + + try + { + return Api.ExecuteJson(request, BaseUri); + } + catch (JsonSerializationException jse) + { + Log.Warn(jse); + return new MusicBrainzReleaseInfo(); // If there is no matching result we do not get returned a JSON string, it just returns "false". + } + } + + public MusicBrainzCoverArt GetCoverArt(string releaseId) + { + Log.Trace("Getting cover art for release: {0}", releaseId); + var request = new RestRequest + { + Resource = "release/{releaseId}", + Method = Method.GET + }; + request.AddUrlSegment("releaseId", releaseId); + + try + { + return Api.Execute(request, new Uri("http://coverartarchive.org/")); + } + catch (Exception e) + { + Log.Warn(e); + return new MusicBrainzCoverArt(); // If there is no matching result we do not get returned a JSON string, it just returns "false". + } + } + + } } \ No newline at end of file diff --git a/PlexRequests.Core/Setup.cs b/PlexRequests.Core/Setup.cs index 537730d8a..0e2f453c5 100644 --- a/PlexRequests.Core/Setup.cs +++ b/PlexRequests.Core/Setup.cs @@ -121,30 +121,45 @@ namespace PlexRequests.Core private async void CacheSonarrQualityProfiles(MemoryCacheProvider cacheProvider) { - var sonarrSettingsService = new SettingsServiceV2(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); - var sonarrSettings = sonarrSettingsService.GetSettings(); - if (sonarrSettings.Enabled) { - cacheProvider.GetOrSet(CacheKeys.SonarrQualityProfiles, () => + try + { + Log.Info("Executing GetSettings call to Sonarr for quality profiles"); + var sonarrSettingsService = new SettingsServiceV2(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); + var sonarrSettings = sonarrSettingsService.GetSettings(); + if (sonarrSettings.Enabled) { + Log.Info("Begin executing GetProfiles call to Sonarr for quality profiles"); SonarrApi sonarrApi = new SonarrApi(); - return sonarrApi.GetProfiles(sonarrSettings.ApiKey, sonarrSettings.FullUri); - - }); + var profiles = sonarrApi.GetProfiles(sonarrSettings.ApiKey, sonarrSettings.FullUri); + cacheProvider.Set(CacheKeys.SonarrQualityProfiles, profiles); + Log.Info("Finished executing GetProfiles call to Sonarr for quality profiles"); + } + } + catch (Exception ex) + { + Log.Error("Failed to cache Sonarr quality profiles!", ex); } } private async void CacheCouchPotatoQualityProfiles(MemoryCacheProvider cacheProvider) { - var cpSettingsService = new SettingsServiceV2(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); - var cpSettings = cpSettingsService.GetSettings(); - if (cpSettings.Enabled) + try { - cacheProvider.GetOrSet(CacheKeys.CouchPotatoQualityProfiles, () => + Log.Info("Executing GetSettings call to CouchPotato for quality profiles"); + var cpSettingsService = new SettingsServiceV2(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); + var cpSettings = cpSettingsService.GetSettings(); + if (cpSettings.Enabled) { + Log.Info("Begin executing GetProfiles call to CouchPotato for quality profiles"); CouchPotatoApi cpApi = new CouchPotatoApi(); - return cpApi.GetProfiles(cpSettings.FullUri, cpSettings.ApiKey); - - }); + var profiles = cpApi.GetProfiles(cpSettings.FullUri, cpSettings.ApiKey); + cacheProvider.Set(CacheKeys.CouchPotatoQualityProfiles, profiles); + Log.Info("Finished executing GetProfiles call to CouchPotato for quality profiles"); + } + } + catch (Exception ex) + { + Log.Error("Failed to cache CouchPotato quality profiles!", ex); } } diff --git a/PlexRequests.Helpers/ICacheProvider.cs b/PlexRequests.Helpers/ICacheProvider.cs index 3e3a920bb..fa957315d 100644 --- a/PlexRequests.Helpers/ICacheProvider.cs +++ b/PlexRequests.Helpers/ICacheProvider.cs @@ -55,7 +55,7 @@ namespace PlexRequests.Helpers /// The key. /// The object we want to store. /// The amount of time we want to cache the object. - void Set(string key, object data, int cacheTime); + void Set(string key, object data, int cacheTime = 20); /// /// Removes the specified object from the cache. diff --git a/PlexRequests.UI/Modules/AdminModule.cs b/PlexRequests.UI/Modules/AdminModule.cs index 17bd24b3c..d62a543b3 100644 --- a/PlexRequests.UI/Modules/AdminModule.cs +++ b/PlexRequests.UI/Modules/AdminModule.cs @@ -76,6 +76,7 @@ namespace PlexRequests.UI.Modules private ICouchPotatoApi CpApi { get; } private IRepository LogsRepo { get; } private INotificationService NotificationService { get; } + private ICacheProvider Cache { get; } private static Logger Log = LogManager.GetCurrentClassLogger(); public AdminModule(ISettingsService prService, @@ -94,7 +95,8 @@ namespace PlexRequests.UI.Modules IPushoverApi pushoverApi, IRepository logsRepo, INotificationService notify, - ISettingsService headphones) : base("admin") + ISettingsService headphones, + ICacheProvider cache) : base("admin") { PrService = prService; CpService = cpService; @@ -113,6 +115,7 @@ namespace PlexRequests.UI.Modules PushoverApi = pushoverApi; NotificationService = notify; HeadphonesService = headphones; + Cache = cache; #if !DEBUG this.RequiresAuthentication(); @@ -377,8 +380,7 @@ namespace PlexRequests.UI.Modules // set the cache if (profiles != null) { - var cache = new MemoryCacheProvider(); - cache.Set(CacheKeys.SonarrQualityProfiles, profiles); + Cache.Set(CacheKeys.SonarrQualityProfiles, profiles); } return Response.AsJson(profiles); @@ -592,8 +594,7 @@ namespace PlexRequests.UI.Modules // set the cache if (profiles != null) { - var cache = new MemoryCacheProvider(); - cache.Set(CacheKeys.CouchPotatoQualityProfiles, profiles); + Cache.Set(CacheKeys.CouchPotatoQualityProfiles, profiles); } return Response.AsJson(profiles); diff --git a/PlexRequests.UI/Modules/RequestsModule.cs b/PlexRequests.UI/Modules/RequestsModule.cs index 371b62f46..2cee0a914 100644 --- a/PlexRequests.UI/Modules/RequestsModule.cs +++ b/PlexRequests.UI/Modules/RequestsModule.cs @@ -59,7 +59,8 @@ namespace PlexRequests.UI.Modules ISettingsService cpSettings, ICouchPotatoApi cpApi, ISonarrApi sonarrApi, - ISickRageApi sickRageApi) : base("requests") + ISickRageApi sickRageApi, + ICacheProvider cache) : base("requests") { Service = service; PrSettings = prSettings; @@ -71,6 +72,7 @@ namespace PlexRequests.UI.Modules SonarrApi = sonarrApi; SickRageApi = sickRageApi; CpApi = cpApi; + Cache = cache; Get["/"] = _ => LoadRequests(); Get["/movies"] = _ => GetMovies(); @@ -96,6 +98,7 @@ namespace PlexRequests.UI.Modules private ISonarrApi SonarrApi { get; } private ISickRageApi SickRageApi { get; } private ICouchPotatoApi CpApi { get; } + private ICacheProvider Cache { get; } private Negotiator LoadRequests() { @@ -126,7 +129,6 @@ namespace PlexRequests.UI.Modules })); - var mc = new MemoryCacheProvider(); List qualities = new List(); if (isAdmin) @@ -136,7 +138,7 @@ namespace PlexRequests.UI.Modules { taskList.Add(Task.Factory.StartNew(() => { - return mc.GetOrSet(CacheKeys.CouchPotatoQualityProfiles, () => + return Cache.GetOrSet(CacheKeys.CouchPotatoQualityProfiles, () => { return CpApi.GetProfiles(cpSettings.FullUri, cpSettings.ApiKey); // TODO: cache this! }); @@ -202,7 +204,6 @@ namespace PlexRequests.UI.Modules } })); - var mc = new MemoryCacheProvider(); List qualities = new List(); if (isAdmin) { @@ -211,7 +212,7 @@ namespace PlexRequests.UI.Modules { taskList.Add(Task.Factory.StartNew(() => { - return mc.GetOrSet(CacheKeys.SonarrQualityProfiles, () => + return Cache.GetOrSet(CacheKeys.SonarrQualityProfiles, () => { return SonarrApi.GetProfiles(sonarrSettings.ApiKey, sonarrSettings.FullUri); // TODO: cache this! diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index 056b0f5b8..5f31ab9e9 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -268,7 +268,7 @@ namespace PlexRequests.UI.Modules }; Log.Trace(settings.DumpJson()); - if (IsAdmin || !settings.RequireMovieApproval || settings.ApprovalWhiteList.Any(x => x.Equals(Username, StringComparison.OrdinalIgnoreCase))) + if (ShouldAutoApprove(RequestType.Movie, settings)) { var cpSettings = CpService.GetSettings(); @@ -422,7 +422,7 @@ namespace PlexRequests.UI.Modules model.SeasonList = seasonsList.ToArray(); - if (IsAdmin || !settings.RequireTvShowApproval || settings.ApprovalWhiteList.Any(x => x.Equals(Username, StringComparison.OrdinalIgnoreCase))) + if (ShouldAutoApprove(RequestType.TvShow, settings)) { var sonarrSettings = SonarrService.GetSettings(); var sender = new TvSender(SonarrApi, SickrageApi); @@ -537,8 +537,7 @@ namespace PlexRequests.UI.Modules }; - if (IsAdmin || !settings.RequireMusicApproval || - settings.ApprovalWhiteList.Any(x => x.Equals(Username, StringComparison.OrdinalIgnoreCase))) + if (ShouldAutoApprove(RequestType.Album, settings)) { Log.Debug("We don't require approval OR the user is in the whitelist"); var hpSettings = HeadphonesService.GetSettings(); @@ -589,5 +588,24 @@ namespace PlexRequests.UI.Modules return img; } + + private bool ShouldAutoApprove(RequestType requestType, PlexRequestSettings prSettings) + { + // if the user is an admin or they are whitelisted, they go ahead and allow auto-approval + if (IsAdmin || prSettings.ApprovalWhiteList.Any(x => x.Equals(Username, StringComparison.OrdinalIgnoreCase))) return true; + + // check by request type if the category requires approval or not + switch (requestType) + { + case RequestType.Movie: + return !prSettings.RequireMovieApproval; + case RequestType.TvShow: + return !prSettings.RequireTvShowApproval; + case RequestType.Album: + return !prSettings.RequireMusicApproval; + default: + return false; + } + } } }