From 9be948b7cc6bf13b9fb7b33f689cdd16fbfaa880 Mon Sep 17 00:00:00 2001 From: ta264 Date: Wed, 31 Mar 2021 21:20:32 +0100 Subject: [PATCH] Fixed: Parse series for search results --- .../Goodreads/GoodreadsProxyFixture.cs | 12 ++++++ .../Goodreads/GoodreadsProxySearchFixture.cs | 12 ++++++ .../Goodreads/GoodreadsProxy.cs | 37 +++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/src/NzbDrone.Core.Test/MetadataSource/Goodreads/GoodreadsProxyFixture.cs b/src/NzbDrone.Core.Test/MetadataSource/Goodreads/GoodreadsProxyFixture.cs index 6db9e67bb..bd3fe94c0 100644 --- a/src/NzbDrone.Core.Test/MetadataSource/Goodreads/GoodreadsProxyFixture.cs +++ b/src/NzbDrone.Core.Test/MetadataSource/Goodreads/GoodreadsProxyFixture.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using FluentAssertions; using Moq; using NUnit.Framework; @@ -54,6 +55,17 @@ namespace NzbDrone.Core.Test.MetadataSource.Goodreads details.Item2.Title.Should().Be(name); } + [TestCase("54837483", "The Book of Dust", "1")] + [TestCase("28360360", "October Daye", "9.3")] + public void should_parse_series_from_title(string id, string series, string position) + { + var result = Subject.GetBookInfo(id); + + var link = result.Item2.SeriesLinks.Value.First(); + link.Series.Value.Title.Should().Be(series); + link.Position.Should().Be(position); + } + [Test] public void getting_details_of_invalid_author() { diff --git a/src/NzbDrone.Core.Test/MetadataSource/Goodreads/GoodreadsProxySearchFixture.cs b/src/NzbDrone.Core.Test/MetadataSource/Goodreads/GoodreadsProxySearchFixture.cs index f63d1f992..9e8d11fdb 100644 --- a/src/NzbDrone.Core.Test/MetadataSource/Goodreads/GoodreadsProxySearchFixture.cs +++ b/src/NzbDrone.Core.Test/MetadataSource/Goodreads/GoodreadsProxySearchFixture.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using FluentAssertions; using Moq; using NUnit.Framework; @@ -104,5 +105,16 @@ namespace NzbDrone.Core.Test.MetadataSource.Goodreads cast.Title.Should().Be(expected); } } + + [TestCase("B01N390U59", "The Book of Dust", "1")] + [TestCase("B0191WS1EE", "October Daye", "9.3")] + public void should_parse_series_from_title(string query, string series, string position) + { + var result = Subject.SearchByField("field", query); + + var link = result.First().SeriesLinks.Value.First(); + link.Series.Value.Title.Should().Be(series); + link.Position.Should().Be(position); + } } } diff --git a/src/NzbDrone.Core/MetadataSource/Goodreads/GoodreadsProxy.cs b/src/NzbDrone.Core/MetadataSource/Goodreads/GoodreadsProxy.cs index db3031d37..92c6e65ad 100644 --- a/src/NzbDrone.Core/MetadataSource/Goodreads/GoodreadsProxy.cs +++ b/src/NzbDrone.Core/MetadataSource/Goodreads/GoodreadsProxy.cs @@ -28,6 +28,9 @@ namespace NzbDrone.Core.MetadataSource.Goodreads private static readonly Regex NoPhotoRegex = new Regex(@"/nophoto/(book|user)/", RegexOptions.IgnoreCase | RegexOptions.Compiled); + private static readonly Regex SeriesRegex = new Regex(@"\((?[^,]+),\s+#(?[\w\d\.]+)\)$", + RegexOptions.Compiled); + private readonly ICachedHttpResponseService _cachedHttpClient; private readonly Logger _logger; private readonly IAuthorService _authorService; @@ -657,6 +660,8 @@ namespace NzbDrone.Core.MetadataSource.Goodreads Debug.Assert(!book.Editions.Value.Any() || book.Editions.Value.Count(x => x.Monitored) == 1, "one edition monitored"); + book.SeriesLinks = MapSearchSeries(resource.Title, resource.TitleWithoutSeries); + return book; } @@ -794,8 +799,40 @@ namespace NzbDrone.Core.MetadataSource.Goodreads book.Author = author; book.AuthorMetadata = book.Author.Value.Metadata.Value; book.CleanTitle = book.Title.CleanAuthorName(); + book.SeriesLinks = MapSearchSeries(resource.Title, resource.BookTitleBare); return book; } + + private static List MapSearchSeries(string title, string titleWithoutSeries) + { + if (title != titleWithoutSeries && + title.Substring(0, titleWithoutSeries.Length) == titleWithoutSeries) + { + var seriesText = title.Substring(titleWithoutSeries.Length); + + var match = SeriesRegex.Match(seriesText); + + if (match.Success) + { + var series = match.Groups["series"].Value; + var position = match.Groups["position"].Value; + + return new List + { + new SeriesBookLink + { + Series = new Series + { + Title = series + }, + Position = position + } + }; + } + } + + return null; + } } }