CsdnCommentServiceImpl.java 9.8 KB
Newer Older
1 2 3 4 5 6 7
package com.kwan.springbootkwan.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
8
import com.kwan.springbootkwan.entity.CsdnUserInfo;
9 10 11
import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse;
import com.kwan.springbootkwan.entity.resp.CommentListResponse;
import com.kwan.springbootkwan.entity.resp.CommentResponse;
12
import com.kwan.springbootkwan.enums.CommentStatus;
13
import com.kwan.springbootkwan.service.CsdnArticleService;
14 15 16
import com.kwan.springbootkwan.service.CsdnCommentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
17
import org.springframework.beans.factory.annotation.Autowired;
18 19 20 21
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
22
import java.util.Objects;
23 24 25 26 27 28 29

@Slf4j
@Service
public class CsdnCommentServiceImpl implements CsdnCommentService {

    @Value("${csdn.cookie}")
    private String csdnCookie;
30
    @Value("#{'${csdn.self_comment}'.split(';')}")
31
    private String[] selfComment;
32 33
    @Value("#{'${csdn.self_reply}'.split(';')}")
    private String[] selfReply;
34 35 36 37 38 39 40
    @Value("${csdn.self_user_name}")
    private String selfUserName;
    @Value("${csdn.url.is_comment_list_url}")
    private String commentListUrl;
    @Value("${csdn.url.comment_url}")
    private String commentUrl;

41 42 43
    @Autowired
    private CsdnArticleService csdnArticleService;

44
    @Override
45 46 47 48 49
    public Boolean isComment(BusinessInfoResponse.ArticleData.Article article, CsdnUserInfo csdnUserInfo) {
        final Integer commentStatus = csdnUserInfo.getCommentStatus();
        if (CommentStatus.HAVE_ALREADY_COMMENT.getCode().equals(commentStatus)
                || CommentStatus.COMMENT_IS_FULL.getCode().equals(commentStatus)
                || CommentStatus.RESTRICTED_COMMENTS.getCode().equals(commentStatus)) {
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
            return true;
        }
        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", 50)
                .form("fold", "unfold")
                .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> list = data.getList();
            if (CollectionUtil.isNotEmpty(list)) {
                for (CommentListResponse.Comment comment : list) {
                    final CommentListResponse.Info info = comment.getInfo();
                    final String userName = info.getUserName();
                    if (StringUtils.equals(userName, selfUserName)) {
73
                        log.info("文章{}已经评论过", articleId);
74
                        csdnUserInfo.setCommentStatus(CommentStatus.HAVE_ALREADY_COMMENT.getCode());
75 76 77 78 79 80 81 82 83 84 85
                        return true;
                    }
                }
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
86 87 88 89 90
    public Boolean comment(String articleId, CsdnUserInfo csdnUserInfo) {
        final Integer commentStatus = csdnUserInfo.getCommentStatus();
        if (CommentStatus.HAVE_ALREADY_COMMENT.getCode().equals(commentStatus)
                || CommentStatus.COMMENT_IS_FULL.getCode().equals(commentStatus)
                || CommentStatus.RESTRICTED_COMMENTS.getCode().equals(commentStatus)) {
91 92 93
            return true;
        }
        //评论
94 95 96 97
        int start = -1;
        int end = selfComment.length;
        int temp_count = (int) (Math.floor(Math.random() * (start - end + 1)) + end);
        CommentResponse comment = this.dealComment(articleId, selfComment[temp_count], null);
98 99 100
        final int code = comment.code;
        final String message = comment.getMessage();
        if (code == 200) {
101
            log.info("文章{}评论成功", articleId);
102
            csdnUserInfo.setCommentStatus(CommentStatus.COMMENT_SUCCESSFUL.getCode());
103 104
        } else if (code == 400 && StringUtils.equals(message, "您已达到当日发送上限,请明天尝试!")) {
            log.info(message);
105 106 107 108
            csdnUserInfo.setCommentStatus(CommentStatus.COMMENT_IS_FULL.getCode());
        } else if (code == 400 && message.contains("因存在恶意评论嫌疑,您的账号已被禁言")) {
            log.info("因存在恶意评论嫌疑,您的账号已被禁言");
            csdnUserInfo.setCommentStatus(CommentStatus.RESTRICTED_COMMENTS.getCode());
109
        } else if (code == 400 && StringUtils.equals(message, "您评论太快了,请休息一下!")) {
110
            log.info("您评论太快了,请休息一下!");
111
            csdnUserInfo.setCommentStatus(CommentStatus.COMMENT_TOO_FAST.getCode());
112 113 114 115 116 117 118 119
            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else {
            log.info("其他错误");
            csdnUserInfo.setCommentStatus(CommentStatus.OTHER_ERRORS.getCode());
120 121 122 123
        }
        return true;
    }

124
    @Override
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    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)) {
                continue;
            }
            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", 100)
                    .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 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 = this.dealComment(articleId, selfReply[temp_count], commentId);
                                log.info(reply.toString());
                            }
                        }
                    }
                }
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
182 183
    }

184 185 186 187 188 189
    /**
     * 评论文章
     *
     * @param articleId
     * @return
     */
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    private CommentResponse dealComment(String articleId, String commentInfo, Integer commentId) {
        HttpResponse response;
        if (Objects.nonNull(commentId)) {
            response = HttpUtil.createPost(commentUrl)
                    .header("Cookie", csdnCookie)
                    .form("articleId", articleId)
                    .form("content", commentInfo)
                    .form("commentId", commentId)
                    .execute();
        } else {
            response = HttpUtil.createPost(commentUrl)
                    .header("Cookie", csdnCookie)
                    .form("articleId", articleId)
                    .form("content", commentInfo)
                    .execute();
        }

207
        final String body = response.body();
208
        log.info(body);
209 210 211 212 213 214 215 216 217 218
        ObjectMapper objectMapper = new ObjectMapper();
        CommentResponse commentResponse = null;
        try {
            commentResponse = objectMapper.readValue(body, CommentResponse.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return commentResponse;
    }
}