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 858e253b7014861e11e08e283ba4623a7b89cdd7..d4492d3a70dc7d1efe4fd4ab937788bb28dcd9f5 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 ce048fb70106c2090b0ed16ff8f8e90f9096e22b..39cdbdad28f7fdfa8bb2b35d89453d3f31a3e806 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