Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Sleezer/Core/PostProcessing/PreImportTagger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,8 @@ FingerprintVerdict VerdictOf(LocalTrack file, Track track) =>
}
}

// Lidarr writes the recording id as the MusicBrainz track id (AudioTagService.GetTrackMetadata).
// Reads the plugin's merged TagLib 2.2, whose MusicBrainzTrackId is the recording frame Lidarr writes;
// TagLib 2.5 (Lidarr 4.0) names that frame MusicBrainzRecordingId, so keep this off the host's TagLib.
private static (string? Album, string? ReleaseId, string? RecordingId) WrittenIdentity(string path)
{
using TagLib.File file = TagLib.File.Create(path);
Expand Down
48 changes: 48 additions & 0 deletions src/Sleezer/Core/Utilities/MusicBrainzTrackIds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Reflection;
using System.Threading;
using NLog;
using NzbDrone.Core.MediaFiles;

namespace NzbDrone.Plugin.Sleezer.Core.Utilities
{
// Lidarr 4.0 moved the recording id to MusicBrainzRecordingId and dropped MusicBrainzReleaseTrackId.
// Never name either property directly: the plugin builds against the older AudioTag and must load on both.
public static class MusicBrainzTrackIds
{
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
private static int _warnedUnknownShape;

/// Sets the ids where the running Lidarr's AudioTag keeps them, else warns once and sets neither; a null id keeps the tag's value.
public static void Set(AudioTag tag, string? recordingId, string? releaseTrackId) => SetOn(tag, recordingId, releaseTrackId);

internal static void SetOn(object tag, string? recordingId, string? releaseTrackId)
{
var type = tag.GetType();
var trackId = StringProperty(type, "MusicBrainzTrackId");
var recording = StringProperty(type, "MusicBrainzRecordingId");
var releaseTrack = StringProperty(type, "MusicBrainzReleaseTrackId");

if (trackId != null && recording != null)
{
SetOrKeep(tag, trackId, releaseTrackId);
SetOrKeep(tag, recording, recordingId);
}
else if (trackId != null && releaseTrack != null)
{
SetOrKeep(tag, trackId, recordingId);
SetOrKeep(tag, releaseTrack, releaseTrackId);
}
else if (Interlocked.Exchange(ref _warnedUnknownShape, 1) == 0)
{
// Not a throw: the caller's retag must still write every other field.
_logger.Warn("{0} keeps the MusicBrainz recording and release track ids in properties this plugin does not know; retagged files keep their own", type.FullName);
}
}

private static PropertyInfo? StringProperty(Type type, string name) =>
type.GetProperty(name) is { CanRead: true, CanWrite: true } property && property.PropertyType == typeof(string) ? property : null;

private static void SetOrKeep(object tag, PropertyInfo property, string? value) => property.SetValue(tag, value ?? property.GetValue(tag));
}
}
13 changes: 11 additions & 2 deletions src/Sleezer/Download/Clients/Bandcamp/BandcampDownloadProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using NzbDrone.Common.Http;
using NzbDrone.Core.Http.Bandcamp;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Plugin.Sleezer.Core.Utilities;

namespace NzbDrone.Core.Download.Clients.Bandcamp
{
Expand Down Expand Up @@ -360,6 +361,8 @@ private void NormalizeExtractedMetadata(string outputDir, BandcampDownloadItem i
return;
}

Exception? firstFailure = null;
var failed = 0;
for (var i = 0; i < audioFiles.Count; i++)
{
try
Expand All @@ -368,9 +371,16 @@ private void NormalizeExtractedMetadata(string outputDir, BandcampDownloadItem i
}
catch (Exception ex)
{
firstFailure ??= ex;
failed++;
_logger.Debug(ex, "Bandcamp download proxy [{0}]: Failed to normalize tags for {1}", item.DownloadId, audioFiles[i].Path);
}
}

if (firstFailure != null)
{
_logger.Warn(firstFailure, "Bandcamp download proxy [{0}]: Failed to normalize tags for {1} of {2} file(s); they keep the store's own tags", item.DownloadId, failed, audioFiles.Count);
}
}

internal static void ApplyCanonicalTagsToFile(string path,
Expand Down Expand Up @@ -404,8 +414,7 @@ internal static void ApplyCanonicalTagsToFile(string path,
tag.MusicBrainzArtistId = context.ArtistMusicBrainzId ?? tag.MusicBrainzArtistId;
tag.MusicBrainzReleaseArtistId = context.PreferredRelease?.ReleaseArtistMusicBrainzId ?? context.ArtistMusicBrainzId ?? tag.MusicBrainzReleaseArtistId;
tag.MusicBrainzReleaseGroupId = context.AlbumMusicBrainzId ?? tag.MusicBrainzReleaseGroupId;
tag.MusicBrainzTrackId = trackContext?.RecordingMusicBrainzId ?? tag.MusicBrainzTrackId;
tag.MusicBrainzReleaseTrackId = trackContext?.ReleaseTrackMusicBrainzId ?? tag.MusicBrainzReleaseTrackId;
MusicBrainzTrackIds.Set(tag, recordingId: trackContext?.RecordingMusicBrainzId, releaseTrackId: trackContext?.ReleaseTrackMusicBrainzId);
tag.MusicBrainzAlbumComment = context.AlbumDisambiguation ?? tag.MusicBrainzAlbumComment;
tag.Write(path);
}
Expand Down
122 changes: 122 additions & 0 deletions tests/Sleezer.Tests/MusicBrainzTrackIdsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System;
using System.IO;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Plugin.Sleezer.Core.Utilities;
using Xunit;

namespace Sleezer.Tests;

// Lidarr 4.0 dropped AudioTag.MusicBrainzReleaseTrackId, so Bandcamp retagging named a member the host no longer had.
public class MusicBrainzTrackIdsTests : IDisposable
{
private const string Recording = "2f3e1c0a-6a54-4c3b-9d1e-0c8f2b7a9e41";
private const string ReleaseTrack = "9b7d4e2c-1f8a-4b6e-a3c5-7d2e9f0b1c63";
private readonly string _dir = Path.Combine(Path.GetTempPath(), "sleezer-mbids-" + Path.GetRandomFileName());

public MusicBrainzTrackIdsTests() => Directory.CreateDirectory(_dir);

public void Dispose()
{
if (Directory.Exists(_dir)) Directory.Delete(_dir, recursive: true);
GC.SuppressFinalize(this);
}

// Same property shape as Lidarr 4.0's AudioTag; files written through the real one were checked outside this suite.
private sealed class RecordingIdAudioTag : AudioTag
{
public string? MusicBrainzRecordingId { get; set; }
}

private sealed class UnknownTag
{
public string? MusicBrainzTrackId { get; set; } = ReleaseTrack;
}

private sealed class GuidRecordingTag
{
public string? MusicBrainzTrackId { get; set; } = ReleaseTrack;

public Guid MusicBrainzRecordingId { get; set; }
}

[Fact]
public void Before_lidarr_4_the_track_id_holds_the_recording()
{
var tag = new AudioTag();

MusicBrainzTrackIds.Set(tag, Recording, ReleaseTrack);

Assert.Equal(Recording, tag.MusicBrainzTrackId);
Assert.Equal(ReleaseTrack, tag.MusicBrainzReleaseTrackId);
}

[Fact]
public void With_a_recording_id_property_the_track_id_holds_the_release_track()
{
var tag = new RecordingIdAudioTag();

MusicBrainzTrackIds.Set(tag, Recording, ReleaseTrack);

Assert.Equal(ReleaseTrack, tag.MusicBrainzTrackId);
Assert.Equal(Recording, tag.MusicBrainzRecordingId);
Assert.Null(tag.MusicBrainzReleaseTrackId);
}

[Fact]
public void Missing_ids_keep_the_tag_values_before_lidarr_4()
{
var tag = new AudioTag { MusicBrainzTrackId = Recording, MusicBrainzReleaseTrackId = ReleaseTrack };

MusicBrainzTrackIds.Set(tag, null, null);

Assert.Equal(Recording, tag.MusicBrainzTrackId);
Assert.Equal(ReleaseTrack, tag.MusicBrainzReleaseTrackId);
}

[Fact]
public void Missing_ids_keep_the_tag_values_with_a_recording_id_property()
{
var tag = new RecordingIdAudioTag { MusicBrainzTrackId = ReleaseTrack, MusicBrainzRecordingId = Recording };

MusicBrainzTrackIds.Set(tag, null, null);

Assert.Equal(ReleaseTrack, tag.MusicBrainzTrackId);
Assert.Equal(Recording, tag.MusicBrainzRecordingId);
}

[Fact]
public void An_unknown_tag_shape_is_left_alone_without_throwing()
{
var tag = new UnknownTag();

MusicBrainzTrackIds.SetOn(tag, Recording, null);

Assert.Equal(ReleaseTrack, tag.MusicBrainzTrackId);
}

[Fact]
public void A_recording_id_property_of_another_type_is_left_alone_without_throwing()
{
var tag = new GuidRecordingTag();

MusicBrainzTrackIds.SetOn(tag, Recording, null);

Assert.Equal(ReleaseTrack, tag.MusicBrainzTrackId);
Assert.Equal(Guid.Empty, tag.MusicBrainzRecordingId);
}

[Fact]
public void Before_lidarr_4_the_ids_written_to_the_file_read_back()
{
string path = Path.Combine(_dir, "track.flac");
File.Copy(Path.Combine(AppContext.BaseDirectory, "Fixtures", "silence.flac"), path);
var tag = new AudioTag(path);

MusicBrainzTrackIds.Set(tag, Recording, ReleaseTrack);
tag.Write(path);

var read = new AudioTag(path);
Assert.Equal(Recording, read.MusicBrainzTrackId);
Assert.Equal(ReleaseTrack, read.MusicBrainzReleaseTrackId);
}
}
3 changes: 2 additions & 1 deletion tests/Sleezer.Tests/PreImportTaggerWholeDownloadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@
}

// As Lidarr's writer: the album title and the release id are the keys it groups a download by.
// The recording goes in by frame name, as every Lidarr writes it; TagLib 2.5 renamed the property that reads it.
private static void TagAsWanted(TrackFile trackFile)
{
using TagLib.File file = TagLib.File.Create(trackFile.Path);
file.Tag.Album = "Wanted Album";
file.Tag.MusicBrainzReleaseId = WantedReleaseId;
file.Tag.MusicBrainzTrackId = trackFile.Tracks.Value[0].ForeignRecordingId;
((TagLib.Ogg.XiphComment)file.GetTag(TagLib.TagTypes.Xiph, true)).SetField("MUSICBRAINZ_TRACKID", trackFile.Tracks.Value[0].ForeignRecordingId);
file.Save();
}

Expand Down Expand Up @@ -457,12 +458,12 @@
});

Task<PreImportTagger.TaggingResult> one = Task.Run(() => Tag(first, album, artist, CancellationToken.None));
await firstLookingUp.Task.WaitAsync(TimeSpan.FromSeconds(10));

Check warning on line 461 in tests/Sleezer.Tests/PreImportTaggerWholeDownloadTests.cs

View workflow job for this annotation

GitHub Actions / build-test

Calls to methods which accept CancellationToken should use TestContext.Current.CancellationToken to allow test cancellation to be more responsive. (https://xunit.net/xunit.analyzers/rules/xUnit1051)
PreImportTagger.TaggingResult second = await Tagger(release, 0.0, TagAsWanted)

Check warning on line 462 in tests/Sleezer.Tests/PreImportTaggerWholeDownloadTests.cs

View workflow job for this annotation

GitHub Actions / build-test

Calls to methods which accept CancellationToken should use TestContext.Current.CancellationToken to allow test cancellation to be more responsive. (https://xunit.net/xunit.analyzers/rules/xUnit1051)
.TagCompletedDownloadAsync(album, artist, null, "other", other, 0.15, false, CancellationToken.None)
.WaitAsync(TimeSpan.FromSeconds(5));
firstMayFinish.SetResult();
await one.WaitAsync(TimeSpan.FromSeconds(30));

Check warning on line 466 in tests/Sleezer.Tests/PreImportTaggerWholeDownloadTests.cs

View workflow job for this annotation

GitHub Actions / build-test

Calls to methods which accept CancellationToken should use TestContext.Current.CancellationToken to allow test cancellation to be more responsive. (https://xunit.net/xunit.analyzers/rules/xUnit1051)

Assert.Equal(1, second.Tagged);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Sleezer.Tests/Sleezer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
<!-- Provenance tag written after the client's own metadata pass. -->
<Compile Include="..\..\src\Sleezer\Core\Utilities\SourceTagWriter.cs"
LinkBase="SourceUnderTest" />
<!-- Recording and release track ids on AudioTag, before and after Lidarr 4.0. -->
<Compile Include="..\..\src\Sleezer\Core\Utilities\MusicBrainzTrackIds.cs"
LinkBase="SourceUnderTest" />
<Compile Include="..\..\src\Sleezer\Core\Utilities\ReleaseFormatter.cs"
LinkBase="SourceUnderTest" />
<!-- Settings for a provider instance resolved without a definition (#126). Lidarr.Core only. -->
Expand Down
Loading