From 0dbf00a055708f70189859df994e72323e497691 Mon Sep 17 00:00:00 2001 From: 1xDed Date: Tue, 1 Dec 2026 02:28:18 +0300 Subject: [PATCH] Release 1.0.0.10: tag-based matching and metadata normalization --- .../AlbumArtistTask.cs | 56 +++++++- .../Jellyfin.Plugin.YandexMusic.csproj | 7 +- .../Matching/TextMatch.cs | 49 ++++++- .../Providers/ArtistCache.cs | 36 +++++ .../Providers/AudioTagReader.cs | 75 ++++++++++ .../Providers/GenreCache.cs | 36 +++++ .../Providers/ProviderIdCache.cs | 37 +++++ .../YandexMusicAudioCustomProvider.cs | 96 +++++++++++-- .../Providers/YandexMusicImageProvider.cs | 3 +- .../Providers/YandexMusicLyricProvider.cs | 3 +- .../Providers/YandexMusicSongProvider.cs | 132 +++++++++++++++--- build.yaml | 2 +- dist/Jellyfin.Plugin.YandexMusic_1.0.0.10.zip | Bin 0 -> 300869 bytes dist/Jellyfin.Plugin.YandexMusic_1.0.0.7.zip | Bin 90692 -> 0 bytes dist/manifest.json | 10 +- scripts/package.sh | 2 +- tests/YandexSmoke/Program.cs | 22 +++ 17 files changed, 513 insertions(+), 53 deletions(-) create mode 100644 Jellyfin.Plugin.YandexMusic/Providers/ArtistCache.cs create mode 100644 Jellyfin.Plugin.YandexMusic/Providers/AudioTagReader.cs create mode 100644 Jellyfin.Plugin.YandexMusic/Providers/GenreCache.cs create mode 100644 Jellyfin.Plugin.YandexMusic/Providers/ProviderIdCache.cs create mode 100644 dist/Jellyfin.Plugin.YandexMusic_1.0.0.10.zip delete mode 100644 dist/Jellyfin.Plugin.YandexMusic_1.0.0.7.zip diff --git a/Jellyfin.Plugin.YandexMusic/AlbumArtistTask.cs b/Jellyfin.Plugin.YandexMusic/AlbumArtistTask.cs index 996d1fc..00ef5b4 100644 --- a/Jellyfin.Plugin.YandexMusic/AlbumArtistTask.cs +++ b/Jellyfin.Plugin.YandexMusic/AlbumArtistTask.cs @@ -62,6 +62,8 @@ public class AlbumArtistTask : IScheduledTask var dtoOptions = new DtoOptions(true); var updated = 0; + var artistNames = new HashSet(StringComparer.OrdinalIgnoreCase); + var genreNames = new HashSet(StringComparer.OrdinalIgnoreCase); for (var i = 0; i < items.Count; i++) { cancellationToken.ThrowIfCancellationRequested(); @@ -136,11 +138,19 @@ public class AlbumArtistTask : IScheduledTask if (finalArtists.Length > 0) { item.Artists = finalArtists; + foreach (var name in finalArtists) + { + artistNames.Add(name); + } } if (finalAlbumArtists.Length > 0) { item.AlbumArtists = finalAlbumArtists; + foreach (var name in finalAlbumArtists) + { + artistNames.Add(name); + } } else if (AlbumArtistCache.TryGet(item.Path, out var cached)) { @@ -154,6 +164,10 @@ public class AlbumArtistTask : IScheduledTask if (finalGenres.Length > 0) { item.Genres = finalGenres; + foreach (var name in finalGenres) + { + genreNames.Add(name); + } } await item.UpdateToRepositoryAsync(ItemUpdateType.MetadataImport, cancellationToken).ConfigureAwait(false); @@ -178,7 +192,47 @@ public class AlbumArtistTask : IScheduledTask progress.Report(100.0 * (i + 1) / Math.Max(1, items.Count)); } - _logger.LogInformation("Yandex Music: repaired metadata links for {Updated} tracks", updated); + foreach (var name in artistNames) + { + cancellationToken.ThrowIfCancellationRequested(); + try + { + var artist = _libraryManager.GetArtist(name); + if (artist is not null && artist.DateLastRefreshed == default) + { + artist.DateLastRefreshed = DateTime.UtcNow; + await artist.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, cancellationToken).ConfigureAwait(false); + } + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + _logger.LogWarning(ex, "Yandex Music: failed to prepare artist {Artist}", name); + } + } + + foreach (var name in genreNames) + { + cancellationToken.ThrowIfCancellationRequested(); + try + { + var genre = _libraryManager.GetMusicGenre(name); + if (genre is not null && genre.DateLastRefreshed == default) + { + genre.DateLastRefreshed = DateTime.UtcNow; + await genre.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, cancellationToken).ConfigureAwait(false); + } + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + _logger.LogWarning(ex, "Yandex Music: failed to prepare genre {Genre}", name); + } + } + + _logger.LogInformation( + "Yandex Music: repaired metadata links for {Updated} tracks, prepared {Artists} artists and {Genres} genres", + updated, + artistNames.Count, + genreNames.Count); } /// diff --git a/Jellyfin.Plugin.YandexMusic/Jellyfin.Plugin.YandexMusic.csproj b/Jellyfin.Plugin.YandexMusic/Jellyfin.Plugin.YandexMusic.csproj index b14fc39..9adba43 100644 --- a/Jellyfin.Plugin.YandexMusic/Jellyfin.Plugin.YandexMusic.csproj +++ b/Jellyfin.Plugin.YandexMusic/Jellyfin.Plugin.YandexMusic.csproj @@ -1,9 +1,9 @@ - 1.0.0.7 - 1.0.0.7 - 1.0.0.7 + 1.0.0.10 + 1.0.0.10 + 1.0.0.10 net10.0 Jellyfin.Plugin.YandexMusic Jellyfin.Plugin.YandexMusic @@ -21,6 +21,7 @@ runtime + diff --git a/Jellyfin.Plugin.YandexMusic/Matching/TextMatch.cs b/Jellyfin.Plugin.YandexMusic/Matching/TextMatch.cs index 2a69f07..93dfdb8 100644 --- a/Jellyfin.Plugin.YandexMusic/Matching/TextMatch.cs +++ b/Jellyfin.Plugin.YandexMusic/Matching/TextMatch.cs @@ -81,6 +81,23 @@ public static partial class TextMatch return best; } + /// Splits a combined artist string on commas. + public static IEnumerable SplitCommaSeparated(string? value) + { + if (string.IsNullOrWhiteSpace(value)) + { + yield break; + } + + foreach (var part in value.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) + { + if (part.Length > 0) + { + yield return part; + } + } + } + /// Splits a combined artist string ("A, B & C feat. D") into individual names. public static IEnumerable SplitArtists(string? value) { @@ -183,10 +200,15 @@ public static class MatchScoring IReadOnlyList artists, long durationMs, double threshold, - int toleranceSeconds) + int toleranceSeconds, + string? album = null) { (TrackDto Track, double Score)? best = null; var hasArtists = artists.Any(a => !string.IsNullOrWhiteSpace(a)); + var artistParts = artists + .SelectMany(TextMatch.SplitArtists) + .Where(a => !string.IsNullOrWhiteSpace(a)) + .ToArray(); foreach (var track in tracks) { var titleScore = TextMatch.Similarity(title, track.Title); @@ -194,6 +216,18 @@ public static class MatchScoring ? TextMatch.ArtistSimilarity(artists, track.Artists.Select(a => a.Name)) : 1; var durationScore = DurationScore(durationMs, track.DurationMs, toleranceSeconds); + var albumScore = 0.5; + if (!string.IsNullOrWhiteSpace(album)) + { + var candidateAlbum = track.Albums.FirstOrDefault()?.Title; + albumScore = string.IsNullOrWhiteSpace(candidateAlbum) + ? 0 + : TextMatch.Similarity(album, candidateAlbum); + } + + var matchedArtistCount = hasArtists + ? artistParts.Count(a => track.Artists.Any(c => TextMatch.Similarity(a, c.Name) >= 0.7)) + : 0; double score; if (hasArtists) @@ -204,22 +238,25 @@ public static class MatchScoring } score = durationScore is null - ? (0.6 * titleScore) + (0.4 * artistScore) - : (0.5 * titleScore) + (0.3 * artistScore) + (0.2 * durationScore.Value); + ? (0.5 * titleScore) + (0.3 * artistScore) + (0.2 * albumScore) + : (0.45 * titleScore) + (0.25 * artistScore) + (0.2 * durationScore.Value) + (0.1 * albumScore); } else { - // No artist tags at all: rely on the title and, when available, the duration. + // No artist tags at all: rely on the title and, when available, the duration and album. if (titleScore < 0.8) { continue; } score = durationScore is null - ? titleScore - : (0.65 * titleScore) + (0.35 * durationScore.Value); + ? (0.75 * titleScore) + (0.25 * albumScore) + : (0.55 * titleScore) + (0.3 * durationScore.Value) + (0.15 * albumScore); } + // Prefer a result that matches more of the listed artists when the other signals are equal. + score += 0.001 * matchedArtistCount; + if (best is null || score > best.Value.Score) { best = (track, score); diff --git a/Jellyfin.Plugin.YandexMusic/Providers/ArtistCache.cs b/Jellyfin.Plugin.YandexMusic/Providers/ArtistCache.cs new file mode 100644 index 0000000..92fe986 --- /dev/null +++ b/Jellyfin.Plugin.YandexMusic/Providers/ArtistCache.cs @@ -0,0 +1,36 @@ +using System.Collections.Concurrent; + +namespace Jellyfin.Plugin.YandexMusic.Providers; + +/// +/// Artists resolved from Yandex for an audio file. Applied after the merge so stale or +/// combined artist names from tags do not accumulate between refreshes. +/// +public static class ArtistCache +{ + private static readonly ConcurrentDictionary Cache = new(StringComparer.Ordinal); + + /// Stores artists for an audio file path. + public static void Set(string? path, string[] artists) + { + if (string.IsNullOrWhiteSpace(path) || artists.Length == 0) + { + return; + } + + Cache[path] = artists; + } + + /// Gets artists for an audio file path. + public static bool TryGet(string? path, out string[] artists) + { + if (!string.IsNullOrWhiteSpace(path) && Cache.TryGetValue(path, out var value)) + { + artists = value; + return true; + } + + artists = []; + return false; + } +} diff --git a/Jellyfin.Plugin.YandexMusic/Providers/AudioTagReader.cs b/Jellyfin.Plugin.YandexMusic/Providers/AudioTagReader.cs new file mode 100644 index 0000000..312ad11 --- /dev/null +++ b/Jellyfin.Plugin.YandexMusic/Providers/AudioTagReader.cs @@ -0,0 +1,75 @@ +namespace Jellyfin.Plugin.YandexMusic.Providers; + +/// +/// Reads the original embedded tags directly from the file, so matching is not affected by +/// metadata previously written into Jellyfin's database. +/// +public static class AudioTagReader +{ + /// Embedded tag values relevant for matching. + public sealed record Tags( + string? Title, + string[] Artists, + string[] AlbumArtists, + string? Album, + int? Year, + string[] Genres, + int? Track); + + /// Reads tags from a media file. Returns null when the file cannot be read. + public static Tags? Read(string? path) + { + if (string.IsNullOrWhiteSpace(path) || !File.Exists(path)) + { + return null; + } + + try + { + using var file = TagLib.File.Create(path); + var tag = file.Tag; + var artists = (tag.Performers ?? []) + .Where(a => !string.IsNullOrWhiteSpace(a)) + .Select(a => a.Trim()) + .ToArray(); + var albumArtists = (tag.AlbumArtists ?? []) + .Where(a => !string.IsNullOrWhiteSpace(a)) + .Select(a => a.Trim()) + .ToArray(); + + return new Tags( + NullIfEmpty(tag.Title), + artists, + albumArtists, + NullIfEmpty(tag.Album), + tag.Year == 0 ? null : (int)tag.Year, + (tag.Genres ?? []).Where(g => !string.IsNullOrWhiteSpace(g)).ToArray(), + tag.Track == 0 ? null : (int)tag.Track); + } + catch (Exception) + { + return null; + } + } + + /// Parses "Artist - Title" from a file name when tags are missing. + public static (string? Artist, string? Title) ParseFileName(string? path) + { + if (string.IsNullOrWhiteSpace(path)) + { + return (null, null); + } + + var name = Path.GetFileNameWithoutExtension(path); + var index = name.LastIndexOf(" - ", StringComparison.Ordinal); + if (index <= 0 || index + 3 >= name.Length) + { + return (null, name); + } + + return (name[..index].Trim(), name[(index + 3)..].Trim()); + } + + private static string? NullIfEmpty(string? value) + => string.IsNullOrWhiteSpace(value) ? null : value.Trim(); +} diff --git a/Jellyfin.Plugin.YandexMusic/Providers/GenreCache.cs b/Jellyfin.Plugin.YandexMusic/Providers/GenreCache.cs new file mode 100644 index 0000000..aa05dcc --- /dev/null +++ b/Jellyfin.Plugin.YandexMusic/Providers/GenreCache.cs @@ -0,0 +1,36 @@ +using System.Collections.Concurrent; + +namespace Jellyfin.Plugin.YandexMusic.Providers; + +/// +/// Normalized genres resolved by the song provider, applied after Jellyfin's metadata merge +/// so raw tag ids (rusrap, pop, ...) do not stick to the item. +/// +public static class GenreCache +{ + private static readonly ConcurrentDictionary Cache = new(StringComparer.Ordinal); + + /// Stores genres for an audio file path. + public static void Set(string? path, string[] genres) + { + if (string.IsNullOrWhiteSpace(path) || genres.Length == 0) + { + return; + } + + Cache[path] = genres; + } + + /// Gets genres for an audio file path. + public static bool TryGet(string? path, out string[] genres) + { + if (!string.IsNullOrWhiteSpace(path) && Cache.TryGetValue(path, out var value)) + { + genres = value; + return true; + } + + genres = []; + return false; + } +} diff --git a/Jellyfin.Plugin.YandexMusic/Providers/ProviderIdCache.cs b/Jellyfin.Plugin.YandexMusic/Providers/ProviderIdCache.cs new file mode 100644 index 0000000..919a9cb --- /dev/null +++ b/Jellyfin.Plugin.YandexMusic/Providers/ProviderIdCache.cs @@ -0,0 +1,37 @@ +using System.Collections.Concurrent; + +namespace Jellyfin.Plugin.YandexMusic.Providers; + +/// +/// Provider ids resolved by the song provider. Jellyfin keeps existing ids during a normal +/// refresh, so a previously wrong match can stick; the custom provider applies the resolved ids. +/// +public static class ProviderIdCache +{ + private static readonly ConcurrentDictionary Cache = + new(StringComparer.Ordinal); + + /// Stores ids for an audio file path. + public static void Set(string? path, string track, string? album, string? artist) + { + if (string.IsNullOrWhiteSpace(path) || string.IsNullOrWhiteSpace(track)) + { + return; + } + + Cache[path] = (track, album, artist); + } + + /// Gets ids for an audio file path. + public static bool TryGet(string? path, out (string Track, string? Album, string? Artist) ids) + { + if (!string.IsNullOrWhiteSpace(path) && Cache.TryGetValue(path, out var value)) + { + ids = value; + return true; + } + + ids = default; + return false; + } +} diff --git a/Jellyfin.Plugin.YandexMusic/Providers/YandexMusicAudioCustomProvider.cs b/Jellyfin.Plugin.YandexMusic/Providers/YandexMusicAudioCustomProvider.cs index e01a80a..d7cb9b6 100644 --- a/Jellyfin.Plugin.YandexMusic/Providers/YandexMusicAudioCustomProvider.cs +++ b/Jellyfin.Plugin.YandexMusic/Providers/YandexMusicAudioCustomProvider.cs @@ -1,3 +1,4 @@ +using Jellyfin.Plugin.YandexMusic.Matching; using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Providers; @@ -6,8 +7,8 @@ using MediaBrowser.Model.Entities; namespace Jellyfin.Plugin.YandexMusic.Providers; /// -/// Runs after the standard metadata merge and fixes fields that the Audio metadata service -/// does not copy from a remote provider (most importantly AlbumArtists). +/// Runs after the standard metadata merge and fixes fields the Audio metadata service +/// either does not copy or merges badly: AlbumArtists, duplicated artist names and raw genre ids. /// public class YandexMusicAudioCustomProvider : ICustomMetadataProvider