2014-02-19 11:50:37 -07:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2014-01-30 14:23:54 -07:00
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
2014-11-17 19:48:22 -07:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-02-17 20:02:41 -07:00
|
|
|
|
using MediaBrowser.Controller.Localization;
|
2014-01-30 14:23:54 -07:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-02-19 21:53:15 -07:00
|
|
|
|
using MediaBrowser.Model.Providers;
|
2014-01-30 14:23:54 -07:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
|
using MediaBrowser.Providers.Movies;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
2014-02-19 11:50:37 -07:00
|
|
|
|
using System.Linq;
|
2014-01-30 14:23:54 -07:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2015-10-03 21:23:11 -07:00
|
|
|
|
using CommonIO;
|
2014-01-30 14:23:54 -07:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.BoxSets
|
|
|
|
|
{
|
2014-02-06 20:10:13 -07:00
|
|
|
|
public class MovieDbBoxSetProvider : IRemoteMetadataProvider<BoxSet, BoxSetInfo>
|
2014-01-30 14:23:54 -07:00
|
|
|
|
{
|
2016-04-20 22:13:33 -07:00
|
|
|
|
private const string GetCollectionInfo3 = @"https://api.themoviedb.org/3/collection/{0}?api_key={1}&append_to_response=images";
|
2014-01-30 14:47:13 -07:00
|
|
|
|
|
|
|
|
|
internal static MovieDbBoxSetProvider Current;
|
|
|
|
|
|
2014-01-30 14:23:54 -07:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IJsonSerializer _json;
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-02-17 20:02:41 -07:00
|
|
|
|
private readonly ILocalizationManager _localization;
|
2014-03-01 15:34:27 -07:00
|
|
|
|
private readonly IHttpClient _httpClient;
|
2014-11-17 19:48:22 -07:00
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2014-01-30 14:23:54 -07:00
|
|
|
|
|
2014-11-17 19:48:22 -07:00
|
|
|
|
public MovieDbBoxSetProvider(ILogger logger, IJsonSerializer json, IServerConfigurationManager config, IFileSystem fileSystem, ILocalizationManager localization, IHttpClient httpClient, ILibraryManager libraryManager)
|
2014-01-30 14:23:54 -07:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_json = json;
|
|
|
|
|
_config = config;
|
|
|
|
|
_fileSystem = fileSystem;
|
2014-02-17 20:02:41 -07:00
|
|
|
|
_localization = localization;
|
2014-03-01 15:34:27 -07:00
|
|
|
|
_httpClient = httpClient;
|
2014-11-17 19:48:22 -07:00
|
|
|
|
_libraryManager = libraryManager;
|
2014-01-30 14:47:13 -07:00
|
|
|
|
Current = this;
|
2014-01-30 14:23:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-01 15:34:27 -07:00
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
|
2014-02-19 21:53:15 -07:00
|
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(BoxSetInfo searchInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-03-01 15:34:27 -07:00
|
|
|
|
var tmdbId = searchInfo.GetProviderId(MetadataProviders.Tmdb);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(tmdbId))
|
|
|
|
|
{
|
|
|
|
|
await EnsureInfo(tmdbId, searchInfo.MetadataLanguage, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var dataFilePath = GetDataFilePath(_config.ApplicationPaths, tmdbId, searchInfo.MetadataLanguage);
|
|
|
|
|
var info = _json.DeserializeFromFile<RootObject>(dataFilePath);
|
|
|
|
|
|
|
|
|
|
var images = (info.images ?? new Images()).posters ?? new List<Poster>();
|
2014-09-22 14:56:54 -07:00
|
|
|
|
|
|
|
|
|
var tmdbSettings = await MovieDbProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2016-04-29 01:06:41 -07:00
|
|
|
|
var tmdbImageUrl = tmdbSettings.images.secure_base_url + "original";
|
2014-09-22 14:56:54 -07:00
|
|
|
|
|
2014-03-01 15:34:27 -07:00
|
|
|
|
var result = new RemoteSearchResult
|
|
|
|
|
{
|
|
|
|
|
Name = info.name,
|
|
|
|
|
|
|
|
|
|
SearchProviderName = Name,
|
|
|
|
|
|
|
|
|
|
ImageUrl = images.Count == 0 ? null : (tmdbImageUrl + images[0].file_path)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
result.SetProviderId(MetadataProviders.Tmdb, info.id.ToString(_usCulture));
|
|
|
|
|
|
|
|
|
|
return new[] { result };
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-17 19:48:22 -07:00
|
|
|
|
return await new MovieDbSearch(_logger, _json, _libraryManager).GetSearchResults(searchInfo, cancellationToken).ConfigureAwait(false);
|
2014-02-19 21:53:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 20:10:13 -07:00
|
|
|
|
public async Task<MetadataResult<BoxSet>> GetMetadata(BoxSetInfo id, CancellationToken cancellationToken)
|
2014-01-30 14:23:54 -07:00
|
|
|
|
{
|
|
|
|
|
var tmdbId = id.GetProviderId(MetadataProviders.Tmdb);
|
|
|
|
|
|
|
|
|
|
// We don't already have an Id, need to fetch it
|
|
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
|
|
|
|
{
|
2014-11-17 19:48:22 -07:00
|
|
|
|
var searchResults = await new MovieDbSearch(_logger, _json, _libraryManager).GetSearchResults(id, cancellationToken).ConfigureAwait(false);
|
2014-03-01 15:34:27 -07:00
|
|
|
|
|
|
|
|
|
var searchResult = searchResults.FirstOrDefault();
|
2014-02-15 09:36:09 -07:00
|
|
|
|
|
|
|
|
|
if (searchResult != null)
|
|
|
|
|
{
|
2014-03-02 10:09:35 -07:00
|
|
|
|
tmdbId = searchResult.GetProviderId(MetadataProviders.Tmdb);
|
2014-02-15 09:36:09 -07:00
|
|
|
|
}
|
2014-01-30 14:23:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = new MetadataResult<BoxSet>();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(tmdbId))
|
|
|
|
|
{
|
2014-01-30 14:47:13 -07:00
|
|
|
|
var mainResult = await GetMovieDbResult(tmdbId, id.MetadataLanguage, cancellationToken).ConfigureAwait(false);
|
2014-01-30 14:23:54 -07:00
|
|
|
|
|
2014-01-30 14:47:13 -07:00
|
|
|
|
if (mainResult != null)
|
2014-01-30 14:23:54 -07:00
|
|
|
|
{
|
|
|
|
|
result.HasMetadata = true;
|
|
|
|
|
result.Item = GetItem(mainResult);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:47:13 -07:00
|
|
|
|
internal async Task<RootObject> GetMovieDbResult(string tmdbId, string language, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("tmdbId");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await EnsureInfo(tmdbId, language, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var dataFilePath = GetDataFilePath(_config.ApplicationPaths, tmdbId, language);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(dataFilePath))
|
|
|
|
|
{
|
|
|
|
|
return _json.DeserializeFromFile<RootObject>(dataFilePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:23:54 -07:00
|
|
|
|
private BoxSet GetItem(RootObject obj)
|
|
|
|
|
{
|
2014-02-10 11:39:41 -07:00
|
|
|
|
var item = new BoxSet
|
|
|
|
|
{
|
2014-02-15 09:36:09 -07:00
|
|
|
|
Name = obj.name,
|
2014-02-10 11:39:41 -07:00
|
|
|
|
Overview = obj.overview
|
|
|
|
|
};
|
2014-01-30 14:23:54 -07:00
|
|
|
|
|
2014-03-02 10:09:35 -07:00
|
|
|
|
item.SetProviderId(MetadataProviders.Tmdb, obj.id.ToString(_usCulture));
|
2014-01-30 14:23:54 -07:00
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task DownloadInfo(string tmdbId, string preferredMetadataLanguage, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var mainResult = await FetchMainResult(tmdbId, preferredMetadataLanguage, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (mainResult == null) return;
|
|
|
|
|
|
|
|
|
|
var dataFilePath = GetDataFilePath(_config.ApplicationPaths, tmdbId, preferredMetadataLanguage);
|
|
|
|
|
|
2015-09-13 14:32:02 -07:00
|
|
|
|
_fileSystem.CreateDirectory(Path.GetDirectoryName(dataFilePath));
|
2014-01-30 14:23:54 -07:00
|
|
|
|
|
|
|
|
|
_json.SerializeToFile(mainResult, dataFilePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<RootObject> FetchMainResult(string id, string language, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-02-17 20:02:41 -07:00
|
|
|
|
var url = string.Format(GetCollectionInfo3, id, MovieDbProvider.ApiKey);
|
2014-01-30 14:23:54 -07:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(language))
|
|
|
|
|
{
|
2016-03-12 08:19:35 -07:00
|
|
|
|
url += string.Format("&language={0}", MovieDbProvider.NormalizeLanguage(language));
|
2014-01-30 14:23:54 -07:00
|
|
|
|
|
2016-03-04 10:38:51 -07:00
|
|
|
|
// Get images in english and with no language
|
|
|
|
|
url += "&include_image_language=" + MovieDbProvider.GetImageLanguagesParam(language);
|
|
|
|
|
}
|
2014-02-17 20:02:41 -07:00
|
|
|
|
|
2014-01-30 14:23:54 -07:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
RootObject mainResult = null;
|
|
|
|
|
|
|
|
|
|
using (var json = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
AcceptHeader = MovieDbSearch.AcceptHeader
|
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
mainResult = _json.DeserializeFromStream<RootObject>(json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2016-04-26 19:51:05 -07:00
|
|
|
|
if (mainResult != null && string.IsNullOrEmpty(mainResult.name))
|
2014-01-30 14:23:54 -07:00
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(language) && !string.Equals(language, "en", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2016-03-04 10:38:51 -07:00
|
|
|
|
url = string.Format(GetCollectionInfo3, id, MovieDbSearch.ApiKey) + "&language=en";
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(language))
|
|
|
|
|
{
|
|
|
|
|
// Get images in english and with no language
|
|
|
|
|
url += "&include_image_language=" + MovieDbProvider.GetImageLanguagesParam(language);
|
|
|
|
|
}
|
2014-01-30 14:23:54 -07:00
|
|
|
|
|
|
|
|
|
using (var json = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
AcceptHeader = MovieDbSearch.AcceptHeader
|
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
mainResult = _json.DeserializeFromStream<RootObject>(json);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return mainResult;
|
|
|
|
|
}
|
2014-02-03 10:44:13 -07:00
|
|
|
|
|
|
|
|
|
private readonly Task _cachedTask = Task.FromResult(true);
|
2014-01-30 14:23:54 -07:00
|
|
|
|
internal Task EnsureInfo(string tmdbId, string preferredMetadataLanguage, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var path = GetDataFilePath(_config.ApplicationPaths, tmdbId, preferredMetadataLanguage);
|
|
|
|
|
|
|
|
|
|
var fileInfo = _fileSystem.GetFileSystemInfo(path);
|
|
|
|
|
|
|
|
|
|
if (fileInfo.Exists)
|
|
|
|
|
{
|
|
|
|
|
// If it's recent or automatic updates are enabled, don't re-download
|
2014-10-06 16:58:46 -07:00
|
|
|
|
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 3)
|
2014-01-30 14:23:54 -07:00
|
|
|
|
{
|
2014-02-03 10:44:13 -07:00
|
|
|
|
return _cachedTask;
|
2014-01-30 14:23:54 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DownloadInfo(tmdbId, preferredMetadataLanguage, cancellationToken);
|
|
|
|
|
}
|
2014-02-15 09:36:09 -07:00
|
|
|
|
|
2014-01-30 14:23:54 -07:00
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "TheMovieDb"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetDataFilePath(IApplicationPaths appPaths, string tmdbId, string preferredLanguage)
|
|
|
|
|
{
|
|
|
|
|
var path = GetDataPath(appPaths, tmdbId);
|
|
|
|
|
|
2016-03-04 10:38:51 -07:00
|
|
|
|
var filename = string.Format("all-{0}.json", preferredLanguage ?? string.Empty);
|
2014-01-30 14:23:54 -07:00
|
|
|
|
|
|
|
|
|
return Path.Combine(path, filename);
|
|
|
|
|
}
|
2014-02-15 09:36:09 -07:00
|
|
|
|
|
2014-01-30 14:23:54 -07:00
|
|
|
|
private static string GetDataPath(IApplicationPaths appPaths, string tmdbId)
|
|
|
|
|
{
|
|
|
|
|
var dataPath = GetCollectionsDataPath(appPaths);
|
|
|
|
|
|
|
|
|
|
return Path.Combine(dataPath, tmdbId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetCollectionsDataPath(IApplicationPaths appPaths)
|
|
|
|
|
{
|
2014-02-20 09:37:41 -07:00
|
|
|
|
var dataPath = Path.Combine(appPaths.CachePath, "tmdb-collections");
|
2014-01-30 14:23:54 -07:00
|
|
|
|
|
|
|
|
|
return dataPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class Part
|
|
|
|
|
{
|
|
|
|
|
public string title { get; set; }
|
|
|
|
|
public int id { get; set; }
|
|
|
|
|
public string release_date { get; set; }
|
|
|
|
|
public string poster_path { get; set; }
|
|
|
|
|
public string backdrop_path { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class Backdrop
|
|
|
|
|
{
|
|
|
|
|
public double aspect_ratio { get; set; }
|
|
|
|
|
public string file_path { get; set; }
|
|
|
|
|
public int height { get; set; }
|
|
|
|
|
public string iso_639_1 { get; set; }
|
2014-01-30 14:47:13 -07:00
|
|
|
|
public double vote_average { get; set; }
|
2014-01-30 14:23:54 -07:00
|
|
|
|
public int vote_count { get; set; }
|
|
|
|
|
public int width { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class Poster
|
|
|
|
|
{
|
|
|
|
|
public double aspect_ratio { get; set; }
|
|
|
|
|
public string file_path { get; set; }
|
|
|
|
|
public int height { get; set; }
|
|
|
|
|
public string iso_639_1 { get; set; }
|
2014-01-30 14:47:13 -07:00
|
|
|
|
public double vote_average { get; set; }
|
2014-01-30 14:23:54 -07:00
|
|
|
|
public int vote_count { get; set; }
|
|
|
|
|
public int width { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class Images
|
|
|
|
|
{
|
|
|
|
|
public List<Backdrop> backdrops { get; set; }
|
|
|
|
|
public List<Poster> posters { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class RootObject
|
|
|
|
|
{
|
|
|
|
|
public int id { get; set; }
|
|
|
|
|
public string name { get; set; }
|
|
|
|
|
public string overview { get; set; }
|
|
|
|
|
public string poster_path { get; set; }
|
|
|
|
|
public string backdrop_path { get; set; }
|
|
|
|
|
public List<Part> parts { get; set; }
|
|
|
|
|
public Images images { get; set; }
|
|
|
|
|
}
|
2014-02-19 21:53:15 -07:00
|
|
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-03-01 15:34:27 -07:00
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = MovieDbProvider.Current.MovieDbResourcePool
|
|
|
|
|
});
|
2014-02-19 21:53:15 -07:00
|
|
|
|
}
|
2014-01-30 14:23:54 -07:00
|
|
|
|
}
|
|
|
|
|
}
|