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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user