diff --git a/PlexRequests.Services/Interfaces/IAvailabilityChecker.cs b/PlexRequests.Services/Interfaces/IAvailabilityChecker.cs index 02d8e8d57..abf2d412b 100644 --- a/PlexRequests.Services/Interfaces/IAvailabilityChecker.cs +++ b/PlexRequests.Services/Interfaces/IAvailabilityChecker.cs @@ -30,5 +30,6 @@ namespace PlexRequests.Services.Interfaces { void CheckAndUpdateAll(long check); bool IsAvailable(string title, string year); + bool IsAvailable(string title, string year, string artist, PlexType type); } } \ No newline at end of file diff --git a/PlexRequests.Services/PlexAvailabilityChecker.cs b/PlexRequests.Services/PlexAvailabilityChecker.cs index a71b75251..f32a1c75a 100644 --- a/PlexRequests.Services/PlexAvailabilityChecker.cs +++ b/PlexRequests.Services/PlexAvailabilityChecker.cs @@ -101,22 +101,29 @@ namespace PlexRequests.Services Log.Trace("Search results from Plex for the following request: {0}", r.Title); Log.Trace(results.DumpJson()); + var directoryResultVal = false; + switch (r.Type) + { + case RequestType.Movie: + directoryResultVal = MovieTvSearch(results, r.Title, r.ReleaseDate.ToString("yyyy")); + break; + case RequestType.TvShow: + directoryResultVal = MovieTvSearch(results, r.Title, r.ReleaseDate.ToString("yyyy")); + break; + case RequestType.Album: + directoryResultVal = MusicSearch(results, r.Title, r.ArtistName); + break; + default: + throw new ArgumentOutOfRangeException(); + } - var videoResult = results.Video.FirstOrDefault(x => x.Title == r.Title); - var directoryResult = results.Directory?.Title.Equals(r.Title, StringComparison.CurrentCultureIgnoreCase); - - Log.Trace("The result from Plex where the title matches for the video : {0}", videoResult != null); - Log.Trace("The result from Plex where the title matches for the directory : {0}", directoryResult != null); - - var directoryResultVal = directoryResult ?? false; - - if (videoResult != null || directoryResultVal) + if (directoryResultVal) { r.Available = true; modifiedModel.Add(r); continue; } - + Log.Trace("The result from Plex where the title's match was null, so that means the content is not yet in Plex."); } @@ -124,8 +131,10 @@ namespace PlexRequests.Services Log.Trace("Requests that will be updates:"); Log.Trace(modifiedModel.SelectMany(x => x.Title).DumpJson()); - if(modifiedModel.Any()) - { RequestService.BatchUpdate(modifiedModel);} + if (modifiedModel.Any()) + { + RequestService.BatchUpdate(modifiedModel); + } } /// @@ -159,7 +168,78 @@ namespace PlexRequests.Services var directoryTitle = string.Equals(results.Directory?.Title, title, StringComparison.CurrentCultureIgnoreCase); return result?.Title != null || directoryTitle; } + } + /// + /// Determines whether the specified title is available. + /// + /// The title. + /// The year. + /// The artist. + /// The type. + /// + /// The settings are not configured for Plex or Authentication + /// null + public bool IsAvailable(string title, string year, string artist, PlexType type) + { + Log.Trace("Checking if the following {0} {1} is available in Plex", title, year); + var plexSettings = Plex.GetSettings(); + var authSettings = Auth.GetSettings(); + + if (!ValidateSettings(plexSettings, authSettings)) + { + Log.Warn("The settings are not configured"); + throw new ApplicationSettingsException("The settings are not configured for Plex or Authentication"); + } + var results = PlexApi.SearchContent(authSettings.PlexAuthToken, title, plexSettings.FullUri); + + switch (type) + { + case PlexType.Movie: + return MovieTvSearch(results, title, year); + case PlexType.TvShow: + return MovieTvSearch(results, title, year); + case PlexType.Music: + return MusicSearch(results, title, artist); + default: + throw new ArgumentOutOfRangeException(nameof(type), type, null); + } + } + + /// + /// Searches the movies and TV shows on Plex. + /// + /// The results. + /// The title. + /// The year. + /// + private bool MovieTvSearch(PlexSearch results, string title, string year) + { + if (!string.IsNullOrEmpty(year)) + { + var result = results.Video?.FirstOrDefault(x => x.Title.Equals(title, StringComparison.InvariantCultureIgnoreCase) && x.Year == year); + var directoryTitle = string.Equals(results.Directory?.Title, title, StringComparison.CurrentCultureIgnoreCase) && results.Directory?.Year == year; + return result?.Title != null || directoryTitle; + } + else + { + var result = results.Video?.FirstOrDefault(x => x.Title.Equals(title, StringComparison.InvariantCultureIgnoreCase)); + var directoryTitle = string.Equals(results.Directory?.Title, title, StringComparison.CurrentCultureIgnoreCase); + return result?.Title != null || directoryTitle; + } + } + + /// + /// Searches the music on Plex. + /// + /// The results. + /// The title. + /// The artist. + /// + private bool MusicSearch(PlexSearch results, string title, string artist) + { + //TODO + return false; } private bool ValidateSettings(PlexSettings plex, AuthenticationSettings auth) diff --git a/PlexRequests.Services/PlexRequests.Services.csproj b/PlexRequests.Services/PlexRequests.Services.csproj index bbb334ca9..dba3cfce0 100644 --- a/PlexRequests.Services/PlexRequests.Services.csproj +++ b/PlexRequests.Services/PlexRequests.Services.csproj @@ -86,6 +86,7 @@ + diff --git a/PlexRequests.Services/PlexType.cs b/PlexRequests.Services/PlexType.cs new file mode 100644 index 000000000..27cd0d1da --- /dev/null +++ b/PlexRequests.Services/PlexType.cs @@ -0,0 +1,35 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: PlexType.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.Services +{ + public enum PlexType + { + Movie, + TvShow, + Music + } +} \ No newline at end of file diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index 5f31ab9e9..b0e3d1e25 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -42,6 +42,7 @@ using PlexRequests.Core; using PlexRequests.Core.SettingModels; using PlexRequests.Helpers; using PlexRequests.Helpers.Exceptions; +using PlexRequests.Services; using PlexRequests.Services.Interfaces; using PlexRequests.Services.Notification; using PlexRequests.Store; @@ -240,7 +241,7 @@ namespace PlexRequests.UI.Modules try { - if (CheckIfTitleExistsInPlex(movieInfo.Title, movieInfo.ReleaseDate?.Year.ToString())) + if (CheckIfTitleExistsInPlex(movieInfo.Title, movieInfo.ReleaseDate?.Year.ToString(),null, PlexType.Movie)) { return Response.AsJson(new JsonResponseModel { Result = false, Message = $"{fullMovieName} is already in Plex!" }); } @@ -376,7 +377,7 @@ namespace PlexRequests.UI.Modules try { - if (CheckIfTitleExistsInPlex(showInfo.name, showInfo.premiered?.Substring(0, 4))) // Take only the year Format = 2014-01-01 + if (CheckIfTitleExistsInPlex(showInfo.name, showInfo.premiered?.Substring(0, 4), null, PlexType.TvShow)) // Take only the year Format = 2014-01-01 { return Response.AsJson(new JsonResponseModel { Result = false, Message = $"{fullShowName} is already in Plex!" }); } @@ -475,9 +476,9 @@ namespace PlexRequests.UI.Modules return Response.AsJson(new JsonResponseModel { Result = true, Message = $"{fullShowName} was successfully added!" }); } - private bool CheckIfTitleExistsInPlex(string title, string year) + private bool CheckIfTitleExistsInPlex(string title, string year, string artist, PlexType type) { - var result = Checker.IsAvailable(title, year); + var result = Checker.IsAvailable(title, year, artist, type); return result; }