2013-10-31 14:03:23 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-05-24 15:29:10 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
2013-06-09 16:47:28 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2013-05-24 15:29:10 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-11-04 20:49:36 +00:00
|
|
|
|
using MediaBrowser.Model.Providers;
|
2013-05-24 15:29:10 +00:00
|
|
|
|
using System;
|
2013-09-15 17:34:48 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-05-24 15:29:10 +00:00
|
|
|
|
using System.IO;
|
2013-10-26 04:12:20 +00:00
|
|
|
|
using System.Linq;
|
2013-05-24 15:29:10 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-06-09 16:47:28 +00:00
|
|
|
|
namespace MediaBrowser.Providers.TV
|
2013-05-24 15:29:10 +00:00
|
|
|
|
{
|
|
|
|
|
public class TvdbSeriesImageProvider : BaseMetadataProvider
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the HTTP client.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The HTTP client.</value>
|
|
|
|
|
protected IHttpClient HttpClient { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _provider manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IProviderManager _providerManager;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-05-24 15:29:10 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="TvdbSeriesImageProvider"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="httpClient">The HTTP client.</param>
|
|
|
|
|
/// <param name="logManager">The log manager.</param>
|
|
|
|
|
/// <param name="configurationManager">The configuration manager.</param>
|
|
|
|
|
/// <param name="providerManager">The provider manager.</param>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">httpClient</exception>
|
2013-10-31 14:03:23 +00:00
|
|
|
|
public TvdbSeriesImageProvider(IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager, IFileSystem fileSystem)
|
2013-05-24 15:29:10 +00:00
|
|
|
|
: base(logManager, configurationManager)
|
|
|
|
|
{
|
|
|
|
|
if (httpClient == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("httpClient");
|
|
|
|
|
}
|
|
|
|
|
HttpClient = httpClient;
|
|
|
|
|
_providerManager = providerManager;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2013-05-24 15:29:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Supportses 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)
|
|
|
|
|
{
|
|
|
|
|
return item is Series;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the priority.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The priority.</value>
|
|
|
|
|
public override MetadataProviderPriority Priority
|
|
|
|
|
{
|
|
|
|
|
// Run after fanart
|
|
|
|
|
get { return MetadataProviderPriority.Fourth; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-25 01:22:21 +00:00
|
|
|
|
public override ItemUpdateType ItemUpdateType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return ItemUpdateType.ImageUpdate;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-15 17:34:48 +00:00
|
|
|
|
|
2013-05-24 15:29:10 +00:00
|
|
|
|
/// <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 "1";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:48:49 +00:00
|
|
|
|
protected override DateTime CompareDate(BaseItem item)
|
2013-05-24 15:29:10 +00:00
|
|
|
|
{
|
|
|
|
|
var seriesId = item.GetProviderId(MetadataProviders.Tvdb);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(seriesId))
|
|
|
|
|
{
|
|
|
|
|
// Process images
|
2013-11-04 20:49:36 +00:00
|
|
|
|
var imagesXmlPath = Path.Combine(TvdbSeriesProvider.GetSeriesDataPath(ConfigurationManager.ApplicationPaths, seriesId), "banners.xml");
|
2013-05-24 15:29:10 +00:00
|
|
|
|
|
|
|
|
|
var imagesFileInfo = new FileInfo(imagesXmlPath);
|
|
|
|
|
|
2013-06-20 14:48:49 +00:00
|
|
|
|
if (imagesFileInfo.Exists)
|
|
|
|
|
{
|
2013-10-31 14:03:23 +00:00
|
|
|
|
return _fileSystem.GetLastWriteTimeUtc(imagesFileInfo);
|
2013-06-20 14:48:49 +00:00
|
|
|
|
}
|
2013-05-24 15:29:10 +00:00
|
|
|
|
}
|
2013-09-15 17:34:48 +00:00
|
|
|
|
|
2013-06-20 14:48:49 +00:00
|
|
|
|
return base.CompareDate(item);
|
2013-05-24 15:29:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:03:21 +00:00
|
|
|
|
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
|
|
|
|
|
{
|
|
|
|
|
if (item.HasImage(ImageType.Primary) && item.HasImage(ImageType.Banner) && item.BackdropImagePaths.Count >= ConfigurationManager.Configuration.MaxBackdrops)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return base.NeedsRefreshInternal(item, providerInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 15:29:10 +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)
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2013-11-04 20:49:36 +00:00
|
|
|
|
var images = await _providerManager.GetAvailableRemoteImages(item, cancellationToken, ManualTvdbSeriesImageProvider.ProviderName).ConfigureAwait(false);
|
2013-05-24 15:29:10 +00:00
|
|
|
|
|
2013-11-04 20:49:36 +00:00
|
|
|
|
const int backdropLimit = 1;
|
2013-05-24 15:29:10 +00:00
|
|
|
|
|
2013-11-04 20:49:36 +00:00
|
|
|
|
await DownloadImages(item, images.ToList(), backdropLimit, cancellationToken).ConfigureAwait(false);
|
2013-05-24 15:29:10 +00:00
|
|
|
|
|
2013-11-04 20:49:36 +00:00
|
|
|
|
SetLastRefreshed(item, DateTime.UtcNow);
|
|
|
|
|
return true;
|
2013-05-24 15:29:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-04 20:49:36 +00:00
|
|
|
|
private async Task DownloadImages(BaseItem item, List<RemoteImageInfo> images, int backdropLimit, CancellationToken cancellationToken)
|
2013-05-24 15:29:10 +00:00
|
|
|
|
{
|
2013-09-15 17:34:48 +00:00
|
|
|
|
if (!item.HasImage(ImageType.Primary))
|
2013-05-24 15:29:10 +00:00
|
|
|
|
{
|
2013-11-04 20:49:36 +00:00
|
|
|
|
var image = images.FirstOrDefault(i => i.Type == ImageType.Primary);
|
2013-06-01 22:35:35 +00:00
|
|
|
|
|
2013-11-04 20:49:36 +00:00
|
|
|
|
if (image != null)
|
|
|
|
|
{
|
|
|
|
|
await _providerManager.SaveImage(item, image.Url, TvdbSeriesProvider.Current.TvDbResourcePool, ImageType.Primary, null, cancellationToken)
|
2013-09-15 17:34:48 +00:00
|
|
|
|
.ConfigureAwait(false);
|
2013-05-24 15:29:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
if (ConfigurationManager.Configuration.DownloadSeriesImages.Banner && !item.HasImage(ImageType.Banner))
|
2013-05-24 15:29:10 +00:00
|
|
|
|
{
|
2013-11-04 20:49:36 +00:00
|
|
|
|
var image = images.FirstOrDefault(i => i.Type == ImageType.Banner);
|
2013-05-24 15:29:10 +00:00
|
|
|
|
|
2013-11-04 20:49:36 +00:00
|
|
|
|
if (image != null)
|
|
|
|
|
{
|
|
|
|
|
await _providerManager.SaveImage(item, image.Url, TvdbSeriesProvider.Current.TvDbResourcePool, ImageType.Banner, null, cancellationToken)
|
2013-09-15 17:34:48 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:03:21 +00:00
|
|
|
|
if (ConfigurationManager.Configuration.DownloadSeriesImages.Backdrops && item.BackdropImagePaths.Count < backdropLimit)
|
2013-09-15 17:34:48 +00:00
|
|
|
|
{
|
|
|
|
|
var bdNo = item.BackdropImagePaths.Count;
|
|
|
|
|
|
2013-11-04 20:49:36 +00:00
|
|
|
|
foreach (var backdrop in images.Where(i => i.Type == ImageType.Backdrop &&
|
|
|
|
|
(!i.Width.HasValue ||
|
|
|
|
|
i.Width.Value >= ConfigurationManager.Configuration.MinSeriesBackdropDownloadWidth)))
|
2013-09-15 17:34:48 +00:00
|
|
|
|
{
|
2013-11-04 20:49:36 +00:00
|
|
|
|
var url = backdrop.Url;
|
2013-10-22 19:03:21 +00:00
|
|
|
|
|
|
|
|
|
if (item.ContainsImageWithSourceUrl(url))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2013-09-15 17:34:48 +00:00
|
|
|
|
|
2013-11-04 20:49:36 +00:00
|
|
|
|
await _providerManager.SaveImage(item, url, TvdbSeriesProvider.Current.TvDbResourcePool, ImageType.Backdrop, bdNo, cancellationToken).ConfigureAwait(false);
|
2013-09-15 17:34:48 +00:00
|
|
|
|
|
|
|
|
|
bdNo++;
|
|
|
|
|
|
|
|
|
|
if (item.BackdropImagePaths.Count >= backdropLimit) break;
|
2013-05-24 15:29:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-15 17:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-24 15:29:10 +00:00
|
|
|
|
}
|