2014-02-20 04:53:15 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
2013-10-14 17:34:54 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-03-04 05:43:06 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
2014-11-18 02:48:22 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-02-17 21:35:08 +00:00
|
|
|
|
using MediaBrowser.Controller.Localization;
|
2013-06-09 16:47:28 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2015-01-18 05:45:10 +00:00
|
|
|
|
using MediaBrowser.Model.Configuration;
|
2014-02-07 00:43:45 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-02-20 04:53:15 +00:00
|
|
|
|
using MediaBrowser.Model.Providers;
|
2013-03-04 05:43:06 +00:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-03-02 17:09:35 +00:00
|
|
|
|
using System.Globalization;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-06-09 16:47:28 +00:00
|
|
|
|
namespace MediaBrowser.Providers.Movies
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class MovieDbProvider
|
|
|
|
|
/// </summary>
|
2014-02-15 16:36:09 +00:00
|
|
|
|
public class MovieDbProvider : IRemoteMetadataProvider<Movie, MovieInfo>, IDisposable, IHasOrder
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-10-13 17:52:57 +00:00
|
|
|
|
internal readonly SemaphoreSlim MovieDbResourcePool = new SemaphoreSlim(1, 1);
|
2013-03-04 05:43:06 +00:00
|
|
|
|
|
|
|
|
|
internal static MovieDbProvider Current { get; private set; }
|
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-02-06 04:39:16 +00:00
|
|
|
|
private readonly IServerConfigurationManager _configurationManager;
|
|
|
|
|
private readonly ILogger _logger;
|
2014-02-17 21:35:08 +00:00
|
|
|
|
private readonly ILocalizationManager _localization;
|
2014-11-18 02:48:22 +00:00
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2013-02-25 00:13:45 +00:00
|
|
|
|
|
2014-03-02 17:09:35 +00:00
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
2014-07-05 03:17:24 +00:00
|
|
|
|
|
2014-11-18 02:48:22 +00:00
|
|
|
|
public MovieDbProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, IFileSystem fileSystem, IServerConfigurationManager configurationManager, ILogger logger, ILocalizationManager localization, ILibraryManager libraryManager)
|
2014-02-06 04:39:16 +00:00
|
|
|
|
{
|
|
|
|
|
_jsonSerializer = jsonSerializer;
|
|
|
|
|
_httpClient = httpClient;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2014-02-06 04:39:16 +00:00
|
|
|
|
_configurationManager = configurationManager;
|
|
|
|
|
_logger = logger;
|
2014-02-17 21:35:08 +00:00
|
|
|
|
_localization = localization;
|
2014-11-18 02:48:22 +00:00
|
|
|
|
_libraryManager = libraryManager;
|
2013-03-04 05:43:06 +00:00
|
|
|
|
Current = this;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-02 17:09:35 +00:00
|
|
|
|
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(MovieInfo searchInfo, CancellationToken cancellationToken)
|
2014-02-20 04:53:15 +00:00
|
|
|
|
{
|
2014-03-02 17:09:35 +00:00
|
|
|
|
return GetMovieSearchResults(searchInfo, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetMovieSearchResults(ItemLookupInfo searchInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var tmdbId = searchInfo.GetProviderId(MetadataProviders.Tmdb);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(tmdbId))
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
await EnsureMovieInfo(tmdbId, searchInfo.MetadataLanguage, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var dataFilePath = GetDataFilePath(tmdbId, searchInfo.MetadataLanguage);
|
|
|
|
|
|
|
|
|
|
var obj = _jsonSerializer.DeserializeFromFile<CompleteMovieData>(dataFilePath);
|
|
|
|
|
|
2014-09-22 21:56:54 +00:00
|
|
|
|
var tmdbSettings = await GetTmdbSettings(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var tmdbImageUrl = tmdbSettings.images.base_url + "original";
|
|
|
|
|
|
2014-03-02 17:09:35 +00:00
|
|
|
|
var remoteResult = new RemoteSearchResult
|
|
|
|
|
{
|
2015-03-10 01:30:20 +00:00
|
|
|
|
Name = obj.GetTitle(),
|
2014-03-02 17:09:35 +00:00
|
|
|
|
SearchProviderName = Name,
|
|
|
|
|
ImageUrl = string.IsNullOrWhiteSpace(obj.poster_path) ? null : tmdbImageUrl + obj.poster_path
|
|
|
|
|
};
|
|
|
|
|
|
2014-03-02 18:01:46 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(obj.release_date))
|
|
|
|
|
{
|
|
|
|
|
DateTime r;
|
|
|
|
|
|
|
|
|
|
// These dates are always in this exact format
|
|
|
|
|
if (DateTime.TryParse(obj.release_date, _usCulture, DateTimeStyles.None, out r))
|
|
|
|
|
{
|
|
|
|
|
remoteResult.PremiereDate = r.ToUniversalTime();
|
|
|
|
|
remoteResult.ProductionYear = remoteResult.PremiereDate.Value.Year;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-05 03:17:24 +00:00
|
|
|
|
|
2014-03-02 17:09:35 +00:00
|
|
|
|
remoteResult.SetProviderId(MetadataProviders.Tmdb, obj.id.ToString(_usCulture));
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(obj.imdb_id))
|
|
|
|
|
{
|
|
|
|
|
remoteResult.SetProviderId(MetadataProviders.Imdb, obj.imdb_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new[] { remoteResult };
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-18 02:48:22 +00:00
|
|
|
|
return await new MovieDbSearch(_logger, _jsonSerializer, _libraryManager).GetMovieSearchResults(searchInfo, cancellationToken).ConfigureAwait(false);
|
2014-02-20 04:53:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-07 03:10:13 +00:00
|
|
|
|
public Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, CancellationToken cancellationToken)
|
2013-03-04 05:43:06 +00:00
|
|
|
|
{
|
2014-02-07 03:10:13 +00:00
|
|
|
|
return GetItemMetadata<Movie>(info, cancellationToken);
|
2013-02-24 21:53:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-07 03:10:13 +00:00
|
|
|
|
public Task<MetadataResult<T>> GetItemMetadata<T>(ItemLookupInfo id, CancellationToken cancellationToken)
|
2015-03-14 20:00:32 +00:00
|
|
|
|
where T : BaseItem, new()
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2015-09-13 21:32:02 +00:00
|
|
|
|
var movieDb = new GenericMovieDbInfo<T>(_logger, _jsonSerializer, _libraryManager, _fileSystem);
|
2014-02-06 04:39:16 +00:00
|
|
|
|
|
|
|
|
|
return movieDb.GetMetadata(id, cancellationToken);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
public string Name
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
get { return "TheMovieDb"; }
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-02-06 04:39:16 +00:00
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// </summary>
|
2014-02-06 04:39:16 +00:00
|
|
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
|
protected virtual void Dispose(bool dispose)
|
2013-05-06 22:52:13 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
if (dispose)
|
2013-05-06 22:52:13 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
MovieDbResourcePool.Dispose();
|
2013-05-06 22:52:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _TMDB settings task
|
|
|
|
|
/// </summary>
|
2013-05-19 19:37:52 +00:00
|
|
|
|
private TmdbSettingsResult _tmdbSettings;
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the TMDB settings.
|
|
|
|
|
/// </summary>
|
2013-05-19 19:37:52 +00:00
|
|
|
|
/// <returns>Task{TmdbSettingsResult}.</returns>
|
|
|
|
|
internal async Task<TmdbSettingsResult> GetTmdbSettings(CancellationToken cancellationToken)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-19 19:37:52 +00:00
|
|
|
|
if (_tmdbSettings != null)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-19 19:37:52 +00:00
|
|
|
|
return _tmdbSettings;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-13 17:27:13 +00:00
|
|
|
|
using (var json = await GetMovieDbResponse(new HttpRequestOptions
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2014-04-13 17:27:13 +00:00
|
|
|
|
Url = string.Format(TmdbConfigUrl, ApiKey),
|
|
|
|
|
CancellationToken = cancellationToken,
|
2014-09-28 20:49:43 +00:00
|
|
|
|
AcceptHeader = AcceptHeader,
|
2014-10-02 00:28:16 +00:00
|
|
|
|
CacheMode = CacheMode.Unconditional,
|
2014-10-06 23:30:05 +00:00
|
|
|
|
CacheLength = TimeSpan.FromDays(1)
|
2013-05-04 04:15:39 +00:00
|
|
|
|
|
2014-04-13 17:27:13 +00:00
|
|
|
|
}).ConfigureAwait(false))
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2014-04-13 17:27:13 +00:00
|
|
|
|
_tmdbSettings = _jsonSerializer.DeserializeFromStream<TmdbSettingsResult>(json);
|
|
|
|
|
|
|
|
|
|
return _tmdbSettings;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private const string TmdbConfigUrl = "http://api.themoviedb.org/3/configuration?api_key={0}";
|
2013-10-22 19:52:33 +00:00
|
|
|
|
private const string GetMovieInfo3 = @"http://api.themoviedb.org/3/movie/{0}?api_key={1}&append_to_response=casts,releases,images,keywords,trailers";
|
2013-05-06 22:52:13 +00:00
|
|
|
|
|
2013-05-04 04:15:39 +00:00
|
|
|
|
internal static string ApiKey = "f6bd687ffa63cd282b6ff2c6877f2669";
|
|
|
|
|
internal static string AcceptHeader = "application/json,image/*";
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-10-14 17:34:54 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the movie data path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
|
/// <param name="tmdbId">The TMDB id.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
2014-01-30 21:23:54 +00:00
|
|
|
|
internal static string GetMovieDataPath(IApplicationPaths appPaths, string tmdbId)
|
2013-10-14 17:34:54 +00:00
|
|
|
|
{
|
2014-01-30 21:23:54 +00:00
|
|
|
|
var dataPath = GetMoviesDataPath(appPaths);
|
2013-10-14 17:34:54 +00:00
|
|
|
|
|
|
|
|
|
return Path.Combine(dataPath, tmdbId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static string GetMoviesDataPath(IApplicationPaths appPaths)
|
|
|
|
|
{
|
2014-10-07 03:03:38 +00:00
|
|
|
|
var dataPath = Path.Combine(appPaths.CachePath, "tmdb-movies2");
|
2013-10-14 17:34:54 +00:00
|
|
|
|
|
|
|
|
|
return dataPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Downloads the movie info.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id">The id.</param>
|
2013-12-27 00:23:58 +00:00
|
|
|
|
/// <param name="preferredMetadataLanguage">The preferred metadata language.</param>
|
2013-10-14 17:34:54 +00:00
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2014-01-30 21:23:54 +00:00
|
|
|
|
internal async Task DownloadMovieInfo(string id, string preferredMetadataLanguage, CancellationToken cancellationToken)
|
2013-10-14 17:34:54 +00:00
|
|
|
|
{
|
2014-09-28 20:49:43 +00:00
|
|
|
|
var mainResult = await FetchMainResult(id, true, preferredMetadataLanguage, cancellationToken).ConfigureAwait(false);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
if (mainResult == null) return;
|
|
|
|
|
|
2014-01-30 21:23:54 +00:00
|
|
|
|
var dataFilePath = GetDataFilePath(id, preferredMetadataLanguage);
|
2013-10-14 17:34:54 +00:00
|
|
|
|
|
2015-09-13 21:32:02 +00:00
|
|
|
|
_fileSystem.CreateDirectory(Path.GetDirectoryName(dataFilePath));
|
2013-10-14 17:34:54 +00:00
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
_jsonSerializer.SerializeToFile(mainResult, dataFilePath);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 17:44:13 +00:00
|
|
|
|
private readonly Task _cachedTask = Task.FromResult(true);
|
2014-02-06 04:39:16 +00:00
|
|
|
|
internal Task EnsureMovieInfo(string tmdbId, string language, CancellationToken cancellationToken)
|
2014-01-10 13:52:01 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("tmdbId");
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(language))
|
2014-02-04 20:19:29 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
throw new ArgumentNullException("language");
|
2014-02-04 20:19:29 +00:00
|
|
|
|
}
|
2014-02-06 04:39:16 +00:00
|
|
|
|
|
|
|
|
|
var path = GetDataFilePath(tmdbId, language);
|
|
|
|
|
|
2014-01-10 13:52:01 +00:00
|
|
|
|
var fileInfo = _fileSystem.GetFileSystemInfo(path);
|
|
|
|
|
|
|
|
|
|
if (fileInfo.Exists)
|
|
|
|
|
{
|
|
|
|
|
// If it's recent or automatic updates are enabled, don't re-download
|
2014-10-06 23:30:05 +00:00
|
|
|
|
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 3)
|
2014-01-10 13:52:01 +00:00
|
|
|
|
{
|
2014-02-03 17:44:13 +00:00
|
|
|
|
return _cachedTask;
|
2014-01-10 13:52:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
return DownloadMovieInfo(tmdbId, language, cancellationToken);
|
2014-01-10 13:52:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
internal string GetDataFilePath(string tmdbId, string preferredLanguage)
|
2013-10-14 17:34:54 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
2013-10-14 17:34:54 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
throw new ArgumentNullException("tmdbId");
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(preferredLanguage))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("preferredLanguage");
|
2013-10-14 17:34:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
var path = GetMovieDataPath(_configurationManager.ApplicationPaths, tmdbId);
|
2013-10-14 17:34:54 +00:00
|
|
|
|
|
2013-12-28 16:58:13 +00:00
|
|
|
|
var filename = string.Format("all-{0}.json",
|
2014-07-26 17:30:15 +00:00
|
|
|
|
preferredLanguage);
|
2013-12-28 16:58:13 +00:00
|
|
|
|
|
|
|
|
|
return Path.Combine(path, filename);
|
2013-11-01 15:55:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-31 03:19:41 +00:00
|
|
|
|
public static string GetImageLanguagesParam(string preferredLanguage)
|
2014-10-06 23:30:05 +00:00
|
|
|
|
{
|
|
|
|
|
var languages = new List<string>();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(preferredLanguage))
|
|
|
|
|
{
|
|
|
|
|
languages.Add(preferredLanguage);
|
|
|
|
|
}
|
|
|
|
|
languages.Add("null");
|
|
|
|
|
if (!string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
languages.Add("en");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string.Join(",", languages.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-14 17:34:54 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches the main result.
|
|
|
|
|
/// </summary>
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <param name="id">The id.</param>
|
2014-09-28 20:49:43 +00:00
|
|
|
|
/// <param name="isTmdbId">if set to <c>true</c> [is TMDB identifier].</param>
|
2013-10-22 19:03:21 +00:00
|
|
|
|
/// <param name="language">The language.</param>
|
2013-03-03 16:05:17 +00:00
|
|
|
|
/// <param name="cancellationToken">The cancellation token</param>
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <returns>Task{CompleteMovieData}.</returns>
|
2014-09-28 20:49:43 +00:00
|
|
|
|
internal async Task<CompleteMovieData> FetchMainResult(string id, bool isTmdbId, string language, CancellationToken cancellationToken)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2014-01-30 21:23:54 +00:00
|
|
|
|
var url = string.Format(GetMovieInfo3, id, ApiKey);
|
2013-10-22 19:52:33 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(language))
|
|
|
|
|
{
|
2013-12-27 03:48:05 +00:00
|
|
|
|
url += string.Format("&language={0}", language);
|
2013-10-22 19:52:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-31 03:19:41 +00:00
|
|
|
|
var includeImageLanguageParam = GetImageLanguagesParam(language);
|
2014-02-17 21:35:08 +00:00
|
|
|
|
// Get images in english and with no language
|
2014-07-05 03:17:24 +00:00
|
|
|
|
url += "&include_image_language=" + includeImageLanguageParam;
|
2014-02-17 21:35:08 +00:00
|
|
|
|
|
2013-03-03 16:05:17 +00:00
|
|
|
|
CompleteMovieData mainResult;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2014-09-28 20:49:43 +00:00
|
|
|
|
// Cache if not using a tmdbId because we won't have the tmdb cache directory structure. So use the lower level cache.
|
2014-10-02 00:28:16 +00:00
|
|
|
|
var cacheMode = isTmdbId ? CacheMode.None : CacheMode.Unconditional;
|
2014-09-28 20:49:43 +00:00
|
|
|
|
var cacheLength = TimeSpan.FromDays(3);
|
|
|
|
|
|
2013-05-19 19:37:52 +00:00
|
|
|
|
using (var json = await GetMovieDbResponse(new HttpRequestOptions
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-19 19:37:52 +00:00
|
|
|
|
Url = url,
|
|
|
|
|
CancellationToken = cancellationToken,
|
2014-09-28 20:49:43 +00:00
|
|
|
|
AcceptHeader = AcceptHeader,
|
2014-10-02 00:28:16 +00:00
|
|
|
|
CacheMode = cacheMode,
|
2014-09-28 20:49:43 +00:00
|
|
|
|
CacheLength = cacheLength
|
2013-05-04 04:15:39 +00:00
|
|
|
|
|
2013-05-19 19:37:52 +00:00
|
|
|
|
}).ConfigureAwait(false))
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
mainResult = _jsonSerializer.DeserializeFromStream<CompleteMovieData>(json);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2015-01-31 03:19:41 +00:00
|
|
|
|
// If the language preference isn't english, then have the overview fallback to english if it's blank
|
|
|
|
|
if (mainResult != null &&
|
|
|
|
|
string.IsNullOrEmpty(mainResult.overview) &&
|
|
|
|
|
!string.IsNullOrEmpty(language) &&
|
|
|
|
|
!string.Equals(language, "en", StringComparison.OrdinalIgnoreCase))
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2015-01-31 03:19:41 +00:00
|
|
|
|
_logger.Info("MovieDbProvider couldn't find meta for language " + language + ". Trying English...");
|
2013-05-06 22:52:13 +00:00
|
|
|
|
|
2015-01-31 03:19:41 +00:00
|
|
|
|
url = string.Format(GetMovieInfo3, id, ApiKey) + "&include_image_language=" + includeImageLanguageParam + "&language=en";
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2015-01-31 03:19:41 +00:00
|
|
|
|
using (var json = await GetMovieDbResponse(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
AcceptHeader = AcceptHeader,
|
|
|
|
|
CacheMode = cacheMode,
|
|
|
|
|
CacheLength = cacheLength
|
2013-05-04 04:15:39 +00:00
|
|
|
|
|
2015-01-31 03:19:41 +00:00
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
var englishResult = _jsonSerializer.DeserializeFromStream<CompleteMovieData>(json);
|
|
|
|
|
|
|
|
|
|
mainResult.overview = englishResult.overview;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-05 03:17:24 +00:00
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
return mainResult;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-19 18:42:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the movie db response.
|
|
|
|
|
/// </summary>
|
2014-10-06 23:30:05 +00:00
|
|
|
|
internal Task<Stream> GetMovieDbResponse(HttpRequestOptions options)
|
2013-05-19 18:42:58 +00:00
|
|
|
|
{
|
2014-10-06 23:30:05 +00:00
|
|
|
|
options.ResourcePool = MovieDbResourcePool;
|
2013-05-19 18:42:58 +00:00
|
|
|
|
|
2014-10-06 23:30:05 +00:00
|
|
|
|
return _httpClient.Get(options);
|
2013-05-19 18:42:58 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2015-01-18 05:45:10 +00:00
|
|
|
|
public TheMovieDbOptions GetTheMovieDbOptions()
|
|
|
|
|
{
|
|
|
|
|
return _configurationManager.GetConfiguration<TheMovieDbOptions>("themoviedb");
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-07 00:43:45 +00:00
|
|
|
|
public bool HasChanged(IHasMetadata item, DateTime date)
|
|
|
|
|
{
|
2015-01-18 05:45:10 +00:00
|
|
|
|
if (!GetTheMovieDbOptions().EnableAutomaticUpdates)
|
2014-02-07 00:43:45 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tmdbId = item.GetProviderId(MetadataProviders.Tmdb);
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(tmdbId))
|
|
|
|
|
{
|
|
|
|
|
// Process images
|
|
|
|
|
var dataFilePath = GetDataFilePath(tmdbId, item.GetPreferredMetadataLanguage());
|
|
|
|
|
|
|
|
|
|
var fileInfo = new FileInfo(dataFilePath);
|
2014-03-02 17:09:35 +00:00
|
|
|
|
|
2014-02-07 00:43:45 +00:00
|
|
|
|
return !fileInfo.Exists || _fileSystem.GetLastWriteTimeUtc(fileInfo) > date;
|
|
|
|
|
}
|
2014-03-02 17:09:35 +00:00
|
|
|
|
|
2014-02-07 00:43:45 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:52:33 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-05-06 22:52:13 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class TmdbTitle
|
|
|
|
|
/// </summary>
|
2013-10-22 19:52:33 +00:00
|
|
|
|
internal class TmdbTitle
|
2013-05-06 22:52:13 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the iso_3166_1.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The iso_3166_1.</value>
|
|
|
|
|
public string iso_3166_1 { get; set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the title.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The title.</value>
|
|
|
|
|
public string title { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class TmdbAltTitleResults
|
|
|
|
|
/// </summary>
|
2013-10-22 19:52:33 +00:00
|
|
|
|
internal class TmdbAltTitleResults
|
2013-05-06 22:52:13 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the id.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The id.</value>
|
|
|
|
|
public int id { get; set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the titles.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The titles.</value>
|
|
|
|
|
public List<TmdbTitle> titles { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:52:33 +00:00
|
|
|
|
internal class BelongsToCollection
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
public int id { get; set; }
|
|
|
|
|
public string name { get; set; }
|
|
|
|
|
public string poster_path { get; set; }
|
|
|
|
|
public string backdrop_path { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:52:33 +00:00
|
|
|
|
internal class GenreItem
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
public int id { get; set; }
|
|
|
|
|
public string name { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:52:33 +00:00
|
|
|
|
internal class ProductionCompany
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
public string name { get; set; }
|
|
|
|
|
public int id { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:52:33 +00:00
|
|
|
|
internal class ProductionCountry
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
public string iso_3166_1 { get; set; }
|
|
|
|
|
public string name { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:52:33 +00:00
|
|
|
|
internal class SpokenLanguage
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
public string iso_639_1 { get; set; }
|
|
|
|
|
public string name { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:52:33 +00:00
|
|
|
|
internal class Cast
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
public int id { get; set; }
|
|
|
|
|
public string name { get; set; }
|
|
|
|
|
public string character { get; set; }
|
|
|
|
|
public int order { get; set; }
|
2013-05-06 22:52:13 +00:00
|
|
|
|
public int cast_id { get; set; }
|
2013-02-21 01:33:05 +00:00
|
|
|
|
public string profile_path { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:52:33 +00:00
|
|
|
|
internal class Crew
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
public int id { get; set; }
|
|
|
|
|
public string name { get; set; }
|
|
|
|
|
public string department { get; set; }
|
|
|
|
|
public string job { get; set; }
|
2013-05-06 22:52:13 +00:00
|
|
|
|
public string profile_path { get; set; }
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:52:33 +00:00
|
|
|
|
internal class Casts
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-06 22:52:13 +00:00
|
|
|
|
public List<Cast> cast { get; set; }
|
|
|
|
|
public List<Crew> crew { get; set; }
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:52:33 +00:00
|
|
|
|
internal class Country
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
public string iso_3166_1 { get; set; }
|
2013-05-06 22:52:13 +00:00
|
|
|
|
public string certification { get; set; }
|
|
|
|
|
public DateTime release_date { get; set; }
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:52:33 +00:00
|
|
|
|
internal class Releases
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-06 22:52:13 +00:00
|
|
|
|
public List<Country> countries { get; set; }
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:52:33 +00:00
|
|
|
|
internal class Backdrop
|
|
|
|
|
{
|
|
|
|
|
public string file_path { get; set; }
|
|
|
|
|
public int width { get; set; }
|
|
|
|
|
public int height { get; set; }
|
|
|
|
|
public object iso_639_1 { get; set; }
|
|
|
|
|
public double aspect_ratio { get; set; }
|
|
|
|
|
public double vote_average { get; set; }
|
|
|
|
|
public int vote_count { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class Poster
|
|
|
|
|
{
|
|
|
|
|
public string file_path { get; set; }
|
|
|
|
|
public int width { get; set; }
|
|
|
|
|
public int height { get; set; }
|
|
|
|
|
public string iso_639_1 { get; set; }
|
|
|
|
|
public double aspect_ratio { get; set; }
|
|
|
|
|
public double vote_average { get; set; }
|
|
|
|
|
public int vote_count { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class Images
|
|
|
|
|
{
|
|
|
|
|
public List<Backdrop> backdrops { get; set; }
|
|
|
|
|
public List<Poster> posters { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class Keyword
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
public int id { get; set; }
|
2013-05-06 22:52:13 +00:00
|
|
|
|
public string name { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:52:33 +00:00
|
|
|
|
internal class Keywords
|
2013-05-06 22:52:13 +00:00
|
|
|
|
{
|
|
|
|
|
public List<Keyword> keywords { get; set; }
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:52:33 +00:00
|
|
|
|
internal class Youtube
|
|
|
|
|
{
|
|
|
|
|
public string name { get; set; }
|
|
|
|
|
public string size { get; set; }
|
|
|
|
|
public string source { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class Trailers
|
|
|
|
|
{
|
|
|
|
|
public List<object> quicktime { get; set; }
|
|
|
|
|
public List<Youtube> youtube { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class CompleteMovieData
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
public bool adult { get; set; }
|
|
|
|
|
public string backdrop_path { get; set; }
|
|
|
|
|
public BelongsToCollection belongs_to_collection { get; set; }
|
|
|
|
|
public int budget { get; set; }
|
2013-05-06 22:52:13 +00:00
|
|
|
|
public List<GenreItem> genres { get; set; }
|
2013-02-21 01:33:05 +00:00
|
|
|
|
public string homepage { get; set; }
|
|
|
|
|
public int id { get; set; }
|
|
|
|
|
public string imdb_id { get; set; }
|
|
|
|
|
public string original_title { get; set; }
|
2015-03-10 01:30:20 +00:00
|
|
|
|
public string original_name { get; set; }
|
2013-02-21 01:33:05 +00:00
|
|
|
|
public string overview { get; set; }
|
|
|
|
|
public double popularity { get; set; }
|
|
|
|
|
public string poster_path { get; set; }
|
|
|
|
|
public List<ProductionCompany> production_companies { get; set; }
|
|
|
|
|
public List<ProductionCountry> production_countries { get; set; }
|
2014-03-02 18:01:46 +00:00
|
|
|
|
public string release_date { get; set; }
|
2013-02-21 01:33:05 +00:00
|
|
|
|
public int revenue { get; set; }
|
|
|
|
|
public int runtime { get; set; }
|
|
|
|
|
public List<SpokenLanguage> spoken_languages { get; set; }
|
2013-05-06 22:52:13 +00:00
|
|
|
|
public string status { get; set; }
|
2013-02-21 01:33:05 +00:00
|
|
|
|
public string tagline { get; set; }
|
|
|
|
|
public string title { get; set; }
|
2013-10-22 19:52:33 +00:00
|
|
|
|
public string name { get; set; }
|
2013-02-21 01:33:05 +00:00
|
|
|
|
public double vote_average { get; set; }
|
|
|
|
|
public int vote_count { get; set; }
|
2013-05-06 22:52:13 +00:00
|
|
|
|
public Casts casts { get; set; }
|
|
|
|
|
public Releases releases { get; set; }
|
2013-10-22 19:52:33 +00:00
|
|
|
|
public Images images { get; set; }
|
2013-05-06 22:52:13 +00:00
|
|
|
|
public Keywords keywords { get; set; }
|
2013-06-27 16:36:41 +00:00
|
|
|
|
public Trailers trailers { get; set; }
|
2015-03-10 01:30:20 +00:00
|
|
|
|
|
|
|
|
|
public string GetOriginalTitle()
|
|
|
|
|
{
|
|
|
|
|
return original_name ?? original_title;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetTitle()
|
|
|
|
|
{
|
|
|
|
|
return name ?? title ?? GetOriginalTitle();
|
|
|
|
|
}
|
2013-06-27 16:36:41 +00:00
|
|
|
|
}
|
2014-02-15 16:36:09 +00:00
|
|
|
|
|
|
|
|
|
public int Order
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
// After Omdb
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-20 04:53:15 +00:00
|
|
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-03-01 22:34:27 +00:00
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = MovieDbResourcePool
|
|
|
|
|
});
|
2014-02-20 04:53:15 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
2015-01-18 05:45:10 +00:00
|
|
|
|
|
|
|
|
|
public class TmdbConfigStore : IConfigurationFactory
|
|
|
|
|
{
|
|
|
|
|
public IEnumerable<ConfigurationStore> GetConfigurations()
|
|
|
|
|
{
|
|
|
|
|
return new List<ConfigurationStore>
|
|
|
|
|
{
|
|
|
|
|
new ConfigurationStore
|
|
|
|
|
{
|
|
|
|
|
Key = "themoviedb",
|
|
|
|
|
ConfigurationType = typeof(TheMovieDbOptions)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|