diff --git a/src/main/java/com/kwan/springbootkwan/entity/CsdnStatusResp.java b/src/main/java/com/kwan/springbootkwan/entity/CsdnStatusResp.java new file mode 100644 index 0000000000000000000000000000000000000000..ef9ba8f25676c6d56e36ea9b10b66cdd0bfaf095 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/entity/CsdnStatusResp.java @@ -0,0 +1,15 @@ +package com.kwan.springbootkwan.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class CsdnStatusResp { + + public Integer code; + public String value; +} diff --git a/src/main/java/com/kwan/springbootkwan/entity/resp/BusinessInfo.java b/src/main/java/com/kwan/springbootkwan/entity/resp/BusinessInfo.java index 82141239c225eb7e0d02a7ca0ddb42ff9dd943d8..29e9209042363d0b80dbcadf15d152a3ef3bc80c 100644 --- a/src/main/java/com/kwan/springbootkwan/entity/resp/BusinessInfo.java +++ b/src/main/java/com/kwan/springbootkwan/entity/resp/BusinessInfo.java @@ -40,6 +40,8 @@ public class BusinessInfo { public Integer viewCount; public Object expandTitle; public Object cover; + public Object fileType; + public Object content; } } diff --git a/src/main/java/com/kwan/springbootkwan/entity/resp/LikeData.java b/src/main/java/com/kwan/springbootkwan/entity/resp/LikeData.java new file mode 100644 index 0000000000000000000000000000000000000000..fb7fd3251f12eb681a3241668063bc0202c8d071 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/entity/resp/LikeData.java @@ -0,0 +1,11 @@ +package com.kwan.springbootkwan.entity.resp; + +import lombok.Data; + +@Data +public class LikeData { + public int code; + public String message; + public String traceId; + public Object data; +} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/schedule/CsdnSchedule.java b/src/main/java/com/kwan/springbootkwan/schedule/CsdnSchedule.java index 8992fdc89905e25c2d57b9473dfdbe2cae8dcc52..f35ca82c11b966a4371fa8f29886cdeef6e9f554 100644 --- a/src/main/java/com/kwan/springbootkwan/schedule/CsdnSchedule.java +++ b/src/main/java/com/kwan/springbootkwan/schedule/CsdnSchedule.java @@ -8,14 +8,14 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; -@Component @Slf4j +@Component public class CsdnSchedule { @Autowired private CsdnService csdnService; - @Scheduled(cron = "0 0 10,15,21 * * ?") + @Scheduled(cron = "0 0 6,8,10,12,14,16,18,20,22 * * ?") public void execute() { log.info("Scheduled task is running ... ..."); csdnService.multiTriplet(); diff --git a/src/main/java/com/kwan/springbootkwan/service/CsdnService.java b/src/main/java/com/kwan/springbootkwan/service/CsdnService.java index 35b294bdaaabb5beb957daef2e6d79f8d07fb7eb..6d04066a9e6f7bd390baf6b740c9fccf51951bc2 100644 --- a/src/main/java/com/kwan/springbootkwan/service/CsdnService.java +++ b/src/main/java/com/kwan/springbootkwan/service/CsdnService.java @@ -1,5 +1,7 @@ package com.kwan.springbootkwan.service; +import com.kwan.springbootkwan.entity.CsdnStatusResp; + /** * csdn博客自动化 * @@ -9,11 +11,11 @@ package com.kwan.springbootkwan.service; */ public interface CsdnService { /** - * 查询单篇文章信息 + * 单人三连 * * @return */ - String singleArticle(String username); + CsdnStatusResp singleArticle(String username); /** * 查询是否评论过 @@ -35,7 +37,7 @@ public interface CsdnService { * * @param articleId */ - void comment(String articleId); + int comment(String articleId); /** * 三连 diff --git a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnServiceImpl.java b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnServiceImpl.java index 8583dfcb071c907396c8c30dec3932bbb19217b7..d46d77bea2a57fda88cf1253f25c7201ea4a9190 100644 --- a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnServiceImpl.java +++ b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnServiceImpl.java @@ -5,9 +5,11 @@ import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpUtil; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.kwan.springbootkwan.entity.CsdnStatusResp; import com.kwan.springbootkwan.entity.CsdnUserInfo; import com.kwan.springbootkwan.entity.resp.BusinessInfo; import com.kwan.springbootkwan.entity.resp.CommentData; +import com.kwan.springbootkwan.entity.resp.LikeData; import com.kwan.springbootkwan.service.CsdnService; import com.kwan.springbootkwan.service.CsdnUserInfoService; import lombok.extern.slf4j.Slf4j; @@ -25,6 +27,10 @@ public class CsdnServiceImpl implements CsdnService { * 构建header参数 */ private static final String CUSTOM_HEADER_NAME = "Cookie"; + /** + * 点赞满了 + */ + private static boolean LIKE_IS_FULL = false; /** * 参数值 */ @@ -34,13 +40,13 @@ public class CsdnServiceImpl implements CsdnService { private CsdnUserInfoService csdnUserInfoService; @Override - public String singleArticle(String username) { + public CsdnStatusResp singleArticle(String username) { String url = "https://blog.csdn.net/community/home-api/v1/get-business-list"; // 使用Hutool发送GET请求 HttpResponse response = HttpUtil.createGet(url) .header(CUSTOM_HEADER_NAME, CUSTOM_HEADER_VALUE) .form("page", 1) - .form("size", 2) + .form("size", 5) .form("businessType", "lately") .form("noMore", false) .form("username", username) @@ -56,23 +62,41 @@ public class CsdnServiceImpl implements CsdnService { } final BusinessInfo.ArticleData data = businessInfo.getData(); final List list = data.getList(); - final BusinessInfo.ArticleData.Article article = list.get(0); - - final String type = article.getType(); - if (!StringUtils.equals("blog", type)) { - return "非博客不能评论"; + //处理评论过 + int commentNum = 0; + int successNum = 0; + int notBlogNum = 0; + int count = 3;//评论3篇 + for (int i = 0; i < count; i++) { + final BusinessInfo.ArticleData.Article article = list.get(i); + final String type = article.getType(); + if (!StringUtils.equals("blog", type)) { + notBlogNum++; + continue; + } + final String urlInfo = article.getUrl(); + String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1); + final Boolean articleInfo = this.getArticleInfo(articleId); + if (!articleInfo) { + //进行点赞和评论 + final int comment = this.comment(articleId); + // 打印响应结果 + if (comment == 200) { + successNum++; + if (!LIKE_IS_FULL) { + this.like(articleId); + } + } else if (comment == 400) { + return new CsdnStatusResp(400, "您评论太快了,请休息一下!"); + } + } else { + commentNum++; + } } - final String urlInfo = article.getUrl(); - String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1); - final Boolean articleInfo = this.getArticleInfo(articleId); - if (!articleInfo) { - //进行点赞和评论 - this.like(articleId); - this.comment(articleId); - return "三连完成"; - } else { - return "文章已经评论过,不能再次评论"; + if (count != successNum) { + return new CsdnStatusResp(300, "评论过的数量=" + commentNum + "非博客的数量:" + notBlogNum); } + return new CsdnStatusResp(200, "三连完成"); } @Override @@ -118,13 +142,25 @@ public class CsdnServiceImpl implements CsdnService { .form("articleId", articleId) .execute(); // 打印响应结果 - if (response.getStatus() == 200) { + final String body = response.body(); + ObjectMapper objectMapper = new ObjectMapper(); + LikeData likeData = null; + try { + likeData = objectMapper.readValue(body, LikeData.class); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + final int code = likeData.code; + if (code == 200) { log.info("点赞成功"); + } else if (code == 400) { + LIKE_IS_FULL = true; + log.info("今日点赞次数已达上限!"); } } @Override - public void comment(String articleId) { + public int comment(String articleId) { String url = "https://blog.csdn.net/phoenix/web/v1/comment/submit"; // 使用Hutool发送GET请求 HttpResponse response = HttpUtil.createPost(url) @@ -132,22 +168,25 @@ public class CsdnServiceImpl implements CsdnService { .form("articleId", articleId) .form("content", "支持博主优质文章,讲解得非常详细,干货满满,通俗易懂,期待博主下次更新") .execute(); - // 打印响应结果 - if (response.getStatus() == 200) { - log.info("评论成功"); - } + return response.getStatus(); } @Override public void multiTriplet() { final List list = csdnUserInfoService.list(); if (CollectionUtil.isNotEmpty(list)) { - list.forEach(csdnUserInfo -> { + for (CsdnUserInfo csdnUserInfo : list) { // 构建参数 String username = csdnUserInfo.getUserName(); - final String res = singleArticle(username); - log.info("用户{}:{}", username, res); - }); + final CsdnStatusResp csdnStatusResp = singleArticle(username); + final Integer code = csdnStatusResp.getCode(); + final String value = csdnStatusResp.getValue(); + if (code.equals(400)) { + log.info("用户{}:code={},{}", username, code, value); + break; + } + log.info("用户{}:{}", username, value); + } } } } \ No newline at end of file