2013-09-04 17:02:19 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-05-12 22:57:51 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
2013-06-25 01:22:21 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2013-06-09 16:47:28 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2013-05-12 22:57:51 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2013-10-22 19:52:33 +00:00
|
|
|
|
using System.IO;
|
2013-05-12 22:57:51 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-06-09 16:47:28 +00:00
|
|
|
|
namespace MediaBrowser.Providers.Movies
|
2013-05-12 22:57:51 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class MovieDbImagesProvider
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class MovieDbImagesProvider : BaseMetadataProvider
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The get images
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const string GetImages = @"http://api.themoviedb.org/3/{2}/{0}/images?api_key={1}";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _provider manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IProviderManager _providerManager;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _json serializer
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="MovieDbImagesProvider"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="logManager">The log manager.</param>
|
|
|
|
|
/// <param name="configurationManager">The configuration manager.</param>
|
|
|
|
|
/// <param name="providerManager">The provider manager.</param>
|
|
|
|
|
/// <param name="jsonSerializer">The json serializer.</param>
|
2013-09-04 17:02:19 +00:00
|
|
|
|
public MovieDbImagesProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager, IJsonSerializer jsonSerializer)
|
2013-05-12 22:57:51 +00:00
|
|
|
|
: base(logManager, configurationManager)
|
|
|
|
|
{
|
|
|
|
|
_providerManager = providerManager;
|
|
|
|
|
_jsonSerializer = jsonSerializer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the priority.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The priority.</value>
|
|
|
|
|
public override MetadataProviderPriority Priority
|
|
|
|
|
{
|
2013-10-21 14:38:48 +00:00
|
|
|
|
get { return MetadataProviderPriority.Fourth; }
|
2013-05-12 22:57:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Supports the specified item.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
|
|
|
|
|
public override bool Supports(BaseItem item)
|
2013-10-30 21:33:27 +00:00
|
|
|
|
{
|
|
|
|
|
return SupportsItem(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool SupportsItem(BaseItem item)
|
2013-05-12 22:57:51 +00:00
|
|
|
|
{
|
|
|
|
|
var trailer = item as Trailer;
|
|
|
|
|
|
|
|
|
|
if (trailer != null)
|
|
|
|
|
{
|
|
|
|
|
return !trailer.IsLocalTrailer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Don't support local trailers
|
2013-05-28 01:59:26 +00:00
|
|
|
|
return item is Movie || item is BoxSet || item is MusicVideo;
|
2013-05-12 22:57:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-25 01:22:21 +00:00
|
|
|
|
public override ItemUpdateType ItemUpdateType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return ItemUpdateType.ImageUpdate;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2013-05-12 22:57:51 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating whether [requires internet].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value><c>true</c> if [requires internet]; otherwise, <c>false</c>.</value>
|
|
|
|
|
public override bool RequiresInternet
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating whether [refresh on version change].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value><c>true</c> if [refresh on version change]; otherwise, <c>false</c>.</value>
|
|
|
|
|
protected override bool RefreshOnVersionChange
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the provider version.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The provider version.</value>
|
|
|
|
|
protected override string ProviderVersion
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return "3";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Needses the refresh internal.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="providerInfo">The provider info.</param>
|
|
|
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
|
|
|
|
|
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Tmdb)))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-14 15:13:57 +00:00
|
|
|
|
// Don't refresh if we already have both poster and backdrop and we're not refreshing images
|
2013-10-22 19:03:21 +00:00
|
|
|
|
if (item.HasImage(ImageType.Primary) && item.BackdropImagePaths.Count >= ConfigurationManager.Configuration.MaxBackdrops)
|
2013-05-14 15:13:57 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2013-05-12 22:57:51 +00:00
|
|
|
|
return base.NeedsRefreshInternal(item, providerInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:52:33 +00:00
|
|
|
|
protected override bool NeedsRefreshBasedOnCompareDate(BaseItem item, BaseProviderInfo providerInfo)
|
|
|
|
|
{
|
2013-10-23 01:11:56 +00:00
|
|
|
|
if (string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Tmdb)))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:52:33 +00:00
|
|
|
|
var path = MovieDbProvider.Current.GetDataFilePath(item, "default");
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
var fileInfo = new FileInfo(path);
|
|
|
|
|
|
|
|
|
|
if (fileInfo.Exists)
|
|
|
|
|
{
|
|
|
|
|
return fileInfo.LastWriteTimeUtc > providerInfo.LastRefreshed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-12 22:57:51 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="force">if set to <c>true</c> [force].</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token</param>
|
|
|
|
|
/// <returns>Task{System.Boolean}.</returns>
|
|
|
|
|
public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2013-10-23 01:11:56 +00:00
|
|
|
|
var id = item.GetProviderId(MetadataProviders.Tmdb);
|
2013-10-22 19:52:33 +00:00
|
|
|
|
|
|
|
|
|
var status = ProviderRefreshStatus.Success;
|
2013-05-12 22:57:51 +00:00
|
|
|
|
|
2013-10-23 01:11:56 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(id))
|
2013-05-12 22:57:51 +00:00
|
|
|
|
{
|
2013-10-30 21:33:27 +00:00
|
|
|
|
var images = FetchImages(item, _jsonSerializer);
|
2013-10-23 01:11:56 +00:00
|
|
|
|
|
|
|
|
|
if (images != null)
|
|
|
|
|
{
|
|
|
|
|
status = await ProcessImages(item, images, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
2013-05-12 22:57:51 +00:00
|
|
|
|
}
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2013-05-12 22:57:51 +00:00
|
|
|
|
SetLastRefreshed(item, DateTime.UtcNow, status);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches the images.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
2013-10-30 21:33:27 +00:00
|
|
|
|
/// <param name="jsonSerializer">The json serializer.</param>
|
2013-05-12 22:57:51 +00:00
|
|
|
|
/// <returns>Task{MovieImages}.</returns>
|
2013-10-30 21:33:27 +00:00
|
|
|
|
internal static MovieDbProvider.Images FetchImages(BaseItem item, IJsonSerializer jsonSerializer)
|
2013-05-12 22:57:51 +00:00
|
|
|
|
{
|
2013-10-22 19:52:33 +00:00
|
|
|
|
var path = MovieDbProvider.Current.GetDataFilePath(item, "default");
|
2013-05-12 22:57:51 +00:00
|
|
|
|
|
2013-10-22 19:52:33 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(path))
|
2013-05-12 22:57:51 +00:00
|
|
|
|
{
|
2013-10-22 19:52:33 +00:00
|
|
|
|
var fileInfo = new FileInfo(path);
|
|
|
|
|
|
|
|
|
|
if (fileInfo.Exists)
|
|
|
|
|
{
|
2013-10-30 21:33:27 +00:00
|
|
|
|
return jsonSerializer.DeserializeFromFile<MovieDbProvider.CompleteMovieData>(path).images;
|
2013-10-22 19:52:33 +00:00
|
|
|
|
}
|
2013-05-12 22:57:51 +00:00
|
|
|
|
}
|
2013-10-22 19:52:33 +00:00
|
|
|
|
|
|
|
|
|
return null;
|
2013-05-12 22:57:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Processes the images.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="images">The images.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2013-10-22 19:52:33 +00:00
|
|
|
|
private async Task<ProviderRefreshStatus> ProcessImages(BaseItem item, MovieDbProvider.Images images, CancellationToken cancellationToken)
|
2013-05-12 22:57:51 +00:00
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
var status = ProviderRefreshStatus.Success;
|
|
|
|
|
|
2013-10-30 21:33:27 +00:00
|
|
|
|
var eligiblePosters = new ManualMovieDbImageProvider(_jsonSerializer, ConfigurationManager).GetPosters(images, item)
|
2013-10-21 14:38:48 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
|
2013-05-12 22:57:51 +00:00
|
|
|
|
// poster
|
2013-10-21 14:38:48 +00:00
|
|
|
|
if (eligiblePosters.Count > 0 && !item.HasImage(ImageType.Primary))
|
2013-05-12 22:57:51 +00:00
|
|
|
|
{
|
2013-05-19 19:37:52 +00:00
|
|
|
|
var tmdbSettings = await MovieDbProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
|
2013-05-12 22:57:51 +00:00
|
|
|
|
|
2013-10-21 01:03:33 +00:00
|
|
|
|
var tmdbImageUrl = tmdbSettings.images.base_url + "original";
|
2013-05-12 22:57:51 +00:00
|
|
|
|
// get highest rated poster for our language
|
|
|
|
|
|
2013-10-30 21:33:27 +00:00
|
|
|
|
var poster = eligiblePosters[0];
|
2013-05-12 22:57:51 +00:00
|
|
|
|
|
2013-10-30 21:33:27 +00:00
|
|
|
|
var url = tmdbImageUrl + poster.file_path;
|
2013-10-22 12:37:11 +00:00
|
|
|
|
|
2013-10-30 21:33:27 +00:00
|
|
|
|
var img = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions
|
2013-05-12 22:57:51 +00:00
|
|
|
|
{
|
2013-10-30 21:33:27 +00:00
|
|
|
|
Url = url,
|
|
|
|
|
CancellationToken = cancellationToken
|
2013-10-22 12:37:11 +00:00
|
|
|
|
|
2013-10-30 21:33:27 +00:00
|
|
|
|
}).ConfigureAwait(false);
|
2013-05-19 18:42:58 +00:00
|
|
|
|
|
2013-10-30 21:33:27 +00:00
|
|
|
|
await _providerManager.SaveImage(item, img, MimeTypes.GetMimeType(poster.file_path), ImageType.Primary, null, url, cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
2013-05-12 22:57:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2013-10-30 21:33:27 +00:00
|
|
|
|
var eligibleBackdrops = new ManualMovieDbImageProvider(_jsonSerializer, ConfigurationManager).GetBackdrops(images, item).ToList();
|
2013-10-21 14:38:48 +00:00
|
|
|
|
|
2013-10-22 19:03:21 +00:00
|
|
|
|
var backdropLimit = ConfigurationManager.Configuration.MaxBackdrops;
|
|
|
|
|
|
2013-05-12 23:16:46 +00:00
|
|
|
|
// backdrops - only download if earlier providers didn't find any (fanart)
|
2013-10-22 19:03:21 +00:00
|
|
|
|
if (eligibleBackdrops.Count > 0 && ConfigurationManager.Configuration.DownloadMovieImages.Backdrops && item.BackdropImagePaths.Count < backdropLimit)
|
2013-05-12 22:57:51 +00:00
|
|
|
|
{
|
2013-05-19 19:37:52 +00:00
|
|
|
|
var tmdbSettings = await MovieDbProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
|
2013-05-12 22:57:51 +00:00
|
|
|
|
|
2013-10-21 01:03:33 +00:00
|
|
|
|
var tmdbImageUrl = tmdbSettings.images.base_url + "original";
|
2013-05-21 17:17:01 +00:00
|
|
|
|
|
2013-10-21 14:38:48 +00:00
|
|
|
|
for (var i = 0; i < eligibleBackdrops.Count; i++)
|
2013-05-12 22:57:51 +00:00
|
|
|
|
{
|
2013-10-22 19:03:21 +00:00
|
|
|
|
var url = tmdbImageUrl + eligibleBackdrops[i].file_path;
|
2013-05-21 17:17:01 +00:00
|
|
|
|
|
2013-10-22 19:03:21 +00:00
|
|
|
|
if (!item.ContainsImageWithSourceUrl(url))
|
2013-05-12 22:57:51 +00:00
|
|
|
|
{
|
2013-05-19 18:42:58 +00:00
|
|
|
|
var img = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions
|
2013-05-12 22:57:51 +00:00
|
|
|
|
{
|
2013-10-22 19:03:21 +00:00
|
|
|
|
Url = url,
|
2013-05-19 18:42:58 +00:00
|
|
|
|
CancellationToken = cancellationToken
|
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
|
2013-10-22 19:03:21 +00:00
|
|
|
|
await _providerManager.SaveImage(item, img, MimeTypes.GetMimeType(eligibleBackdrops[i].file_path), ImageType.Backdrop, item.BackdropImagePaths.Count, url, cancellationToken)
|
2013-06-28 20:25:58 +00:00
|
|
|
|
.ConfigureAwait(false);
|
2013-05-12 22:57:51 +00:00
|
|
|
|
}
|
2013-05-31 18:53:45 +00:00
|
|
|
|
|
2013-10-22 19:03:21 +00:00
|
|
|
|
if (item.BackdropImagePaths.Count >= backdropLimit)
|
2013-05-31 18:53:45 +00:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-05-12 22:57:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|