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
@@ -0,0 +1,75 @@
|
||||
namespace Jellyfin.Plugin.YandexMusic.Providers;
|
||||
|
||||
/// <summary>
|
||||
/// Reads the original embedded tags directly from the file, so matching is not affected by
|
||||
/// metadata previously written into Jellyfin's database.
|
||||
/// </summary>
|
||||
public static class AudioTagReader
|
||||
{
|
||||
/// <summary>Embedded tag values relevant for matching.</summary>
|
||||
public sealed record Tags(
|
||||
string? Title,
|
||||
string[] Artists,
|
||||
string[] AlbumArtists,
|
||||
string? Album,
|
||||
int? Year,
|
||||
string[] Genres,
|
||||
int? Track);
|
||||
|
||||
/// <summary>Reads tags from a media file. Returns null when the file cannot be read.</summary>
|
||||
public static Tags? Read(string? path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path) || !File.Exists(path))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var file = TagLib.File.Create(path);
|
||||
var tag = file.Tag;
|
||||
var artists = (tag.Performers ?? [])
|
||||
.Where(a => !string.IsNullOrWhiteSpace(a))
|
||||
.Select(a => a.Trim())
|
||||
.ToArray();
|
||||
var albumArtists = (tag.AlbumArtists ?? [])
|
||||
.Where(a => !string.IsNullOrWhiteSpace(a))
|
||||
.Select(a => a.Trim())
|
||||
.ToArray();
|
||||
|
||||
return new Tags(
|
||||
NullIfEmpty(tag.Title),
|
||||
artists,
|
||||
albumArtists,
|
||||
NullIfEmpty(tag.Album),
|
||||
tag.Year == 0 ? null : (int)tag.Year,
|
||||
(tag.Genres ?? []).Where(g => !string.IsNullOrWhiteSpace(g)).ToArray(),
|
||||
tag.Track == 0 ? null : (int)tag.Track);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Parses "Artist - Title" from a file name when tags are missing.</summary>
|
||||
public static (string? Artist, string? Title) ParseFileName(string? path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
{
|
||||
return (null, null);
|
||||
}
|
||||
|
||||
var name = Path.GetFileNameWithoutExtension(path);
|
||||
var index = name.LastIndexOf(" - ", StringComparison.Ordinal);
|
||||
if (index <= 0 || index + 3 >= name.Length)
|
||||
{
|
||||
return (null, name);
|
||||
}
|
||||
|
||||
return (name[..index].Trim(), name[(index + 3)..].Trim());
|
||||
}
|
||||
|
||||
private static string? NullIfEmpty(string? value)
|
||||
=> string.IsNullOrWhiteSpace(value) ? null : value.Trim();
|
||||
}
|
||||
Reference in new issue
Block a user