From 34243decc37f596eb3b0f42b5beaae2157bfcf1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Fri, 27 Oct 2023 15:54:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=8F=AA=E5=A4=84=E7=90=86=E5=8D=9A?= =?UTF-8?q?=E5=AE=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/CsdnArticleServiceImpl.java | 2 +- .../impl/CsdnAutoReplyServiceImpl.java | 130 +++++++++--------- 2 files changed, 64 insertions(+), 68 deletions(-) diff --git a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnArticleServiceImpl.java b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnArticleServiceImpl.java index 858e253..d4492d3 100644 --- a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnArticleServiceImpl.java +++ b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnArticleServiceImpl.java @@ -29,7 +29,7 @@ public class CsdnArticleServiceImpl implements CsdnArticleService { .header("Cookie", csdnCookie) .form("page", 1) .form("size", 5) - .form("businessType", "lately") + .form("businessType", "blog") .form("noMore", false) .form("username", username) .execute(); diff --git a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnAutoReplyServiceImpl.java b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnAutoReplyServiceImpl.java index ce048fb..39cdbda 100644 --- a/src/main/java/com/kwan/springbootkwan/service/impl/CsdnAutoReplyServiceImpl.java +++ b/src/main/java/com/kwan/springbootkwan/service/impl/CsdnAutoReplyServiceImpl.java @@ -1,6 +1,5 @@ package com.kwan.springbootkwan.service.impl; - import cn.hutool.core.collection.CollectionUtil; import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpUtil; @@ -25,7 +24,6 @@ import org.springframework.stereotype.Service; import java.util.List; import java.util.Objects; - @Slf4j @Service public class CsdnAutoReplyServiceImpl implements CsdnAutoReplyService { @@ -37,7 +35,6 @@ public class CsdnAutoReplyServiceImpl implements CsdnAutoReplyService { private String selfUserName; @Value("${csdn.url.is_comment_list_url}") private String commentListUrl; - @Autowired private CsdnService csdnService; @Autowired @@ -50,77 +47,76 @@ public class CsdnAutoReplyServiceImpl implements CsdnAutoReplyService { @Override public void commentSelf() { List list = csdnArticleService.getArticles(selfUserName); - if (list == null) { - return; - } - for (BusinessInfoResponse.ArticleData.Article article : list) { - final String type = article.getType(); - if (StringUtils.equals("blog", type)) { - final String urlInfo = article.getUrl(); - String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1); - String url = commentListUrl + articleId; - HttpResponse response = HttpUtil.createPost(url) - .header("Cookie", csdnCookie) - .form("page", 1) - .form("size", 200)//获取当前文章的200条评论 - .execute(); - final String body = response.body(); - ObjectMapper objectMapper = new ObjectMapper(); - CommentListResponse articleInfo; - try { - articleInfo = objectMapper.readValue(body, CommentListResponse.class); - final CommentListResponse.DataInfo data = articleInfo.getData(); - final List otherCommentList = data.getList(); - if (CollectionUtil.isNotEmpty(otherCommentList)) { - for (CommentListResponse.Comment oneComment : otherCommentList) { - final CommentListResponse.Info info = oneComment.getInfo(); - final String userName = info.getUserName(); - final String nickName = info.getNickName(); - final Integer commentId = info.getCommentId(); - if (!StringUtils.equals(userName, selfUserName)) { - final List sub = oneComment.getSub(); - boolean flag = false; - if (CollectionUtil.isNotEmpty(sub)) { - for (CommentListResponse.SubComment subComment : sub) { - //如果没有自己的评论,需要评论 - final String subUserName = subComment.getUserName(); - if (StringUtils.equals(subUserName, selfUserName)) { - flag = true; + if (CollectionUtil.isNotEmpty(list)) { + for (BusinessInfoResponse.ArticleData.Article article : list) { + final String type = article.getType(); + if (StringUtils.equals("blog", type)) { + final String urlInfo = article.getUrl(); + String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1); + String url = commentListUrl + articleId; + HttpResponse response = HttpUtil.createPost(url) + .header("Cookie", csdnCookie) + .form("page", 1) + .form("size", 200)//获取当前文章的200条评论 + .execute(); + final String body = response.body(); + ObjectMapper objectMapper = new ObjectMapper(); + CommentListResponse articleInfo; + try { + articleInfo = objectMapper.readValue(body, CommentListResponse.class); + final CommentListResponse.DataInfo data = articleInfo.getData(); + final List otherCommentList = data.getList(); + if (CollectionUtil.isNotEmpty(otherCommentList)) { + for (CommentListResponse.Comment oneComment : otherCommentList) { + final CommentListResponse.Info info = oneComment.getInfo(); + final String userName = info.getUserName(); + final String nickName = info.getNickName(); + final Integer commentId = info.getCommentId(); + if (!StringUtils.equals(userName, selfUserName)) { + final List sub = oneComment.getSub(); + boolean flag = false; + if (CollectionUtil.isNotEmpty(sub)) { + for (CommentListResponse.SubComment subComment : sub) { + //如果没有自己的评论,需要评论 + final String subUserName = subComment.getUserName(); + if (StringUtils.equals(subUserName, selfUserName)) { + flag = true; + } } } + if (CollectionUtil.isEmpty(sub) || !flag) { + //需要评论 + int start = -1; + int end = selfReply.length; + int temp_count = (int) (Math.floor(Math.random() * (start - end + 1)) + end); + CommentResponse reply = csdnCommentService.dealComment(articleId, selfReply[temp_count], commentId); + log.info(reply.toString()); + } + //三连此评论人 + QueryWrapper wrapper = new QueryWrapper<>(); + wrapper.eq("is_delete", 0); + wrapper.eq("user_name", userName).last("limit 1"); + CsdnUserInfo csdnUserInfo = csdnUserInfoService.getOne(wrapper); + if (Objects.isNull(csdnUserInfo)) { + //新增用户 + csdnUserInfo = new CsdnUserInfo(); + csdnUserInfo.setUserName(userName); + csdnUserInfo.setNickName(nickName); + csdnUserInfo.setLikeStatus(0); + csdnUserInfo.setCollectStatus(0); + csdnUserInfo.setCommentStatus(0); + csdnUserInfo.setUserWeight(7); + csdnUserInfoService.save(csdnUserInfo); + } + csdnService.singleArticle(csdnUserInfo); } - if (CollectionUtil.isEmpty(sub) || !flag) { - //需要评论 - int start = -1; - int end = selfReply.length; - int temp_count = (int) (Math.floor(Math.random() * (start - end + 1)) + end); - CommentResponse reply = csdnCommentService.dealComment(articleId, selfReply[temp_count], commentId); - log.info(reply.toString()); - } - //三连此评论人 - QueryWrapper wrapper = new QueryWrapper<>(); - wrapper.eq("is_delete", 0); - wrapper.eq("user_name", userName).last("limit 1"); - CsdnUserInfo csdnUserInfo = csdnUserInfoService.getOne(wrapper); - if (Objects.isNull(csdnUserInfo)) { - //新增用户 - csdnUserInfo = new CsdnUserInfo(); - csdnUserInfo.setUserName(userName); - csdnUserInfo.setNickName(nickName); - csdnUserInfo.setLikeStatus(0); - csdnUserInfo.setCollectStatus(0); - csdnUserInfo.setCommentStatus(0); - csdnUserInfo.setUserWeight(7); - csdnUserInfoService.save(csdnUserInfo); - } - csdnService.singleArticle(csdnUserInfo); } } + } catch (JsonProcessingException e) { + e.printStackTrace(); } - } catch (JsonProcessingException e) { - e.printStackTrace(); } } } } -} +} \ No newline at end of file -- GitLab