Improve searching term creation for amazon and goodreads
This commit is contained in:
+6
-4
@@ -81,6 +81,7 @@ public class AmazonBookParser implements BookParser {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Failed to get asin: {}", e.getMessage(), e);
|
log.error("Failed to get asin: {}", e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
log.info("Amazon: Found {} book ids", bookIds.size());
|
||||||
return bookIds;
|
return bookIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,17 +91,17 @@ public class AmazonBookParser implements BookParser {
|
|||||||
Element link = item.select("a:containsOwn(" + type + ")").first();
|
Element link = item.select("a:containsOwn(" + type + ")").first();
|
||||||
if (link != null) {
|
if (link != null) {
|
||||||
bookLink = link.attr("href");
|
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
|
break; // Take the first found link, whether Paperback or Hardcover
|
||||||
} else {
|
} else {
|
||||||
log.info("No link containing '{}' found.", type);
|
//log.info("No link containing '{}' found.", type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (bookLink != null) {
|
if (bookLink != null) {
|
||||||
return extractAsinFromUrl(bookLink);
|
return extractAsinFromUrl(bookLink);
|
||||||
} else {
|
} else {
|
||||||
String asin = item.attr("data-asin");
|
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;
|
return asin;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -149,9 +150,10 @@ public class AmazonBookParser implements BookParser {
|
|||||||
// Add title if present, otherwise check filename if title is absent
|
// Add title if present, otherwise check filename if title is absent
|
||||||
String title = fetchMetadataRequest.getTitle();
|
String title = fetchMetadataRequest.getTitle();
|
||||||
if (title != null && !title.isEmpty()) {
|
if (title != null && !title.isEmpty()) {
|
||||||
|
title = BookUtils.cleanAndTruncateSearchTerm(title);
|
||||||
queryBuilder.append("&field-title=").append(title.replace(" ", "%20"));
|
queryBuilder.append("&field-title=").append(title.replace(" ", "%20"));
|
||||||
} else {
|
} else {
|
||||||
String filename = BookUtils.cleanFileName(book.getFileName());
|
String filename = BookUtils.cleanAndTruncateSearchTerm(BookUtils.cleanFileName(book.getFileName()));
|
||||||
if (filename != null && !filename.isEmpty()) {
|
if (filename != null && !filename.isEmpty()) {
|
||||||
queryBuilder.append("&field-title=").append(filename.replace(" ", "%20"));
|
queryBuilder.append("&field-title=").append(filename.replace(" ", "%20"));
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-13
@@ -295,21 +295,9 @@ public class GoodReadsParser implements BookParser {
|
|||||||
: (book.getFileName() != null && !book.getFileName().isEmpty()
|
: (book.getFileName() != null && !book.getFileName().isEmpty()
|
||||||
? BookUtils.cleanFileName(book.getFileName())
|
? BookUtils.cleanFileName(book.getFileName())
|
||||||
: null);
|
: null);
|
||||||
|
|
||||||
if (searchTerm != null) {
|
if (searchTerm != null) {
|
||||||
searchTerm = searchTerm.replaceAll("[.,\\-\\[\\]{}()!@#$%^&*_=+|~`<>?/\";:]", "").trim();
|
BookUtils.cleanAndTruncateSearchTerm(searchTerm);
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return searchTerm;
|
return searchTerm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,16 +7,26 @@ public class BookUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
fileName = fileName.replace("(Z-Library)", "").trim();
|
fileName = fileName.replace("(Z-Library)", "").trim();
|
||||||
|
fileName = fileName.replaceAll("\\s?\\(.*?\\)", "").trim(); // Remove the author name inside parentheses (e.g. (Jon Yablonski))
|
||||||
// Remove the author name inside parentheses (e.g. (Jon Yablonski))
|
int dotIndex = fileName.lastIndexOf('.'); // Remove the file extension (e.g., .pdf, .docx)
|
||||||
fileName = fileName.replaceAll("\\s?\\(.*?\\)", "").trim();
|
|
||||||
|
|
||||||
// Remove the file extension (e.g., .pdf, .docx)
|
|
||||||
int dotIndex = fileName.lastIndexOf('.');
|
|
||||||
if (dotIndex > 0) {
|
if (dotIndex > 0) {
|
||||||
fileName = fileName.substring(0, dotIndex).trim();
|
fileName = fileName.substring(0, dotIndex).trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
return fileName;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user