jellyfin/MediaBrowser.Providers/Plugins/AudioDb/ArtistImageProvider.cs

149 lines
4.7 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
2014-02-09 00:27:44 -07:00
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using MediaBrowser.Model.Serialization;
2020-03-02 10:07:31 -07:00
namespace MediaBrowser.Providers.Plugins.AudioDb
2014-02-09 00:27:44 -07:00
{
public class AudioDbArtistImageProvider : IRemoteImageProvider, IHasOrder
{
private readonly IServerConfigurationManager _config;
private readonly IHttpClientFactory _httpClientFactory;
2014-02-09 00:27:44 -07:00
private readonly IJsonSerializer _json;
public AudioDbArtistImageProvider(IServerConfigurationManager config, IJsonSerializer json, IHttpClientFactory httpClientFactory)
2014-02-09 00:27:44 -07:00
{
_config = config;
_json = json;
_httpClientFactory = httpClientFactory;
2014-02-09 00:27:44 -07:00
}
2019-09-10 13:37:53 -07:00
/// <inheritdoc />
public string Name => "TheAudioDB";
/// <inheritdoc />
// After fanart
public int Order => 1;
/// <inheritdoc />
2018-09-12 10:26:21 -07:00
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
2014-02-09 00:27:44 -07:00
{
return new List<ImageType>
{
2019-01-07 16:27:46 -07:00
ImageType.Primary,
2014-02-09 00:27:44 -07:00
ImageType.Logo,
ImageType.Banner,
ImageType.Backdrop
};
}
2019-09-10 13:37:53 -07:00
/// <inheritdoc />
2018-09-12 10:26:21 -07:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
2014-02-09 00:27:44 -07:00
{
2020-06-06 12:17:49 -07:00
var id = item.GetProviderId(MetadataProvider.MusicBrainzArtist);
2014-02-09 00:27:44 -07:00
if (!string.IsNullOrWhiteSpace(id))
{
await AudioDbArtistProvider.Current.EnsureArtistInfo(id, cancellationToken).ConfigureAwait(false);
var path = AudioDbArtistProvider.GetArtistInfoPath(_config.ApplicationPaths, id);
var obj = _json.DeserializeFromFile<AudioDbArtistProvider.RootObject>(path);
if (obj != null && obj.artists != null && obj.artists.Count > 0)
{
return GetImages(obj.artists[0]);
}
}
return new List<RemoteImageInfo>();
}
private IEnumerable<RemoteImageInfo> GetImages(AudioDbArtistProvider.Artist item)
{
var list = new List<RemoteImageInfo>();
if (!string.IsNullOrWhiteSpace(item.strArtistThumb))
{
list.Add(new RemoteImageInfo
{
ProviderName = Name,
Url = item.strArtistThumb,
Type = ImageType.Primary
});
}
if (!string.IsNullOrWhiteSpace(item.strArtistLogo))
{
list.Add(new RemoteImageInfo
{
ProviderName = Name,
Url = item.strArtistLogo,
Type = ImageType.Logo
});
}
if (!string.IsNullOrWhiteSpace(item.strArtistBanner))
{
list.Add(new RemoteImageInfo
{
ProviderName = Name,
Url = item.strArtistBanner,
Type = ImageType.Banner
});
}
if (!string.IsNullOrWhiteSpace(item.strArtistFanart))
{
list.Add(new RemoteImageInfo
{
ProviderName = Name,
Url = item.strArtistFanart,
Type = ImageType.Backdrop
});
}
if (!string.IsNullOrWhiteSpace(item.strArtistFanart2))
{
list.Add(new RemoteImageInfo
{
ProviderName = Name,
Url = item.strArtistFanart2,
Type = ImageType.Backdrop
});
}
if (!string.IsNullOrWhiteSpace(item.strArtistFanart3))
{
list.Add(new RemoteImageInfo
{
ProviderName = Name,
Url = item.strArtistFanart3,
Type = ImageType.Backdrop
});
}
return list;
}
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
2014-02-09 00:27:44 -07:00
{
var httpClient = _httpClientFactory.CreateClient();
return httpClient.GetAsync(url, cancellationToken);
2014-02-09 00:27:44 -07:00
}
2019-09-10 13:37:53 -07:00
/// <inheritdoc />
public bool Supports(BaseItem item)
=> Plugin.Instance.Configuration.Enable && item is MusicArtist;
2014-02-09 00:27:44 -07:00
}
}