jellyfin/MediaBrowser.Providers/Movies/MovieDbImagesProvider.cs

251 lines
8.9 KiB
C#
Raw Normal View History

using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net;
2013-05-13 00:57:51 +02:00
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Library;
2013-06-09 18:47:28 +02:00
using MediaBrowser.Controller.Providers;
2013-05-13 00:57:51 +02:00
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Providers;
2013-05-13 00:57:51 +02:00
using System;
using System.Collections.Generic;
2013-10-22 21:52:33 +02:00
using System.IO;
2013-05-13 00:57:51 +02:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2013-06-09 18:47:28 +02:00
namespace MediaBrowser.Providers.Movies
2013-05-13 00:57:51 +02:00
{
/// <summary>
/// Class MovieDbImagesProvider
/// </summary>
public class MovieDbImagesProvider : BaseMetadataProvider
{
/// <summary>
/// The _provider manager
/// </summary>
private readonly IProviderManager _providerManager;
private readonly IFileSystem _fileSystem;
2013-05-13 00:57:51 +02:00
/// <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>
public MovieDbImagesProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager, IFileSystem fileSystem)
2013-05-13 00:57:51 +02:00
: base(logManager, configurationManager)
{
_providerManager = providerManager;
_fileSystem = fileSystem;
2013-05-13 00:57:51 +02:00
}
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.Fourth; }
2013-05-13 00:57:51 +02: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 22:33:27 +01:00
{
return SupportsItem(item);
}
2013-12-21 19:37:34 +01:00
internal static bool SupportsItem(IHasImages item)
2013-05-13 00:57:51 +02:00
{
var trailer = item as Trailer;
if (trailer != null)
{
return !trailer.IsLocalTrailer;
}
// Don't support local trailers
return item is Movie || item is BoxSet || item is MusicVideo;
2013-05-13 00:57:51 +02:00
}
public override ItemUpdateType ItemUpdateType
{
get
{
return ItemUpdateType.ImageUpdate;
}
}
2013-05-13 00:57:51 +02: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;
}
// Don't refresh if we already have both poster and backdrop and we're not refreshing images
2013-12-26 07:17:19 +01:00
if (item.HasImage(ImageType.Primary) &&
item.BackdropImagePaths.Count >= ConfigurationManager.Configuration.MovieOptions.MaxBackdrops &&
2013-12-15 01:26:01 +01:00
!item.LockedFields.Contains(MetadataFields.Images))
{
return false;
}
2013-05-13 00:57:51 +02:00
return base.NeedsRefreshInternal(item, providerInfo);
}
2013-10-22 21:52:33 +02:00
protected override bool NeedsRefreshBasedOnCompareDate(BaseItem item, BaseProviderInfo providerInfo)
{
var path = MovieDbProvider.Current.GetDataFilePath(item);
2013-10-22 21:52:33 +02:00
if (!string.IsNullOrEmpty(path))
{
var fileInfo = new FileInfo(path);
if (fileInfo.Exists)
{
return _fileSystem.GetLastWriteTimeUtc(fileInfo) > providerInfo.LastRefreshed;
2013-10-22 21:52:33 +02:00
}
}
return false;
}
2013-05-13 00:57:51 +02: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>
2013-12-06 21:07:34 +01:00
public override async Task<bool> FetchAsync(BaseItem item, bool force, BaseProviderInfo providerInfo, CancellationToken cancellationToken)
2013-05-13 00:57:51 +02:00
{
var images = await _providerManager.GetAvailableRemoteImages(item, cancellationToken, ManualMovieDbImageProvider.ProviderName).ConfigureAwait(false);
await ProcessImages(item, images.ToList(), cancellationToken).ConfigureAwait(false);
2013-12-06 21:07:34 +01:00
SetLastRefreshed(item, DateTime.UtcNow, providerInfo);
2013-05-13 00:57:51 +02:00
return true;
}
/// <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>
private async Task ProcessImages(BaseItem item, List<RemoteImageInfo> images, CancellationToken cancellationToken)
2013-05-13 00:57:51 +02:00
{
cancellationToken.ThrowIfCancellationRequested();
var eligiblePosters = images
2013-11-03 21:48:16 +01:00
.Where(i => i.Type == ImageType.Primary)
.ToList();
2013-05-13 00:57:51 +02:00
// poster
if (eligiblePosters.Count > 0 && !item.HasImage(ImageType.Primary) && !item.LockedFields.Contains(MetadataFields.Images))
2013-05-13 00:57:51 +02:00
{
2013-10-30 22:33:27 +01:00
var poster = eligiblePosters[0];
2013-05-13 00:57:51 +02:00
var url = poster.Url;
2013-10-30 22:33:27 +01:00
var img = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions
2013-05-13 00:57:51 +02:00
{
2013-10-30 22:33:27 +01:00
Url = url,
CancellationToken = cancellationToken
2013-10-30 22:33:27 +01:00
}).ConfigureAwait(false);
await _providerManager.SaveImage(item, img, MimeTypes.GetMimeType(url), ImageType.Primary, null, url, cancellationToken)
2013-10-30 22:33:27 +01:00
.ConfigureAwait(false);
2013-05-13 00:57:51 +02:00
}
cancellationToken.ThrowIfCancellationRequested();
var eligibleBackdrops = images
2013-12-26 07:17:19 +01:00
.Where(i => i.Type == ImageType.Backdrop && i.Width.HasValue && i.Width.Value >= ConfigurationManager.Configuration.MovieOptions.MinBackdropWidth)
.ToList();
2013-12-26 07:17:19 +01:00
var backdropLimit = ConfigurationManager.Configuration.MovieOptions.MaxBackdrops;
2013-05-13 01:16:46 +02:00
// backdrops - only download if earlier providers didn't find any (fanart)
if (eligibleBackdrops.Count > 0 &&
ConfigurationManager.Configuration.DownloadMovieImages.Backdrops &&
item.BackdropImagePaths.Count < backdropLimit &&
!item.LockedFields.Contains(MetadataFields.Backdrops))
2013-05-13 00:57:51 +02:00
{
for (var i = 0; i < eligibleBackdrops.Count; i++)
2013-05-13 00:57:51 +02:00
{
var url = eligibleBackdrops[i].Url;
2013-05-21 19:17:01 +02:00
if (!item.ContainsImageWithSourceUrl(url))
2013-05-13 00:57:51 +02:00
{
var img = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions
2013-05-13 00:57:51 +02:00
{
Url = url,
CancellationToken = cancellationToken
}).ConfigureAwait(false);
await _providerManager.SaveImage(item, img, MimeTypes.GetMimeType(url), ImageType.Backdrop, null, url, cancellationToken)
.ConfigureAwait(false);
2013-05-13 00:57:51 +02:00
}
2013-05-31 20:53:45 +02:00
if (item.BackdropImagePaths.Count >= backdropLimit)
2013-05-31 20:53:45 +02:00
{
break;
}
2013-05-13 00:57:51 +02:00
}
}
}
}
}