CsdnAutoReplyServiceImpl.java 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
package com.kwan.springbootkwan.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kwan.springbootkwan.entity.CsdnUserInfo;
import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse;
import com.kwan.springbootkwan.entity.resp.CommentListResponse;
import com.kwan.springbootkwan.entity.resp.CommentResponse;
13
import com.kwan.springbootkwan.service.CsdnArticleInfoService;
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
import com.kwan.springbootkwan.service.CsdnAutoReplyService;
import com.kwan.springbootkwan.service.CsdnCommentService;
import com.kwan.springbootkwan.service.CsdnService;
import com.kwan.springbootkwan.service.CsdnUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class CsdnAutoReplyServiceImpl implements CsdnAutoReplyService {
    @Value("${csdn.cookie}")
    private String csdnCookie;
    @Value("#{'${csdn.self_reply}'.split(';')}")
    private String[] selfReply;
    @Value("${csdn.self_user_name}")
    private String selfUserName;
    @Value("${csdn.url.is_comment_list_url}")
    private String commentListUrl;
    @Autowired
    private CsdnService csdnService;
    @Autowired
41
    private CsdnArticleInfoService csdnArticleInfoService;
42 43 44 45 46 47 48
    @Autowired
    private CsdnCommentService csdnCommentService;
    @Autowired
    private CsdnUserInfoService csdnUserInfoService;

    @Override
    public void commentSelf() {
49
        List<BusinessInfoResponse.ArticleData.Article> list = csdnArticleInfoService.getArticles(selfUserName);
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        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;
                                            }
85 86
                                        }
                                    }
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
                                    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);
112 113 114
                                }
                            }
                        }
115 116
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
117 118 119 120 121
                    }
                }
            }
        }
    }
122
}