jellyfin-server/MediaBrowser.Providers/Studios/StudioImageProvider.cs

146 lines
4.7 KiB
C#
Raw Normal View History

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;
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;
using System.Net;
2013-12-29 05:32:03 +00:00
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Providers.Studios
{
public class StudioImageProvider : BaseMetadataProvider
{
private readonly IProviderManager _providerManager;
private readonly SemaphoreSlim _resourcePool = new SemaphoreSlim(5, 5);
public StudioImageProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager)
: base(logManager, configurationManager)
{
_providerManager = providerManager;
}
public override bool Supports(BaseItem item)
{
return item is Studio;
}
public override bool RequiresInternet
{
get
{
return true;
}
}
public override ItemUpdateType ItemUpdateType
{
get
{
return ItemUpdateType.ImageUpdate;
}
}
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{
if (!string.IsNullOrEmpty(item.PrimaryImagePath) && item.BackdropImagePaths.Count == 0)
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
{
return "3";
2013-12-29 05:32:03 +00:00
}
}
public override async Task<bool> FetchAsync(BaseItem item, bool force, BaseProviderInfo providerInfo, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(item.PrimaryImagePath) || item.BackdropImagePaths.Count == 0)
2013-12-29 05:32:03 +00:00
{
var images = await _providerManager.GetAvailableRemoteImages(item, cancellationToken, StudiosManualImageProvider.ProviderName).ConfigureAwait(false);
2013-12-29 05:32:03 +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;
}
private async Task DownloadImages(BaseItem item, List<RemoteImageInfo> images, CancellationToken cancellationToken)
2013-12-29 05:32:03 +00:00
{
if (!item.LockedFields.Contains(MetadataFields.Images))
{
cancellationToken.ThrowIfCancellationRequested();
2013-12-29 05:32:03 +00:00
if (!item.HasImage(ImageType.Primary))
{
await SaveImage(item, images, ImageType.Primary, cancellationToken).ConfigureAwait(false);
}
}
2013-12-29 05:32:03 +00:00
if (!item.LockedFields.Contains(MetadataFields.Backdrops))
{
cancellationToken.ThrowIfCancellationRequested();
2013-12-29 05:32:03 +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
}
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
{
try
2013-12-29 05:32:03 +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
{
continue;
2013-12-29 05:32:03 +00:00
}
break;
2013-12-29 05:32:03 +00:00
}
}
}
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.Third; }
}
}
}