@ -1,31 +1,36 @@
using System.Collections.Generic ;
using System ;
using System.Collections.Generic ;
using System.Net ;
using System.Net ;
using System.Net.Http ;
using System.Net.Http ;
using System.Threading.Tasks ;
using System.Threading.Tasks ;
using AutoMapper ;
using AutoMapper ;
using Nito.AsyncEx ;
using Ombi.Api.TheMovieDb.Models ;
using Ombi.Api.TheMovieDb.Models ;
using Ombi.Helpers ;
using Ombi.Core.Settings ;
using Ombi.Core.Settings.Models.External ;
using Ombi.TheMovieDbApi.Models ;
using Ombi.TheMovieDbApi.Models ;
namespace Ombi.Api.TheMovieDb
namespace Ombi.Api.TheMovieDb
{
{
public class TheMovieDbApi : IMovieDbApi
public class TheMovieDbApi : IMovieDbApi
{
{
public TheMovieDbApi ( IMapper mapper , IApi api )
public TheMovieDbApi ( IMapper mapper , IApi api , ISettingsService < TheMovieDbSettings > settingsService )
{
{
Api = api ;
Api = api ;
Mapper = mapper ;
Mapper = mapper ;
Settings = new AsyncLazy < TheMovieDbSettings > ( ( ) = > settingsService . GetSettingsAsync ( ) ) ;
}
}
private const string ApiToken = "b8eabaf5608b88d0298aa189dd90bf00" ;
private const string BaseUri = "http://api.themoviedb.org/3/" ;
private IMapper Mapper { get ; }
private IMapper Mapper { get ; }
private readonly string ApiToken = "b8eabaf5608b88d0298aa189dd90bf00" ;
private static readonly string BaseUri = "http://api.themoviedb.org/3/" ;
private IApi Api { get ; }
private IApi Api { get ; }
private AsyncLazy < TheMovieDbSettings > Settings { get ; }
public async Task < MovieResponseDto > GetMovieInformation ( int movieId )
public async Task < MovieResponseDto > GetMovieInformation ( int movieId )
{
{
var request = new Request ( $"movie/{movieId}" , BaseUri , HttpMethod . Get ) ;
var request = new Request ( $"movie/{movieId}" , BaseUri , HttpMethod . Get ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "api_key" , ApiToken ) ;
request . AddQueryString ( "api_key" , ApiToken ) ;
AddRetry ( request ) ;
AddRetry ( request ) ;
var result = await Api . Request < MovieResponse > ( request ) ;
var result = await Api . Request < MovieResponse > ( request ) ;
@ -35,7 +40,7 @@ namespace Ombi.Api.TheMovieDb
public async Task < FindResult > Find ( string externalId , ExternalSource source )
public async Task < FindResult > Find ( string externalId , ExternalSource source )
{
{
var request = new Request ( $"find/{externalId}" , BaseUri , HttpMethod . Get ) ;
var request = new Request ( $"find/{externalId}" , BaseUri , HttpMethod . Get ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "api_key" , ApiToken ) ;
request . AddQueryString ( "api_key" , ApiToken ) ;
AddRetry ( request ) ;
AddRetry ( request ) ;
request . AddQueryString ( "external_source" , source . ToString ( ) ) ;
request . AddQueryString ( "external_source" , source . ToString ( ) ) ;
@ -46,9 +51,11 @@ namespace Ombi.Api.TheMovieDb
public async Task < TheMovieDbContainer < ActorResult > > SearchByActor ( string searchTerm , string langCode )
public async Task < TheMovieDbContainer < ActorResult > > SearchByActor ( string searchTerm , string langCode )
{
{
var request = new Request ( $"search/person" , BaseUri , HttpMethod . Get ) ;
var request = new Request ( $"search/person" , BaseUri , HttpMethod . Get ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "api_key" , ApiToken ) ;
request . AddQueryString ( "api_key" , ApiToken ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "query" , searchTerm ) ;
request . AddQueryString ( "query" , searchTerm ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "language" , langCode ) ;
request . AddQueryString ( "language" , langCode ) ;
var settings = await Settings ;
request . AddQueryString ( "include_adult" , settings . ShowAdultMovies . ToString ( ) . ToLower ( ) ) ;
var result = await Api . Request < TheMovieDbContainer < ActorResult > > ( request ) ;
var result = await Api . Request < TheMovieDbContainer < ActorResult > > ( request ) ;
return result ;
return result ;
@ -57,8 +64,8 @@ namespace Ombi.Api.TheMovieDb
public async Task < ActorCredits > GetActorMovieCredits ( int actorId , string langCode )
public async Task < ActorCredits > GetActorMovieCredits ( int actorId , string langCode )
{
{
var request = new Request ( $"person/{actorId}/movie_credits" , BaseUri , HttpMethod . Get ) ;
var request = new Request ( $"person/{actorId}/movie_credits" , BaseUri , HttpMethod . Get ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "api_key" , ApiToken ) ;
request . AddQueryString ( "api_key" , ApiToken ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "language" , langCode ) ;
request . AddQueryString ( "language" , langCode ) ;
var result = await Api . Request < ActorCredits > ( request ) ;
var result = await Api . Request < ActorCredits > ( request ) ;
return result ;
return result ;
@ -67,8 +74,8 @@ namespace Ombi.Api.TheMovieDb
public async Task < List < TvSearchResult > > SearchTv ( string searchTerm )
public async Task < List < TvSearchResult > > SearchTv ( string searchTerm )
{
{
var request = new Request ( $"search/tv" , BaseUri , HttpMethod . Get ) ;
var request = new Request ( $"search/tv" , BaseUri , HttpMethod . Get ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "api_key" , ApiToken ) ;
request . AddQueryString ( "api_key" , ApiToken ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "query" , searchTerm ) ;
request . AddQueryString ( "query" , searchTerm ) ;
AddRetry ( request ) ;
AddRetry ( request ) ;
var result = await Api . Request < TheMovieDbContainer < SearchResult > > ( request ) ;
var result = await Api . Request < TheMovieDbContainer < SearchResult > > ( request ) ;
@ -78,7 +85,7 @@ namespace Ombi.Api.TheMovieDb
public async Task < TvExternals > GetTvExternals ( int theMovieDbId )
public async Task < TvExternals > GetTvExternals ( int theMovieDbId )
{
{
var request = new Request ( $"/tv/{theMovieDbId}/external_ids" , BaseUri , HttpMethod . Get ) ;
var request = new Request ( $"/tv/{theMovieDbId}/external_ids" , BaseUri , HttpMethod . Get ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "api_key" , ApiToken ) ;
request . AddQueryString ( "api_key" , ApiToken ) ;
AddRetry ( request ) ;
AddRetry ( request ) ;
return await Api . Request < TvExternals > ( request ) ;
return await Api . Request < TvExternals > ( request ) ;
@ -87,9 +94,8 @@ namespace Ombi.Api.TheMovieDb
public async Task < List < MovieSearchResult > > SimilarMovies ( int movieId , string langCode )
public async Task < List < MovieSearchResult > > SimilarMovies ( int movieId , string langCode )
{
{
var request = new Request ( $"movie/{movieId}/similar" , BaseUri , HttpMethod . Get ) ;
var request = new Request ( $"movie/{movieId}/similar" , BaseUri , HttpMethod . Get ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "api_key" , ApiToken ) ;
request . AddQueryString ( "api_key" , ApiToken ) ;
request . AddQueryString ( "language" , langCode ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "language" , langCode ) ;
AddRetry ( request ) ;
AddRetry ( request ) ;
var result = await Api . Request < TheMovieDbContainer < SearchResult > > ( request ) ;
var result = await Api . Request < TheMovieDbContainer < SearchResult > > ( request ) ;
@ -99,65 +105,95 @@ namespace Ombi.Api.TheMovieDb
public async Task < MovieResponseDto > GetMovieInformationWithExtraInfo ( int movieId , string langCode = "en" )
public async Task < MovieResponseDto > GetMovieInformationWithExtraInfo ( int movieId , string langCode = "en" )
{
{
var request = new Request ( $"movie/{movieId}" , BaseUri , HttpMethod . Get ) ;
var request = new Request ( $"movie/{movieId}" , BaseUri , HttpMethod . Get ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "api_key" , ApiToken ) ;
request . AddQueryString ( "api_key" , ApiToken ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "append_to_response" , "videos,release_dates" ) ;
request . AddQueryString ( "append_to_response" , "videos,release_dates" ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "language" , langCode ) ;
request . AddQueryString ( "language" , langCode ) ;
AddRetry ( request ) ;
AddRetry ( request ) ;
var result = await Api . Request < MovieResponse > ( request ) ;
var result = await Api . Request < MovieResponse > ( request ) ;
return Mapper . Map < MovieResponseDto > ( result ) ;
return Mapper . Map < MovieResponseDto > ( result ) ;
}
}
public async Task < List < MovieSearchResult > > SearchMovie ( string searchTerm , int? year , string lang age Code)
public async Task < List < MovieSearchResult > > SearchMovie ( string searchTerm , int? year , string lang Code)
{
{
var request = new Request ( $"search/movie" , BaseUri , HttpMethod . Get ) ;
var request = new Request ( $"search/movie" , BaseUri , HttpMethod . Get ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "api_key" , ApiToken ) ;
request . AddQueryString ( "api_key" , ApiToken ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "query" , searchTerm ) ;
request . AddQueryString ( "query" , searchTerm ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "language" , lang age Code) ;
request . AddQueryString ( "language" , lang Code) ;
if ( year . HasValue & & year . Value > 0 )
if ( year . HasValue & & year . Value > 0 )
{
{
request . FullUri = request . FullUri . AddQueryParameter ( "year" , year . Value . ToString ( ) ) ;
request . AddQueryString ( "year" , year . Value . ToString ( ) ) ;
}
}
var settings = await Settings ;
request . AddQueryString ( "include_adult" , settings . ShowAdultMovies . ToString ( ) . ToLower ( ) ) ;
AddRetry ( request ) ;
AddRetry ( request ) ;
var result = await Api . Request < TheMovieDbContainer < SearchResult > > ( request ) ;
var result = await Api . Request < TheMovieDbContainer < SearchResult > > ( request ) ;
return Mapper . Map < List < MovieSearchResult > > ( result . results ) ;
return Mapper . Map < List < MovieSearchResult > > ( result . results ) ;
}
}
public async Task < List < MovieSearchResult > > PopularMovies ( string lang age Code)
public async Task < List < MovieSearchResult > > PopularMovies ( string lang Code)
{
{
var request = new Request ( $"movie/popular" , BaseUri , HttpMethod . Get ) ;
var request = new Request ( $"discover/movie" , BaseUri , HttpMethod . Get ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "api_key" , ApiToken ) ;
request . AddQueryString ( "api_key" , ApiToken ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "language" , langageCode ) ;
request . AddQueryString ( "language" , langCode ) ;
var settings = await Settings ;
request . AddQueryString ( "include_adult" , settings . ShowAdultMovies . ToString ( ) . ToLower ( ) ) ;
request . AddQueryString ( "without_keywords" , settings . ExcludedKeywordIds ) ;
AddRetry ( request ) ;
AddRetry ( request ) ;
var result = await Api . Request < TheMovieDbContainer < SearchResult > > ( request ) ;
var result = await Api . Request < TheMovieDbContainer < SearchResult > > ( request ) ;
return Mapper . Map < List < MovieSearchResult > > ( result . results ) ;
return Mapper . Map < List < MovieSearchResult > > ( result . results ) ;
}
}
public async Task < List < MovieSearchResult > > TopRated ( string lang age Code)
public async Task < List < MovieSearchResult > > TopRated ( string lang Code)
{
{
var request = new Request ( $"movie/top_rated" , BaseUri , HttpMethod . Get ) ;
var request = new Request ( $"discover/movie" , BaseUri , HttpMethod . Get ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "api_key" , ApiToken ) ;
request . AddQueryString ( "api_key" , ApiToken ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "language" , langageCode ) ;
request . AddQueryString ( "language" , langCode ) ;
request . AddQueryString ( "vote_count.gte" , "250" ) ;
request . AddQueryString ( "sort_by" , "vote_average.desc" ) ;
var settings = await Settings ;
request . AddQueryString ( "include_adult" , settings . ShowAdultMovies . ToString ( ) . ToLower ( ) ) ;
request . AddQueryString ( "without_keywords" , settings . ExcludedKeywordIds ) ;
AddRetry ( request ) ;
AddRetry ( request ) ;
var result = await Api . Request < TheMovieDbContainer < SearchResult > > ( request ) ;
var result = await Api . Request < TheMovieDbContainer < SearchResult > > ( request ) ;
return Mapper . Map < List < MovieSearchResult > > ( result . results ) ;
return Mapper . Map < List < MovieSearchResult > > ( result . results ) ;
}
}
public async Task < List < MovieSearchResult > > Upcoming ( string langageCode )
public async Task < List < MovieSearchResult > > Upcoming ( string lang Code)
{
{
var request = new Request ( $"movie/upcoming" , BaseUri , HttpMethod . Get ) ;
var request = new Request ( $"discover/movie" , BaseUri , HttpMethod . Get ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "api_key" , ApiToken ) ;
request . AddQueryString ( "api_key" , ApiToken ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "language" , langageCode ) ;
request . AddQueryString ( "language" , langCode ) ;
request . AddQueryString ( "with_release_type" , "2|3" ) ;
var startDate = DateTime . Today . AddDays ( 7 ) ;
request . AddQueryString ( "release_date.gte" , startDate . ToString ( "yyyy-MM-dd" ) ) ;
request . AddQueryString ( "release_date.lte" , startDate . AddDays ( 17 ) . ToString ( "yyyy-MM-dd" ) ) ;
var settings = await Settings ;
request . AddQueryString ( "include_adult" , settings . ShowAdultMovies . ToString ( ) . ToLower ( ) ) ;
request . AddQueryString ( "without_keywords" , settings . ExcludedKeywordIds ) ;
AddRetry ( request ) ;
AddRetry ( request ) ;
var result = await Api . Request < TheMovieDbContainer < SearchResult > > ( request ) ;
var result = await Api . Request < TheMovieDbContainer < SearchResult > > ( request ) ;
return Mapper . Map < List < MovieSearchResult > > ( result . results ) ;
return Mapper . Map < List < MovieSearchResult > > ( result . results ) ;
}
}
public async Task < List < MovieSearchResult > > NowPlaying ( string langageCode )
public async Task < List < MovieSearchResult > > NowPlaying ( string lang Code)
{
{
var request = new Request ( $"movie/now_playing" , BaseUri , HttpMethod . Get ) ;
var request = new Request ( $"discover/movie" , BaseUri , HttpMethod . Get ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "api_key" , ApiToken ) ;
request . AddQueryString ( "api_key" , ApiToken ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "language" , langageCode ) ;
request . AddQueryString ( "language" , langCode ) ;
request . AddQueryString ( "with_release_type" , "2|3" ) ;
var today = DateTime . Today ;
request . AddQueryString ( "release_date.gte" , today . AddDays ( - 42 ) . ToString ( "yyyy-MM-dd" ) ) ;
request . AddQueryString ( "release_date.lte" , today . AddDays ( 6 ) . ToString ( "yyyy-MM-dd" ) ) ;
var settings = await Settings ;
request . AddQueryString ( "include_adult" , settings . ShowAdultMovies . ToString ( ) . ToLower ( ) ) ;
request . AddQueryString ( "without_keywords" , settings . ExcludedKeywordIds ) ;
AddRetry ( request ) ;
AddRetry ( request ) ;
var result = await Api . Request < TheMovieDbContainer < SearchResult > > ( request ) ;
var result = await Api . Request < TheMovieDbContainer < SearchResult > > ( request ) ;
return Mapper . Map < List < MovieSearchResult > > ( result . results ) ;
return Mapper . Map < List < MovieSearchResult > > ( result . results ) ;
@ -166,7 +202,7 @@ namespace Ombi.Api.TheMovieDb
public async Task < TvInfo > GetTVInfo ( string themoviedbid )
public async Task < TvInfo > GetTVInfo ( string themoviedbid )
{
{
var request = new Request ( $"/tv/{themoviedbid}" , BaseUri , HttpMethod . Get ) ;
var request = new Request ( $"/tv/{themoviedbid}" , BaseUri , HttpMethod . Get ) ;
request . FullUri = request . FullUri . AddQueryParameter ( "api_key" , ApiToken ) ;
request . AddQueryString ( "api_key" , ApiToken ) ;
AddRetry ( request ) ;
AddRetry ( request ) ;
return await Api . Request < TvInfo > ( request ) ;
return await Api . Request < TvInfo > ( request ) ;