fix(amazon): improve localization of Amazon Parser (#1706)
* fix(amazon): review date parsing to support Japanese formats Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * refactor: enhance Amazon metadata parser robustness and domain support - Add missing Amazon domains (AE, SA, CN, SG, TR, EG, BE) to DOMAIN_LOCALE_MAP. - Implement fallback selectors for Title, Authors, Description, Publisher, Publication Date, and ISBNs to support legacy and variant page layouts. - Support localized 'Publisher' labels (e.g., Herausgeber, Éditeur, Uitgever) in detail bullets. - Fix brittle date parsing by searching for dates in detail bullets when RPI attributes are missing. - Improve author extraction with multiple fallback strategies. --------- Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
+182
-46
@@ -38,10 +38,11 @@ public class AmazonBookParser implements BookParser {
|
|||||||
private static final Pattern SERIES_FORMAT_PATTERN = Pattern.compile("Book \\d+ of \\d+");
|
private static final Pattern SERIES_FORMAT_PATTERN = Pattern.compile("Book \\d+ of \\d+");
|
||||||
private static final Pattern SERIES_FORMAT_WITH_DECIMAL_PATTERN = Pattern.compile("Book \\d+(\\.\\d+)? of \\d+");
|
private static final Pattern SERIES_FORMAT_WITH_DECIMAL_PATTERN = Pattern.compile("Book \\d+(\\.\\d+)? of \\d+");
|
||||||
private static final Pattern PARENTHESES_WITH_WHITESPACE_PATTERN = Pattern.compile("\\s*\\(.*?\\)");
|
private static final Pattern PARENTHESES_WITH_WHITESPACE_PATTERN = Pattern.compile("\\s*\\(.*?\\)");
|
||||||
private static final Pattern NON_ALPHANUMERIC_PATTERN = Pattern.compile("[^a-zA-Z0-9]");
|
private static final Pattern NON_ALPHANUMERIC_PATTERN = Pattern.compile("[^\\p{L}\\p{M}0-9]");
|
||||||
private static final Pattern DP_SEPARATOR_PATTERN = Pattern.compile("/dp/");
|
private static final Pattern DP_SEPARATOR_PATTERN = Pattern.compile("/dp/");
|
||||||
// Pattern to extract country and date from strings like "Reviewed in ... on ..."
|
// Pattern to extract country and date from strings like "Reviewed in ... on ..." or Japanese format
|
||||||
private static final Pattern REVIEWED_IN_ON_PATTERN = Pattern.compile("Reviewed in (.+?) on (.+)");
|
private static final Pattern REVIEWED_IN_ON_PATTERN = Pattern.compile("(?i)(?:Reviewed in|Rezension aus|Beoordeeld in|Recensie uit|Commenté en|Recensito in|Revisado en)\\s+(.+?)\\s+(?:on|vom|op|le|il|el)\\s+(.+)");
|
||||||
|
private static final Pattern JAPANESE_REVIEW_DATE_PATTERN = Pattern.compile("(\\d{4}年\\d{1,2}月\\d{1,2}日).+");
|
||||||
|
|
||||||
private static final Map<String, LocaleInfo> DOMAIN_LOCALE_MAP = Map.ofEntries(
|
private static final Map<String, LocaleInfo> DOMAIN_LOCALE_MAP = Map.ofEntries(
|
||||||
Map.entry("com", new LocaleInfo("en-US,en;q=0.9", Locale.US)),
|
Map.entry("com", new LocaleInfo("en-US,en;q=0.9", Locale.US)),
|
||||||
@@ -58,7 +59,14 @@ public class AmazonBookParser implements BookParser {
|
|||||||
Map.entry("com.mx", new LocaleInfo("en-US,en;q=0.9,es;q=0.8", new Locale.Builder().setLanguage("es").setRegion("MX").build())),
|
Map.entry("com.mx", new LocaleInfo("en-US,en;q=0.9,es;q=0.8", new Locale.Builder().setLanguage("es").setRegion("MX").build())),
|
||||||
Map.entry("nl", new LocaleInfo("en-GB,en;q=0.9,nl;q=0.8", new Locale.Builder().setLanguage("nl").setRegion("NL").build())),
|
Map.entry("nl", new LocaleInfo("en-GB,en;q=0.9,nl;q=0.8", new Locale.Builder().setLanguage("nl").setRegion("NL").build())),
|
||||||
Map.entry("se", new LocaleInfo("en-GB,en;q=0.9,sv;q=0.8", new Locale.Builder().setLanguage("sv").setRegion("SE").build())),
|
Map.entry("se", new LocaleInfo("en-GB,en;q=0.9,sv;q=0.8", new Locale.Builder().setLanguage("sv").setRegion("SE").build())),
|
||||||
Map.entry("pl", new LocaleInfo("en-GB,en;q=0.9,pl;q=0.8", new Locale.Builder().setLanguage("pl").setRegion("PL").build()))
|
Map.entry("pl", new LocaleInfo("en-GB,en;q=0.9,pl;q=0.8", new Locale.Builder().setLanguage("pl").setRegion("PL").build())),
|
||||||
|
Map.entry("ae", new LocaleInfo("en-US,en;q=0.9,ar;q=0.8", new Locale.Builder().setLanguage("en").setRegion("AE").build())),
|
||||||
|
Map.entry("sa", new LocaleInfo("en-US,en;q=0.9,ar;q=0.8", new Locale.Builder().setLanguage("en").setRegion("SA").build())),
|
||||||
|
Map.entry("cn", new LocaleInfo("zh-CN,zh;q=0.9", Locale.CHINA)),
|
||||||
|
Map.entry("sg", new LocaleInfo("en-GB,en;q=0.9", new Locale.Builder().setLanguage("en").setRegion("SG").build())),
|
||||||
|
Map.entry("tr", new LocaleInfo("en-GB,en;q=0.9,tr;q=0.8", new Locale.Builder().setLanguage("tr").setRegion("TR").build())),
|
||||||
|
Map.entry("eg", new LocaleInfo("en-US,en;q=0.9,ar;q=0.8", new Locale.Builder().setLanguage("en").setRegion("EG").build())),
|
||||||
|
Map.entry("com.be", new LocaleInfo("en-GB,en;q=0.9,fr;q=0.8,nl;q=0.8", new Locale.Builder().setLanguage("fr").setRegion("BE").build()))
|
||||||
);
|
);
|
||||||
|
|
||||||
private final AppSettingService appSettingService;
|
private final AppSettingService appSettingService;
|
||||||
@@ -281,56 +289,104 @@ public class AmazonBookParser implements BookParser {
|
|||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getTitle(Document doc) {
|
private String getTextBySelectors(Document doc, String... selectors) {
|
||||||
Element titleElement = doc.getElementById("productTitle");
|
for (String selector : selectors) {
|
||||||
if (titleElement != null) {
|
try {
|
||||||
String fullTitle = titleElement.text();
|
Element element = doc.selectFirst(selector);
|
||||||
return fullTitle.split(":", 2)[0].trim();
|
if (element != null && !element.text().isBlank()) {
|
||||||
|
return element.text().trim();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.debug("Failed to extract text with selector '{}': {}", selector, e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
log.warn("Failed to parse title: Element not found.");
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getTitle(Document doc) {
|
||||||
|
String title = getTextBySelectors(doc,
|
||||||
|
"#productTitle",
|
||||||
|
"#ebooksProductTitle",
|
||||||
|
"h1#title",
|
||||||
|
"span#productTitle"
|
||||||
|
);
|
||||||
|
if (title != null) {
|
||||||
|
return title.split(":", 2)[0].trim();
|
||||||
|
}
|
||||||
|
log.warn("Failed to parse title: No suitable element found.");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getSubtitle(Document doc) {
|
private String getSubtitle(Document doc) {
|
||||||
Element titleElement = doc.getElementById("productTitle");
|
String title = getTextBySelectors(doc,
|
||||||
if (titleElement != null) {
|
"#productTitle",
|
||||||
String fullTitle = titleElement.text();
|
"#ebooksProductTitle",
|
||||||
String[] parts = fullTitle.split(":", 2);
|
"h1#title",
|
||||||
|
"span#productTitle"
|
||||||
|
);
|
||||||
|
if (title != null) {
|
||||||
|
String[] parts = title.split(":", 2);
|
||||||
if (parts.length > 1) {
|
if (parts.length > 1) {
|
||||||
return parts[1].trim();
|
return parts[1].trim();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.warn("Failed to parse subtitle: No suitable element found.");
|
||||||
log.warn("Failed to parse subtitle: Element not found.");
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<String> getAuthors(Document doc) {
|
private Set<String> getAuthors(Document doc) {
|
||||||
|
Set<String> authors = new HashSet<>();
|
||||||
try {
|
try {
|
||||||
Element bylineDiv = doc.select("#bylineInfo_feature_div").first();
|
// Primary strategy: #bylineInfo_feature_div .author a
|
||||||
|
Element bylineDiv = doc.selectFirst("#bylineInfo_feature_div");
|
||||||
if (bylineDiv != null) {
|
if (bylineDiv != null) {
|
||||||
return bylineDiv
|
authors.addAll(bylineDiv.select(".author a").stream().map(Element::text).collect(Collectors.toSet()));
|
||||||
.select(".author a")
|
}
|
||||||
.stream()
|
|
||||||
.map(Element::text)
|
// Fallback: #bylineInfo .author a
|
||||||
.collect(Collectors.toSet());
|
if (authors.isEmpty()) {
|
||||||
|
Element bylineInfo = doc.selectFirst("#bylineInfo");
|
||||||
|
if (bylineInfo != null) {
|
||||||
|
authors.addAll(bylineInfo.select(".author a").stream().map(Element::text).collect(Collectors.toSet()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: simple .author a check (broadest)
|
||||||
|
if (authors.isEmpty()) {
|
||||||
|
authors.addAll(doc.select(".author a").stream().map(Element::text).collect(Collectors.toSet()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (authors.isEmpty()) {
|
||||||
|
log.warn("Failed to parse authors: No author elements found.");
|
||||||
}
|
}
|
||||||
log.warn("Failed to parse authors: Byline element not found.");
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("Failed to parse authors: {}", e.getMessage());
|
log.warn("Failed to parse authors: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
return Set.of();
|
return authors;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getDescription(Document doc) {
|
private String getDescription(Document doc) {
|
||||||
try {
|
try {
|
||||||
|
// Primary: data-a-expander-name
|
||||||
Elements descriptionElements = doc.select("[data-a-expander-name=book_description_expander] .a-expander-content");
|
Elements descriptionElements = doc.select("[data-a-expander-name=book_description_expander] .a-expander-content");
|
||||||
if (!descriptionElements.isEmpty()) {
|
if (!descriptionElements.isEmpty()) {
|
||||||
String html = descriptionElements.getFirst().html();
|
String html = descriptionElements.getFirst().html();
|
||||||
html = html.replace("\n", "<br>");
|
html = html.replace("\n", "<br>");
|
||||||
return html;
|
return html;
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
// Fallback: #bookDescription_feature_div noscript (often contains the clean HTML)
|
||||||
|
Element noscriptDesc = doc.selectFirst("#bookDescription_feature_div noscript");
|
||||||
|
if (noscriptDesc != null) {
|
||||||
|
return noscriptDesc.html(); // usually clean HTML inside noscript
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: div.product-description
|
||||||
|
Element simpleDesc = doc.selectFirst("div.product-description");
|
||||||
|
if (simpleDesc != null) {
|
||||||
|
return simpleDesc.html();
|
||||||
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("Failed to parse description: {}", e.getMessage());
|
log.warn("Failed to parse description: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
@@ -338,29 +394,41 @@ public class AmazonBookParser implements BookParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getIsbn10(Document doc) {
|
private String getIsbn10(Document doc) {
|
||||||
|
// Strategy 1: RPI attribute
|
||||||
try {
|
try {
|
||||||
Element isbn10Element = doc.select("#rpi-attribute-book_details-isbn10 .rpi-attribute-value span").first();
|
Element isbn10Element = doc.select("#rpi-attribute-book_details-isbn10 .rpi-attribute-value span").first();
|
||||||
if (isbn10Element != null) {
|
if (isbn10Element != null) {
|
||||||
String rawIsbn = isbn10Element.text();
|
return ParserUtils.cleanIsbn(isbn10Element.text());
|
||||||
return ParserUtils.cleanIsbn(rawIsbn);
|
|
||||||
}
|
}
|
||||||
log.warn("Failed to parse isbn10: Element not found.");
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("Failed to parse isbn10: {}", e.getMessage());
|
log.debug("RPI ISBN-10 extraction failed: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strategy 2: Detail bullets
|
||||||
|
try {
|
||||||
|
return extractFromDetailBullets(doc, "ISBN-10");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("DetailBullets ISBN-10 extraction failed: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getIsbn13(Document doc) {
|
private String getIsbn13(Document doc) {
|
||||||
|
// Strategy 1: RPI attribute
|
||||||
try {
|
try {
|
||||||
Element isbn13Element = doc.select("#rpi-attribute-book_details-isbn13 .rpi-attribute-value span").first();
|
Element isbn13Element = doc.select("#rpi-attribute-book_details-isbn13 .rpi-attribute-value span").first();
|
||||||
if (isbn13Element != null) {
|
if (isbn13Element != null) {
|
||||||
String rawIsbn = isbn13Element.text();
|
return ParserUtils.cleanIsbn(isbn13Element.text());
|
||||||
return ParserUtils.cleanIsbn(rawIsbn);
|
|
||||||
}
|
}
|
||||||
log.warn("Failed to parse isbn13: Element not found.");
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("Failed to parse isbn13: {}", e.getMessage());
|
log.debug("RPI ISBN-13 extraction failed: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strategy 2: Detail bullets
|
||||||
|
try {
|
||||||
|
return extractFromDetailBullets(doc, "ISBN-13");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("DetailBullets ISBN-13 extraction failed: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -372,16 +440,29 @@ public class AmazonBookParser implements BookParser {
|
|||||||
Elements listItems = featureElement.select("li");
|
Elements listItems = featureElement.select("li");
|
||||||
for (Element listItem : listItems) {
|
for (Element listItem : listItems) {
|
||||||
Element boldText = listItem.selectFirst("span.a-text-bold");
|
Element boldText = listItem.selectFirst("span.a-text-bold");
|
||||||
if (boldText != null && boldText.text().contains("Publisher")) {
|
if (boldText != null) {
|
||||||
Element publisherSpan = boldText.nextElementSibling();
|
String header = boldText.text().toLowerCase();
|
||||||
if (publisherSpan != null) {
|
// Check against known localized "Publisher" labels
|
||||||
String fullPublisher = publisherSpan.text().trim();
|
if (header.contains("publisher") ||
|
||||||
return PARENTHESES_WITH_WHITESPACE_PATTERN.matcher(fullPublisher.split(";")[0].trim()).replaceAll("").trim();
|
header.contains("herausgeber") ||
|
||||||
|
header.contains("éditeur") ||
|
||||||
|
header.contains("editoriale") ||
|
||||||
|
header.contains("editorial") ||
|
||||||
|
header.contains("uitgever") ||
|
||||||
|
header.contains("wydawca") ||
|
||||||
|
header.contains("出版社") || // Japanese
|
||||||
|
header.contains("editora")) {
|
||||||
|
|
||||||
|
Element publisherSpan = boldText.nextElementSibling();
|
||||||
|
if (publisherSpan != null) {
|
||||||
|
String fullPublisher = publisherSpan.text().trim();
|
||||||
|
return PARENTHESES_WITH_WHITESPACE_PATTERN.matcher(fullPublisher.split(";")[0].trim()).replaceAll("").trim();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.warn("Failed to parse publisher: Element 'detailBullets_feature_div' not found.");
|
log.debug("Failed to parse publisher: Element 'detailBullets_feature_div' not found.");
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("Failed to parse publisher: {}", e.getMessage());
|
log.warn("Failed to parse publisher: {}", e.getMessage());
|
||||||
@@ -390,19 +471,63 @@ public class AmazonBookParser implements BookParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private LocalDate getPublicationDate(Document doc) {
|
private LocalDate getPublicationDate(Document doc) {
|
||||||
|
// Strategy 1: RPI attribute
|
||||||
try {
|
try {
|
||||||
Element publicationDateElement = doc.select("#rpi-attribute-book_details-publication_date .rpi-attribute-value span").first();
|
Element publicationDateElement = doc.select("#rpi-attribute-book_details-publication_date .rpi-attribute-value span").first();
|
||||||
if (publicationDateElement != null) {
|
if (publicationDateElement != null) {
|
||||||
String dateText = publicationDateElement.text();
|
String dateText = publicationDateElement.text();
|
||||||
LocalDate parsedDate = parseAmazonDate(dateText);
|
LocalDate parsedDate = parseAmazonDate(dateText);
|
||||||
if (parsedDate == null) {
|
if (parsedDate != null) return parsedDate;
|
||||||
log.warn("Failed to parse publication date: '{}', returning null", dateText);
|
|
||||||
}
|
|
||||||
return parsedDate;
|
|
||||||
}
|
}
|
||||||
log.warn("Failed to parse publishedDate: Element not found.");
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("Failed to parse publishedDate: {}", e.getMessage());
|
log.debug("RPI Publication Date extraction failed: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strategy 2: Detail bullets (look for specific date patterns in values)
|
||||||
|
try {
|
||||||
|
Element featureElement = doc.getElementById("detailBullets_feature_div");
|
||||||
|
if (featureElement != null) {
|
||||||
|
Elements listItems = featureElement.select("li");
|
||||||
|
for (Element listItem : listItems) {
|
||||||
|
// We look for any value that parses as a date, as the label varies wildly ("Publication date", "Seitenzahl"?? no, "Erscheinungsdatum", etc.)
|
||||||
|
// But usually it's associated with "Publisher" line sometimes: "Publisher: XYZ (Jan 1, 2020)"
|
||||||
|
Element boldText = listItem.selectFirst("span.a-text-bold");
|
||||||
|
Element valueSpan = boldText != null ? boldText.nextElementSibling() : null;
|
||||||
|
|
||||||
|
if (valueSpan != null) {
|
||||||
|
// Sometimes date is inside the value span, e.g. "January 1, 2020"
|
||||||
|
LocalDate d = parseAmazonDate(valueSpan.text());
|
||||||
|
if (d != null) return d;
|
||||||
|
|
||||||
|
// Sometimes it's in parentheses after publisher: "Publisher: Name (January 1, 2020)"
|
||||||
|
Matcher matcher = Pattern.compile("\\((.*?)\\)").matcher(valueSpan.text());
|
||||||
|
while (matcher.find()) {
|
||||||
|
LocalDate pd = parseAmazonDate(matcher.group(1));
|
||||||
|
if (pd != null) return pd;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("DetailBullets Publication Date extraction failed: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String extractFromDetailBullets(Document doc, String keyPart) {
|
||||||
|
Element featureElement = doc.getElementById("detailBullets_feature_div");
|
||||||
|
if (featureElement != null) {
|
||||||
|
Elements listItems = featureElement.select("li");
|
||||||
|
for (Element listItem : listItems) {
|
||||||
|
Element boldText = listItem.selectFirst("span.a-text-bold");
|
||||||
|
if (boldText != null && boldText.text().contains(keyPart)) {
|
||||||
|
Element valueSpan = boldText.nextElementSibling();
|
||||||
|
if (valueSpan != null) {
|
||||||
|
return ParserUtils.cleanIsbn(valueSpan.text());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -568,6 +693,13 @@ public class AmazonBookParser implements BookParser {
|
|||||||
country = country.substring(4).trim();
|
country = country.substring(4).trim();
|
||||||
}
|
}
|
||||||
datePart = matcher.group(2).trim();
|
datePart = matcher.group(2).trim();
|
||||||
|
} else {
|
||||||
|
// Try Japanese format
|
||||||
|
Matcher japaneseMatcher = JAPANESE_REVIEW_DATE_PATTERN.matcher(fullDateText);
|
||||||
|
if (japaneseMatcher.find()) {
|
||||||
|
datePart = japaneseMatcher.group(1);
|
||||||
|
country = "日本";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LocalDate localDate = parseReviewDate(datePart, localeInfo);
|
LocalDate localDate = parseReviewDate(datePart, localeInfo);
|
||||||
@@ -735,7 +867,11 @@ public class AmazonBookParser implements BookParser {
|
|||||||
"MMM. d, yyyy",
|
"MMM. d, yyyy",
|
||||||
"d MMM yyyy",
|
"d MMM yyyy",
|
||||||
"d MMM. yyyy",
|
"d MMM. yyyy",
|
||||||
"d. MMM yyyy"
|
"d. MMM yyyy",
|
||||||
|
// Japanese date patterns
|
||||||
|
"yyyy/M/d",
|
||||||
|
"yyyy/MM/dd",
|
||||||
|
"yyyy年M月d日"
|
||||||
};
|
};
|
||||||
|
|
||||||
for (String pattern : patterns) {
|
for (String pattern : patterns) {
|
||||||
|
|||||||
Reference in New Issue
Block a user