Improved the availabilty check to include music results #32

pull/140/head
tidusjar 9 years ago
parent f2ba56f131
commit cf83e0e6d9

@ -30,5 +30,6 @@ namespace PlexRequests.Services.Interfaces
{ {
void CheckAndUpdateAll(long check); void CheckAndUpdateAll(long check);
bool IsAvailable(string title, string year); bool IsAvailable(string title, string year);
bool IsAvailable(string title, string year, string artist, PlexType type);
} }
} }

@ -101,22 +101,29 @@ namespace PlexRequests.Services
Log.Trace("Search results from Plex for the following request: {0}", r.Title); Log.Trace("Search results from Plex for the following request: {0}", r.Title);
Log.Trace(results.DumpJson()); 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); if (directoryResultVal)
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)
{ {
r.Available = true; r.Available = true;
modifiedModel.Add(r); modifiedModel.Add(r);
continue; continue;
} }
Log.Trace("The result from Plex where the title's match was null, so that means the content is not yet in Plex."); 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("Requests that will be updates:");
Log.Trace(modifiedModel.SelectMany(x => x.Title).DumpJson()); Log.Trace(modifiedModel.SelectMany(x => x.Title).DumpJson());
if(modifiedModel.Any()) if (modifiedModel.Any())
{ RequestService.BatchUpdate(modifiedModel);} {
RequestService.BatchUpdate(modifiedModel);
}
} }
/// <summary> /// <summary>
@ -159,7 +168,78 @@ namespace PlexRequests.Services
var directoryTitle = string.Equals(results.Directory?.Title, title, StringComparison.CurrentCultureIgnoreCase); var directoryTitle = string.Equals(results.Directory?.Title, title, StringComparison.CurrentCultureIgnoreCase);
return result?.Title != null || directoryTitle; return result?.Title != null || directoryTitle;
} }
}
/// <summary>
/// Determines whether the specified title is available.
/// </summary>
/// <param name="title">The title.</param>
/// <param name="year">The year.</param>
/// <param name="artist">The artist.</param>
/// <param name="type">The type.</param>
/// <returns></returns>
/// <exception cref="ApplicationSettingsException">The settings are not configured for Plex or Authentication</exception>
/// <exception cref="System.ArgumentOutOfRangeException">null</exception>
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);
}
}
/// <summary>
/// Searches the movies and TV shows on Plex.
/// </summary>
/// <param name="results">The results.</param>
/// <param name="title">The title.</param>
/// <param name="year">The year.</param>
/// <returns></returns>
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;
}
}
/// <summary>
/// Searches the music on Plex.
/// </summary>
/// <param name="results">The results.</param>
/// <param name="title">The title.</param>
/// <param name="artist">The artist.</param>
/// <returns></returns>
private bool MusicSearch(PlexSearch results, string title, string artist)
{
//TODO
return false;
} }
private bool ValidateSettings(PlexSettings plex, AuthenticationSettings auth) private bool ValidateSettings(PlexSettings plex, AuthenticationSettings auth)

@ -86,6 +86,7 @@
<Compile Include="Notification\PushoverNotification.cs" /> <Compile Include="Notification\PushoverNotification.cs" />
<Compile Include="Notification\PushbulletNotification.cs" /> <Compile Include="Notification\PushbulletNotification.cs" />
<Compile Include="PlexAvailabilityChecker.cs" /> <Compile Include="PlexAvailabilityChecker.cs" />
<Compile Include="PlexType.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UpdateInterval.cs" /> <Compile Include="UpdateInterval.cs" />
</ItemGroup> </ItemGroup>

@ -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
}
}

@ -42,6 +42,7 @@ using PlexRequests.Core;
using PlexRequests.Core.SettingModels; using PlexRequests.Core.SettingModels;
using PlexRequests.Helpers; using PlexRequests.Helpers;
using PlexRequests.Helpers.Exceptions; using PlexRequests.Helpers.Exceptions;
using PlexRequests.Services;
using PlexRequests.Services.Interfaces; using PlexRequests.Services.Interfaces;
using PlexRequests.Services.Notification; using PlexRequests.Services.Notification;
using PlexRequests.Store; using PlexRequests.Store;
@ -240,7 +241,7 @@ namespace PlexRequests.UI.Modules
try 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!" }); return Response.AsJson(new JsonResponseModel { Result = false, Message = $"{fullMovieName} is already in Plex!" });
} }
@ -376,7 +377,7 @@ namespace PlexRequests.UI.Modules
try 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!" }); 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!" }); 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; return result;
} }

Loading…
Cancel
Save