1
This commit is contained in:
@@ -13,23 +13,15 @@ public class ErpClientSbApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ConfigurableApplicationContext applicationContext = SpringApplication.run(ErpClientSbApplication.class, args);
|
||||
try {
|
||||
ErrorReporter errorReporter = applicationContext.getBean(ErrorReporter.class);
|
||||
Thread.setDefaultUncaughtExceptionHandler((thread, ex) -> {
|
||||
log.error("捕获到未处理异常: " + ex.getMessage(), ex);
|
||||
errorReporter.reportSystemError("未捕获异常: " + thread.getName(), (Exception) ex);
|
||||
});
|
||||
log.info("Started Success");
|
||||
} catch (Exception e) {
|
||||
log.warn("未设置 ErrorReporter,继续启动: {}", e.getMessage());
|
||||
}
|
||||
ErrorReporter errorReporter = applicationContext.getBean(ErrorReporter.class);
|
||||
Thread.setDefaultUncaughtExceptionHandler((thread, ex) -> {
|
||||
log.error("捕获到未处理异常: " + ex.getMessage(), ex);
|
||||
errorReporter.reportSystemError("未捕获异常: " + thread.getName(), (Exception) ex);
|
||||
});
|
||||
log.info("Started Success");
|
||||
ResourcePreloader.init();
|
||||
ResourcePreloader.preloadErpDashboard();
|
||||
ResourcePreloader.executePreloading();
|
||||
|
||||
try {
|
||||
ResourcePreloader.init();
|
||||
ResourcePreloader.preloadErpDashboard();
|
||||
ResourcePreloader.executePreloading();
|
||||
} catch (Throwable t) {
|
||||
log.warn("资源预加载失败: {}", t.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,9 @@ public class AmazonProductEntity {
|
||||
@Column
|
||||
private String asin;
|
||||
|
||||
@Column(name = "region", length = 10)
|
||||
private String region; // 地区代码(JP/US)
|
||||
|
||||
@Column(name = "price")
|
||||
private String price;
|
||||
|
||||
|
||||
@@ -21,10 +21,10 @@ import java.util.Optional;
|
||||
public interface AmazonProductRepository extends JpaRepository<AmazonProductEntity, Long> {
|
||||
|
||||
/**
|
||||
* 根据ASIN查找产品(取最新的一条)
|
||||
* 根据ASIN和地区查找产品(取最新的一条)
|
||||
*/
|
||||
@Query(value = "SELECT * FROM amazon_products WHERE asin = :asin ORDER BY created_at DESC LIMIT 1", nativeQuery = true)
|
||||
Optional<AmazonProductEntity> findByAsin(@Param("asin") String asin);
|
||||
@Query(value = "SELECT * FROM amazon_products WHERE asin = :asin AND region = :region ORDER BY created_at DESC LIMIT 1", nativeQuery = true)
|
||||
Optional<AmazonProductEntity> findByAsinAndRegion(@Param("asin") String asin, @Param("region") String region);
|
||||
|
||||
/**
|
||||
* 根据会话ID查找产品(分页)
|
||||
|
||||
@@ -113,7 +113,7 @@ public class AmazonScrapingServiceImpl implements IAmazonScrapingService, PagePr
|
||||
if (asin == null || asin.trim().isEmpty()) continue;
|
||||
String cleanAsin = asin.replaceAll("[^a-zA-Z0-9]", "");
|
||||
|
||||
Optional<AmazonProductEntity> cached = amazonProductRepository.findByAsin(cleanAsin);
|
||||
Optional<AmazonProductEntity> cached = amazonProductRepository.findByAsinAndRegion(cleanAsin, region);
|
||||
if (cached.isPresent() && !isEmpty(cached.get().getPrice()) && !isEmpty(cached.get().getSeller())) {
|
||||
AmazonProductEntity entity = cached.get();
|
||||
entity.setSessionId(sessionId);
|
||||
@@ -131,6 +131,7 @@ public class AmazonScrapingServiceImpl implements IAmazonScrapingService, PagePr
|
||||
}
|
||||
AmazonProductEntity entity = resultCache.getOrDefault(cleanAsin, new AmazonProductEntity());
|
||||
entity.setAsin(cleanAsin);
|
||||
entity.setRegion(region);
|
||||
entity.setSessionId(sessionId);
|
||||
entity.setUpdatedAt(LocalDateTime.now());
|
||||
amazonProductRepository.save(entity);
|
||||
@@ -160,56 +161,27 @@ public class AmazonScrapingServiceImpl implements IAmazonScrapingService, PagePr
|
||||
* 根据地区配置Site
|
||||
*/
|
||||
private Site configureSiteForRegion(String region) {
|
||||
Site baseSite = Site.me()
|
||||
boolean isUS = "US".equals(region);
|
||||
return Site.me()
|
||||
.setRetryTimes(3)
|
||||
.setSleepTime(3000 + random.nextInt(3000))
|
||||
.setTimeOut(20000)
|
||||
.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0")
|
||||
.addHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
|
||||
.addHeader("accept-encoding", "gzip, deflate, br, zstd")
|
||||
.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36")
|
||||
.addHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8")
|
||||
.addHeader("accept-encoding", "gzip, deflate, br")
|
||||
.addHeader("accept-language", isUS ? "zh-CN,zh;q=0.9,en;q=0.8" : "ja,en;q=0.9,zh-CN;q=0.8")
|
||||
.addHeader("cache-control", "max-age=0")
|
||||
.addHeader("upgrade-insecure-requests", "1")
|
||||
.addHeader("sec-ch-ua", "\"Not)A;Brand\";v=\"99\", \"Microsoft Edge\";v=\"127\", \"Chromium\";v=\"127\"")
|
||||
.addHeader("sec-ch-ua-mobile", "?0")
|
||||
.addHeader("sec-ch-ua-platform", "\"Windows\"")
|
||||
.addHeader("sec-ch-ua-platform-version", "\"10.0.0\"")
|
||||
.addHeader("sec-ch-device-memory", "8")
|
||||
.addHeader("sec-ch-viewport-width", "1400")
|
||||
.addHeader("device-memory", "8")
|
||||
.addHeader("viewport-width", "1400")
|
||||
.addHeader("dpr", "0.9")
|
||||
.addHeader("downlink", "10")
|
||||
.addHeader("ect", "4g")
|
||||
.addHeader("rtt", "150")
|
||||
.addHeader("referer", isUS ? "https://www.amazon.com/" : "https://www.amazon.co.jp/")
|
||||
.addHeader("sec-fetch-site", "none")
|
||||
.addHeader("sec-fetch-mode", "navigate")
|
||||
.addHeader("sec-fetch-user", "?1")
|
||||
.addHeader("sec-fetch-dest", "document");
|
||||
|
||||
if ("US".equals(region)) {
|
||||
// 美区配置
|
||||
baseSite.addHeader("accept-language", "zh-CN,zh;q=0.9,en;q=0.8")
|
||||
.addHeader("referer", "https://www.amazon.com/")
|
||||
.addCookie("i18n-prefs", "USD")
|
||||
.addCookie("lc-main", "en_US")
|
||||
.addCookie("session-id", "134-6097934-2082600")
|
||||
.addCookie("session-id-time", "2082787201l")
|
||||
.addCookie("ubid-main", "132-7547587-3056927")
|
||||
.addCookie("skin", "noskin")
|
||||
.addCookie("csm-hit", "tb:s-6ZB8JV440R1VZ54PSE5W|1759200029304&t:1759200030204&adb:adblk_no");
|
||||
} else {
|
||||
// 日区配置
|
||||
baseSite.addHeader("accept-language", "ja,en;q=0.9,zh-CN;q=0.8,zh;q=0.7")
|
||||
.addHeader("referer", "https://www.amazon.co.jp/")
|
||||
.addCookie("i18n-prefs", "JPY")
|
||||
.addCookie("lc-acbjp", "zh_CN")
|
||||
.addCookie("session-id", "358-1261309-0483141")
|
||||
.addCookie("session-id-time", "2082787201l")
|
||||
.addCookie("ubid-acbjp", "357-8224002-9668932")
|
||||
.addCookie("skin", "noskin");
|
||||
}
|
||||
|
||||
return baseSite;
|
||||
.addHeader("sec-fetch-dest", "document")
|
||||
.addCookie("i18n-prefs", isUS ? "USD" : "JPY")
|
||||
.addCookie(isUS ? "lc-main" : "lc-acbjp", isUS ? "en_US" : "zh_CN")
|
||||
.addCookie("session-id", isUS ? "134-6097934-2082600" : "358-1261309-0483141")
|
||||
.addCookie("session-id-time", "2082787201l")
|
||||
.addCookie(isUS ? "ubid-main" : "ubid-acbjp", isUS ? "132-7547587-3056927" : "357-8224002-9668932")
|
||||
.addCookie("skin", "noskin");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user