@ -42,6 +42,7 @@ using PlexRequests.Helpers;
using PlexRequests.Services.Interfaces ;
using PlexRequests.Services.Interfaces ;
using PlexRequests.Services.Notification ;
using PlexRequests.Services.Notification ;
using PlexRequests.Store ;
using PlexRequests.Store ;
using PlexRequests.UI.Helpers ;
using PlexRequests.UI.Models ;
using PlexRequests.UI.Models ;
namespace PlexRequests.UI.Modules
namespace PlexRequests.UI.Modules
@ -51,7 +52,7 @@ 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 ,
IRequestService request , ISonarrApi sonarrApi , ISettingsService < SonarrSettings > sonarrSettings ,
I CouchPotatoApi cp Api) : base ( "search" )
I SettingsService< SickRageSettings > sickRageService , I CouchPotatoApi cp Api, ISickRageApi sr Api) : base ( "search" )
{
{
CpService = cpSettings ;
CpService = cpSettings ;
PrService = prSettings ;
PrService = prSettings ;
@ -63,6 +64,8 @@ namespace PlexRequests.UI.Modules
SonarrApi = sonarrApi ;
SonarrApi = sonarrApi ;
SonarrService = sonarrSettings ;
SonarrService = sonarrSettings ;
CouchPotatoApi = cpApi ;
CouchPotatoApi = cpApi ;
SickRageService = sickRageService ;
SickrageApi = srApi ;
Get [ "/" ] = parameters = > RequestLoad ( ) ;
Get [ "/" ] = parameters = > RequestLoad ( ) ;
@ -79,11 +82,13 @@ namespace PlexRequests.UI.Modules
private ICouchPotatoApi CouchPotatoApi { get ; }
private ICouchPotatoApi CouchPotatoApi { get ; }
private ISonarrApi SonarrApi { get ; }
private ISonarrApi SonarrApi { get ; }
private TheTvDbApi TvApi { get ; }
private TheTvDbApi TvApi { get ; }
private ISickRageApi SickrageApi { get ; }
private IRequestService RequestService { get ; }
private IRequestService RequestService { get ; }
private ICacheProvider Cache { get ; }
private ICacheProvider Cache { get ; }
private ISettingsService < CouchPotatoSettings > CpService { get ; }
private ISettingsService < CouchPotatoSettings > CpService { get ; }
private ISettingsService < PlexRequestSettings > PrService { get ; }
private ISettingsService < PlexRequestSettings > PrService { get ; }
private ISettingsService < SonarrSettings > SonarrService { get ; }
private ISettingsService < SonarrSettings > SonarrService { get ; }
private ISettingsService < SickRageSettings > SickRageService { get ; }
private IAvailabilityChecker Checker { get ; }
private IAvailabilityChecker Checker { get ; }
private static Logger Log = LogManager . GetCurrentClassLogger ( ) ;
private static Logger Log = LogManager . GetCurrentClassLogger ( ) ;
private string AuthToken = > Cache . GetOrSet ( CacheKeys . TvDbToken , TvApi . Authenticate , 50 ) ;
private string AuthToken = > Cache . GetOrSet ( CacheKeys . TvDbToken , TvApi . Authenticate , 50 ) ;
@ -124,7 +129,7 @@ namespace PlexRequests.UI.Modules
// http://thetvdb.com/banners/_cache/posters/ID-1.jpg
// http://thetvdb.com/banners/_cache/posters/ID-1.jpg
Banner = t . show . image ? . medium ,
Banner = t . show . image ? . medium ,
FirstAired = t . show . premiered ,
FirstAired = t . show . premiered ,
Id = t . show . id ,
Id = t . show . externals? . thetvdb ? ? 0 ,
ImdbId = t . show . externals ? . imdb ,
ImdbId = t . show . externals ? . imdb ,
Network = t . show . network ? . name ,
Network = t . show . network ? . name ,
NetworkId = t . show . network ? . id . ToString ( ) ,
NetworkId = t . show . network ? . id . ToString ( ) ,
@ -133,7 +138,7 @@ namespace PlexRequests.UI.Modules
Runtime = t . show . runtime . ToString ( ) ,
Runtime = t . show . runtime . ToString ( ) ,
SeriesId = t . show . id ,
SeriesId = t . show . id ,
SeriesName = t . show . name ,
SeriesName = t . show . name ,
Status = t . show . status ,
Status = t . show . status ,
} ) ;
} ) ;
}
}
@ -186,12 +191,12 @@ 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 !DEBUG
//#if !DEBUG
if ( CheckIfTitleExistsInPlex ( movieInfo . Title , movieInfo . ReleaseDate ? . Year . ToString ( ) ) )
if ( CheckIfTitleExistsInPlex ( movieInfo . Title , movieInfo . ReleaseDate ? . Year . ToString ( ) ) )
{
{
return Response . AsJson ( new JsonResponseModel { Result = false , Message = $"{movieInfo.Title} is already in Plex!" } ) ;
return Response . AsJson ( new JsonResponseModel { Result = false , Message = $"{movieInfo.Title} is already in Plex!" } ) ;
}
}
//#endif
//#endif
var model = new RequestedModel
var model = new RequestedModel
{
{
@ -215,7 +220,7 @@ namespace PlexRequests.UI.Modules
if ( ! settings . RequireApproval )
if ( ! settings . RequireApproval )
{
{
Log . Info ( "Adding movie to CP (No approval required)" ) ;
Log . Info ( "Adding movie to CP (No approval required)" ) ;
var result = CouchPotatoApi . AddMovie ( model . ImdbId , cpSettings . ApiKey , model . Title , cpSettings . FullUri , cpSettings . ProfileId ) ;
var result = CouchPotatoApi . AddMovie ( model . ImdbId , cpSettings . ApiKey , model . Title , cpSettings . FullUri , cpSettings . ProfileId ) ;
Log . Debug ( "Adding movie to CP result {0}" , result ) ;
Log . Debug ( "Adding movie to CP result {0}" , result ) ;
if ( result )
if ( result )
{
{
@ -260,21 +265,21 @@ namespace PlexRequests.UI.Modules
var tvApi = new TvMazeApi ( ) ;
var tvApi = new TvMazeApi ( ) ;
var showInfo = tvApi . ShowLookup ( showId ) ;
var showInfo = tvApi . ShowLookup ByTheTvDbId ( showId ) ;
//#if !DEBUG
//#if !DEBUG
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 ) ) ) // Take only the year Format = 2014-01-01
{
{
return Response . AsJson ( new JsonResponseModel { Result = false , Message = $"{showInfo.name} is already in Plex!" } ) ;
return Response . AsJson ( new JsonResponseModel { Result = false , Message = $"{showInfo.name} is already in Plex!" } ) ;
}
}
//#endif
//#endif
DateTime firstAir ;
DateTime firstAir ;
DateTime . TryParse ( showInfo . premiered , out firstAir ) ;
DateTime . TryParse ( showInfo . premiered , out firstAir ) ;
var model = new RequestedModel
var model = new RequestedModel
{
{
ProviderId = showInfo . id ,
ProviderId = showInfo . externals? . thetvdb ? ? 0 ,
Type = RequestType . TvShow ,
Type = RequestType . TvShow ,
Overview = showInfo . summary . RemoveHtml ( ) ,
Overview = showInfo . summary . RemoveHtml ( ) ,
PosterPath = showInfo . image ? . medium ,
PosterPath = showInfo . image ? . medium ,
@ -293,20 +298,39 @@ namespace PlexRequests.UI.Modules
if ( ! settings . RequireApproval )
if ( ! settings . RequireApproval )
{
{
var sonarrSettings = SonarrService . GetSettings ( ) ;
var sonarrSettings = SonarrService . GetSettings ( ) ;
int qualityProfile ;
var sender = new TvSender ( SonarrApi , SickrageApi ) ;
int . TryParse ( sonarrSettings . QualityProfile , out qualityProfile ) ;
if ( sonarrSettings . Enabled )
var result = SonarrApi . AddSeries ( model . ProviderId , model . Title , qualityProfile ,
sonarrSettings . SeasonFolders , sonarrSettings . RootPath , model . LatestTv , sonarrSettings . ApiKey ,
sonarrSettings . FullUri ) ;
if ( result ! = null )
{
{
model . Approved = true ;
var result = sender . SendToSonarr ( sonarrSettings , model ) ;
Log . Debug ( "Adding tv to database requests (No approval required)" ) ;
if ( result ! = null )
RequestService . AddRequest ( model ) ;
{
model . Approved = true ;
Log . Debug ( "Adding tv to database requests (No approval required & Sonarr)" ) ;
RequestService . AddRequest ( model ) ;
return Response . AsJson ( new JsonResponseModel { Result = true } ) ;
}
return Response . AsJson ( new JsonResponseModel { Result = false , Message = "Something went wrong adding the movie to Sonarr! Please check your settings." } ) ;
return Response . AsJson ( new JsonResponseModel { Result = true } ) ;
}
}
return Response . AsJson ( new JsonResponseModel { Result = false , Message = "Something went wrong adding the movie to CouchPotato! Please check your settings." } ) ;
var srSettings = SickRageService . GetSettings ( ) ;
if ( srSettings . Enabled )
{
var result = sender . SendToSickRage ( srSettings , model ) ;
if ( result ? . result = = "success" )
{
model . Approved = true ;
Log . Debug ( "Adding tv to database requests (No approval required & SickRage)" ) ;
RequestService . AddRequest ( model ) ;
return Response . AsJson ( new JsonResponseModel { Result = true } ) ;
}
return Response . AsJson ( new JsonResponseModel { Result = false , Message = result ? . message ! = null ? "<b>Message From SickRage: </b>" + result . message : "Something went wrong adding the movie to SickRage! Please check your settings." } ) ;
}
return Response . AsJson ( "The request of TV Shows is not correctly set up. Please contact your admin." ) ;
}
}
RequestService . AddRequest ( model ) ;
RequestService . AddRequest ( model ) ;
@ -314,15 +338,30 @@ namespace PlexRequests.UI.Modules
return Response . AsJson ( new { Result = true } ) ;
return Response . AsJson ( new { Result = true } ) ;
}
}
private string GetTvDbAuthToken ( TheTvDbApi api )
{
return Cache . GetOrSet ( CacheKeys . TvDbToken , api . Authenticate , 50 ) ;
}
private bool CheckIfTitleExistsInPlex ( string title , string year )
private bool CheckIfTitleExistsInPlex ( string title , string year )
{
{
var result = Checker . IsAvailable ( title , year ) ;
var result = Checker . IsAvailable ( title , year ) ;
return result ;
return result ;
}
}
private Response SendToSickRage ( SickRageSettings sickRageSettings , RequestedModel model )
{
var result = SickrageApi . AddSeries ( model . ProviderId , model . LatestTv , sickRageSettings . QualityProfile ,
sickRageSettings . ApiKey , sickRageSettings . FullUri ) ;
Log . Trace ( "SickRage Result: " ) ;
Log . Trace ( result . DumpJson ( ) ) ;
if ( result ? . result = = "success" )
{
model . Approved = true ;
Log . Debug ( "Adding tv to database requests (No approval required & SickRage)" ) ;
RequestService . AddRequest ( model ) ;
return Response . AsJson ( new JsonResponseModel { Result = true } ) ;
}
return Response . AsJson ( new JsonResponseModel { Result = false , Message = "Something went wrong adding the movie to SickRage! Please check your settings." } ) ;
}
}
}
}
}