Yandex Music metadata plugin for Jellyfin 12.4
This commit is contained in:
commit
c3dc901337
25 files changed
+3233
No files matched your search
@@ -0,0 +1,151 @@
|
||||
using System.Globalization;
|
||||
using Jellyfin.Plugin.YandexMusic.Configuration;
|
||||
using Jellyfin.Plugin.YandexMusic.Yandex;
|
||||
using MediaBrowser.Model.Entities;
|
||||
|
||||
namespace Jellyfin.Plugin.YandexMusic.Providers;
|
||||
|
||||
/// <summary>Provider id keys used by this plugin.</summary>
|
||||
public static class YandexProviderIds
|
||||
{
|
||||
/// <summary>Track id.</summary>
|
||||
public const string Track = "YandexMusicTrack";
|
||||
|
||||
/// <summary>Album id.</summary>
|
||||
public const string Album = "YandexMusicAlbum";
|
||||
|
||||
/// <summary>Artist id.</summary>
|
||||
public const string Artist = "YandexMusicArtist";
|
||||
}
|
||||
|
||||
/// <summary>Shared mapping helpers.</summary>
|
||||
public static class Mapping
|
||||
{
|
||||
/// <summary>Provider display name.</summary>
|
||||
public const string ProviderName = "Yandex Music";
|
||||
|
||||
private static PluginConfiguration Config => Plugin.Instance?.Configuration ?? new PluginConfiguration();
|
||||
|
||||
/// <summary>Gets a provider id from lookup info.</summary>
|
||||
public static long? GetId(IReadOnlyDictionary<string, string>? 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;
|
||||
}
|
||||
|
||||
/// <summary>Gets a provider id from an item.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>Parses a Yandex release date.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>Validates a release year.</summary>
|
||||
public static int? ValidYear(int? year)
|
||||
=> year is >= 1900 and <= 2100 ? year : null;
|
||||
|
||||
/// <summary>Maps a Yandex album genre to a Jellyfin genre name.</summary>
|
||||
public static async Task<string[]> GenresAsync(
|
||||
YandexMusicClient client,
|
||||
string? albumGenre,
|
||||
IEnumerable<string>? artistGenres,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (!Config.EnableGenres)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var result = new List<string>();
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>Gets label names.</summary>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>Adds a primary remote image when allowed.</summary>
|
||||
public static void AddPrimaryImage<T>(
|
||||
MediaBrowser.Controller.Providers.MetadataResult<T> 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));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Adds a remote image of a given type.</summary>
|
||||
public static void AddImage<T>(
|
||||
MediaBrowser.Controller.Providers.MetadataResult<T> 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user