fix:只处理博客

上级 b4e7ee8d
......@@ -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();
......
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<BusinessInfoResponse.ArticleData.Article> 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<CommentListResponse.Comment> 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<CommentListResponse.SubComment> 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<CommentListResponse.Comment> 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<CommentListResponse.SubComment> 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<CsdnUserInfo> 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<CsdnUserInfo> 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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册