Yandex Music metadata plugin for Jellyfin 12.4
This commit is contained in:
commit
c3dc901337
25 files changed
+3233
No files matched your search
@@ -0,0 +1,173 @@
|
||||
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 albums.
|
||||
/// </summary>
|
||||
public class YandexMusicAlbumProvider : IRemoteMetadataProvider<MusicAlbum, AlbumInfo>, IHasOrder
|
||||
{
|
||||
private readonly YandexMusicClient _client;
|
||||
private readonly ILogger<YandexMusicAlbumProvider> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="YandexMusicAlbumProvider"/> class.
|
||||
/// </summary>
|
||||
public YandexMusicAlbumProvider(YandexMusicClient client, ILogger<YandexMusicAlbumProvider> logger)
|
||||
{
|
||||
_client = client;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Name => Mapping.ProviderName;
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Order => 1;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<MetadataResult<MusicAlbum>> GetMetadata(AlbumInfo info, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = new MetadataResult<MusicAlbum> { Item = new MusicAlbum() };
|
||||
var config = Plugin.Instance?.Configuration ?? new PluginConfiguration();
|
||||
if (!config.EnableAlbumMetadata)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var album = await ResolveAlbumAsync(info, config, cancellationToken).ConfigureAwait(false);
|
||||
if (album is null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
var artists = album.Artists
|
||||
.Select(a => a.Name)
|
||||
.Where(n => !string.IsNullOrWhiteSpace(n))
|
||||
.Select(n => n!)
|
||||
.ToArray();
|
||||
if (artists.Length == 0)
|
||||
{
|
||||
artists = info.AlbumArtists.ToArray();
|
||||
}
|
||||
|
||||
var item = result.Item;
|
||||
if (!string.IsNullOrWhiteSpace(album.Title))
|
||||
{
|
||||
item.Name = album.Title!;
|
||||
}
|
||||
|
||||
item.Artists = artists;
|
||||
item.AlbumArtists = artists;
|
||||
item.ProductionYear = Mapping.ValidYear(album.Year) ?? info.Year;
|
||||
item.PremiereDate = Mapping.ParseReleaseDate(album.ReleaseDate);
|
||||
item.Genres = await Mapping.GenresAsync(_client, album.Genre, null, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var labels = Mapping.Labels(album);
|
||||
if (labels.Length > 0)
|
||||
{
|
||||
item.Studios = labels;
|
||||
}
|
||||
|
||||
item.SetProviderId(YandexProviderIds.Album, album.Id.ToString(CultureInfo.InvariantCulture));
|
||||
var primaryArtist = album.Artists.FirstOrDefault(a => a.Id > 0);
|
||||
if (primaryArtist is not null)
|
||||
{
|
||||
item.SetProviderId(YandexProviderIds.Artist, primaryArtist.Id.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
Mapping.AddPrimaryImage(result, album.CoverUri ?? album.OgImage, 1200);
|
||||
result.HasMetadata = true;
|
||||
result.QueriedById = Mapping.GetId(info.ProviderIds, YandexProviderIds.Album) is not null;
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
_logger.LogWarning(ex, "Yandex Music failed to provide album metadata for {Name}", info.Name);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(AlbumInfo searchInfo, CancellationToken cancellationToken)
|
||||
{
|
||||
var results = new List<RemoteSearchResult>();
|
||||
try
|
||||
{
|
||||
var albums = await SearchAlbumsAsync(searchInfo, cancellationToken).ConfigureAwait(false);
|
||||
foreach (var album in albums)
|
||||
{
|
||||
results.Add(new RemoteSearchResult
|
||||
{
|
||||
Name = album.Title,
|
||||
ProductionYear = album.Year,
|
||||
Artists = album.Artists
|
||||
.Select(a => new RemoteSearchResult { Name = a.Name })
|
||||
.ToArray(),
|
||||
ImageUrl = YandexMusicClient.BuildCoverUrl(album.CoverUri, 300),
|
||||
ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
[YandexProviderIds.Album] = album.Id.ToString(CultureInfo.InvariantCulture)
|
||||
},
|
||||
SearchProviderName = Name
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
_logger.LogWarning(ex, "Yandex Music album 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<AlbumDto?> ResolveAlbumAsync(AlbumInfo info, PluginConfiguration config, CancellationToken cancellationToken)
|
||||
{
|
||||
var albumId = Mapping.GetId(info.ProviderIds, YandexProviderIds.Album);
|
||||
if (albumId is not null)
|
||||
{
|
||||
var byId = await _client.GetAlbumAsync(albumId.Value, cancellationToken).ConfigureAwait(false);
|
||||
if (byId is not null)
|
||||
{
|
||||
return byId;
|
||||
}
|
||||
}
|
||||
|
||||
var albums = await SearchAlbumsAsync(info, cancellationToken).ConfigureAwait(false);
|
||||
var match = MatchScoring.FindBestAlbum(
|
||||
albums,
|
||||
info.Name,
|
||||
info.AlbumArtists,
|
||||
info.Year,
|
||||
config.MatchThreshold);
|
||||
return match?.Album;
|
||||
}
|
||||
|
||||
private async Task<List<AlbumDto>> SearchAlbumsAsync(AlbumInfo info, CancellationToken cancellationToken)
|
||||
{
|
||||
var artist = info.AlbumArtists.FirstOrDefault();
|
||||
var query = string.IsNullOrWhiteSpace(artist) ? info.Name : $"{artist} {info.Name}";
|
||||
var albums = await _client.SearchAlbumsAsync(query, cancellationToken).ConfigureAwait(false);
|
||||
if (albums.Count == 0 && !string.IsNullOrWhiteSpace(info.Name))
|
||||
{
|
||||
albums = await _client.SearchAlbumsAsync(info.Name, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return albums;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user