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

217 lines
9.5 KiB

using System;
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 Nito.AsyncEx;
8 years ago
using Ombi.Api.TheMovieDb.Models;
using Ombi.Core.Settings;
using Ombi.Core.Settings.Models.External;
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, ISettingsService<TheMovieDbSettings> settingsService)
8 years ago
{
Api = api;
8 years ago
Mapper = mapper;
Settings = new AsyncLazy<TheMovieDbSettings>(() => settingsService.GetSettingsAsync());
8 years ago
}
8 years ago
private const string ApiToken = "b8eabaf5608b88d0298aa189dd90bf00";
private const string BaseUri ="http://api.themoviedb.org/3/";
8 years ago
private IMapper Mapper { get; }
private IApi Api { get; }
private AsyncLazy<TheMovieDbSettings> Settings { 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.AddQueryString("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.AddQueryString("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.AddQueryString("api_key", ApiToken);
request.AddQueryString("query", searchTerm);
request.AddQueryString("language", langCode);
var settings = await Settings;
request.AddQueryString("include_adult", settings.ShowAdultMovies.ToString().ToLower());
var result = await Api.Request<TheMovieDbContainer<ActorResult>>(request);
return result;
}
public async Task<ActorCredits> GetActorMovieCredits(int actorId, string langCode)
{
var request = new Request($"person/{actorId}/movie_credits", BaseUri, HttpMethod.Get);
request.AddQueryString("api_key", ApiToken);
request.AddQueryString("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.AddQueryString("api_key", ApiToken);
request.AddQueryString("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.AddQueryString("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.AddQueryString("api_key", ApiToken);
request.AddQueryString("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.AddQueryString("api_key", ApiToken);
request.AddQueryString("append_to_response", "videos,release_dates");
request.AddQueryString("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 langCode)
8 years ago
{
8 years ago
var request = new Request($"search/movie", BaseUri, HttpMethod.Get);
request.AddQueryString("api_key", ApiToken);
request.AddQueryString("query", searchTerm);
request.AddQueryString("language", langCode);
if (year.HasValue && year.Value > 0)
{
request.AddQueryString("year", year.Value.ToString());
}
var settings = await Settings;
request.AddQueryString("include_adult", settings.ShowAdultMovies.ToString().ToLower());
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 langCode)
{
var request = new Request($"discover/movie", BaseUri, HttpMethod.Get);
request.AddQueryString("api_key", ApiToken);
request.AddQueryString("language", langCode);
var settings = await Settings;
request.AddQueryString("include_adult", settings.ShowAdultMovies.ToString().ToLower());
request.AddQueryString("without_keywords", settings.ExcludedKeywordIds);
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 langCode)
{
var request = new Request($"discover/movie", BaseUri, HttpMethod.Get);
request.AddQueryString("api_key", ApiToken);
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);
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 langCode)
{
var request = new Request($"discover/movie", BaseUri, HttpMethod.Get);
request.AddQueryString("api_key", ApiToken);
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);
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 langCode)
{
var request = new Request($"discover/movie", BaseUri, HttpMethod.Get);
request.AddQueryString("api_key", ApiToken);
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);
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.AddQueryString("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
}
}