@ -1,8 +1,7 @@
#region Copyright
#region Copyright
// /************************************************************************
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// Copyright (c) 2016 Jamie Rees
// File: CouchPotato Api.cs
// File: Sickrage Api.cs
// Created By: Jamie Rees
// Created By: Jamie Rees
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// Permission is hereby granted, free of charge, to any person obtaining
@ -24,31 +23,28 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
// ************************************************************************/
using Polly ;
# endregion
# endregion
using System.Linq ;
using System ;
using System ;
using System.ComponentModel ;
using System.Diagnostics ;
using System.Diagnostics ;
using System.Linq ;
using System.Threading ;
using System.Threading ;
using System.Threading.Tasks ;
using System.Threading.Tasks ;
using Newtonsoft.Json ;
using NLog ;
using NLog ;
using PlexRequests.Api.Interfaces ;
using PlexRequests.Api.Interfaces ;
using PlexRequests.Api.Models.SickRage ;
using PlexRequests.Api.Models.SickRage ;
using PlexRequests.Helpers ;
using PlexRequests.Helpers ;
using RestSharp ;
using Newtonsoft.Json.Linq ;
using PlexRequests.Helpers.Exceptions ;
using PlexRequests.Helpers.Exceptions ;
using RestSharp ;
namespace PlexRequests.Api
namespace PlexRequests.Api
{
{
public class SickrageApi : ISickRageApi
public class SickrageApi : ISickRageApi
{
{
private static Logger Log = LogManager . GetCurrentClassLogger ( ) ;
private static readonly Logger Log = LogManager . GetCurrentClassLogger ( ) ;
public SickrageApi ( )
public SickrageApi ( )
{
{
@ -57,22 +53,38 @@ namespace PlexRequests.Api
private ApiRequest Api { get ; }
private ApiRequest Api { get ; }
public SickRageSeasonList VerifyShowHasLoaded ( int tvdbId , string apiKey , Uri baseUrl )
public async Task < SickRageTvAdd > AddSeries ( int tvdbId , int seasonCount , int [ ] seasons , string quality , string apiKey ,
Uri baseUrl )
{
{
Log . Trace ( "Entered `VerifyShowHasLoaded({0} <- id)`" , tvdbId ) ;
var request = new RestRequest { Resource = "/api/{apiKey}/?cmd=show.seasonlist" , Method = Method . GET } ;
request . AddUrlSegment ( "apiKey" , apiKey ) ;
request . AddQueryParameter ( "tvdbid" , tvdbId . ToString ( ) ) ;
try
{
var policy = RetryHandler . RetryAndWaitPolicy (
null ,
( exception , timespan ) = > Log . Error ( exception , "Exception when calling VerifyShowHasLoaded for SR, Retrying {0}" , timespan ) ) ;
var obj = policy . Execute ( ( ) = > Api . ExecuteJson < SickRageSeasonList > ( request , baseUrl ) ) ;
return obj ;
}
catch ( Exception e )
{
Log . Error ( e ) ;
return new SickRageSeasonList ( ) ;
}
}
public async Task < SickRageTvAdd > AddSeries ( int tvdbId , int seasonCount , int [ ] seasons , string quality , string apiKey , Uri baseUrl )
{
var futureStatus = seasons . Length > 0 & & ! seasons . Any ( x = > x = = seasonCount ) ? SickRageStatus . Skipped : SickRageStatus . Wanted ;
var futureStatus = seasons . Length > 0 & & ! seasons . Any ( x = > x = = seasonCount ) ? SickRageStatus . Skipped : SickRageStatus . Wanted ;
var status = seasons . Length > 0 ? SickRageStatus . Skipped : SickRageStatus . Wanted ;
var status = seasons . Length > 0 ? SickRageStatus . Skipped : SickRageStatus . Wanted ;
Log . Trace ( "Future Status: {0}" , futureStatus ) ;
Log . Trace ( "Future Status: {0}" , futureStatus ) ;
Log . Trace ( "Current Status: {0}" , status ) ;
Log . Trace ( "Current Status: {0}" , status ) ;
var request = new RestRequest
var request = new RestRequest { Resource = "/api/{apiKey}/?cmd=show.addnew" , Method = Method . GET } ;
{
Resource = "/api/{apiKey}/?cmd=show.addnew" ,
Method = Method . GET
} ;
request . AddUrlSegment ( "apiKey" , apiKey ) ;
request . AddUrlSegment ( "apiKey" , apiKey ) ;
request . AddQueryParameter ( "tvdbid" , tvdbId . ToString ( ) ) ;
request . AddQueryParameter ( "tvdbid" , tvdbId . ToString ( ) ) ;
request . AddQueryParameter ( "status" , status ) ;
request . AddQueryParameter ( "status" , status ) ;
@ -83,10 +95,11 @@ namespace PlexRequests.Api
request . AddQueryParameter ( "initial" , quality ) ;
request . AddQueryParameter ( "initial" , quality ) ;
}
}
var policy = RetryHandler . RetryAndWaitPolicy ( null , ( exception , timespan ) = > Log . Error ( exception , "Exception when calling AddSeries for SR, Retrying {0}" , timespan ) ) ;
var policy = RetryHandler . RetryAndWaitPolicy (
null ,
( exception , timespan ) = > Log . Error ( exception , "Exception when calling AddSeries for SR, Retrying {0}" , timespan ) ) ;
var obj = policy . Execute ( ( ) = > Api . Execute < SickRageTvAdd > ( request , baseUrl ) ) ;
var obj = policy . Execute ( ( ) = > Api . Execute < SickRageTvAdd > ( request , baseUrl ) ) ;
Log . Trace ( "obj Result:" ) ;
Log . Trace ( "obj Result:" ) ;
Log . Trace ( obj . DumpJson ( ) ) ;
Log . Trace ( obj . DumpJson ( ) ) ;
@ -134,7 +147,7 @@ namespace PlexRequests.Api
{
{
Log . Trace ( "Adding season {0}" , s ) ;
Log . Trace ( "Adding season {0}" , s ) ;
var result = await AddSeason ( tvdbId , s , apiKey , baseUrl ) ;
var result = await AddSeason ( tvdbId , s , apiKey , baseUrl ) ;
Log . Trace ( "SickRage adding season results: " ) ;
Log . Trace ( "SickRage adding season results: " ) ;
Log . Trace ( result . DumpJson ( ) ) ;
Log . Trace ( result . DumpJson ( ) ) ;
}
}
@ -153,105 +166,61 @@ namespace PlexRequests.Api
public SickRagePing Ping ( string apiKey , Uri baseUrl )
public SickRagePing Ping ( string apiKey , Uri baseUrl )
{
{
var request = new RestRequest
var request = new RestRequest { Resource = "/api/{apiKey}/?cmd=sb.ping" , Method = Method . GET } ;
{
Resource = "/api/{apiKey}/?cmd=sb.ping" ,
Method = Method . GET
} ;
var policy = RetryHandler . RetryAndWaitPolicy ( null , ( exception , timespan ) = > Log . Error ( exception , "Exception when calling Ping for SR, Retrying {0}" , timespan ) ) ;
var policy = RetryHandler . RetryAndWaitPolicy (
null ,
( exception , timespan ) = > Log . Error ( exception , "Exception when calling Ping for SR, Retrying {0}" , timespan ) ) ;
request . AddUrlSegment ( "apiKey" , apiKey ) ;
request . AddUrlSegment ( "apiKey" , apiKey ) ;
var obj = policy . Execute ( ( ) = > Api . ExecuteJson < SickRagePing > ( request , baseUrl ) ) ;
var obj = policy . Execute ( ( ) = > Api . ExecuteJson < SickRagePing > ( request , baseUrl ) ) ;
return obj ;
return obj ;
}
}
public SickRageSeasonList VerifyShowHasLoaded ( int tvdbId , string apiKey , Uri baseUrl )
{
Log . Trace ( "Entered `VerifyShowHasLoaded({0} <- id)`" , tvdbId ) ;
var request = new RestRequest
{
Resource = "/api/{apiKey}/?cmd=show.seasonlist" ,
Method = Method . GET
} ;
request . AddUrlSegment ( "apiKey" , apiKey ) ;
request . AddQueryParameter ( "tvdbid" , tvdbId . ToString ( ) ) ;
try
{
var policy = RetryHandler . RetryAndWaitPolicy ( null , ( exception , timespan ) = >
Log . Error ( exception , "Exception when calling VerifyShowHasLoaded for SR, Retrying {0}" , timespan ) ) ;
var obj = policy . Execute ( ( ) = > Api . ExecuteJson < SickRageSeasonList > ( request , baseUrl ) ) ;
return obj ;
}
catch ( Exception e )
{
Log . Error ( e ) ;
return new SickRageSeasonList ( ) ;
}
}
public async Task < SickRageTvAdd > AddSeason ( int tvdbId , int season , string apiKey , Uri baseUrl )
public async Task < SickRageTvAdd > AddSeason ( int tvdbId , int season , string apiKey , Uri baseUrl )
{
{
var request = new RestRequest
var request = new RestRequest { Resource = "/api/{apiKey}/?cmd=episode.setstatus" , Method = Method . GET } ;
{
Resource = "/api/{apiKey}/?cmd=episode.setstatus" ,
Method = Method . GET
} ;
request . AddUrlSegment ( "apiKey" , apiKey ) ;
request . AddUrlSegment ( "apiKey" , apiKey ) ;
request . AddQueryParameter ( "tvdbid" , tvdbId . ToString ( ) ) ;
request . AddQueryParameter ( "tvdbid" , tvdbId . ToString ( ) ) ;
request . AddQueryParameter ( "season" , season . ToString ( ) ) ;
request . AddQueryParameter ( "season" , season . ToString ( ) ) ;
request . AddQueryParameter ( "status" , SickRageStatus . Wanted ) ;
request . AddQueryParameter ( "status" , SickRageStatus . Wanted ) ;
await Task . Run ( ( ) = > Thread . Sleep ( 2000 ) ) ;
await Task . Run ( ( ) = > Thread . Sleep ( 2000 ) ) ;
return await Task . Run ( ( ) = >
return await Task . Run (
{
( ) = >
{
var policy = RetryHandler . RetryAndWaitPolicy ( null , ( exception , timespan ) = >
var policy = RetryHandler . RetryAndWaitPolicy (
Log . Error ( exception , "Exception when calling AddSeason for SR, Retrying {0}" , timespan ) ) ;
null ,
( exception , timespan ) = > Log . Error ( exception , "Exception when calling AddSeason for SR, Retrying {0}" , timespan ) ) ;
var result = policy . Execute ( ( ) = > Api . Execute < SickRageTvAdd > ( request , baseUrl ) ) ;
var result = policy . Execute ( ( ) = > Api . Execute < SickRageTvAdd > ( request , baseUrl ) ) ;
return result ;
return result ;
} ) . ConfigureAwait ( false ) ;
} ) . ConfigureAwait ( false ) ;
}
}
public async Task < SickrageShows > GetShows ( string apiKey , Uri baseUrl )
public async Task < SickrageShows > GetShows ( string apiKey , Uri baseUrl )
{
{
var request = new RestRequest
var request = new RestRequest { Resource = "/api/{apiKey}/?cmd=shows" , Method = Method . GET } ;
{
Resource = "/api/{apiKey}/?cmd=shows" ,
Method = Method . GET
} ;
request . AddUrlSegment ( "apiKey" , apiKey ) ;
request . AddUrlSegment ( "apiKey" , apiKey ) ;
return await Task . Run (
return await Task . Run (
( ) = >
( ) = >
{
{
try
try
{
{
var policy = RetryHandler . RetryAndWaitPolicy ( new TimeSpan [ ] {
var policy = RetryHandler . RetryAndWaitPolicy (
TimeSpan . FromSeconds ( 5 ) ,
new [ ] { TimeSpan . FromSeconds ( 5 ) , TimeSpan . FromSeconds ( 10 ) , TimeSpan . FromSeconds ( 30 ) } ,
TimeSpan . FromSeconds ( 10 ) ,
( exception , timespan ) = > Log . Error ( exception , "Exception when calling GetShows for SR, Retrying {0}" , timespan ) ) ;
TimeSpan . FromSeconds ( 30 )
} , ( exception , timespan ) = >
Log . Error ( exception , "Exception when calling GetShows for SR, Retrying {0}" , timespan ) ) ;
return policy . Execute ( ( ) = > Api . Execute < SickrageShows > ( request , baseUrl ) ) ;
return policy . Execute ( ( ) = > Api . Execute < SickrageShows > ( request , baseUrl ) ) ;
}
}
catch ( ApiRequestException )
catch ( ApiRequestException )
{
{
Log . Error ( "There has been a API exception when Getting the Sickrage shows" ) ;
Log . Error ( "There has been a API exception when Getting the Sickrage shows" ) ;
return null ;
return null ;
}
}
} ) . ConfigureAwait ( false ) ;
} ) . ConfigureAwait ( false ) ;
}
}
}
}