Release 1.0.0.10: tag-based matching and metadata normalization
This commit is contained in:
1 parent
106e8f09e0
commit
0dbf00a055
17 files changed
+513
-53
No files matched your search
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public class YandexMusicAudioCustomProvider : ICustomMetadataProvider<Audio>
|
||||
{
|
||||
@@ -17,23 +18,96 @@ public class YandexMusicAudioCustomProvider : ICustomMetadataProvider<Audio>
|
||||
/// <inheritdoc />
|
||||
public Task<ItemUpdateType> FetchAsync(Audio item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
||||
{
|
||||
if (AlbumArtistCache.TryGet(item.Path, out var albumArtists) && albumArtists.Length > 0)
|
||||
var update = ItemUpdateType.None;
|
||||
|
||||
var artists = NormalizeArtists(item.Artists);
|
||||
if (ArtistCache.TryGet(item.Path, out var cachedArtists))
|
||||
{
|
||||
if (!item.AlbumArtists.SequenceEqual(albumArtists, StringComparer.OrdinalIgnoreCase))
|
||||
artists = NormalizeArtists(cachedArtists);
|
||||
}
|
||||
|
||||
if (!artists.SequenceEqual(item.Artists, StringComparer.Ordinal))
|
||||
{
|
||||
item.Artists = artists;
|
||||
update |= ItemUpdateType.MetadataEdit;
|
||||
}
|
||||
|
||||
string[] albumArtists;
|
||||
if (AlbumArtistCache.TryGet(item.Path, out var cachedAlbumArtists))
|
||||
{
|
||||
albumArtists = NormalizeArtists(cachedAlbumArtists);
|
||||
}
|
||||
else
|
||||
{
|
||||
albumArtists = item.AlbumArtists.Count > 0
|
||||
? NormalizeArtists(item.AlbumArtists)
|
||||
: artists.Length > 0 ? [artists[0]] : [];
|
||||
}
|
||||
|
||||
if (!albumArtists.SequenceEqual(item.AlbumArtists, StringComparer.Ordinal))
|
||||
{
|
||||
item.AlbumArtists = albumArtists;
|
||||
update |= ItemUpdateType.MetadataEdit;
|
||||
}
|
||||
|
||||
if (ProviderIdCache.TryGet(item.Path, out var ids))
|
||||
{
|
||||
if (!string.Equals(item.GetProviderId(YandexProviderIds.Track), ids.Track, StringComparison.Ordinal))
|
||||
{
|
||||
item.AlbumArtists = albumArtists;
|
||||
return Task.FromResult(ItemUpdateType.MetadataEdit);
|
||||
item.SetProviderId(YandexProviderIds.Track, ids.Track);
|
||||
update |= ItemUpdateType.MetadataEdit;
|
||||
}
|
||||
|
||||
return Task.FromResult(ItemUpdateType.None);
|
||||
if (!string.IsNullOrWhiteSpace(ids.Album)
|
||||
&& !string.Equals(item.GetProviderId(YandexProviderIds.Album), ids.Album, StringComparison.Ordinal))
|
||||
{
|
||||
item.SetProviderId(YandexProviderIds.Album, ids.Album!);
|
||||
update |= ItemUpdateType.MetadataEdit;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(ids.Artist)
|
||||
&& !string.Equals(item.GetProviderId(YandexProviderIds.Artist), ids.Artist, StringComparison.Ordinal))
|
||||
{
|
||||
item.SetProviderId(YandexProviderIds.Artist, ids.Artist!);
|
||||
update |= ItemUpdateType.MetadataEdit;
|
||||
}
|
||||
}
|
||||
|
||||
if (item.AlbumArtists.Count == 0 && item.Artists.Count > 0)
|
||||
if (GenreCache.TryGet(item.Path, out var genres) && genres.Length > 0)
|
||||
{
|
||||
item.AlbumArtists = [item.Artists[0]];
|
||||
return Task.FromResult(ItemUpdateType.MetadataEdit);
|
||||
var distinctGenres = genres
|
||||
.Select(g => g.Trim())
|
||||
.Where(g => g.Length > 0)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
if (!distinctGenres.SequenceEqual(item.Genres, StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
item.Genres = distinctGenres;
|
||||
update |= ItemUpdateType.MetadataEdit;
|
||||
}
|
||||
}
|
||||
|
||||
return Task.FromResult(ItemUpdateType.None);
|
||||
return Task.FromResult(update);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Splits combined artist strings ("A, B") and removes duplicates. Ampersands are kept
|
||||
/// because they can be part of a single artist name (Miyagi & Andy Panda).
|
||||
/// </summary>
|
||||
public static string[] NormalizeArtists(IEnumerable<string> artists)
|
||||
{
|
||||
var result = new List<string>();
|
||||
foreach (var artist in artists)
|
||||
{
|
||||
foreach (var part in TextMatch.SplitCommaSeparated(artist))
|
||||
{
|
||||
if (!result.Contains(part, StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
result.Add(part);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user