114 lines
4.0 KiB
C#
114 lines
4.0 KiB
C#
using Jellyfin.Plugin.YandexMusic.Matching;
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
using MediaBrowser.Controller.Library;
|
|
using MediaBrowser.Controller.Providers;
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
namespace Jellyfin.Plugin.YandexMusic.Providers;
|
|
|
|
/// <summary>
|
|
/// 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>
|
|
{
|
|
/// <inheritdoc />
|
|
public string Name => Mapping.ProviderName;
|
|
|
|
/// <inheritdoc />
|
|
public Task<ItemUpdateType> FetchAsync(Audio item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
|
{
|
|
var update = ItemUpdateType.None;
|
|
|
|
var artists = NormalizeArtists(item.Artists);
|
|
if (ArtistCache.TryGet(item.Path, out var cachedArtists))
|
|
{
|
|
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.SetProviderId(YandexProviderIds.Track, ids.Track);
|
|
update |= ItemUpdateType.MetadataEdit;
|
|
}
|
|
|
|
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 (GenreCache.TryGet(item.Path, out var genres) && genres.Length > 0)
|
|
{
|
|
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(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();
|
|
}
|
|
}
|