@ -38,7 +38,6 @@ using PlexRequests.Core.SettingModels;
using PlexRequests.Helpers ;
using PlexRequests.Helpers ;
using PlexRequests.Services.Interfaces ;
using PlexRequests.Services.Interfaces ;
using PlexRequests.Store ;
using PlexRequests.Store ;
using PlexRequests.UI.Jobs ;
using PlexRequests.UI.Models ;
using PlexRequests.UI.Models ;
namespace PlexRequests.UI.Modules
namespace PlexRequests.UI.Modules
@ -47,7 +46,8 @@ namespace PlexRequests.UI.Modules
{
{
public SearchModule ( ICacheProvider cache , ISettingsService < CouchPotatoSettings > cpSettings ,
public SearchModule ( ICacheProvider cache , ISettingsService < CouchPotatoSettings > cpSettings ,
ISettingsService < PlexRequestSettings > prSettings , IAvailabilityChecker checker ,
ISettingsService < PlexRequestSettings > prSettings , IAvailabilityChecker checker ,
IRequestService request , ISonarrApi sonarrApi , ISettingsService < SonarrSettings > sonarrSettings ) : base ( "search" )
IRequestService request , ISonarrApi sonarrApi , ISettingsService < SonarrSettings > sonarrSettings ,
ICouchPotatoApi cpApi ) : base ( "search" )
{
{
CpService = cpSettings ;
CpService = cpSettings ;
PrService = prSettings ;
PrService = prSettings ;
@ -58,6 +58,7 @@ namespace PlexRequests.UI.Modules
RequestService = request ;
RequestService = request ;
SonarrApi = sonarrApi ;
SonarrApi = sonarrApi ;
SonarrService = sonarrSettings ;
SonarrService = sonarrSettings ;
CouchPotatoApi = cpApi ;
Get [ "/" ] = parameters = > RequestLoad ( ) ;
Get [ "/" ] = parameters = > RequestLoad ( ) ;
@ -71,6 +72,7 @@ namespace PlexRequests.UI.Modules
Post [ "request/tv" ] = parameters = > RequestTvShow ( ( int ) Request . Form . tvId , ( bool ) Request . Form . latest ) ;
Post [ "request/tv" ] = parameters = > RequestTvShow ( ( int ) Request . Form . tvId , ( bool ) Request . Form . latest ) ;
}
}
private TheMovieDbApi MovieApi { get ; }
private TheMovieDbApi MovieApi { get ; }
private ICouchPotatoApi CouchPotatoApi { get ; }
private ISonarrApi SonarrApi { get ; }
private ISonarrApi SonarrApi { get ; }
private TheTvDbApi TvApi { get ; }
private TheTvDbApi TvApi { get ; }
private IRequestService RequestService { get ; }
private IRequestService RequestService { get ; }
@ -168,15 +170,17 @@ namespace PlexRequests.UI.Modules
if ( RequestService . CheckRequest ( movieId ) )
if ( RequestService . CheckRequest ( movieId ) )
{
{
Log . Trace ( "movie with id {0} exists" , movieId ) ;
Log . Trace ( "movie with id {0} exists" , movieId ) ;
return Response . AsJson ( new { Result = false , Message = "Movie has already been requested!" } ) ;
return Response . AsJson ( new JsonResponseModel { Result = false , Message = "Movie has already been requested!" } ) ;
}
}
Log . Debug ( "movie with id {0} doesnt exists" , movieId ) ;
Log . Debug ( "movie with id {0} doesnt exists" , movieId ) ;
var cpSettings = CpService . GetSettings ( ) ;
var cpSettings = CpService . GetSettings ( ) ;
if ( cpSettings . ApiKey = = null )
if ( cpSettings . ApiKey = = null )
{
{
Log . Warn ( "CP apiKey is null" ) ;
Log . Warn ( "CP apiKey is null" ) ;
return Response . AsJson ( new { Result = false , Message = "CouchPotato is not yet configured, If you are the Admin, please log in." } ) ;
return Response . AsJson ( new JsonResponseModel { Result = false , Message = "CouchPotato is not yet configured, If you are the Admin, please log in." } ) ;
}
}
Log . Trace ( "Settings: " ) ;
Log . Trace ( "Settings: " ) ;
Log . Trace ( cpSettings . DumpJson ) ;
Log . Trace ( cpSettings . DumpJson ) ;
@ -185,6 +189,11 @@ namespace PlexRequests.UI.Modules
Log . Trace ( "Getting movie info from TheMovieDb" ) ;
Log . Trace ( "Getting movie info from TheMovieDb" ) ;
Log . Trace ( movieInfo . DumpJson ) ;
Log . Trace ( movieInfo . DumpJson ) ;
if ( CheckIfTitleExistsInPlex ( movieInfo . Title ) )
{
return Response . AsJson ( new JsonResponseModel { Result = false , Message = $"{movieInfo.Title} is already in Plex!" } ) ;
}
var model = new RequestedModel
var model = new RequestedModel
{
{
ProviderId = movieInfo . Id ,
ProviderId = movieInfo . Id ,
@ -206,9 +215,8 @@ namespace PlexRequests.UI.Modules
Log . Trace ( settings . DumpJson ( ) ) ;
Log . Trace ( settings . DumpJson ( ) ) ;
if ( ! settings . RequireApproval )
if ( ! settings . RequireApproval )
{
{
var cp = new CouchPotatoApi ( ) ;
Log . Info ( "Adding movie to CP (No approval required)" ) ;
Log . Info ( "Adding movie to CP (No approval required)" ) ;
var result = cp. AddMovie ( model . ImdbId , cpSettings . ApiKey , model . Title , cpSettings . FullUri ) ;
var result = Cou chPotatoA pi . AddMovie ( model . ImdbId , cpSettings . ApiKey , model . Title , cpSettings . FullUri ) ;
Log . Debug ( "Adding movie to CP result {0}" , result ) ;
Log . Debug ( "Adding movie to CP result {0}" , result ) ;
if ( result )
if ( result )
{
{
@ -216,9 +224,9 @@ namespace PlexRequests.UI.Modules
Log . Debug ( "Adding movie to database requests (No approval required)" ) ;
Log . Debug ( "Adding movie to database requests (No approval required)" ) ;
RequestService . AddRequest ( movieId , model ) ;
RequestService . AddRequest ( movieId , model ) ;
return Response . AsJson ( new { Result = true } ) ;
return Response . AsJson ( new JsonResponseModel { Result = true } ) ;
}
}
return Response . AsJson ( new { Result = false , Message = "Something went wrong adding the movie to CouchPotato! Please check your settings." } ) ;
return Response . AsJson ( new JsonResponseModel { Result = false , Message = "Something went wrong adding the movie to CouchPotato! Please check your settings." } ) ;
}
}
try
try
@ -227,13 +235,13 @@ namespace PlexRequests.UI.Modules
var id = RequestService . AddRequest ( movieId , model ) ;
var id = RequestService . AddRequest ( movieId , model ) ;
//BackgroundJob.Enqueue(() => Checker.CheckAndUpdate(model.Title, (int)id));
//BackgroundJob.Enqueue(() => Checker.CheckAndUpdate(model.Title, (int)id));
return Response . AsJson ( new { Result = true } ) ;
return Response . AsJson ( new JsonResponseModel { Result = true } ) ;
}
}
catch ( Exception e )
catch ( Exception e )
{
{
Log . Fatal ( e ) ;
Log . Fatal ( e ) ;
return Response . AsJson ( new { Result = false , Message = "Something went wrong adding the movie to CouchPotato! Please check your settings." } ) ;
return Response . AsJson ( new JsonResponseModel { Result = false , Message = "Something went wrong adding the movie to CouchPotato! Please check your settings." } ) ;
}
}
}
}
@ -247,14 +255,19 @@ namespace PlexRequests.UI.Modules
{
{
if ( RequestService . CheckRequest ( showId ) )
if ( RequestService . CheckRequest ( showId ) )
{
{
return Response . AsJson ( new { Result = false , Message = "TV Show has already been requested!" } ) ;
return Response . AsJson ( new JsonResponseModel { Result = false , Message = "TV Show has already been requested!" } ) ;
}
}
var tvApi = new TheTvDbApi ( ) ;
var tvApi = new TheTvDbApi ( ) ;
var token = Get AuthToken( tvApi ) ;
var token = Get TvDb AuthToken( tvApi ) ;
var showInfo = tvApi . GetInformation ( showId , token ) . data ;
var showInfo = tvApi . GetInformation ( showId , token ) . data ;
if ( CheckIfTitleExistsInPlex ( showInfo . seriesName ) )
{
return Response . AsJson ( new JsonResponseModel { Result = false , Message = $"{showInfo.seriesName} is already in Plex!" } ) ;
}
DateTime firstAir ;
DateTime firstAir ;
DateTime . TryParse ( showInfo . firstAired , out firstAir ) ;
DateTime . TryParse ( showInfo . firstAired , out firstAir ) ;
@ -271,7 +284,7 @@ namespace PlexRequests.UI.Modules
Approved = false ,
Approved = false ,
RequestedBy = Session [ SessionKeys . UsernameKey ] . ToString ( ) ,
RequestedBy = Session [ SessionKeys . UsernameKey ] . ToString ( ) ,
Issues = IssueState . None ,
Issues = IssueState . None ,
LatestTv = latest
LatestTv = latest
} ;
} ;
RequestService . AddRequest ( showId , model ) ;
RequestService . AddRequest ( showId , model ) ;
@ -292,9 +305,15 @@ namespace PlexRequests.UI.Modules
return Response . AsJson ( new { Result = true } ) ;
return Response . AsJson ( new { Result = true } ) ;
}
}
private string Get AuthToken( TheTvDbApi api )
private string Get TvDb AuthToken( TheTvDbApi api )
{
{
return Cache . GetOrSet ( CacheKeys . TvDbToken , api . Authenticate , 50 ) ;
return Cache . GetOrSet ( CacheKeys . TvDbToken , api . Authenticate , 50 ) ;
}
}
private bool CheckIfTitleExistsInPlex ( string title )
{
var result = Checker . IsAvailable ( title ) ;
return result ;
}
}
}
}
}