diff --git a/src/AudioTagger.Console/AudioTagger.Console.csproj b/src/AudioTagger.Console/AudioTagger.Console.csproj index 591f40e..2ab19dc 100644 --- a/src/AudioTagger.Console/AudioTagger.Console.csproj +++ b/src/AudioTagger.Console/AudioTagger.Console.csproj @@ -19,9 +19,9 @@ - - - + + + diff --git a/src/AudioTagger.Console/Operations/TagUpdater.cs b/src/AudioTagger.Console/Operations/TagUpdater.cs index 60affc9..9669df5 100644 --- a/src/AudioTagger.Console/Operations/TagUpdater.cs +++ b/src/AudioTagger.Console/Operations/TagUpdater.cs @@ -89,8 +89,8 @@ private static bool UpdateTags( } IEnumerable matchedTags = match.Groups - .OfType() - .Where(g => g.Success); + .OfType() + .Where(g => g.Success); if (!matchedTags.Any()) { diff --git a/src/AudioTagger.Library/AudioTagger.Library.csproj b/src/AudioTagger.Library/AudioTagger.Library.csproj index a4d8478..0d09f34 100644 --- a/src/AudioTagger.Library/AudioTagger.Library.csproj +++ b/src/AudioTagger.Library/AudioTagger.Library.csproj @@ -6,9 +6,9 @@ - - - + + + diff --git a/src/AudioTagger.Library/OutputLine.cs b/src/AudioTagger.Library/OutputLine.cs index 48ac7c8..5acfba8 100644 --- a/src/AudioTagger.Library/OutputLine.cs +++ b/src/AudioTagger.Library/OutputLine.cs @@ -109,8 +109,9 @@ public static Dictionary GetTagKeyValuePairs( { var lines = new Dictionary { - { "Title", fileData.Title }, + { "Album Artist(s)", fileData.AlbumArtists.Join() }, { "Artist(s)", fileData.Artists.Join() }, + { "Title", fileData.Title }, { "Album", fileData.Album }, { "Year", fileData.Year.ToString() }, { "Duration", fileData.Duration.ToString("m\\:ss") } diff --git a/src/AudioTagger.Library/UpdatableFields.cs b/src/AudioTagger.Library/UpdatableFields.cs index 87df93d..addb073 100644 --- a/src/AudioTagger.Library/UpdatableFields.cs +++ b/src/AudioTagger.Library/UpdatableFields.cs @@ -1,5 +1,6 @@ using System.Text.RegularExpressions; using System.Globalization; +using static System.StringSplitOptions; using AudioTagger.Library.MediaFiles; namespace AudioTagger.Library; @@ -26,71 +27,75 @@ public UpdatableFields( { ArgumentNullException.ThrowIfNull(matchedGroups); + const StringSplitOptions splitOptions = RemoveEmptyEntries | TrimEntries; + foreach (Group element in matchedGroups) { - if (element.Name == "title") + switch (element.Name) { - // TODO: Relocate the replacements. - Title = element.Value.Trim().Normalize() + case "albumArtists": + AlbumArtists = element.Value + .Trim().Normalize() + .Replace("___", " ") + .Replace("__", " ") + .Split([";"], splitOptions) + .Select(a => a.Normalize()) + .ToArray(); + Count++; + break; + case "artists": + Artists = element.Value + .Trim().Normalize() .Replace("___", " ") - .Replace("__", " "); - Count++; - } - else if (element.Name == "albumArtists") - { - AlbumArtists = element.Value - .Replace("___", " ") - .Replace("__", " ") - .Split([";"], - StringSplitOptions.RemoveEmptyEntries | - StringSplitOptions.TrimEntries) - .Select(a => a.Normalize()) - .ToArray(); - Count++; - } - else if (element.Name == "artists") - { - Artists = element.Value.Replace("___", " ") - .Replace("__", " ") - .Split([";"], - StringSplitOptions.RemoveEmptyEntries | - StringSplitOptions.TrimEntries) - .Select(a => a.Normalize()) - .ToArray(); - Count++; - } - else if (element.Name == "album") - { - Album = element.Value.Trim().Normalize() - .Replace("___", " ") - .Replace("__", " "); - Count++; - } - else if (element.Name == "genres") - { - Genres = element.Value.Replace("___", " ") - .Replace("__", " ") - .Split([";"], - StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) - .Select(g => g.Normalize()) - .ToArray(); - Count++; - } - else if (element.Name == "year") - { - Year = uint.TryParse(element.Value, out var parsed) ? parsed : 0; - Count++; - } - else if (element.Name == "trackNo") - { - TrackNo = uint.TryParse(element.Value, out var parsed) ? parsed : null; - Count++; + .Replace("__", " ") + .Split([";"], splitOptions) + .Select(a => a.Normalize()) + .ToArray(); + Count++; + break; + case "album": + Album = element.Value + .Trim().Normalize() + .Replace("___", " ") + .Replace("__", " "); + Count++; + break; + case "title": + // TODO: Relocate the replacements. + Title = element.Value + .Trim().Normalize() + .Replace("___", " ") + .Replace("__", " "); + Count++; + break; + case "genres": + Genres = element.Value + .Trim().Normalize() + .Replace("___", " ") + .Replace("__", " ") + .Split([";"], splitOptions) + .Select(g => g.Normalize()) + .ToArray(); + Count++; + break; + case "year": + { + Year = uint.TryParse(element.Value, out var parsed) ? parsed : 0; + Count++; + break; + } + case "trackNo": + { + TrackNo = uint.TryParse(element.Value, out var parsed) ? parsed : null; + Count++; + break; + } } } // If no genre was manually passed in, check the settings for a registered one. - if (Genres?.Any() != true && - Artists?.Any() == true && + if (Genres?.Any() is not true && + Artists?.Any() is true && artistsWithGenres.Any()) { if (artistsWithGenres.Any() && @@ -106,19 +111,21 @@ public UpdatableFields(string tagField, dynamic newValue) ArgumentNullException.ThrowIfNull(tagField); ArgumentNullException.ThrowIfNull(newValue); - if (tagField.Equals("year", StringComparison.OrdinalIgnoreCase) && + const StringComparison comparisonType = StringComparison.OrdinalIgnoreCase; + + if (tagField.Equals("year", comparisonType) && newValue is int newYear) { Year = (uint)newYear; Count++; } - else if (tagField.Equals("genre", StringComparison.OrdinalIgnoreCase) && + else if (tagField.Equals("genre", comparisonType) && newValue is string newGenre) { Genres = [newGenre]; Count++; } - else if (tagField.Equals("genre", StringComparison.OrdinalIgnoreCase) && + else if (tagField.Equals("genre", comparisonType) && newValue is string[] newGenres) { Genres = newGenres; @@ -130,11 +137,6 @@ public Dictionary GetUpdateKeyValuePairs(MediaFile fileData) { var updateOutput = new Dictionary(); - if (Title != null && Title != fileData.Title) - { - updateOutput.Add("Title", Title); - } - if (AlbumArtists?.All(a => fileData.AlbumArtists.Contains(a)) == false) { updateOutput.Add("Album Artists", string.Join("; ", AlbumArtists)); @@ -150,6 +152,11 @@ public Dictionary GetUpdateKeyValuePairs(MediaFile fileData) updateOutput.Add("Album", Album); } + if (Title != null && Title != fileData.Title) + { + updateOutput.Add("Title", Title); + } + if (Year != null && Year != fileData.Year) { updateOutput.Add("Year", Year.Value.ToString(CultureInfo.InvariantCulture)); diff --git a/src/AudioTagger.Tests/AudioTagger.Tests.csproj b/src/AudioTagger.Tests/AudioTagger.Tests.csproj index 1c3a135..0885794 100644 --- a/src/AudioTagger.Tests/AudioTagger.Tests.csproj +++ b/src/AudioTagger.Tests/AudioTagger.Tests.csproj @@ -6,13 +6,13 @@ false - + - + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all