Improve searching term creation for amazon and goodreads

This commit is contained in:
aditya.chandel
2025-01-16 23:31:43 -07:00
parent 289b6aff36
commit 0a37e0ec65
3 changed files with 24 additions and 24 deletions
@@ -81,6 +81,7 @@ public class AmazonBookParser implements BookParser {
} catch (Exception e) {
log.error("Failed to get asin: {}", e.getMessage(), e);
}
log.info("Amazon: Found {} book ids", bookIds.size());
return bookIds;
}
@@ -90,17 +91,17 @@ public class AmazonBookParser implements BookParser {
Element link = item.select("a:containsOwn(" + type + ")").first();
if (link != null) {
bookLink = link.attr("href");
log.info("{} link found: {}", type, bookLink);
//log.info("{} link found: {}", type, bookLink);
break; // Take the first found link, whether Paperback or Hardcover
} else {
log.info("No link containing '{}' found.", type);
//log.info("No link containing '{}' found.", type);
}
}
if (bookLink != null) {
return extractAsinFromUrl(bookLink);
} else {
String asin = item.attr("data-asin");
log.info("No book link found, returning ASIN: {}", asin);
//log.info("No book link found, returning ASIN: {}", asin);
return asin;
}
}
@@ -149,9 +150,10 @@ public class AmazonBookParser implements BookParser {
// Add title if present, otherwise check filename if title is absent
String title = fetchMetadataRequest.getTitle();
if (title != null && !title.isEmpty()) {
title = BookUtils.cleanAndTruncateSearchTerm(title);
queryBuilder.append("&field-title=").append(title.replace(" ", "%20"));
} else {
String filename = BookUtils.cleanFileName(book.getFileName());
String filename = BookUtils.cleanAndTruncateSearchTerm(BookUtils.cleanFileName(book.getFileName()));
if (filename != null && !filename.isEmpty()) {
queryBuilder.append("&field-title=").append(filename.replace(" ", "%20"));
}
@@ -295,21 +295,9 @@ public class GoodReadsParser implements BookParser {
: (book.getFileName() != null && !book.getFileName().isEmpty()
? BookUtils.cleanFileName(book.getFileName())
: null);
if (searchTerm != null) {
searchTerm = searchTerm.replaceAll("[.,\\-\\[\\]{}()!@#$%^&*_=+|~`<>?/\";:]", "").trim();
if (searchTerm.length() > 60) {
String[] words = searchTerm.split("\\s+");
StringBuilder truncated = new StringBuilder();
for (String word : words) {
if (truncated.length() + word.length() + 1 > 60) break;
if (!truncated.isEmpty()) truncated.append(" ");
truncated.append(word);
}
searchTerm = truncated.toString();
}
BookUtils.cleanAndTruncateSearchTerm(searchTerm);
}
return searchTerm;
}
@@ -7,16 +7,26 @@ public class BookUtils {
return null;
}
fileName = fileName.replace("(Z-Library)", "").trim();
// Remove the author name inside parentheses (e.g. (Jon Yablonski))
fileName = fileName.replaceAll("\\s?\\(.*?\\)", "").trim();
// Remove the file extension (e.g., .pdf, .docx)
int dotIndex = fileName.lastIndexOf('.');
fileName = fileName.replaceAll("\\s?\\(.*?\\)", "").trim(); // Remove the author name inside parentheses (e.g. (Jon Yablonski))
int dotIndex = fileName.lastIndexOf('.'); // Remove the file extension (e.g., .pdf, .docx)
if (dotIndex > 0) {
fileName = fileName.substring(0, dotIndex).trim();
}
return fileName;
}
public static String cleanAndTruncateSearchTerm(String term) {
term = term.replaceAll("[.,\\-\\[\\]{}()!@#$%^&*_=+|~`<>?/\";:]", "").trim();
if (term.length() > 60) {
String[] words = term.split("\\s+");
StringBuilder truncated = new StringBuilder();
for (String word : words) {
if (truncated.length() + word.length() + 1 > 60) break;
if (!truncated.isEmpty()) truncated.append(" ");
truncated.append(word);
}
term = truncated.toString();
}
return term;
}
}