refactor(amazon): use predefined domains (#2367)
Signed-off-by: king-407 <95581750+king-407@users.noreply.github.com> Co-authored-by: James Ward <james@notjam.es>
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
package org.booklore.service.metadata.parser;
|
||||
|
||||
import org.booklore.exception.ApiError;
|
||||
import org.booklore.model.dto.Book;
|
||||
import org.booklore.model.dto.BookMetadata;
|
||||
import org.booklore.model.dto.BookReview;
|
||||
import org.booklore.model.dto.request.FetchMetadataRequest;
|
||||
import org.booklore.model.dto.settings.AppSettings;
|
||||
import org.booklore.model.dto.settings.MetadataProviderSettings;
|
||||
import org.booklore.model.dto.settings.MetadataPublicReviewsSettings;
|
||||
import org.booklore.model.enums.MetadataProvider;
|
||||
import org.booklore.service.appsettings.AppSettingService;
|
||||
import org.booklore.util.BookUtils;
|
||||
@@ -68,6 +72,33 @@ public class AmazonBookParser implements BookParser, DetailedMetadataProvider {
|
||||
"yyyy/M/d", "yyyy/MM/dd", "yyyy年M月d日"
|
||||
};
|
||||
|
||||
private static final String DEFAULT_DOMAIN = "com";
|
||||
|
||||
private static final Map<String, String> BASE_URIS = Map.ofEntries(
|
||||
Map.entry("com", "https://www.amazon.com"),
|
||||
Map.entry("co.uk", "https://www.amazon.co.uk"),
|
||||
Map.entry("de", "https://www.amazon.de"),
|
||||
Map.entry("fr", "https://www.amazon.fr"),
|
||||
Map.entry("it", "https://www.amazon.it"),
|
||||
Map.entry("es", "https://www.amazon.es"),
|
||||
Map.entry("ca", "https://www.amazon.ca"),
|
||||
Map.entry("com.au", "https://www.amazon.com.au"),
|
||||
Map.entry("co.jp", "https://www.amazon.co.jp"),
|
||||
Map.entry("in", "https://www.amazon.in"),
|
||||
Map.entry("com.br", "https://www.amazon.com.br"),
|
||||
Map.entry("com.mx", "https://www.amazon.com.mx"),
|
||||
Map.entry("nl", "https://www.amazon.nl"),
|
||||
Map.entry("se", "https://www.amazon.se"),
|
||||
Map.entry("pl", "https://www.amazon.pl"),
|
||||
Map.entry("ae", "https://www.amazon.ae"),
|
||||
Map.entry("sa", "https://www.amazon.sa"),
|
||||
Map.entry("cn", "https://www.amazon.cn"),
|
||||
Map.entry("sg", "https://www.amazon.sg"),
|
||||
Map.entry("tr", "https://www.amazon.com.tr"),
|
||||
Map.entry("eg", "https://www.amazon.eg"),
|
||||
Map.entry("com.be", "https://www.amazon.com.be")
|
||||
);
|
||||
|
||||
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("co.uk", new LocaleInfo("en-GB,en;q=0.9", Locale.UK)),
|
||||
@@ -93,8 +124,6 @@ public class AmazonBookParser implements BookParser, DetailedMetadataProvider {
|
||||
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 static final LocaleInfo DEFAULT_LOCALE_INFO = new LocaleInfo("en-US,en;q=0.9", Locale.US);
|
||||
|
||||
private final AppSettingService appSettingService;
|
||||
|
||||
private record LocaleInfo(String acceptLanguage, Locale locale) {}
|
||||
@@ -270,18 +299,15 @@ public class AmazonBookParser implements BookParser, DetailedMetadataProvider {
|
||||
private BookMetadata getBookMetadata(String amazonBookId) {
|
||||
log.info("Amazon: Fetching metadata for: {}", amazonBookId);
|
||||
|
||||
String domain = appSettingService.getAppSettings().getMetadataProviderSettings().getAmazon().getDomain();
|
||||
Document doc;
|
||||
try {
|
||||
doc = fetchDocument("https://www.amazon." + domain + BASE_BOOK_URL_SUFFIX + amazonBookId);
|
||||
doc = fetchDocument(getBaseURI() + BASE_BOOK_URL_SUFFIX + amazonBookId);
|
||||
} catch (AmazonAntiScrapingException e) {
|
||||
log.debug("Aborting metadata fetch for ID {} due to status code (503).", amazonBookId);
|
||||
return null;
|
||||
}
|
||||
|
||||
List<BookReview> reviews = appSettingService.getAppSettings()
|
||||
.getMetadataPublicReviewsSettings()
|
||||
.getProviders()
|
||||
List<BookReview> reviews = getReviewProviderConfigs()
|
||||
.stream()
|
||||
.filter(cfg -> cfg.getProvider() == MetadataProvider.Amazon && cfg.isEnabled())
|
||||
.findFirst()
|
||||
@@ -320,10 +346,10 @@ public class AmazonBookParser implements BookParser, DetailedMetadataProvider {
|
||||
}
|
||||
|
||||
private String buildQueryUrl(FetchMetadataRequest fetchMetadataRequest, Book book) {
|
||||
String domain = appSettingService.getAppSettings().getMetadataProviderSettings().getAmazon().getDomain();
|
||||
String baseURI = getBaseURI();
|
||||
String isbnCleaned = ParserUtils.cleanIsbn(fetchMetadataRequest.getIsbn());
|
||||
if (isbnCleaned != null && !isbnCleaned.isEmpty()) {
|
||||
String url = "https://www.amazon." + domain + "/s?k=" + fetchMetadataRequest.getIsbn();
|
||||
String url = baseURI + "/s?k=" + fetchMetadataRequest.getIsbn();
|
||||
log.info("Amazon Query URL (ISBN): {}", url);
|
||||
return url;
|
||||
}
|
||||
@@ -353,7 +379,7 @@ public class AmazonBookParser implements BookParser, DetailedMetadataProvider {
|
||||
}
|
||||
|
||||
String encodedSearchTerm = searchTerm.toString().replace(" ", "+");
|
||||
String url = "https://www.amazon." + domain + "/s?k=" + encodedSearchTerm;
|
||||
String url = baseURI + "/s?k=" + encodedSearchTerm;
|
||||
log.info("Amazon Query URL: {}", url);
|
||||
return url;
|
||||
}
|
||||
@@ -639,8 +665,6 @@ public class AmazonBookParser implements BookParser, DetailedMetadataProvider {
|
||||
|
||||
private List<BookReview> getReviews(Document doc, int maxReviews) {
|
||||
List<BookReview> reviews = new ArrayList<>();
|
||||
String domain = appSettingService.getAppSettings().getMetadataProviderSettings().getAmazon().getDomain();
|
||||
LocaleInfo localeInfo = getLocaleInfoForDomain(domain);
|
||||
|
||||
try {
|
||||
Elements reviewElements = doc.select("li[data-hook=review]");
|
||||
@@ -701,7 +725,7 @@ public class AmazonBookParser implements BookParser, DetailedMetadataProvider {
|
||||
}
|
||||
}
|
||||
|
||||
LocalDate localDate = parseDate(datePart, localeInfo);
|
||||
LocalDate localDate = parseDate(datePart);
|
||||
if (localDate != null) {
|
||||
dateInstant = localDate.atStartOfDay(ZoneOffset.UTC).toInstant();
|
||||
}
|
||||
@@ -803,10 +827,9 @@ public class AmazonBookParser implements BookParser, DetailedMetadataProvider {
|
||||
|
||||
private Document fetchDocument(String url) {
|
||||
try {
|
||||
String domain = appSettingService.getAppSettings().getMetadataProviderSettings().getAmazon().getDomain();
|
||||
String amazonCookie = appSettingService.getAppSettings().getMetadataProviderSettings().getAmazon().getCookie();
|
||||
|
||||
LocaleInfo localeInfo = getLocaleInfoForDomain(domain);
|
||||
String baseURI = getBaseURI();
|
||||
String amazonCookie = getAmazonCookie();
|
||||
LocaleInfo localeInfo = getLocaleInfo();
|
||||
|
||||
Connection connection = Jsoup.connect(url)
|
||||
.header("accept", "text/html, application/json")
|
||||
@@ -816,7 +839,7 @@ public class AmazonBookParser implements BookParser, DetailedMetadataProvider {
|
||||
.header("downlink", "10")
|
||||
.header("dpr", "2")
|
||||
.header("ect", "4g")
|
||||
.header("origin", "https://www.amazon." + domain)
|
||||
.header("origin", baseURI)
|
||||
.header("priority", "u=1, i")
|
||||
.header("rtt", "50")
|
||||
.header("sec-ch-device-memory", "8")
|
||||
@@ -857,11 +880,64 @@ public class AmazonBookParser implements BookParser, DetailedMetadataProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private static LocaleInfo getLocaleInfoForDomain(String domain) {
|
||||
return DOMAIN_LOCALE_MAP.getOrDefault(domain, DEFAULT_LOCALE_INFO);
|
||||
private LocaleInfo getLocaleInfo() {
|
||||
String domain = getDomain();
|
||||
|
||||
if (!DOMAIN_LOCALE_MAP.containsKey(domain)) {
|
||||
throw ApiError.INVALID_INPUT.createException("Unsupported Amazon domain: " + domain);
|
||||
}
|
||||
|
||||
return DOMAIN_LOCALE_MAP.get(domain);
|
||||
}
|
||||
|
||||
private static LocalDate parseDate(String dateString, LocaleInfo localeInfo) {
|
||||
private Optional<AppSettings> getAppSettings() {
|
||||
return Optional.ofNullable(appSettingService.getAppSettings());
|
||||
}
|
||||
|
||||
private Optional<MetadataProviderSettings.Amazon> getAmazonSettings() {
|
||||
return getAppSettings()
|
||||
.map(AppSettings::getMetadataProviderSettings)
|
||||
.map(MetadataProviderSettings::getAmazon);
|
||||
}
|
||||
|
||||
private String getDomain() {
|
||||
String domain = getAmazonSettings()
|
||||
.map(MetadataProviderSettings.Amazon::getDomain)
|
||||
.orElse(DEFAULT_DOMAIN);
|
||||
|
||||
if (domain.isBlank()) {
|
||||
return DEFAULT_DOMAIN;
|
||||
}
|
||||
|
||||
return domain;
|
||||
}
|
||||
|
||||
private String getAmazonCookie() {
|
||||
return getAmazonSettings()
|
||||
.map(MetadataProviderSettings.Amazon::getCookie)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private Set<MetadataPublicReviewsSettings.ReviewProviderConfig> getReviewProviderConfigs() {
|
||||
return getAppSettings()
|
||||
.map(AppSettings::getMetadataPublicReviewsSettings)
|
||||
.map(MetadataPublicReviewsSettings::getProviders)
|
||||
.orElse(Collections.emptySet());
|
||||
}
|
||||
|
||||
private String getBaseURI() {
|
||||
String domain = getDomain();
|
||||
|
||||
if (!BASE_URIS.containsKey(domain)) {
|
||||
throw ApiError.INVALID_INPUT.createException("Unsupported Amazon domain: " + domain);
|
||||
}
|
||||
|
||||
return BASE_URIS.get(domain);
|
||||
}
|
||||
|
||||
private LocalDate parseDate(String dateString) {
|
||||
LocaleInfo localeInfo = getLocaleInfo();
|
||||
|
||||
if (dateString == null || dateString.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
@@ -888,11 +964,6 @@ public class AmazonBookParser implements BookParser, DetailedMetadataProvider {
|
||||
return null;
|
||||
}
|
||||
|
||||
private LocalDate parseDate(String dateString) {
|
||||
String domain = appSettingService.getAppSettings().getMetadataProviderSettings().getAmazon().getDomain();
|
||||
return parseDate(dateString, getLocaleInfoForDomain(domain));
|
||||
}
|
||||
|
||||
private boolean isWhitespaceNode(Node node) {
|
||||
if (node instanceof TextNode textNode) {
|
||||
return textNode.isBlank();
|
||||
|
||||
Reference in New Issue
Block a user