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