2020-06-19 11:24:13 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 13:03:10 -07:00
|
|
|
using System.Collections.Generic;
|
2020-12-23 05:06:29 -07:00
|
|
|
using System.IO;
|
2020-08-17 10:55:58 -07:00
|
|
|
using System.Net.Http;
|
2020-12-23 05:06:29 -07:00
|
|
|
using System.Text.Json;
|
2019-01-13 12:26:31 -07:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2021-06-19 09:02:33 -07:00
|
|
|
using Jellyfin.Extensions.Json;
|
2020-08-31 10:05:21 -07:00
|
|
|
using MediaBrowser.Common.Net;
|
2014-02-09 14:11:11 -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;
|
|
|
|
|
2020-03-02 10:07:31 -07:00
|
|
|
namespace MediaBrowser.Providers.Plugins.AudioDb
|
2014-02-09 14:11:11 -07:00
|
|
|
{
|
|
|
|
public class AudioDbAlbumImageProvider : IRemoteImageProvider, IHasOrder
|
|
|
|
{
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2020-08-17 10:55:58 -07:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2021-03-08 21:57:38 -07:00
|
|
|
private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
|
2014-02-09 14:11:11 -07:00
|
|
|
|
2020-12-23 05:06:29 -07:00
|
|
|
public AudioDbAlbumImageProvider(IServerConfigurationManager config, IHttpClientFactory httpClientFactory)
|
2014-02-09 14:11:11 -07:00
|
|
|
{
|
|
|
|
_config = config;
|
2020-08-17 10:55:58 -07:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2014-02-09 14:11:11 -07:00
|
|
|
}
|
|
|
|
|
2019-09-10 13:37:53 -07:00
|
|
|
/// <inheritdoc />
|
|
|
|
public string Name => "TheAudioDB";
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
// After embedded and fanart
|
|
|
|
public int Order => 2;
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-09-12 10:26:21 -07:00
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
2014-02-09 14:11:11 -07:00
|
|
|
{
|
|
|
|
return new List<ImageType>
|
|
|
|
{
|
2019-01-07 16:27:46 -07:00
|
|
|
ImageType.Primary,
|
2014-02-09 14:11:11 -07:00
|
|
|
ImageType.Disc
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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 14:11:11 -07:00
|
|
|
{
|
2020-06-06 12:17:49 -07:00
|
|
|
var id = item.GetProviderId(MetadataProvider.MusicBrainzReleaseGroup);
|
2014-02-09 14:11:11 -07:00
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(id))
|
|
|
|
{
|
|
|
|
await AudioDbAlbumProvider.Current.EnsureInfo(id, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
var path = AudioDbAlbumProvider.GetAlbumInfoPath(_config.ApplicationPaths, id);
|
|
|
|
|
2020-12-23 11:24:58 -07:00
|
|
|
await using FileStream jsonStream = File.OpenRead(path);
|
2020-12-24 03:15:12 -07:00
|
|
|
var obj = await JsonSerializer.DeserializeAsync<AudioDbAlbumProvider.RootObject>(jsonStream, _jsonOptions, cancellationToken).ConfigureAwait(false);
|
2014-02-09 14:11:11 -07:00
|
|
|
|
|
|
|
if (obj != null && obj.album != null && obj.album.Count > 0)
|
|
|
|
{
|
|
|
|
return GetImages(obj.album[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new List<RemoteImageInfo>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerable<RemoteImageInfo> GetImages(AudioDbAlbumProvider.Album item)
|
|
|
|
{
|
|
|
|
var list = new List<RemoteImageInfo>();
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(item.strAlbumThumb))
|
|
|
|
{
|
|
|
|
list.Add(new RemoteImageInfo
|
|
|
|
{
|
|
|
|
ProviderName = Name,
|
|
|
|
Url = item.strAlbumThumb,
|
|
|
|
Type = ImageType.Primary
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(item.strAlbumCDart))
|
|
|
|
{
|
|
|
|
list.Add(new RemoteImageInfo
|
|
|
|
{
|
|
|
|
ProviderName = Name,
|
|
|
|
Url = item.strAlbumCDart,
|
|
|
|
Type = ImageType.Disc
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2019-01-07 16:27:46 -07:00
|
|
|
|
2019-09-10 13:37:53 -07:00
|
|
|
/// <inheritdoc />
|
2020-08-17 10:55:58 -07:00
|
|
|
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
2014-02-09 14:11:11 -07:00
|
|
|
{
|
2020-08-31 10:05:21 -07:00
|
|
|
var httpClient = _httpClientFactory.CreateClient(NamedClient.Default);
|
2020-08-17 10:55:58 -07:00
|
|
|
return httpClient.GetAsync(url, cancellationToken);
|
2014-02-09 14:11:11 -07:00
|
|
|
}
|
|
|
|
|
2019-09-10 13:37:53 -07:00
|
|
|
/// <inheritdoc />
|
2020-03-07 20:10:25 -07:00
|
|
|
public bool Supports(BaseItem item)
|
2020-11-28 07:55:56 -07:00
|
|
|
=> item is MusicAlbum;
|
2014-02-09 14:11:11 -07:00
|
|
|
}
|
|
|
|
}
|