You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ombi/src/Ombi.TheMovieDbApi/TheMovieDbApi.cs

182 lines
8.2 KiB

8 years ago
using System.Collections.Generic;
using System.Net;
8 years ago
using System.Net.Http;
8 years ago
using System.Threading.Tasks;
8 years ago
using AutoMapper;
using Ombi.Api.TheMovieDb.Models;
8 years ago
using Ombi.Helpers;
8 years ago
using Ombi.TheMovieDbApi.Models;
8 years ago
namespace Ombi.Api.TheMovieDb
8 years ago
{
public class TheMovieDbApi : IMovieDbApi
8 years ago
{
public TheMovieDbApi(IMapper mapper, IApi api)
8 years ago
{
Api = api;
8 years ago
Mapper = mapper;
8 years ago
}
8 years ago
private IMapper Mapper { get; }
8 years ago
private readonly string ApiToken = "b8eabaf5608b88d0298aa189dd90bf00";
8 years ago
private static readonly string BaseUri ="http://api.themoviedb.org/3/";
private IApi Api { get; }
8 years ago
8 years ago
public async Task<MovieResponseDto> GetMovieInformation(int movieId)
8 years ago
{
8 years ago
var request = new Request($"movie/{movieId}", BaseUri, HttpMethod.Get);
request.FullUri = request.FullUri.AddQueryParameter("api_key", ApiToken);
AddRetry(request);
8 years ago
var result = await Api.Request<MovieResponse>(request);
return Mapper.Map<MovieResponseDto>(result);
8 years ago
}
public async Task<FindResult> Find(string externalId, ExternalSource source)
{
var request = new Request($"find/{externalId}", BaseUri, HttpMethod.Get);
request.FullUri = request.FullUri.AddQueryParameter("api_key", ApiToken);
AddRetry(request);
request.AddQueryString("external_source", source.ToString());
return await Api.Request<FindResult>(request);
}
public async Task<TheMovieDbContainer<ActorResult>> SearchByActor(string searchTerm, string langCode)
{
var request = new Request($"search/person", BaseUri, HttpMethod.Get);
request.FullUri = request.FullUri.AddQueryParameter("api_key", ApiToken);
request.FullUri = request.FullUri.AddQueryParameter("query", searchTerm);
request.FullUri = request.FullUri.AddQueryParameter("language", langCode);
var result = await Api.Request<TheMovieDbContainer<ActorResult>>(request);
return result;
}
public async Task<ActorCredits> GetActorMovieCredits(int actorId, string langCode)
{
var request = new Request($"search/person", BaseUri, HttpMethod.Get);
request.FullUri = request.FullUri.AddQueryParameter("api_key", ApiToken);
request.FullUri = request.FullUri.AddQueryParameter("person_id", actorId.ToString());
request.FullUri = request.FullUri.AddQueryParameter("language", langCode);
var result = await Api.Request<ActorCredits>(request);
return result;
}
public async Task<List<TvSearchResult>> SearchTv(string searchTerm)
{
var request = new Request($"search/tv", BaseUri, HttpMethod.Get);
request.FullUri = request.FullUri.AddQueryParameter("api_key", ApiToken);
request.FullUri = request.FullUri.AddQueryParameter("query", searchTerm);
AddRetry(request);
var result = await Api.Request<TheMovieDbContainer<SearchResult>>(request);
return Mapper.Map<List<TvSearchResult>>(result.results);
}
public async Task<TvExternals> GetTvExternals(int theMovieDbId)
{
var request = new Request($"/tv/{theMovieDbId}/external_ids", BaseUri, HttpMethod.Get);
request.FullUri = request.FullUri.AddQueryParameter("api_key", ApiToken);
AddRetry(request);
return await Api.Request<TvExternals>(request);
}
public async Task<List<MovieSearchResult>> SimilarMovies(int movieId, string langCode)
{
var request = new Request($"movie/{movieId}/similar", BaseUri, HttpMethod.Get);
request.FullUri = request.FullUri.AddQueryParameter("api_key", ApiToken);
request.FullUri = request.FullUri.AddQueryParameter("language", langCode);
AddRetry(request);
var result = await Api.Request<TheMovieDbContainer<SearchResult>>(request);
return Mapper.Map<List<MovieSearchResult>>(result.results);
}
public async Task<MovieResponseDto> GetMovieInformationWithExtraInfo(int movieId, string langCode = "en")
8 years ago
{
8 years ago
var request = new Request($"movie/{movieId}", BaseUri, HttpMethod.Get);
request.FullUri = request.FullUri.AddQueryParameter("api_key", ApiToken);
request.FullUri = request.FullUri.AddQueryParameter("append_to_response", "videos,release_dates");
request.FullUri = request.FullUri.AddQueryParameter("language", langCode);
AddRetry(request);
8 years ago
var result = await Api.Request<MovieResponse>(request);
return Mapper.Map<MovieResponseDto>(result);
8 years ago
}
public async Task<List<MovieSearchResult>> SearchMovie(string searchTerm, int? year, string langageCode)
8 years ago
{
8 years ago
var request = new Request($"search/movie", BaseUri, HttpMethod.Get);
request.FullUri = request.FullUri.AddQueryParameter("api_key", ApiToken);
request.FullUri = request.FullUri.AddQueryParameter("query", searchTerm);
request.FullUri = request.FullUri.AddQueryParameter("language", langageCode);
if (year.HasValue && year.Value > 0)
{
request.FullUri = request.FullUri.AddQueryParameter("year", year.Value.ToString());
}
AddRetry(request);
8 years ago
8 years ago
var result = await Api.Request<TheMovieDbContainer<SearchResult>>(request);
return Mapper.Map<List<MovieSearchResult>>(result.results);
8 years ago
}
public async Task<List<MovieSearchResult>> PopularMovies(string langageCode)
{
8 years ago
var request = new Request($"movie/popular", BaseUri, HttpMethod.Get);
request.FullUri = request.FullUri.AddQueryParameter("api_key", ApiToken);
request.FullUri = request.FullUri.AddQueryParameter("language", langageCode);
AddRetry(request);
8 years ago
var result = await Api.Request<TheMovieDbContainer<SearchResult>>(request);
return Mapper.Map<List<MovieSearchResult>>(result.results);
}
public async Task<List<MovieSearchResult>> TopRated(string langageCode)
{
8 years ago
var request = new Request($"movie/top_rated", BaseUri, HttpMethod.Get);
request.FullUri = request.FullUri.AddQueryParameter("api_key", ApiToken);
request.FullUri = request.FullUri.AddQueryParameter("language", langageCode);
AddRetry(request);
8 years ago
var result = await Api.Request<TheMovieDbContainer<SearchResult>>(request);
return Mapper.Map<List<MovieSearchResult>>(result.results);
}
public async Task<List<MovieSearchResult>> Upcoming(string langageCode)
{
8 years ago
var request = new Request($"movie/upcoming", BaseUri, HttpMethod.Get);
request.FullUri = request.FullUri.AddQueryParameter("api_key", ApiToken);
request.FullUri = request.FullUri.AddQueryParameter("language", langageCode);
AddRetry(request);
8 years ago
var result = await Api.Request<TheMovieDbContainer<SearchResult>>(request);
return Mapper.Map<List<MovieSearchResult>>(result.results);
}
public async Task<List<MovieSearchResult>> NowPlaying(string langageCode)
{
8 years ago
var request = new Request($"movie/now_playing", BaseUri, HttpMethod.Get);
request.FullUri = request.FullUri.AddQueryParameter("api_key", ApiToken);
request.FullUri = request.FullUri.AddQueryParameter("language", langageCode);
AddRetry(request);
8 years ago
var result = await Api.Request<TheMovieDbContainer<SearchResult>>(request);
return Mapper.Map<List<MovieSearchResult>>(result.results);
}
public async Task<TvInfo> GetTVInfo(string themoviedbid)
{
var request = new Request($"/tv/{themoviedbid}", BaseUri, HttpMethod.Get);
request.FullUri = request.FullUri.AddQueryParameter("api_key", ApiToken);
AddRetry(request);
return await Api.Request<TvInfo>(request);
}
private static void AddRetry(Request request)
{
request.Retry = true;
request.StatusCodeToRetry.Add((HttpStatusCode)429);
}
8 years ago
}
}