CsdnCommentServiceImpl.java 6.3 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.CsdnTripletDayInfo;
9
import com.kwan.springbootkwan.entity.CsdnUserInfo;
10 11 12
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.enums.CommentStatus;
14 15 16 17 18 19 20
import com.kwan.springbootkwan.service.CsdnCommentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class CsdnCommentServiceImpl implements CsdnCommentService {

    @Value("${csdn.cookie}")
    private String csdnCookie;
29
    @Value("#{'${csdn.self_comment}'.split(';')}")
30
    private String[] selfComment;
31 32 33 34 35 36 37
    @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;

38

39
    @Override
40 41
    public Boolean isComment(BusinessInfoResponse.ArticleData.Article article, CsdnUserInfo csdnUserInfo) {
        final Integer commentStatus = csdnUserInfo.getCommentStatus();
42
        if (CommentStatus.COMMENT_IS_FULL.getCode().equals(commentStatus)
43
                || CommentStatus.RESTRICTED_COMMENTS.getCode().equals(commentStatus)) {
44 45 46 47 48 49 50 51
            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)
52
                .form("size", 200)
53 54 55 56 57 58 59 60 61 62 63 64 65
                .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)) {
66
                        log.info("文章{}已经评论过", articleId);
67
                        csdnUserInfo.setCommentStatus(CommentStatus.HAVE_ALREADY_COMMENT.getCode());
68 69 70 71 72 73 74
                        return true;
                    }
                }
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
75
        csdnUserInfo.setCommentStatus(CommentStatus.UN_PROCESSED.getCode());
76 77 78 79
        return false;
    }

    @Override
80
    public Boolean comment(String articleId, CsdnUserInfo csdnUserInfo, CsdnTripletDayInfo csdnTripletDayInfo) {
81
        final Integer commentStatus = csdnUserInfo.getCommentStatus();
82
        if (CommentStatus.COMMENT_IS_FULL.getCode().equals(commentStatus)
83
                || CommentStatus.RESTRICTED_COMMENTS.getCode().equals(commentStatus)) {
84 85 86
            return true;
        }
        //评论
87 88 89 90
        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);
91 92 93
        final int code = comment.code;
        final String message = comment.getMessage();
        if (code == 200) {
94
            log.info("文章{}评论成功", articleId);
95
            csdnUserInfo.setCommentStatus(CommentStatus.COMMENT_SUCCESSFUL.getCode());
96
            csdnTripletDayInfo.setCommentNum(csdnTripletDayInfo.getCommentNum() + 1);
97 98
        } else if (code == 400 && StringUtils.equals(message, "您已达到当日发送上限,请明天尝试!")) {
            log.info(message);
99 100 101 102
            csdnUserInfo.setCommentStatus(CommentStatus.COMMENT_IS_FULL.getCode());
        } else if (code == 400 && message.contains("因存在恶意评论嫌疑,您的账号已被禁言")) {
            log.info("因存在恶意评论嫌疑,您的账号已被禁言");
            csdnUserInfo.setCommentStatus(CommentStatus.RESTRICTED_COMMENTS.getCode());
103
        } else if (code == 400 && StringUtils.equals(message, "您评论太快了,请休息一下!")) {
104
            log.info("您评论太快了,请休息一下!");
105
            csdnUserInfo.setCommentStatus(CommentStatus.COMMENT_TOO_FAST.getCode());
106 107 108
        } else {
            log.info("其他错误");
            csdnUserInfo.setCommentStatus(CommentStatus.OTHER_ERRORS.getCode());
109 110 111 112 113 114 115 116 117 118
        }
        return true;
    }

    /**
     * 评论文章
     *
     * @param articleId
     * @return
     */
119 120
    @Override
    public CommentResponse dealComment(String articleId, String commentInfo, Integer commentId) {
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        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();
        }

137
        final String body = response.body();
138
        log.info(body);
139 140 141 142 143 144 145 146 147 148
        ObjectMapper objectMapper = new ObjectMapper();
        CommentResponse commentResponse = null;
        try {
            commentResponse = objectMapper.readValue(body, CommentResponse.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return commentResponse;
    }
}