2013-12-29 05:32:03 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-12-29 06:36:13 +00:00
|
|
|
|
using MediaBrowser.Model.Net;
|
|
|
|
|
using MediaBrowser.Model.Providers;
|
2013-12-29 05:32:03 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2013-12-29 06:36:13 +00:00
|
|
|
|
using System.Net;
|
2013-12-29 05:32:03 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-12-30 16:32:01 +00:00
|
|
|
|
namespace MediaBrowser.Providers.ImagesByName
|
2013-12-29 05:32:03 +00:00
|
|
|
|
{
|
2013-12-30 16:32:01 +00:00
|
|
|
|
public class GenreImageProvider : BaseMetadataProvider
|
2013-12-29 05:32:03 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IProviderManager _providerManager;
|
|
|
|
|
private readonly SemaphoreSlim _resourcePool = new SemaphoreSlim(5, 5);
|
|
|
|
|
|
2013-12-30 16:32:01 +00:00
|
|
|
|
public GenreImageProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager)
|
2013-12-29 05:32:03 +00:00
|
|
|
|
: base(logManager, configurationManager)
|
|
|
|
|
{
|
|
|
|
|
_providerManager = providerManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Supports(BaseItem item)
|
|
|
|
|
{
|
2013-12-30 16:32:01 +00:00
|
|
|
|
return item is Genre;
|
2013-12-29 05:32:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool RequiresInternet
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override ItemUpdateType ItemUpdateType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return ItemUpdateType.ImageUpdate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
|
|
|
|
|
{
|
2013-12-29 14:54:11 +00:00
|
|
|
|
if (item.HasImage(ImageType.Primary) && item.HasImage(ImageType.Thumb))
|
2013-12-29 05:32:03 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return base.NeedsRefreshInternal(item, providerInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool RefreshOnVersionChange
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override string ProviderVersion
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2013-12-30 16:32:01 +00:00
|
|
|
|
return "6";
|
2013-12-29 05:32:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async Task<bool> FetchAsync(BaseItem item, bool force, BaseProviderInfo providerInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2013-12-29 14:54:11 +00:00
|
|
|
|
if (item.HasImage(ImageType.Primary) && item.HasImage(ImageType.Thumb))
|
2013-12-29 05:32:03 +00:00
|
|
|
|
{
|
2013-12-29 14:54:11 +00:00
|
|
|
|
SetLastRefreshed(item, DateTime.UtcNow, providerInfo);
|
|
|
|
|
return true;
|
2013-12-29 05:32:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-30 16:32:01 +00:00
|
|
|
|
var images = await _providerManager.GetAvailableRemoteImages(item, cancellationToken, GenresManualImageProvider.ProviderName).ConfigureAwait(false);
|
2013-12-29 14:54:11 +00:00
|
|
|
|
|
|
|
|
|
await DownloadImages(item, images.ToList(), cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2013-12-29 05:32:03 +00:00
|
|
|
|
SetLastRefreshed(item, DateTime.UtcNow, providerInfo);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-29 06:36:13 +00:00
|
|
|
|
private async Task DownloadImages(BaseItem item, List<RemoteImageInfo> images, CancellationToken cancellationToken)
|
2013-12-29 05:32:03 +00:00
|
|
|
|
{
|
2013-12-29 06:36:13 +00:00
|
|
|
|
if (!item.LockedFields.Contains(MetadataFields.Images))
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-12-29 05:32:03 +00:00
|
|
|
|
|
2013-12-29 06:36:13 +00:00
|
|
|
|
if (!item.HasImage(ImageType.Primary))
|
|
|
|
|
{
|
|
|
|
|
await SaveImage(item, images, ImageType.Primary, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
2013-12-29 14:54:11 +00:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
if (!item.HasImage(ImageType.Thumb))
|
|
|
|
|
{
|
|
|
|
|
await SaveImage(item, images, ImageType.Thumb, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
2013-12-29 06:36:13 +00:00
|
|
|
|
}
|
2013-12-29 05:32:03 +00:00
|
|
|
|
|
2013-12-29 06:36:13 +00:00
|
|
|
|
if (!item.LockedFields.Contains(MetadataFields.Backdrops))
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-12-29 05:32:03 +00:00
|
|
|
|
|
2013-12-29 06:36:13 +00:00
|
|
|
|
if (item.BackdropImagePaths.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (var image in images.Where(i => i.Type == ImageType.Backdrop))
|
|
|
|
|
{
|
|
|
|
|
await _providerManager.SaveImage(item, image.Url, _resourcePool, ImageType.Backdrop, null, cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-29 05:32:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-12-29 06:36:13 +00:00
|
|
|
|
private async Task SaveImage(BaseItem item, IEnumerable<RemoteImageInfo> images, ImageType type, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
foreach (var image in images.Where(i => i.Type == type))
|
2013-12-29 05:32:03 +00:00
|
|
|
|
{
|
2013-12-29 06:36:13 +00:00
|
|
|
|
try
|
2013-12-29 05:32:03 +00:00
|
|
|
|
{
|
2013-12-29 06:36:13 +00:00
|
|
|
|
await _providerManager.SaveImage(item, image.Url, _resourcePool, 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)
|
2013-12-29 05:32:03 +00:00
|
|
|
|
{
|
2013-12-29 06:36:13 +00:00
|
|
|
|
continue;
|
2013-12-29 05:32:03 +00:00
|
|
|
|
}
|
2013-12-29 06:36:13 +00:00
|
|
|
|
break;
|
2013-12-29 05:32:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override MetadataProviderPriority Priority
|
|
|
|
|
{
|
|
|
|
|
get { return MetadataProviderPriority.Third; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|