fix(api): Select correct Hardcover.app ISBN from lookup results (#2530)
This commit is contained in:
+38
-13
@@ -8,10 +8,15 @@ import com.adityachandel.booklore.service.metadata.parser.hardcover.GraphQLRespo
|
||||
import com.adityachandel.booklore.service.metadata.parser.hardcover.HardcoverBookDetails;
|
||||
import com.adityachandel.booklore.service.metadata.parser.hardcover.HardcoverBookSearchService;
|
||||
import com.adityachandel.booklore.service.metadata.parser.hardcover.HardcoverMoodFilter;
|
||||
import com.adityachandel.booklore.util.BookUtils;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.text.WordUtils;
|
||||
import org.apache.commons.text.similarity.FuzzyScore;
|
||||
import org.apache.commons.text.similarity.LevenshteinDistance;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
@@ -280,23 +285,43 @@ public class HardcoverParser implements BookParser {
|
||||
}
|
||||
|
||||
String inputIsbn = request.getIsbn();
|
||||
|
||||
if (inputIsbn != null && inputIsbn.length() == 10 && doc.getIsbns().contains(inputIsbn)) {
|
||||
metadata.setIsbn10(inputIsbn);
|
||||
String matchingIsbn = null;
|
||||
if (StringUtils.isBlank(inputIsbn)) {
|
||||
// If we didn't search by ISBN, use first ISBN from results
|
||||
matchingIsbn = doc.getIsbns().stream()
|
||||
.filter(isbn -> isbn.length() == 10 || isbn.length() == 13)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
} else if (doc.getIsbns().contains(inputIsbn)) {
|
||||
// If we searched by ISBN and it matches a result perfectly, use that
|
||||
matchingIsbn = inputIsbn;
|
||||
} else {
|
||||
metadata.setIsbn10(doc.getIsbns().stream()
|
||||
.filter(isbn -> isbn.length() == 10)
|
||||
.findFirst()
|
||||
.orElse(null));
|
||||
// If we searched by ISBN but got no exact matches, get response ISBN that most closely matches it
|
||||
LevenshteinDistance distance = LevenshteinDistance.getDefaultInstance();
|
||||
int smallestDistance = Integer.MAX_VALUE;
|
||||
for (String isbn : doc.getIsbns()) {
|
||||
if (isbn.length() != 10 && isbn.length() != 13) {
|
||||
continue;
|
||||
}
|
||||
int currentDistance = distance.apply(isbn, inputIsbn);
|
||||
if (smallestDistance > currentDistance) {
|
||||
smallestDistance = currentDistance;
|
||||
matchingIsbn = isbn;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (inputIsbn != null && inputIsbn.length() == 13 && doc.getIsbns().contains(inputIsbn)) {
|
||||
metadata.setIsbn13(inputIsbn);
|
||||
// Whatever ISBN we end up with, calculate the other one
|
||||
if (matchingIsbn != null && matchingIsbn.length() == 10) {
|
||||
metadata.setIsbn10(matchingIsbn);
|
||||
metadata.setIsbn13(BookUtils.isbn10To13(matchingIsbn));
|
||||
} else if (matchingIsbn != null && matchingIsbn.length() == 13) {
|
||||
metadata.setIsbn10(BookUtils.isbn13to10(matchingIsbn));
|
||||
metadata.setIsbn13(matchingIsbn);
|
||||
} else {
|
||||
metadata.setIsbn13(doc.getIsbns().stream()
|
||||
.filter(isbn -> isbn.length() == 13)
|
||||
.findFirst()
|
||||
.orElse(null));
|
||||
// Can only happen if doc.getIsbns() is empty or doesn't have any 10/13 length strings
|
||||
metadata.setIsbn10(null);
|
||||
metadata.setIsbn13(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -105,4 +105,41 @@ public class BookUtils {
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public static String isbn10To13(String isbn10) {
|
||||
if (isbn10 == null || isbn10.length() != 10) {
|
||||
return null;
|
||||
}
|
||||
String isbn13 = "978" + isbn10.substring(0, 9);
|
||||
boolean oneThree = false;
|
||||
int total = 0;
|
||||
for (char c : isbn13.toCharArray()) {
|
||||
total += (c - '0') * (oneThree ? 3 : 1);
|
||||
oneThree = !oneThree;
|
||||
}
|
||||
int checkDigit = 10 - (total % 10);
|
||||
isbn13 += checkDigit;
|
||||
return isbn13;
|
||||
}
|
||||
|
||||
public static String isbn13to10(String isbn13) {
|
||||
if (isbn13 == null || isbn13.length() != 13 || !"978".equals(isbn13.substring(0, 3))) {
|
||||
// Only ISBN-13s that start with "978" have an equivalent ISBN-10
|
||||
return null;
|
||||
}
|
||||
String isbn10 = isbn13.substring(3, 12);
|
||||
int mult = 10;
|
||||
int total = 0;
|
||||
for (char c : isbn10.toCharArray()) {
|
||||
total += (c - '0') * mult;
|
||||
mult--;
|
||||
}
|
||||
int checkDigit = (11 - (total % 11)) % 11;
|
||||
if (checkDigit == 10) {
|
||||
isbn10 += "X";
|
||||
} else {
|
||||
isbn10 += checkDigit;
|
||||
}
|
||||
return isbn10;
|
||||
}
|
||||
}
|
||||
|
||||
+45
-7
@@ -206,12 +206,12 @@ class HardcoverParserTest {
|
||||
Book book = Book.builder().title("Any Book").build();
|
||||
FetchMetadataRequest request = FetchMetadataRequest.builder()
|
||||
.title("Any Book")
|
||||
.isbn("1234567890")
|
||||
.isbn("123456789X")
|
||||
.author("Wrong Author") // Should be ignored
|
||||
.build();
|
||||
|
||||
GraphQLResponse.Hit hit = createHitWithAuthor("Any Book", "Correct Author");
|
||||
when(hardcoverBookSearchService.searchBooks("1234567890"))
|
||||
when(hardcoverBookSearchService.searchBooks("123456789X"))
|
||||
.thenReturn(List.of(hit));
|
||||
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
||||
|
||||
@@ -229,11 +229,11 @@ class HardcoverParserTest {
|
||||
Book book = Book.builder().title("Test").build();
|
||||
FetchMetadataRequest request = FetchMetadataRequest.builder()
|
||||
.title("Test")
|
||||
.isbn("9781234567890")
|
||||
.isbn("9781234567897")
|
||||
.build();
|
||||
|
||||
GraphQLResponse.Hit hit = createFullyPopulatedHit();
|
||||
when(hardcoverBookSearchService.searchBooks("9781234567890"))
|
||||
when(hardcoverBookSearchService.searchBooks("9781234567897"))
|
||||
.thenReturn(List.of(hit));
|
||||
|
||||
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
||||
@@ -253,11 +253,49 @@ class HardcoverParserTest {
|
||||
assertThat(metadata.getSeriesName()).isEqualTo("Test Series");
|
||||
assertThat(metadata.getSeriesNumber()).isEqualTo(2.0f);
|
||||
assertThat(metadata.getSeriesTotal()).isEqualTo(5);
|
||||
assertThat(metadata.getIsbn13()).isEqualTo("9781234567890");
|
||||
assertThat(metadata.getIsbn10()).isEqualTo("1234567890");
|
||||
assertThat(metadata.getIsbn13()).isEqualTo("9781234567897");
|
||||
assertThat(metadata.getIsbn10()).isEqualTo("123456789X");
|
||||
assertThat(metadata.getProvider()).isEqualTo(MetadataProvider.Hardcover);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should map correct ISBN-13 from ISBN-10")
|
||||
void fetchMetadata_fullDocument_isbn10() {
|
||||
Book book = Book.builder().title("Test").build();
|
||||
FetchMetadataRequest request = FetchMetadataRequest.builder()
|
||||
.title("Test")
|
||||
.isbn("123456789X")
|
||||
.build();
|
||||
|
||||
GraphQLResponse.Hit hit = createFullyPopulatedHit();
|
||||
when(hardcoverBookSearchService.searchBooks("123456789X"))
|
||||
.thenReturn(List.of(hit));
|
||||
|
||||
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
||||
|
||||
assertThat(results.get(0).getIsbn10()).isEqualTo("123456789X");
|
||||
assertThat(results.get(0).getIsbn13()).isEqualTo("9781234567897");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("ISBN-13 not starting with 978 should not have an ISBN-10")
|
||||
void fetchMetadata_fullDocument_noIsbn10() {
|
||||
Book book = Book.builder().title("Test").build();
|
||||
FetchMetadataRequest request = FetchMetadataRequest.builder()
|
||||
.title("Test")
|
||||
.isbn("9791111111112")
|
||||
.build();
|
||||
|
||||
GraphQLResponse.Hit hit = createFullyPopulatedHit();
|
||||
when(hardcoverBookSearchService.searchBooks("9791111111112"))
|
||||
.thenReturn(List.of(hit));
|
||||
|
||||
List<BookMetadata> results = parser.fetchMetadata(book, request);
|
||||
|
||||
assertThat(results.get(0).getIsbn10()).isNull();
|
||||
assertThat(results.get(0).getIsbn13()).isEqualTo("9791111111112");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should map thumbnail URL correctly")
|
||||
void fetchMetadata_withImage_mapsThumbnailUrl() {
|
||||
@@ -531,7 +569,7 @@ class HardcoverParserTest {
|
||||
doc.setRatingsCount(100);
|
||||
doc.setPages(350);
|
||||
doc.setReleaseDate("2023-01-15");
|
||||
doc.setIsbns(List.of("9781234567890", "1234567890"));
|
||||
doc.setIsbns(List.of("9781111111113", "1111111111", "9781234567897", "123456789X", "9791111111112"));
|
||||
doc.setGenres(List.of("Fiction", "Fantasy"));
|
||||
doc.setMoods(List.of("adventurous", "exciting"));
|
||||
doc.setTags(List.of("Epic"));
|
||||
|
||||
Reference in New Issue
Block a user