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,216 @@
|
||||
using System.Globalization;
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using Jellyfin.Plugin.YandexMusic.Configuration;
|
||||
using Jellyfin.Plugin.YandexMusic.Matching;
|
||||
using Jellyfin.Plugin.YandexMusic.Yandex;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
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>
|
||||
/// Cover art provider.
|
||||
/// </summary>
|
||||
public class YandexMusicImageProvider : IRemoteImageProvider, IHasOrder
|
||||
{
|
||||
private readonly YandexMusicClient _client;
|
||||
private readonly ILogger<YandexMusicImageProvider> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="YandexMusicImageProvider"/> class.
|
||||
/// </summary>
|
||||
public YandexMusicImageProvider(YandexMusicClient client, ILogger<YandexMusicImageProvider> logger)
|
||||
{
|
||||
_client = client;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Name => Mapping.ProviderName;
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Order => 1;
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool Supports(BaseItem item) => item is Audio or MusicAlbum or MusicArtist;
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
||||
=> item is MusicArtist ? [ImageType.Primary, ImageType.Backdrop] : [ImageType.Primary];
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
|
||||
{
|
||||
var config = Plugin.Instance?.Configuration ?? new PluginConfiguration();
|
||||
if (!config.EnableImages)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var covers = item switch
|
||||
{
|
||||
Audio audio => await GetAudioCoversAsync(audio, config, cancellationToken).ConfigureAwait(false),
|
||||
MusicAlbum album => await GetAlbumCoversAsync(album, config, cancellationToken).ConfigureAwait(false),
|
||||
MusicArtist artist => await GetArtistCoversAsync(artist, cancellationToken).ConfigureAwait(false),
|
||||
_ => new List<(string Url, ImageType Type, int Size)>()
|
||||
};
|
||||
|
||||
return covers.Select(c => new RemoteImageInfo
|
||||
{
|
||||
ProviderName = Name,
|
||||
Url = c.Url,
|
||||
Type = c.Type,
|
||||
Width = c.Size,
|
||||
Height = c.Size
|
||||
}).ToArray();
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
_logger.LogWarning(ex, "Yandex Music failed to find images for {Name}", item.Name);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
||||
{
|
||||
var bytes = await _client.GetImageBytesAsync(url, cancellationToken).ConfigureAwait(false);
|
||||
if (bytes is null)
|
||||
{
|
||||
return new HttpResponseMessage(HttpStatusCode.NotFound);
|
||||
}
|
||||
|
||||
var content = new ByteArrayContent(bytes);
|
||||
content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
|
||||
return new HttpResponseMessage(HttpStatusCode.OK) { Content = content };
|
||||
}
|
||||
|
||||
private async Task<List<(string Url, ImageType Type, int Size)>> GetAudioCoversAsync(
|
||||
Audio audio,
|
||||
PluginConfiguration config,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var trackId = Mapping.GetId(audio, YandexProviderIds.Track);
|
||||
if (trackId is not null)
|
||||
{
|
||||
var track = await _client.GetTrackAsync(trackId.Value, cancellationToken).ConfigureAwait(false);
|
||||
if (track is not null)
|
||||
{
|
||||
return Collect(track.CoverUri ?? track.Albums.FirstOrDefault()?.CoverUri, ImageType.Primary);
|
||||
}
|
||||
}
|
||||
|
||||
var albumId = Mapping.GetId(audio, YandexProviderIds.Album);
|
||||
if (albumId is not null)
|
||||
{
|
||||
var album = await _client.GetAlbumAsync(albumId.Value, cancellationToken).ConfigureAwait(false);
|
||||
if (album is not null)
|
||||
{
|
||||
return Collect(album.CoverUri, ImageType.Primary);
|
||||
}
|
||||
}
|
||||
|
||||
var tracks = await _client.SearchTracksAsync($"{audio.Artists.FirstOrDefault()} {audio.Name}", cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
var match = MatchScoring.FindBestTrack(
|
||||
tracks,
|
||||
audio.Name,
|
||||
audio.Artists,
|
||||
MatchScoring.TicksToMilliseconds(audio.RunTimeTicks),
|
||||
config.MatchThreshold,
|
||||
config.DurationToleranceSeconds);
|
||||
return match is null
|
||||
? []
|
||||
: Collect(match.Value.Track.CoverUri ?? match.Value.Track.Albums.FirstOrDefault()?.CoverUri, ImageType.Primary);
|
||||
}
|
||||
|
||||
private async Task<List<(string Url, ImageType Type, int Size)>> GetAlbumCoversAsync(
|
||||
MusicAlbum album,
|
||||
PluginConfiguration config,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var albumId = Mapping.GetId(album, YandexProviderIds.Album);
|
||||
if (albumId is not null)
|
||||
{
|
||||
var byId = await _client.GetAlbumAsync(albumId.Value, cancellationToken).ConfigureAwait(false);
|
||||
if (byId is not null)
|
||||
{
|
||||
return Collect(byId.CoverUri, ImageType.Primary, 1200);
|
||||
}
|
||||
}
|
||||
|
||||
var artist = album.AlbumArtists.FirstOrDefault();
|
||||
var query = string.IsNullOrWhiteSpace(artist) ? album.Name : $"{artist} {album.Name}";
|
||||
var albums = await _client.SearchAlbumsAsync(query, cancellationToken).ConfigureAwait(false);
|
||||
var match = MatchScoring.FindBestAlbum(
|
||||
albums,
|
||||
album.Name,
|
||||
album.AlbumArtists,
|
||||
album.ProductionYear,
|
||||
config.MatchThreshold);
|
||||
return match is null
|
||||
? []
|
||||
: Collect(match.Value.Album.CoverUri, ImageType.Primary, 1200);
|
||||
}
|
||||
|
||||
private async Task<List<(string Url, ImageType Type, int Size)>> GetArtistCoversAsync(
|
||||
MusicArtist artist,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var artistId = Mapping.GetId(artist, YandexProviderIds.Artist);
|
||||
ArtistDto? dto = null;
|
||||
if (artistId is not null)
|
||||
{
|
||||
dto = await _client.GetArtistAsync(artistId.Value, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
var artists = await _client.SearchArtistsAsync(artist.Name, cancellationToken).ConfigureAwait(false);
|
||||
dto = MatchScoring.FindBestArtist(artists, artist.Name);
|
||||
}
|
||||
|
||||
if (dto is null)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var covers = new List<(string Url, ImageType Type, int Size)>();
|
||||
var details = await _client.GetArtistInfoAsync(dto.Id, cancellationToken).ConfigureAwait(false);
|
||||
var uris = new List<string?>();
|
||||
if (details?.Covers is { Count: > 0 })
|
||||
{
|
||||
uris.AddRange(details.Covers.Select(c => c.Uri));
|
||||
}
|
||||
|
||||
uris.Add(dto.Cover?.Uri);
|
||||
uris.Add(dto.OgImage);
|
||||
|
||||
var primaryAdded = false;
|
||||
foreach (var uri in uris.Where(u => !string.IsNullOrWhiteSpace(u)).Distinct(StringComparer.Ordinal))
|
||||
{
|
||||
var url = YandexMusicClient.BuildCoverUrl(uri, primaryAdded ? 1920 : 1200);
|
||||
if (url is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
covers.Add((url, primaryAdded ? ImageType.Backdrop : ImageType.Primary, primaryAdded ? 1920 : 1200));
|
||||
primaryAdded = true;
|
||||
}
|
||||
|
||||
return covers;
|
||||
}
|
||||
|
||||
private static List<(string Url, ImageType Type, int Size)> Collect(string? coverUri, ImageType type, int size = 1000)
|
||||
{
|
||||
var url = YandexMusicClient.BuildCoverUrl(coverUri, size);
|
||||
return url is null ? [] : [(url, type, size)];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user