Automatic ISBN 13 Correction/Normalization (#1219)
This commit is contained in:
+6
-5
@@ -193,8 +193,8 @@ public class AmazonBookParser implements BookParser {
|
||||
}
|
||||
|
||||
private String buildQueryUrl(FetchMetadataRequest fetchMetadataRequest, Book book) {
|
||||
// 1. Prefer ISBN if present
|
||||
if (fetchMetadataRequest.getIsbn() != null && !fetchMetadataRequest.getIsbn().isEmpty()) {
|
||||
String isbnCleaned = ParserUtils.cleanIsbn(fetchMetadataRequest.getIsbn());
|
||||
if (isbnCleaned != null && !isbnCleaned.isEmpty()) {
|
||||
String url = "https://www.amazon."
|
||||
+ appSettingService.getAppSettings().getMetadataProviderSettings().getAmazon().getDomain()
|
||||
+ "/s?k=" + fetchMetadataRequest.getIsbn();
|
||||
@@ -202,7 +202,6 @@ public class AmazonBookParser implements BookParser {
|
||||
return url;
|
||||
}
|
||||
|
||||
// 2. Otherwise, fall back to title + author + filename
|
||||
StringBuilder searchTerm = new StringBuilder();
|
||||
|
||||
String title = fetchMetadataRequest.getTitle();
|
||||
@@ -307,7 +306,8 @@ public class AmazonBookParser implements BookParser {
|
||||
try {
|
||||
Element isbn10Element = doc.select("#rpi-attribute-book_details-isbn10 .rpi-attribute-value span").first();
|
||||
if (isbn10Element != null) {
|
||||
return isbn10Element.text();
|
||||
String rawIsbn = isbn10Element.text();
|
||||
return ParserUtils.cleanIsbn(rawIsbn);
|
||||
}
|
||||
log.warn("Failed to parse isbn10: Element not found.");
|
||||
} catch (Exception e) {
|
||||
@@ -320,7 +320,8 @@ public class AmazonBookParser implements BookParser {
|
||||
try {
|
||||
Element isbn13Element = doc.select("#rpi-attribute-book_details-isbn13 .rpi-attribute-value span").first();
|
||||
if (isbn13Element != null) {
|
||||
return isbn13Element.text();
|
||||
String rawIsbn = isbn13Element.text();
|
||||
return ParserUtils.cleanIsbn(rawIsbn);
|
||||
}
|
||||
log.warn("Failed to parse isbn13: Element not found.");
|
||||
} catch (Exception e) {
|
||||
|
||||
+1
-1
@@ -53,7 +53,7 @@ public class GoodReadsParser implements BookParser {
|
||||
|
||||
@Override
|
||||
public List<BookMetadata> fetchMetadata(Book book, FetchMetadataRequest fetchMetadataRequest) {
|
||||
String isbn = fetchMetadataRequest.getIsbn();
|
||||
String isbn = ParserUtils.cleanIsbn(fetchMetadataRequest.getIsbn());
|
||||
if (isbn != null && !isbn.isBlank()) {
|
||||
log.info("Goodreads Query URL (ISBN): " + BASE_ISBN_URL + "{}", isbn);
|
||||
Document doc = fetchDoc(BASE_ISBN_URL + isbn);
|
||||
|
||||
+1
-4
@@ -41,12 +41,9 @@ public class GoogleParser implements BookParser {
|
||||
|
||||
@Override
|
||||
public List<BookMetadata> fetchMetadata(Book book, FetchMetadataRequest fetchMetadataRequest) {
|
||||
// 1. If ISBN exists, prioritize it
|
||||
if (fetchMetadataRequest.getIsbn() != null && !fetchMetadataRequest.getIsbn().isBlank()) {
|
||||
return getMetadataListByIsbn(fetchMetadataRequest.getIsbn());
|
||||
return getMetadataListByIsbn(ParserUtils.cleanIsbn(fetchMetadataRequest.getIsbn()));
|
||||
}
|
||||
|
||||
// 2. Otherwise fallback to existing title/author search
|
||||
String searchTerm = getSearchTerm(book, fetchMetadataRequest);
|
||||
return searchTerm != null ? getMetadataListByTerm(searchTerm) : List.of();
|
||||
}
|
||||
|
||||
+2
-2
@@ -27,8 +27,8 @@ public class HardcoverParser implements BookParser {
|
||||
|
||||
@Override
|
||||
public List<BookMetadata> fetchMetadata(Book book, FetchMetadataRequest fetchMetadataRequest) {
|
||||
|
||||
boolean searchByIsbn = fetchMetadataRequest.getIsbn() != null && !fetchMetadataRequest.getIsbn().isBlank();
|
||||
String isbnCleaned = ParserUtils.cleanIsbn(fetchMetadataRequest.getIsbn());
|
||||
boolean searchByIsbn = isbnCleaned != null && !isbnCleaned.isBlank();
|
||||
|
||||
List<GraphQLResponse.Hit> hits;
|
||||
if (searchByIsbn) {
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.adityachandel.booklore.service.metadata.parser;
|
||||
|
||||
public class ParserUtils {
|
||||
|
||||
public static String cleanIsbn(String isbn) {
|
||||
if (isbn == null) return null;
|
||||
return isbn.replaceAll("[^0-9]", "");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user