modify apple inventory monitor schedule
This commit is contained in:
parent
2919d50eb6
commit
e1b705d9b0
@ -48,18 +48,20 @@ public class AppleInventorySchedule {
|
||||
// @Value("${baogutang.apple.storeList}")
|
||||
private List<String> storeList = new ArrayList<>();
|
||||
|
||||
@Resource
|
||||
private IphoneProductParserUtils iphoneProductParserUtils;
|
||||
@Resource
|
||||
private DingTalkMsgPushUtils dingTalkMsgPushUtils;
|
||||
|
||||
@Scheduled(cron = "0 0/5 * * * ? ")
|
||||
public void appleInventoryMonitor() {
|
||||
// 获取设备信息
|
||||
List<IphoneProductDto> products = IphoneProductParserUtils.getProducts(deviceCode, countryCode);
|
||||
List<IphoneProductDto> products = iphoneProductParserUtils.getProducts(deviceCode, countryCode);
|
||||
//监视机型型号
|
||||
products.forEach(product -> {
|
||||
this.doMonitor(product);
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
Thread.sleep(1500);
|
||||
} catch (InterruptedException e) {
|
||||
log.error(">>>>>>>>>>apple inventory monitor error:{}<<<<<<<<<<", e.getMessage(), e);
|
||||
}
|
||||
@ -76,11 +78,8 @@ public class AppleInventorySchedule {
|
||||
queryMap.put("location", location);
|
||||
|
||||
String baseUrl = String.format("https://www.apple.com.%s", countryCode);
|
||||
|
||||
Map<String, List<String>> headers = buildHeaders(baseUrl, deviceCode, product.getModel());
|
||||
|
||||
String url = baseUrl + "/shop/fulfillment-messages?" + URLUtil.buildQuery(queryMap, CharsetUtil.CHARSET_UTF_8);
|
||||
|
||||
try {
|
||||
HttpResponse httpResponse = HttpRequest.get(url)
|
||||
.header(headers)
|
||||
@ -89,18 +88,13 @@ public class AppleInventorySchedule {
|
||||
log.warn(">>>>>>>>>>请求可能过于频繁,请稍后再试~<<<<<<<<<<");
|
||||
return;
|
||||
}
|
||||
|
||||
JSONObject responseJsonObject = JSON.parseObject(httpResponse.body());
|
||||
|
||||
JSONObject pickupMessage = responseJsonObject.getJSONObject("body").getJSONObject("content").getJSONObject("pickupMessage");
|
||||
|
||||
JSONArray stores = pickupMessage.getJSONArray("stores");
|
||||
|
||||
if (stores == null) {
|
||||
log.debug(pickupMessage.toString());
|
||||
return;
|
||||
}
|
||||
|
||||
if (stores.isEmpty()) {
|
||||
log.info("您所在的 {} 附近没有Apple直营店,请检查您的地址是否正确", location);
|
||||
return;
|
||||
|
||||
@ -3,32 +3,53 @@ package top.baogutang.admin.utils;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import top.baogutang.admin.domain.IphoneProductDto;
|
||||
import top.baogutang.common.constants.ErrorCodeEnum;
|
||||
import top.baogutang.common.exceptions.BusinessException;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static top.baogutang.common.constants.CacheConstant.PREFIX_APPLE_PRODUCT;
|
||||
|
||||
/**
|
||||
* @description:
|
||||
* @author: nikooh
|
||||
* @date: 2023/10/13 : 15:47
|
||||
*/
|
||||
@Component
|
||||
public class IphoneProductParserUtils {
|
||||
|
||||
public static List<IphoneProductDto> getProducts(String code, String country) {
|
||||
@Resource
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<IphoneProductDto> getProducts(String code, String country) {
|
||||
String cacheKey = String.format(PREFIX_APPLE_PRODUCT, country, code);
|
||||
Object productObj = redisTemplate.opsForValue().get(cacheKey);
|
||||
if (Objects.nonNull(productObj)) {
|
||||
return (List<IphoneProductDto>) productObj;
|
||||
}
|
||||
String url = "https://www.apple.com/" + country + "/shop/buy-iphone/iphone-" + code;
|
||||
String content = sendHttpRequest(url);
|
||||
|
||||
assert content.contains("productSelectionData");
|
||||
|
||||
return parseProducts(content);
|
||||
if (Objects.isNull(content)) {
|
||||
throw new BusinessException(ErrorCodeEnum.E_BIZ_ERROR);
|
||||
}
|
||||
List<IphoneProductDto> iphoneProductDtos = parseProducts(content);
|
||||
redisTemplate.opsForValue().set(cacheKey, iphoneProductDtos, 30, TimeUnit.DAYS);
|
||||
return iphoneProductDtos;
|
||||
}
|
||||
|
||||
private static String sendHttpRequest(String url) {
|
||||
private String sendHttpRequest(String url) {
|
||||
try {
|
||||
URL urlObj = new URL(url);
|
||||
HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
|
||||
@ -54,7 +75,7 @@ public class IphoneProductParserUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static List<IphoneProductDto> parseProducts(String content) {
|
||||
private List<IphoneProductDto> parseProducts(String content) {
|
||||
String selectText = null;
|
||||
String[] parts = content.split("window.PRODUCT_SELECTION_BOOTSTRAP = ");
|
||||
if (parts.length > 1) {
|
||||
|
||||
@ -39,4 +39,6 @@ public class CacheConstant {
|
||||
* 钉钉应用access_token缓存KEY前缀
|
||||
*/
|
||||
public static final String PREFIX_DING_TALK_ACCESS_TOKEN = "top:baogutang:dingtalk:access_token:";
|
||||
|
||||
public static final String PREFIX_APPLE_PRODUCT = "top:baogutang:apple:product:%s:%s:";
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user