129 lines
4.5 KiB
C#
129 lines
4.5 KiB
C#
using Jellyfin.Data.Enums;
|
|
using Jellyfin.Plugin.YandexMusic.Providers;
|
|
using MediaBrowser.Controller.Dto;
|
|
using MediaBrowser.Controller.Entities;
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
using MediaBrowser.Controller.Library;
|
|
using MediaBrowser.Model.Dto;
|
|
using MediaBrowser.Model.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Jellyfin.Plugin.YandexMusic;
|
|
|
|
/// <summary>
|
|
/// Repairs music metadata links after a Jellyfin 10 -> 12 migration:
|
|
/// fills AlbumArtists, writes Artists/Genres into the new item-value tables and creates person links.
|
|
/// </summary>
|
|
public class AlbumArtistTask : IScheduledTask
|
|
{
|
|
private readonly ILibraryManager _libraryManager;
|
|
private readonly IDtoService _dtoService;
|
|
private readonly ILogger<AlbumArtistTask> _logger;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="AlbumArtistTask"/> class.
|
|
/// </summary>
|
|
public AlbumArtistTask(ILibraryManager libraryManager, IDtoService dtoService, ILogger<AlbumArtistTask> logger)
|
|
{
|
|
_libraryManager = libraryManager;
|
|
_dtoService = dtoService;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public string Name => "Yandex Music: исправить исполнителей и жанры";
|
|
|
|
/// <inheritdoc />
|
|
public string Key => "YandexMusicAlbumArtists";
|
|
|
|
/// <inheritdoc />
|
|
public string Description => "Переносит исполнителей, альбомных исполнителей и жанры в новую модель Jellyfin 12 и создаёт связи артистов.";
|
|
|
|
/// <inheritdoc />
|
|
public string Category => "Yandex Music";
|
|
|
|
/// <inheritdoc />
|
|
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
|
|
{
|
|
var items = _libraryManager.GetItemList(new InternalItemsQuery
|
|
{
|
|
Recursive = true,
|
|
IncludeItemTypes = [BaseItemKind.Audio],
|
|
IsVirtualItem = false
|
|
}).OfType<Audio>().ToList();
|
|
|
|
var dtoOptions = new DtoOptions(true);
|
|
var updated = 0;
|
|
for (var i = 0; i < items.Count; i++)
|
|
{
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
var item = items[i];
|
|
|
|
var dto = _dtoService.GetBaseItemDto(item, dtoOptions);
|
|
var dtoArtists = (dto.Artists ?? [])
|
|
.Where(a => !string.IsNullOrWhiteSpace(a))
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
.ToArray();
|
|
var dtoAlbumArtists = (dto.AlbumArtists ?? [])
|
|
.Select(a => a.Name)
|
|
.Where(n => !string.IsNullOrWhiteSpace(n))
|
|
.Select(n => n!)
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
.ToArray();
|
|
var dtoGenres = (dto.Genres ?? [])
|
|
.Where(g => !string.IsNullOrWhiteSpace(g))
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
.ToArray();
|
|
|
|
if (dtoArtists.Length > 0)
|
|
{
|
|
item.Artists = dtoArtists;
|
|
}
|
|
|
|
if (dtoAlbumArtists.Length > 0)
|
|
{
|
|
item.AlbumArtists = dtoAlbumArtists;
|
|
}
|
|
else if (AlbumArtistCache.TryGet(item.Path, out var cached))
|
|
{
|
|
item.AlbumArtists = cached;
|
|
}
|
|
else if (item.Artists.Count > 0)
|
|
{
|
|
item.AlbumArtists = [item.Artists[0]];
|
|
}
|
|
|
|
if (dtoGenres.Length > 0)
|
|
{
|
|
item.Genres = dtoGenres;
|
|
}
|
|
|
|
await item.UpdateToRepositoryAsync(ItemUpdateType.MetadataImport, cancellationToken).ConfigureAwait(false);
|
|
|
|
var people = new List<PersonInfo>();
|
|
foreach (var name in item.AlbumArtists)
|
|
{
|
|
people.Add(new PersonInfo { Name = name, Type = PersonKind.AlbumArtist });
|
|
}
|
|
|
|
foreach (var name in item.Artists)
|
|
{
|
|
people.Add(new PersonInfo { Name = name, Type = PersonKind.Artist });
|
|
}
|
|
|
|
if (people.Count > 0)
|
|
{
|
|
await _libraryManager.UpdatePeopleAsync(item, people, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
|
|
updated++;
|
|
progress.Report(100.0 * (i + 1) / Math.Max(1, items.Count));
|
|
}
|
|
|
|
_logger.LogInformation("Yandex Music: repaired metadata links for {Updated} tracks", updated);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() => [];
|
|
}
|