refactor(metadata): avoid regex for AmazonBookParser (#926)
This commit is contained in:
@@ -14,6 +14,8 @@ import org.jsoup.HttpStatusException;
|
|||||||
import org.jsoup.Jsoup;
|
import org.jsoup.Jsoup;
|
||||||
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Document;
|
||||||
import org.jsoup.nodes.Element;
|
import org.jsoup.nodes.Element;
|
||||||
|
import org.jsoup.nodes.Node;
|
||||||
|
import org.jsoup.nodes.TextNode;
|
||||||
import org.jsoup.select.Elements;
|
import org.jsoup.select.Elements;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -34,10 +36,6 @@ import java.util.stream.Collectors;
|
|||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class AmazonBookParser implements BookParser, DetailedMetadataProvider {
|
public class AmazonBookParser implements BookParser, DetailedMetadataProvider {
|
||||||
private static final Pattern ASIN_PATTERN = Pattern.compile("([A-Z0-9]{10})");
|
private static final Pattern ASIN_PATTERN = Pattern.compile("([A-Z0-9]{10})");
|
||||||
private static final Pattern TRAILING_BR_TAGS_PATTERN = Pattern.compile("(\\s*<br\\s*/?>\\s*)+$");
|
|
||||||
private static final Pattern LEADING_BR_TAGS_PATTERN = Pattern.compile("^(\\s*<br\\s*/?>\\s*)+");
|
|
||||||
private static final Pattern MULTIPLE_BR_TAGS_PATTERN = Pattern.compile("(<br\\s*/?>\\s*){3,}");
|
|
||||||
private static final Pattern MULTIPLE_BR_CLOSING_TAGS_PATTERN = Pattern.compile("(<br>\\s*){3,}");
|
|
||||||
|
|
||||||
private static class AmazonAntiScrapingException extends RuntimeException {
|
private static class AmazonAntiScrapingException extends RuntimeException {
|
||||||
public AmazonAntiScrapingException(String message) {
|
public AmazonAntiScrapingException(String message) {
|
||||||
@@ -885,6 +883,18 @@ public class AmazonBookParser implements BookParser, DetailedMetadataProvider {
|
|||||||
return parseDate(dateString, getLocaleInfoForDomain(domain));
|
return parseDate(dateString, getLocaleInfoForDomain(domain));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isWhitespaceNode(Node node) {
|
||||||
|
if (node instanceof TextNode textNode) {
|
||||||
|
return textNode.isBlank();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node instanceof Element element) {
|
||||||
|
return "br".equals(element.tagName());
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private String cleanDescriptionHtml(String html) {
|
private String cleanDescriptionHtml(String html) {
|
||||||
try {
|
try {
|
||||||
Document document = Jsoup.parse(html);
|
Document document = Jsoup.parse(html);
|
||||||
@@ -914,8 +924,7 @@ public class AmazonBookParser implements BookParser, DetailedMetadataProvider {
|
|||||||
|
|
||||||
// Remove excessive line breaks (more than 2 consecutive <br> tags)
|
// Remove excessive line breaks (more than 2 consecutive <br> tags)
|
||||||
Elements brTags = document.select("br");
|
Elements brTags = document.select("br");
|
||||||
for (int i = 0; i < brTags.size(); i++) {
|
for (Element br : brTags) {
|
||||||
Element br = brTags.get(i);
|
|
||||||
int consecutiveBrCount = 1;
|
int consecutiveBrCount = 1;
|
||||||
Element next = br.nextElementSibling();
|
Element next = br.nextElementSibling();
|
||||||
|
|
||||||
@@ -932,18 +941,17 @@ public class AmazonBookParser implements BookParser, DetailedMetadataProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// "trim" the start and end of the document of whitespace & <br>
|
||||||
|
Node node;
|
||||||
|
while ((node = document.body().firstChild()) != null && isWhitespaceNode(node)) {
|
||||||
|
node.remove();
|
||||||
|
}
|
||||||
|
while ((node = document.body().lastChild()) != null && isWhitespaceNode(node)) {
|
||||||
|
node.remove();
|
||||||
|
}
|
||||||
|
|
||||||
// Clean up any remaining whitespace issues
|
// Clean up any remaining whitespace issues
|
||||||
String cleanedHtml = document.body().html();
|
return document.body().html().trim();
|
||||||
|
|
||||||
// Replace multiple consecutive <br> patterns that might still exist
|
|
||||||
cleanedHtml = MULTIPLE_BR_CLOSING_TAGS_PATTERN.matcher(cleanedHtml).replaceAll("<br><br>");
|
|
||||||
cleanedHtml = MULTIPLE_BR_TAGS_PATTERN.matcher(cleanedHtml).replaceAll("<br><br>");
|
|
||||||
|
|
||||||
// Remove leading/trailing <br> tags
|
|
||||||
cleanedHtml = LEADING_BR_TAGS_PATTERN.matcher(cleanedHtml).replaceAll("");
|
|
||||||
cleanedHtml = TRAILING_BR_TAGS_PATTERN.matcher(cleanedHtml).replaceAll("");
|
|
||||||
|
|
||||||
return cleanedHtml;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("Error cleaning html description, Error: {}", e.getMessage());
|
log.warn("Error cleaning html description, Error: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
@@ -192,4 +193,29 @@ public class AmazonBookParserTest {
|
|||||||
|
|
||||||
mockJsoup.verify(() -> Jsoup.connect("https://www.amazon.com/dp/SEARCHASIN"));
|
mockJsoup.verify(() -> Jsoup.connect("https://www.amazon.com/dp/SEARCHASIN"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void fetchTopMetadata_cleansLineBreaks() throws Exception {
|
||||||
|
String fakePageHtml = """
|
||||||
|
<html><body>
|
||||||
|
<div class="product-description">
|
||||||
|
<br /><br ><br><div>Hello</div><br /><br /><br><div>World</div><br /><br ><br>
|
||||||
|
</div>
|
||||||
|
</body></html>
|
||||||
|
""";
|
||||||
|
|
||||||
|
mockAmazonIDSearch("Example");
|
||||||
|
mockJsoupConnect(
|
||||||
|
"https://www.amazon.com/dp/SEARCHASIN", fakePageHtml);
|
||||||
|
|
||||||
|
Book book = getBook(null);
|
||||||
|
FetchMetadataRequest fetchMetadataRequest = FetchMetadataRequest.builder().title("Example").build();
|
||||||
|
|
||||||
|
mockJsoup.when(() -> Jsoup.parse(anyString())).thenCallRealMethod();
|
||||||
|
|
||||||
|
BookMetadata metadata = amazonBookParser.fetchTopMetadata(book, fetchMetadataRequest);
|
||||||
|
|
||||||
|
String actual = metadata.getDescription().replace("\n", "");
|
||||||
|
assertEquals("<div>Hello</div><br><br><div>World</div>", actual);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user