using System.Globalization;
using Jellyfin.Plugin.YandexMusic.Configuration;
using Jellyfin.Plugin.YandexMusic.Yandex;
using MediaBrowser.Model.Entities;
namespace Jellyfin.Plugin.YandexMusic.Providers;
/// Provider id keys used by this plugin.
public static class YandexProviderIds
{
/// Track id.
public const string Track = "YandexMusicTrack";
/// Album id.
public const string Album = "YandexMusicAlbum";
/// Artist id.
public const string Artist = "YandexMusicArtist";
}
/// Shared mapping helpers.
public static class Mapping
{
/// Provider display name.
public const string ProviderName = "Yandex Music";
private static PluginConfiguration Config => Plugin.Instance?.Configuration ?? new PluginConfiguration();
/// Adds composers and lyricists from Yandex track credits.
public static void AddCredits(
MediaBrowser.Controller.Providers.MetadataResult result,
IEnumerable credits)
{
foreach (var name in CreditValues(credits, IsComposerRole).SelectMany(SplitNames))
{
result.AddPerson(new MediaBrowser.Controller.Entities.PersonInfo
{
Name = name,
Type = Jellyfin.Data.Enums.PersonKind.Composer
});
}
foreach (var name in CreditValues(credits, IsLyricistRole).SelectMany(SplitNames))
{
result.AddPerson(new MediaBrowser.Controller.Entities.PersonInfo
{
Name = name,
Type = Jellyfin.Data.Enums.PersonKind.Lyricist
});
}
}
/// Returns credit values whose role matches the predicate.
public static IEnumerable CreditValues(IEnumerable credits, Func role)
=> credits
.Where(c => role(c.Title) && !string.IsNullOrWhiteSpace(c.Value))
.Select(c => c.Value!);
/// Splits a credit value into individual names.
public static IEnumerable SplitNames(string value)
=> value
.Split([',', ';'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Where(v => v.Length > 0);
/// Gets a value indicating whether the credit role is a composer.
public static bool IsComposerRole(string? title)
=> title is not null
&& (title.Contains("музык", StringComparison.OrdinalIgnoreCase)
|| title.Contains("composer", StringComparison.OrdinalIgnoreCase)
|| title.Equals("music", StringComparison.OrdinalIgnoreCase));
/// Gets a value indicating whether the credit role is a lyricist.
public static bool IsLyricistRole(string? title)
=> title is not null
&& (title.Contains("текст", StringComparison.OrdinalIgnoreCase)
|| title.Contains("lyric", StringComparison.OrdinalIgnoreCase)
|| title.Contains("writer", StringComparison.OrdinalIgnoreCase));
/// Gets a value indicating whether the credit role is a label.
public static bool IsLabelRole(string? title)
=> title is not null
&& (title.Contains("лейбл", StringComparison.OrdinalIgnoreCase)
|| title.Contains("label", StringComparison.OrdinalIgnoreCase));
/// Gets a provider id from lookup info.
public static long? GetId(IReadOnlyDictionary? ids, string key)
{
if (ids is null || !ids.TryGetValue(key, out var value) || string.IsNullOrWhiteSpace(value))
{
return null;
}
return long.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var id) ? id : null;
}
/// Gets a provider id from an item.
public static long? GetId(MediaBrowser.Controller.Entities.BaseItem item, string key)
{
var value = item.GetProviderId(key);
return long.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var id) ? id : null;
}
/// Parses a Yandex release date.
public static DateTime? ParseReleaseDate(string? releaseDate)
{
if (string.IsNullOrWhiteSpace(releaseDate))
{
return null;
}
if (DateTime.TryParse(releaseDate, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out var parsed))
{
return parsed.Year is >= 1900 and <= 2100 ? parsed : null;
}
return null;
}
/// Validates a release year.
public static int? ValidYear(int? year)
=> year is >= 1900 and <= 2100 ? year : null;
/// Maps a Yandex album genre to a Jellyfin genre name.
public static async Task GenresAsync(
YandexMusicClient client,
string? albumGenre,
IEnumerable? artistGenres,
CancellationToken cancellationToken)
{
if (!Config.EnableGenres)
{
return [];
}
var result = new List();
var name = await client.GetGenreNameAsync(albumGenre, cancellationToken).ConfigureAwait(false);
if (!string.IsNullOrWhiteSpace(name))
{
result.Add(name);
}
foreach (var genre in (artistGenres ?? []).Take(4))
{
var artistGenre = await client.GetGenreNameAsync(genre, cancellationToken).ConfigureAwait(false);
if (!string.IsNullOrWhiteSpace(artistGenre)
&& !result.Contains(artistGenre, StringComparer.OrdinalIgnoreCase))
{
result.Add(artistGenre);
}
}
return result.ToArray();
}
/// Gets label names.
public static string[] Labels(AlbumDto? album)
{
if (album is null || !Config.EnableLabels)
{
return [];
}
return album.Labels
.Select(l => l.Name)
.Where(n => !string.IsNullOrWhiteSpace(n))
.Select(n => n!)
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToArray();
}
/// Adds a primary remote image when allowed.
public static void AddPrimaryImage(
MediaBrowser.Controller.Providers.MetadataResult result,
string? coverUri,
int size = 1000)
{
if (!Config.EnableImages)
{
return;
}
var url = YandexMusicClient.BuildCoverUrl(coverUri, size);
if (!string.IsNullOrWhiteSpace(url))
{
result.RemoteImages.Add((url, MediaBrowser.Model.Entities.ImageType.Primary));
}
}
/// Adds a remote image of a given type.
public static void AddImage(
MediaBrowser.Controller.Providers.MetadataResult result,
string? coverUri,
MediaBrowser.Model.Entities.ImageType type,
int size = 1000)
{
if (!Config.EnableImages)
{
return;
}
var url = YandexMusicClient.BuildCoverUrl(coverUri, size);
if (!string.IsNullOrWhiteSpace(url))
{
result.RemoteImages.Add((url, type));
}
}
}