From 665d87b8d778039a6582586e2e355a527e970876 Mon Sep 17 00:00:00 2001 From: ta264 Date: Sat, 21 Aug 2021 14:34:35 +0100 Subject: [PATCH] Fixed: Prefer 13 character ISBN from epub metadata Fixes #1210 --- .../MediaFiles/EbookTagServiceFixture.cs | 31 +++++++++++++++++++ .../MediaFiles/EbookTagService.cs | 17 +++++----- 2 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 src/NzbDrone.Core.Test/MediaFiles/EbookTagServiceFixture.cs diff --git a/src/NzbDrone.Core.Test/MediaFiles/EbookTagServiceFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/EbookTagServiceFixture.cs new file mode 100644 index 000000000..03c25dcf7 --- /dev/null +++ b/src/NzbDrone.Core.Test/MediaFiles/EbookTagServiceFixture.cs @@ -0,0 +1,31 @@ +using System.Collections.Generic; +using System.Linq; +using FizzWare.NBuilder; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.MediaFiles; +using NzbDrone.Core.Test.Framework; +using NzbDrone.Test.Common; +using VersOne.Epub.Schema; + +namespace NzbDrone.Core.Test.MediaFiles.AudioTagServiceFixture +{ + [TestFixture] + public class EbookTagServiceFixture : CoreTest + { + [Test] + public void should_prefer_isbn13() + { + var ids = Builder + .CreateListOfSize(2) + .TheFirst(1) + .With(x => x.Identifier = "4087738574") + .TheNext(1) + .With(x => x.Identifier = "9781455546176") + .Build() + .ToList(); + + Subject.GetIsbn(ids).Should().Be("9781455546176"); + } + } +} diff --git a/src/NzbDrone.Core/MediaFiles/EbookTagService.cs b/src/NzbDrone.Core/MediaFiles/EbookTagService.cs index 5e0c5d56e..216e1dd3c 100644 --- a/src/NzbDrone.Core/MediaFiles/EbookTagService.cs +++ b/src/NzbDrone.Core/MediaFiles/EbookTagService.cs @@ -377,18 +377,15 @@ namespace NzbDrone.Core.MediaFiles return result; } - private string GetIsbn(IEnumerable ids) + public string GetIsbn(IEnumerable ids) { - foreach (var id in ids) - { - var isbn = StripIsbn(id?.Identifier); - if (isbn != null) - { - return isbn; - } - } + var candidates = ids.Select(x => StripIsbn(x?.Identifier)) + .Where(x => x != null) + .OrderByDescending(x => x.Length); - return null; + return candidates.FirstOrDefault(x => x.StartsWith("978")) + ?? candidates.FirstOrDefault(x => x.StartsWith("979")) + ?? candidates.FirstOrDefault(); } private string GetIsbnChars(string input)