From 6cecf23b8803532da895a7449c527f6925a66333 Mon Sep 17 00:00:00 2001 From: ta264 Date: Tue, 23 Nov 2021 21:04:42 +0000 Subject: [PATCH] Fixed: Removing from Goodreads shelf when different edition imported --- .../Goodreads/Bookshelf/GoodreadsBookshelf.cs | 76 +++++++++++++++---- 1 file changed, 63 insertions(+), 13 deletions(-) diff --git a/src/NzbDrone.Core/Notifications/Goodreads/Bookshelf/GoodreadsBookshelf.cs b/src/NzbDrone.Core/Notifications/Goodreads/Bookshelf/GoodreadsBookshelf.cs index 2c832238c..be6a37da9 100644 --- a/src/NzbDrone.Core/Notifications/Goodreads/Bookshelf/GoodreadsBookshelf.cs +++ b/src/NzbDrone.Core/Notifications/Goodreads/Bookshelf/GoodreadsBookshelf.cs @@ -22,8 +22,21 @@ public GoodreadsBookshelf(IHttpClient httpClient, public override void OnReleaseImport(BookDownloadMessage message) { - var bookId = message.Book.Editions.Value.Single(x => x.Monitored).ForeignEditionId; - RemoveBookFromShelves(bookId, Settings.RemoveIds); + var importedBook = message.Book; + + foreach (var shelf in Settings.RemoveIds) + { + // try to find the edition that we need to remove + var listBooks = SearchShelf(shelf, importedBook.AuthorMetadata.Value.Name); + var toRemove = listBooks.Where(x => x.Book.WorkId.ToString() == importedBook.ForeignBookId); + + foreach (var listBook in toRemove) + { + RemoveBookFromShelves(listBook.Book.Id, shelf); + } + } + + var bookId = importedBook.Editions.Value.Single(x => x.Monitored).ForeignEditionId; AddToShelves(bookId, Settings.AddIds); } @@ -104,22 +117,59 @@ private IReadOnlyList GetShelfList(int page) } } - private void RemoveBookFromShelves(string bookId, IEnumerable shelves) + private IReadOnlyList SearchShelf(string shelf, string query) { - foreach (var shelf in shelves) + List results = new (); + + while (true) { - var req = RequestBuilder() - .Post() - .SetSegment("route", "shelf/add_to_shelf.xml") - .AddFormParameter("name", shelf) - .AddFormParameter("book_id", bookId) - .AddFormParameter("a", "remove"); + var page = 1; - // in case not found in shelf - req.SuppressHttpError = true; + try + { + var builder = RequestBuilder() + .SetSegment("route", $"review/list.xml") + .AddQueryParam("v", 2) + .AddQueryParam("id", Settings.UserId) + .AddQueryParam("shelf", shelf) + .AddQueryParam("per_page", 200) + .AddQueryParam("page", page++) + .AddQueryParam("search[query]", query); - OAuthExecute(req); + var httpResponse = OAuthExecute(builder); + + var resource = httpResponse.Deserialize>("reviews"); + + results.AddRange(resource.List); + + if (resource.Pagination.End >= resource.Pagination.TotalItems) + { + break; + } + } + catch (Exception ex) + { + _logger.Warn(ex, "Error fetching bookshelves from Goodreads"); + return results; + } } + + return results; + } + + private void RemoveBookFromShelves(long bookId, string shelf) + { + var req = RequestBuilder() + .Post() + .SetSegment("route", "shelf/add_to_shelf.xml") + .AddFormParameter("name", shelf) + .AddFormParameter("book_id", bookId) + .AddFormParameter("a", "remove"); + + // in case not found in shelf + req.SuppressHttpError = true; + + OAuthExecute(req); } private void AddToShelves(string bookId, IEnumerable shelves)