157 lines
5.7 KiB
C#
157 lines
5.7 KiB
C#
using System.Globalization;
|
|
using Jellyfin.Plugin.YandexMusic.Configuration;
|
|
using Jellyfin.Plugin.YandexMusic.Matching;
|
|
using Jellyfin.Plugin.YandexMusic.Yandex;
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
using MediaBrowser.Controller.Providers;
|
|
using MediaBrowser.Model.Entities;
|
|
using MediaBrowser.Model.Providers;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Jellyfin.Plugin.YandexMusic.Providers;
|
|
|
|
/// <summary>
|
|
/// Metadata provider for music artists.
|
|
/// </summary>
|
|
public class YandexMusicArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>, IHasOrder
|
|
{
|
|
private readonly YandexMusicClient _client;
|
|
private readonly ILogger<YandexMusicArtistProvider> _logger;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="YandexMusicArtistProvider"/> class.
|
|
/// </summary>
|
|
public YandexMusicArtistProvider(YandexMusicClient client, ILogger<YandexMusicArtistProvider> logger)
|
|
{
|
|
_client = client;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public string Name => Mapping.ProviderName;
|
|
|
|
/// <inheritdoc />
|
|
public int Order => 1;
|
|
|
|
/// <inheritdoc />
|
|
public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo info, CancellationToken cancellationToken)
|
|
{
|
|
var result = new MetadataResult<MusicArtist> { Item = new MusicArtist() };
|
|
var config = Plugin.Instance?.Configuration ?? new PluginConfiguration();
|
|
if (!config.EnableArtistMetadata)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
try
|
|
{
|
|
var artist = await ResolveArtistAsync(info, cancellationToken).ConfigureAwait(false);
|
|
if (artist is null)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
var details = await _client.GetArtistInfoAsync(artist.Id, cancellationToken).ConfigureAwait(false);
|
|
var item = result.Item;
|
|
item.Name = artist.Name ?? info.Name;
|
|
item.Genres = await Mapping.GenresAsync(_client, null, artist.Genres, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
if (!string.IsNullOrWhiteSpace(details?.Description))
|
|
{
|
|
item.Overview = details!.Description;
|
|
}
|
|
else if (!string.IsNullOrWhiteSpace(artist.Description))
|
|
{
|
|
item.Overview = artist.Description;
|
|
}
|
|
|
|
item.SetProviderId(YandexProviderIds.Artist, artist.Id.ToString(CultureInfo.InvariantCulture));
|
|
|
|
var covers = new List<string?>();
|
|
if (details?.Covers is { Count: > 0 })
|
|
{
|
|
covers.AddRange(details.Covers.Select(c => c.Uri));
|
|
}
|
|
|
|
covers.Add(artist.Cover?.Uri);
|
|
covers.Add(artist.OgImage);
|
|
covers = covers.Where(c => !string.IsNullOrWhiteSpace(c)).Distinct(StringComparer.Ordinal).ToList();
|
|
|
|
if (covers.Count > 0)
|
|
{
|
|
Mapping.AddPrimaryImage(result, covers[0], 1200);
|
|
foreach (var cover in covers.Skip(1).Take(4))
|
|
{
|
|
Mapping.AddImage(result, cover, ImageType.Backdrop, 1920);
|
|
}
|
|
}
|
|
|
|
result.HasMetadata = true;
|
|
result.QueriedById = Mapping.GetId(info.ProviderIds, YandexProviderIds.Artist) is not null;
|
|
}
|
|
catch (Exception ex) when (ex is not OperationCanceledException)
|
|
{
|
|
_logger.LogWarning(ex, "Yandex Music failed to provide artist metadata for {Name}", info.Name);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken)
|
|
{
|
|
var results = new List<RemoteSearchResult>();
|
|
try
|
|
{
|
|
var artists = await _client.SearchArtistsAsync(searchInfo.Name ?? string.Empty, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
foreach (var artist in artists)
|
|
{
|
|
results.Add(new RemoteSearchResult
|
|
{
|
|
Name = artist.Name,
|
|
ImageUrl = YandexMusicClient.BuildCoverUrl(artist.Cover?.Uri ?? artist.OgImage, 300),
|
|
ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
|
|
{
|
|
[YandexProviderIds.Artist] = artist.Id.ToString(CultureInfo.InvariantCulture)
|
|
},
|
|
SearchProviderName = Name
|
|
});
|
|
}
|
|
}
|
|
catch (Exception ex) when (ex is not OperationCanceledException)
|
|
{
|
|
_logger.LogWarning(ex, "Yandex Music artist search failed for {Name}", searchInfo.Name);
|
|
}
|
|
|
|
return results;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
=> ImageResponseFactory.FromUrlAsync(_client, url, cancellationToken);
|
|
|
|
private async Task<ArtistDto?> ResolveArtistAsync(ArtistInfo info, CancellationToken cancellationToken)
|
|
{
|
|
var artistId = Mapping.GetId(info.ProviderIds, YandexProviderIds.Artist);
|
|
if (artistId is not null)
|
|
{
|
|
var byId = await _client.GetArtistAsync(artistId.Value, cancellationToken).ConfigureAwait(false);
|
|
if (byId is not null)
|
|
{
|
|
return byId;
|
|
}
|
|
}
|
|
|
|
var artists = await _client.SearchArtistsAsync(info.Name ?? string.Empty, cancellationToken).ConfigureAwait(false);
|
|
var match = MatchScoring.FindBestArtist(artists, info.Name);
|
|
if (match is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return await _client.GetArtistAsync(match.Id, cancellationToken).ConfigureAwait(false) ?? match;
|
|
}
|
|
}
|