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,166 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using Jellyfin.Plugin.YandexMusic.Configuration;
|
||||
using Jellyfin.Plugin.YandexMusic.Matching;
|
||||
using Jellyfin.Plugin.YandexMusic.Yandex;
|
||||
using MediaBrowser.Controller.Lyrics;
|
||||
using MediaBrowser.Controller.Providers;
|
||||
using MediaBrowser.Model.Lyrics;
|
||||
using MediaBrowser.Model.Providers;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.YandexMusic.Providers;
|
||||
|
||||
/// <summary>
|
||||
/// Lyrics provider backed by Yandex Music (requires an OAuth token).
|
||||
/// </summary>
|
||||
public class YandexMusicLyricProvider : ILyricProvider, IHasOrder
|
||||
{
|
||||
private static readonly ConcurrentDictionary<long, (string Text, string Format, DateTime At)> Cache = new();
|
||||
|
||||
private readonly YandexMusicClient _client;
|
||||
private readonly ILogger<YandexMusicLyricProvider> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="YandexMusicLyricProvider"/> class.
|
||||
/// </summary>
|
||||
public YandexMusicLyricProvider(YandexMusicClient client, ILogger<YandexMusicLyricProvider> logger)
|
||||
{
|
||||
_client = client;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Name => Mapping.ProviderName;
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Order => 1;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IEnumerable<RemoteLyricInfo>> SearchAsync(LyricSearchRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var config = Plugin.Instance?.Configuration ?? new PluginConfiguration();
|
||||
if (!config.EnableLyrics || !YandexMusicClient.HasToken)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var trackId = Mapping.GetId(request.ProviderIds, YandexProviderIds.Track);
|
||||
TrackDto? track = null;
|
||||
if (trackId is not null)
|
||||
{
|
||||
track = await _client.GetTrackAsync(trackId.Value, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (track is null)
|
||||
{
|
||||
var artist = request.ArtistNames?.FirstOrDefault() ?? request.AlbumArtistsNames?.FirstOrDefault();
|
||||
var query = string.IsNullOrWhiteSpace(artist)
|
||||
? request.SongName
|
||||
: $"{artist} {request.SongName}";
|
||||
var tracks = await _client.SearchTracksAsync(query ?? string.Empty, cancellationToken).ConfigureAwait(false);
|
||||
var durationMs = request.Duration is null ? 0 : request.Duration.Value / TimeSpan.TicksPerMillisecond;
|
||||
var artists = (IReadOnlyList<string>?)request.ArtistNames
|
||||
?? request.AlbumArtistsNames
|
||||
?? Array.Empty<string>();
|
||||
var match = MatchScoring.FindBestTrack(
|
||||
tracks,
|
||||
request.SongName,
|
||||
artists,
|
||||
durationMs,
|
||||
config.MatchThreshold,
|
||||
config.DurationToleranceSeconds);
|
||||
track = match?.Track;
|
||||
}
|
||||
|
||||
if (track is null)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
if (track.LyricsAvailable is false && track.LyricsInfo is { HasLyrics: false })
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var lyrics = await GetLyricsTextAsync(track.Id, cancellationToken).ConfigureAwait(false);
|
||||
if (lyrics is null)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var (text, format) = lyrics.Value;
|
||||
return
|
||||
[
|
||||
new RemoteLyricInfo
|
||||
{
|
||||
Id = track.Id.ToString(CultureInfo.InvariantCulture),
|
||||
ProviderName = Name,
|
||||
Metadata = new LyricMetadata
|
||||
{
|
||||
Title = track.Title,
|
||||
Artist = string.Join(", ", track.Artists.Select(a => a.Name).Where(n => !string.IsNullOrWhiteSpace(n))),
|
||||
Album = track.Albums.FirstOrDefault()?.Title,
|
||||
Length = request.Duration,
|
||||
IsSynced = string.Equals(format, "lrc", StringComparison.OrdinalIgnoreCase)
|
||||
},
|
||||
Lyrics = new LyricResponse
|
||||
{
|
||||
Stream = new MemoryStream(Encoding.UTF8.GetBytes(text)),
|
||||
Format = format
|
||||
}
|
||||
}
|
||||
];
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
_logger.LogWarning(ex, "Yandex Music lyrics search failed for {Song}", request.SongName);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<LyricResponse?> GetLyricsAsync(string id, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!long.TryParse(id, NumberStyles.Integer, CultureInfo.InvariantCulture, out var trackId))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var lyrics = await GetLyricsTextAsync(trackId, cancellationToken).ConfigureAwait(false);
|
||||
if (lyrics is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new LyricResponse
|
||||
{
|
||||
Stream = new MemoryStream(Encoding.UTF8.GetBytes(lyrics.Value.Text)),
|
||||
Format = lyrics.Value.Format
|
||||
};
|
||||
}
|
||||
|
||||
private async Task<(string Text, string Format)?> GetLyricsTextAsync(long trackId, CancellationToken cancellationToken)
|
||||
{
|
||||
if (Cache.TryGetValue(trackId, out var cached) && DateTime.UtcNow - cached.At < TimeSpan.FromHours(2))
|
||||
{
|
||||
return (cached.Text, cached.Format);
|
||||
}
|
||||
|
||||
foreach (var format in new[] { "LRC", "TEXT" })
|
||||
{
|
||||
var text = await _client.GetLyricsTextAsync(trackId, format, cancellationToken).ConfigureAwait(false);
|
||||
if (!string.IsNullOrWhiteSpace(text))
|
||||
{
|
||||
var normalizedFormat = format == "LRC" ? "lrc" : "txt";
|
||||
Cache[trackId] = (text, normalizedFormat, DateTime.UtcNow);
|
||||
return (text, normalizedFormat);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user