fix(metadata): migrate GoodReads parser from HTML scraping to GraphQL (#1734)
Co-authored-by: James Ward <james@notjam.es>
This commit is contained in:
co-authored by
James Ward
parent
284b7a41ef
commit
90fa4d7c42
+6
-35
@@ -9,14 +9,10 @@ import org.booklore.model.dto.settings.MetadataProviderSettings;
|
||||
import org.booklore.model.dto.settings.MetadataPublicReviewsSettings;
|
||||
import org.booklore.model.enums.MetadataProvider;
|
||||
import org.booklore.service.appsettings.AppSettingService;
|
||||
import org.jsoup.Connection;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
|
||||
@@ -40,13 +36,11 @@ public class GoodReadsParserTest {
|
||||
@Mock
|
||||
private HttpClient httpClient;
|
||||
|
||||
private MockedStatic<Jsoup> mockJsoup;
|
||||
|
||||
private GoodReadsParser parser;
|
||||
|
||||
private String exampleSearchJsonFixture;
|
||||
|
||||
private String exampleBookHtmlFixture;
|
||||
private String exampleGraphqlResponseFixture;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws IOException {
|
||||
@@ -57,16 +51,7 @@ public class GoodReadsParserTest {
|
||||
);
|
||||
|
||||
exampleSearchJsonFixture = readFixture("example-search.json");
|
||||
exampleBookHtmlFixture = readFixture("example-book.html");
|
||||
|
||||
|
||||
|
||||
mockJsoup = mockStatic(Jsoup.class);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
mockJsoup.close();
|
||||
exampleGraphqlResponseFixture = readFixture("example-graphql-response.json");
|
||||
}
|
||||
|
||||
private void mockSettings(boolean enabled) {
|
||||
@@ -114,30 +99,16 @@ public class GoodReadsParserTest {
|
||||
return httpResponse;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void mockHttpClientResponse(String urlPrefix, int statusCode, String response) throws Exception {
|
||||
when(
|
||||
httpClient.<String>send(
|
||||
argThat(r -> r.uri().toString().startsWith(urlPrefix)),
|
||||
argThat(r -> r != null && r.uri() != null && r.uri().toString().startsWith(urlPrefix)),
|
||||
any()
|
||||
)
|
||||
).thenAnswer((_) -> getMockResponse(statusCode, response));
|
||||
}
|
||||
|
||||
private void mockJsoupResponse(String urlPrefix, String response) throws Exception {
|
||||
Connection mockConnection = mock(Connection.class);
|
||||
|
||||
when(mockConnection.header(anyString(), anyString())).thenReturn(mockConnection);
|
||||
when(mockConnection.method(any())).thenReturn(mockConnection);
|
||||
|
||||
// There may be a better way to get the parse to work here.
|
||||
// However, this was the quickest and simplest way I could find.
|
||||
mockJsoup.when(() -> Jsoup.parse(response)).thenCallRealMethod();
|
||||
|
||||
when(mockConnection.get()).thenAnswer(i -> Jsoup.parse(response));
|
||||
|
||||
mockJsoup.when(() -> Jsoup.connect(startsWith(urlPrefix))).thenReturn(mockConnection);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFetchMetadata_EmptyQuery() {
|
||||
// Given
|
||||
@@ -172,9 +143,9 @@ public class GoodReadsParserTest {
|
||||
// Mock enabled provider
|
||||
mockSettings(true);
|
||||
|
||||
// Two expected URLs
|
||||
// Two expected URLs: search + GraphQL
|
||||
mockHttpClientResponse("https://www.goodreads.com/book/auto_complete", 200, exampleSearchJsonFixture);
|
||||
mockJsoupResponse("https://www.goodreads.com/book/show/", exampleBookHtmlFixture);
|
||||
mockHttpClientResponse("https://kxbwmqov6jgg3daaamb744ycu4.appsync-api.us-east-1.amazonaws.com/graphql", 200, exampleGraphqlResponseFixture);
|
||||
|
||||
// When
|
||||
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"data": {
|
||||
"getBookByLegacyId": {
|
||||
"title": "A Clockwork Orange",
|
||||
"description": "In Anthony Burgess's influential nightmare vision of the future, criminals take over after dark. Teen gang leader Alex narrates in fantastically inventive slang that echoes the brutal realities of a world that is both horrifying and yet mesmerizing. This edition includes the controversial final chapter that was omitted from the original American publication, offering readers a complete and unexpurgated experience of one of the most powerful and thought-provoking works of dystopian fiction ever written. A c",
|
||||
"imageUrl": "https://m.media-amazon.com/images/S/compressed.photo.goodreads.com/books/1549260060i/41817486.jpg",
|
||||
"primaryContributorEdge": {
|
||||
"node": {
|
||||
"name": "Anthony Burgess"
|
||||
}
|
||||
},
|
||||
"secondaryContributorEdges": [],
|
||||
"bookSeries": [],
|
||||
"bookGenres": [
|
||||
{ "genre": { "name": "Classics" } },
|
||||
{ "genre": { "name": "Fiction" } },
|
||||
{ "genre": { "name": "Science Fiction" } },
|
||||
{ "genre": { "name": "Dystopia" } },
|
||||
{ "genre": { "name": "Literature" } }
|
||||
],
|
||||
"details": {
|
||||
"numPages": "240",
|
||||
"publicationTime": 1.426896E12,
|
||||
"publisher": "W. W. Norton & Company",
|
||||
"isbn": "0393341763",
|
||||
"isbn13": "9780393341768",
|
||||
"language": {
|
||||
"name": "English"
|
||||
}
|
||||
},
|
||||
"work": {
|
||||
"stats": {
|
||||
"averageRating": "4.00",
|
||||
"ratingsCount": "782433"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user