2013-12-08 19:38:32 +01:00
|
|
|
|
using System.Net;
|
|
|
|
|
using MediaBrowser.Common.IO;
|
2013-10-31 15:03:23 +01:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-02-25 01:13:45 +01:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2013-03-03 07:58:04 +01:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2013-06-09 18:47:28 +02:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-03-02 18:59:15 +01:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-12-08 19:38:32 +01:00
|
|
|
|
using MediaBrowser.Model.Net;
|
2013-11-04 20:04:23 +01:00
|
|
|
|
using MediaBrowser.Model.Providers;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using System;
|
2013-11-04 20:04:23 +01:00
|
|
|
|
using System.Collections.Generic;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using System.IO;
|
2013-11-04 20:04:23 +01:00
|
|
|
|
using System.Linq;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-06-09 18:47:28 +02:00
|
|
|
|
namespace MediaBrowser.Providers.TV
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class RemoteSeasonProvider
|
|
|
|
|
/// </summary>
|
2013-11-04 21:49:36 +01:00
|
|
|
|
class TvdbSeasonProvider : BaseMetadataProvider
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-05-21 05:16:43 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _provider manager
|
|
|
|
|
/// </summary>
|
2013-03-08 06:08:27 +01:00
|
|
|
|
private readonly IProviderManager _providerManager;
|
2013-10-31 15:03:23 +01:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-05-21 05:16:43 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-11-04 21:49:36 +01:00
|
|
|
|
/// Initializes a new instance of the <see cref="TvdbSeasonProvider"/> class.
|
2013-05-21 05:16:43 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <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-11-04 21:49:36 +01:00
|
|
|
|
public TvdbSeasonProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager, IFileSystem fileSystem)
|
2013-03-04 06:43:06 +01:00
|
|
|
|
: base(logManager, configurationManager)
|
2013-02-25 01:13:45 +01:00
|
|
|
|
{
|
2013-03-08 06:08:27 +01:00
|
|
|
|
_providerManager = providerManager;
|
2013-10-31 15:03:23 +01:00
|
|
|
|
_fileSystem = fileSystem;
|
2013-02-25 01:13:45 +01:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01: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 Season;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the priority.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The priority.</value>
|
|
|
|
|
public override MetadataProviderPriority Priority
|
|
|
|
|
{
|
2013-05-13 01:36:42 +02:00
|
|
|
|
// Run after fanart
|
|
|
|
|
get { return MetadataProviderPriority.Fourth; }
|
2013-02-21 02:33:05 +01: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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-25 03:22:21 +02:00
|
|
|
|
public override ItemUpdateType ItemUpdateType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return ItemUpdateType.ImageUpdate;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-15 19:34:48 +02:00
|
|
|
|
|
2013-05-21 05:16:43 +02: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
|
|
|
|
|
{
|
2013-09-15 19:34:48 +02:00
|
|
|
|
return "2";
|
2013-05-21 05:16:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-17 01:35:11 +02:00
|
|
|
|
protected override bool NeedsRefreshBasedOnCompareDate(BaseItem item, BaseProviderInfo providerInfo)
|
2013-05-21 05:16:43 +02:00
|
|
|
|
{
|
|
|
|
|
var season = (Season)item;
|
|
|
|
|
var seriesId = season.Series != null ? season.Series.GetProviderId(MetadataProviders.Tvdb) : null;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(seriesId))
|
|
|
|
|
{
|
|
|
|
|
// Process images
|
2013-11-04 21:49:36 +01:00
|
|
|
|
var imagesXmlPath = Path.Combine(TvdbSeriesProvider.GetSeriesDataPath(ConfigurationManager.ApplicationPaths, seriesId), "banners.xml");
|
2013-05-21 05:16:43 +02:00
|
|
|
|
|
|
|
|
|
var imagesFileInfo = new FileInfo(imagesXmlPath);
|
|
|
|
|
|
2013-06-20 03:41:50 +02:00
|
|
|
|
if (imagesFileInfo.Exists)
|
|
|
|
|
{
|
2013-10-31 15:03:23 +01:00
|
|
|
|
return _fileSystem.GetLastWriteTimeUtc(imagesFileInfo) > providerInfo.LastRefreshed;
|
2013-06-20 03:41:50 +02:00
|
|
|
|
}
|
2013-05-21 05:16:43 +02:00
|
|
|
|
}
|
2013-10-17 01:35:11 +02:00
|
|
|
|
return false;
|
2013-05-21 05:16:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 21:03:21 +02:00
|
|
|
|
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
|
|
|
|
|
{
|
|
|
|
|
if (item.HasImage(ImageType.Primary) && item.HasImage(ImageType.Banner) && item.BackdropImagePaths.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return base.NeedsRefreshInternal(item, providerInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01: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-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-02-25 01:13:45 +01:00
|
|
|
|
|
2013-12-15 19:33:07 +01:00
|
|
|
|
var images = await _providerManager.GetAvailableRemoteImages(item, cancellationToken, ManualTvdbSeasonImageProvider.ProviderName).ConfigureAwait(false);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-12-15 19:33:07 +01:00
|
|
|
|
const int backdropLimit = 1;
|
2013-05-21 05:16:43 +02:00
|
|
|
|
|
2013-12-15 19:33:07 +01:00
|
|
|
|
await DownloadImages(item, images.ToList(), backdropLimit, cancellationToken).ConfigureAwait(false);
|
2013-09-15 19:34:48 +02:00
|
|
|
|
|
2013-12-06 21:07:34 +01:00
|
|
|
|
SetLastRefreshed(item, DateTime.UtcNow, providerInfo);
|
2013-11-04 20:04:23 +01:00
|
|
|
|
return true;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-04 20:04:23 +01:00
|
|
|
|
private async Task DownloadImages(BaseItem item, List<RemoteImageInfo> images, int backdropLimit, CancellationToken cancellationToken)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-12-15 19:33:07 +01:00
|
|
|
|
if (!item.LockedFields.Contains(MetadataFields.Images))
|
2013-09-15 19:34:48 +02:00
|
|
|
|
{
|
2013-12-15 19:33:07 +01:00
|
|
|
|
if (!item.HasImage(ImageType.Primary))
|
|
|
|
|
{
|
|
|
|
|
await SaveImage(item, images, ImageType.Primary, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
2013-09-15 19:34:48 +02:00
|
|
|
|
|
2013-12-15 19:33:07 +01:00
|
|
|
|
if (ConfigurationManager.Configuration.DownloadSeasonImages.Banner && !item.HasImage(ImageType.Banner))
|
|
|
|
|
{
|
|
|
|
|
await SaveImage(item, images, ImageType.Banner, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
2013-05-13 01:16:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-15 19:33:07 +01:00
|
|
|
|
if (ConfigurationManager.Configuration.DownloadSeasonImages.Backdrops && item.BackdropImagePaths.Count < backdropLimit && !item.LockedFields.Contains(MetadataFields.Backdrops))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-11-04 20:04:23 +01:00
|
|
|
|
foreach (var backdrop in images.Where(i => i.Type == ImageType.Backdrop))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-11-04 20:04:23 +01:00
|
|
|
|
var url = backdrop.Url;
|
2013-10-22 21:03:21 +02:00
|
|
|
|
|
|
|
|
|
if (item.ContainsImageWithSourceUrl(url))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-11-07 17:48:23 +01:00
|
|
|
|
await _providerManager.SaveImage(item, url, TvdbSeriesProvider.Current.TvDbResourcePool, ImageType.Backdrop, null, cancellationToken).ConfigureAwait(false);
|
2013-09-15 19:34:48 +02:00
|
|
|
|
|
|
|
|
|
if (item.BackdropImagePaths.Count >= backdropLimit) break;
|
2013-05-13 01:16:46 +02:00
|
|
|
|
}
|
2013-05-21 05:16:43 +02:00
|
|
|
|
}
|
2013-09-15 19:34:48 +02:00
|
|
|
|
}
|
2013-12-08 19:38:32 +01:00
|
|
|
|
|
|
|
|
|
private async Task SaveImage(BaseItem item, List<RemoteImageInfo> images, ImageType type, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
foreach (var image in images.Where(i => i.Type == type))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _providerManager.SaveImage(item, image.Url, TvdbSeriesProvider.Current.TvDbResourcePool, type, null, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
catch (HttpException ex)
|
|
|
|
|
{
|
|
|
|
|
// Sometimes fanart has bad url's in their xml
|
|
|
|
|
if (ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.NotFound)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|