CsdnCommentServiceImpl.java 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
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;
import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse;
import com.kwan.springbootkwan.entity.resp.CommentListResponse;
import com.kwan.springbootkwan.entity.resp.CommentResponse;
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;

@Slf4j
@Service
public class CsdnCommentServiceImpl implements CsdnCommentService {

    @Value("${csdn.cookie}")
    private String csdnCookie;
25 26
    @Value("${csdn.self_comment}")
    private String[] selfComment;
27 28 29 30 31 32 33
    @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;
    /**
34
     * 评论满了
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
     */
    private static boolean COMMENT_IS_FULL = false;

    @Override
    public Boolean isComment(BusinessInfoResponse.ArticleData.Article article) {
        if (COMMENT_IS_FULL) {
            return true;
        }
        final String urlInfo = article.getUrl();
        String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1);
        String url = commentListUrl + articleId;
        // 使用Hutool发送GET请求
        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)) {
                        //评论过
67
                        log.info("文章{}已经评论过", articleId);
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
                        return true;
                    }
                }
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Boolean comment(String articleId) {
        if (COMMENT_IS_FULL) {
            return true;
        }
        //评论
        CommentResponse comment = this.dealComment(articleId);
        final int code = comment.code;
        final String message = comment.getMessage();
        if (code == 200) {
88
            log.info("文章{}评论成功", articleId);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        } else if (code == 400 && StringUtils.equals(message, "您已达到当日发送上限,请明天尝试!")) {
            log.info(message);
            COMMENT_IS_FULL = true;
        } else {
            log.info("您评论太快了,请休息一下!");
        }
        return true;
    }

    /**
     * 评论文章
     *
     * @param articleId
     * @return
     */
    private CommentResponse dealComment(String articleId) {
105 106 107
        int start = -1;
        int end = selfComment.length;
        int temp_count = (int) (Math.floor(Math.random() * (start - end + 1)) + end);
108 109 110
        HttpResponse response = HttpUtil.createPost(commentUrl)
                .header("Cookie", csdnCookie)
                .form("articleId", articleId)
111
                .form("content", selfComment[temp_count])
112 113
                .execute();
        final String body = response.body();
114
        log.info(body);
115 116 117 118 119 120 121 122 123 124
        ObjectMapper objectMapper = new ObjectMapper();
        CommentResponse commentResponse = null;
        try {
            commentResponse = objectMapper.readValue(body, CommentResponse.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return commentResponse;
    }
}