如何每天自动申请京东试用
上篇文章我们简单概述了一下如何通过java代码来实现自动京东签到,在此功能上现在继续完善了代码,可以实现每天自动申请京东试用,支持价格过滤、预计中奖概率过滤、和屏蔽关键词。只需登录一次,以后就能免登录。废话不多说 ,直接看代码:
1.核心代码
try {
CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpPost);
String result = EntityUtils.toString(closeableHttpResponse.getEntity());
log.info("获取{}分类第{}页的京东试用频道商品列表结果:{}", tabId, page, result);
if (StringUtils.hasText(result)) {
JSONObject resJSONObject = JSONObject.parseObject(result);
JSONObject dataJSONObject = resJSONObject.getJSONObject("data");
JSONArray feedListJSONArray = dataJSONObject.getJSONArray("feedList");
for (int j = 0; j < feedListJSONArray.size(); j++) {
JSONObject jsonObject = feedListJSONArray.getJSONObject(j);
log.info("商品信息:类别:{},{}",tabId, jsonObject.toJSONString());
JdTrialProduct jdTrialProduct = jsonObject.toJavaObject(JdTrialProduct.class);
jdTrialProduct.setCategoryId(tabId);
// 只获取商品类型=1的数据
if (jdTrialProduct.getType() == 1) {
/*if (jdTrialProduct.getApplyState() != null && jdTrialProduct.getApplyState() == 1) { log.info("该商品已申请过了,跳过"); } else*/ if (jdTrialProduct.getJdPrice() == null) {
log.info("无价格信息,跳过");
} else if (jdTrialProduct.getJdPrice().compareTo(minJdPrice) < 0) {
log.info("价格:{},价格低于设定最低价:{},跳过", jdTrialProduct.getJdPrice(), minJdPrice);
} else if (isOnlyFree == 1 && jdTrialProduct.getTrialPrice().compareTo(BigDecimal.ZERO) > 0) {
log.info("不是0元商品,试用价格为:{},跳过", jdTrialProduct.getTrialPrice());
} else if (jdTrialProduct.getTagList().contains("付费试用")) {
log.info("需要付费试用,试用价格为:{},跳过", jdTrialProduct.getTrialPrice());
} else {
boolean isExist = jdTrialProductList.stream().anyMatch(jdTrialProductInfoVO -> jdTrialProductInfoVO.getTrialActivityId().equals(jdTrialProduct.getTrialActivityId()));
if (isExist) {
log.info("{}已存在!", jdTrialProduct.getSkuTitle());
} else {
// 创建一个数值格式化对象
NumberFormat numberFormat = NumberFormat.getInstance();
// 设置精确到小数点后2位
numberFormat.setMaximumFractionDigits(8);
double probability = 0;
if(jdTrialProduct.getApplyNum() != 0){
probability = Double.valueOf(numberFormat.format((float) jdTrialProduct.getSupplyNum() / (float) jdTrialProduct.getApplyNum() * 100).replaceAll(",",""));
probability = probability > 100?100:probability;
} else {
probability = 100;
}
jdTrialProduct.setProbability(probability+"");
log.info("预计中奖几率:{}",jdTrialProduct.getProbability());
log.info("{}添加成功!", jdTrialProduct.getSkuTitle());
jdTrialProduct.setCreateTime(new Date());
jdTrialProduct.setUpdateTime(jdTrialProduct.getCreateTime());
jdTrialProductList.add(jdTrialProduct);
}
}
}
}
page++;
} else {
break;
}
} catch (Exception e) {
e.printStackTrace();
}
定时触发
@Scheduled(cron = "0 30 3 1/1 * ?")
// @Scheduled(cron = "0 49 23 1/1 * ?")
public void runListAllJdTrialProduct() {
long startTime = new Date().getTime();
log.info("京东试用商品列表更新开始");
LambdaUpdateWrapper<JdTrialProductCategory> lqw = new LambdaUpdateWrapper<JdTrialProductCategory>().eq(JdTrialProductCategory::getIsSkip, 0);
jdTrialProductCategoryService.list(lqw).stream().forEach(jdTrialProductCategory -> {
new Thread(()->{
jdTrialProductService.listJdTrialProductInfoVOByCategoryTabId(jdTrialProductCategory.getTabId(), Constants.DEFAULT.maxMage, new BigDecimal(5), new BigDecimal(9999), 1);
}).start();
});
long endTime = new Date().getTime();
log.info("京东试用商品列表更新完成,耗时{}秒", (endTime - startTime) / 1000);
}
@Scheduled(cron = "0 30 4 1/1 * ?")
// @Scheduled(cron = "0 49 23 1/1 * ?")
public void runApplyTrial() {
long startTime = new Date().getTime();
log.info("申请京东试用商品开始");
userService.list(new LambdaQueryWrapper<User>().eq(User::getNeedLogin,0)).stream().forEach(user -> {
new Thread(() -> {
log.info("==user.getApplyType()=={}",user.getApplyType());
if(user.getApplyType() == 1){
jdMallService.batchApplyByUserId(user.getId());
} else if (user.getApplyType() == 2){
jdProductService.getProductFromForEachId(user.getId());
}
}).start();
});
long endTime = new Date().getTime();
log.info("申请京东试用商品完成,耗时{}秒", (endTime - startTime) / 1000);
}
目前软件已经正常运行,支持windows和mac,信得过我也可以直接在我电脑登录,由我帮你代挂!
今天的文章如何每天自动申请京东试用分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/65067.html