CsdnServiceImpl.java 11.6 KB
Newer Older
1 2
package com.kwan.springbootkwan.service.impl;

3
import cn.hutool.core.collection.CollectionUtil;
4 5 6 7
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.google.common.collect.Lists;
9
import com.kwan.springbootkwan.entity.CsdnStatusResp;
10
import com.kwan.springbootkwan.entity.CsdnUserInfo;
11
import com.kwan.springbootkwan.entity.resp.BusinessInfo;
12 13
import com.kwan.springbootkwan.entity.resp.CollectData;
import com.kwan.springbootkwan.entity.resp.CollectInfo;
14 15
import com.kwan.springbootkwan.entity.resp.CommentData;
import com.kwan.springbootkwan.service.CsdnService;
16 17
import com.kwan.springbootkwan.service.CsdnUserInfoService;
import lombok.extern.slf4j.Slf4j;
18
import org.apache.commons.lang3.StringUtils;
19
import org.springframework.beans.factory.annotation.Autowired;
20 21
import org.springframework.stereotype.Service;

22 23
import java.util.List;

24 25

@Slf4j
26 27
@Service
public class CsdnServiceImpl implements CsdnService {
28 29 30
    /**
     * 构建header参数
     */
31
    private static final String CUSTOM_HEADER_NAME = "Cookie";
32 33 34 35
    /**
     * 点赞满了
     */
    private static boolean LIKE_IS_FULL = false;
36 37 38 39
    /**
     * 收藏满了
     */
    private static boolean COLLECT_IS_FULL = false;
40 41 42
    /**
     * 参数值
     */
43 44
    private static final String CUSTOM_HEADER_VALUE = "uuid_tt_dd=10_20285116700-1697522872601-604163; c_adb=1; loginbox_strategy=%7B%22taskId%22%3A308%2C%22abCheckTime%22%3A1697522874474%2C%22version%22%3A%22control%22%7D; UserName=qyj19920704; UserInfo=a7d3b88c53a841ebb5792202cb43c84f; UserToken=a7d3b88c53a841ebb5792202cb43c84f; UserNick=%E6%AA%80%E8%B6%8A%E5%89%91%E6%8C%87%E5%A4%A7%E5%8E%82; AU=769; UN=qyj19920704; BT=1697522886100; p_uid=U010000; Hm_up_6bcd52f51e9b3dce32bec4a3997715ac=%7B%22islogin%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isonline%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isvip%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%2C%22uid_%22%3A%7B%22value%22%3A%22qyj19920704%22%2C%22scope%22%3A1%7D%7D; management_ques=1697592152734; blog_details_recommend_nps=1697616147423; c_segment=3; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1697522874,1697551027,1697636661,1697683407; csrfToken=jTRD3_1IBVA703lu2FkVkazx; dc_sid=9f7a02ed8375e91d86271ddaf1cf2ce4; c_first_ref=default; c_first_page=https%3A//liucy.blog.csdn.net/article/details/133852303%3Fspm%3D1001.2014.3001.5502; c_segment=3; Hm_lvt_e5ef47b9f471504959267fd614d579cd=1697764951; Hm_lpvt_e5ef47b9f471504959267fd614d579cd=1697764951; 404_page_nps=1697764955523; ssxmod_itna=YqUxBD97kOGHD8D2BmYite44zxcYo1=WOiox0vmvqGzDAxn40iDtgeqODDwu0/DNMQiDpiDuWOntaV73S5fd3iPeaN3D84i7DKqibDCqD1D3qDktzDYA8Dt4DTD34DYDiO=DBRsUQDFATN/3wUjmGtDG4DgDB=DxBEl3djBAq4DCr4DbxPy7mrDtLNSeLLdXT0/nn5Yf445SOG5lipwm0GdG7vwBA4PlDxNt8v1wADoGGGQYf+IE75ep753zpDxD; ssxmod_itna2=YqUxBD97kOGHD8D2BmYite44zxcYo1=WOYxn9EenDDsYebDLGQCoqQuGvHdDQRKYubdD6QqomsRD3xyRI/W9Q5nhe8oeYUvimaWYrNsqwcDgL6mDjYUhZK09LYcY/t5hSuP2va/kclf6hRkNh+LPyDwic73=9i4d94Lvw/R=oQ7E+jT4jjjA2Y+=QplLm8nbePRb+7=Re1CpjmZ4wIrnNPSKofWnIj=WOtkFtphylt+GhUxr0KkGtTzDXKDEEkbn3qhIn3Sj/ezt1u2MO3M9h7zEL6RunrXaKE+YdkRnIKiTG93cqpGFq8Fwr7ehksk1ZSWhoN2hQydddqQ1bTOSW8ddX+7g24v4TZ0+52=Y8PsOwfhavW4ECY8+GZOK=ro97bK0=Yi3YD5ZGsq25FhPodIRGCD2O8xidfo8+NR88v+rq4+b=fUOEK2frPEPdLFuYkao9uCfM1+cFZQ7q6HEMfRLjf6bhof0=1YhEYO1U=NZCPvDDw2Psluciq4De13mDNBODwqLwwg44qwOiDf=j1fKK5q+8gd7DDjKDeMv/04YDmLGvrwqOz4cov79500D4D==; c_first_ref=default; c_first_page=https%3A//mp.csdn.net/; creative_btn_mp=3; c_hasSub=true; write_guide_show=3; log_Id_click=177; dc_session_id=11_1698022100317.787671; c_dsid=11_1698022100317.794351; log_Id_pv=149; log_Id_view=1105; c_pref=https%3A//i.csdn.net/; c_ref=https%3A//blog.csdn.net/imwucx; c_page_id=default; dc_session_id=11_1698022100317.787671; c_dsid=11_1698022100317.794351; log_Id_view=1106; dc_tos=s2yi9b; log_Id_click=178; c_pref=https%3A//blog.csdn.net/imwucx; log_Id_pv=150; creativeSetApiNew=%7B%22toolbarImg%22%3Anull%2C%22publishSuccessImg%22%3Anull%2C%22articleNum%22%3A895%2C%22type%22%3A0%2C%22oldUser%22%3Afalse%2C%22useSeven%22%3Afalse%2C%22userName%22%3A%22qyj19920704%22%7D; c_ref=https%3A//cxian.blog.csdn.net/article/details/131299195; c_page_id=default; Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac=1698022433; dc_tos=s2yinc";

45 46 47
    @Autowired
    private CsdnUserInfoService csdnUserInfoService;

48
    @Override
49
    public CsdnStatusResp singleArticle(String username) {
50 51 52
        String url = "https://blog.csdn.net/community/home-api/v1/get-business-list";
        // 使用Hutool发送GET请求
        HttpResponse response = HttpUtil.createGet(url)
53
                .header(CUSTOM_HEADER_NAME, CUSTOM_HEADER_VALUE)
54
                .form("page", 1)
55
                .form("size", 5)
56 57 58 59 60 61 62 63 64 65 66 67 68
                .form("businessType", "lately")
                .form("noMore", false)
                .form("username", username)
                .execute();
        // 打印响应结果
        final String body = response.body();
        ObjectMapper objectMapper = new ObjectMapper();
        BusinessInfo businessInfo = null;
        try {
            businessInfo = objectMapper.readValue(body, BusinessInfo.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
69 70
        final BusinessInfo.ArticleData data = businessInfo.getData();
        final List<BusinessInfo.ArticleData.Article> list = data.getList();
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        //处理评论过
        int commentNum = 0;
        int successNum = 0;
        int notBlogNum = 0;
        int count = 3;//评论3篇
        for (int i = 0; i < count; i++) {
            final BusinessInfo.ArticleData.Article article = list.get(i);
            final String type = article.getType();
            if (!StringUtils.equals("blog", type)) {
                notBlogNum++;
                continue;
            }
            final String urlInfo = article.getUrl();
            String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1);
            final Boolean articleInfo = this.getArticleInfo(articleId);
            if (!articleInfo) {
                //进行点赞和评论
                final int comment = this.comment(articleId);
                // 打印响应结果
                if (comment == 200) {
                    successNum++;
92 93 94
                    if (!COLLECT_IS_FULL) {
                        this.collect(username, article, articleId);
                    }
95 96 97 98 99 100 101 102 103
                    if (!LIKE_IS_FULL) {
                        this.like(articleId);
                    }
                } else if (comment == 400) {
                    return new CsdnStatusResp(400, "您评论太快了,请休息一下!");
                }
            } else {
                commentNum++;
            }
104
        }
105 106
        if (count != successNum) {
            return new CsdnStatusResp(300, "评论过的数量=" + commentNum + "非博客的数量:" + notBlogNum);
107
        }
108
        return new CsdnStatusResp(200, "三连完成");
109 110
    }

111 112 113 114 115 116 117 118 119 120 121 122 123 124 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

    /**
     * 收藏
     *
     * @param username
     * @param article
     * @param articleId
     */
    private void collect(String username, BusinessInfo.ArticleData.Article article, String articleId) {
        CollectData collectData = null;
        try {
            CollectInfo collectInfo = new CollectInfo();
            collectInfo.setSourceId(Integer.valueOf(articleId));
            collectInfo.setFromType("PC");
            collectInfo.setAuthor(username);
            collectInfo.setDescription(article.getDescription());
            collectInfo.setSource("blog");
            collectInfo.setFolderIdList(Lists.newArrayList(7589042));
            collectInfo.setTitle(article.getTitle());
            collectInfo.setUrl(article.getUrl());
            collectInfo.setUsername("qyj19920704");
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonCollectInfo = objectMapper.writeValueAsString(collectInfo);
            System.out.println("jsonCollectInfo=" + jsonCollectInfo);
            String collectUrl = "https://mp-action.csdn.net/interact/wrapper/pc/favorite/v1/api/addFavoriteInFolds";
            // 使用Hutool发送GET请求
            HttpResponse response = HttpUtil.createPost(collectUrl)
                    .header(CUSTOM_HEADER_NAME, CUSTOM_HEADER_VALUE)
                    .header("Content-Type", "application/json")
                    .body(jsonCollectInfo)
                    .execute();
            // 打印响应结果
            final String body = response.body();
            log.info(body);
            collectData = objectMapper.readValue(body, CollectData.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        final Long code = collectData.code;
        if (code.equals(200)) {
            log.info("收藏成功");
        } else if (code.equals(400000101)) {
            log.info("参数缺失");
        } else if (code.equals(400)) {
            COLLECT_IS_FULL = true;
            log.info("今日收藏次数已达上限!");
        }
    }

160
    @Override
161
    public Boolean getArticleInfo(String articleId) {
162 163 164
        String url = "https://blog.csdn.net/phoenix/web/v1/comment/list/" + articleId;
        // 使用Hutool发送GET请求
        HttpResponse response = HttpUtil.createPost(url)
165
                .header(CUSTOM_HEADER_NAME, CUSTOM_HEADER_VALUE)
166 167 168 169 170 171 172 173 174 175 176 177 178
                .form("page", 1)
                .form("size", 100)
                .form("fold", "unfold")
                .execute();
        // 打印响应结果
        final String body = response.body();
        ObjectMapper objectMapper = new ObjectMapper();
        CommentData articleInfo = null;
        try {
            articleInfo = objectMapper.readValue(body, CommentData.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        final CommentData.DataInfo data = articleInfo.getData();
        final List<CommentData.Comment> list = data.getList();
        if (CollectionUtil.isNotEmpty(list)) {
            for (CommentData.Comment comment : list) {
                final CommentData.Info info = comment.getInfo();
                final String userName = info.getUserName();
                if (StringUtils.equals(userName, "qyj19920704")) {
                    //评论过
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public void like(String articleId) {
196

197 198 199
    }

    @Override
200
    public int comment(String articleId) {
201 202 203 204 205 206 207
        String url = "https://blog.csdn.net/phoenix/web/v1/comment/submit";
        // 使用Hutool发送GET请求
        HttpResponse response = HttpUtil.createPost(url)
                .header(CUSTOM_HEADER_NAME, CUSTOM_HEADER_VALUE)  // 添加自定义header参数
                .form("articleId", articleId)
                .form("content", "支持博主优质文章,讲解得非常详细,干货满满,通俗易懂,期待博主下次更新")
                .execute();
208
        return response.getStatus();
209 210 211 212 213 214
    }

    @Override
    public void multiTriplet() {
        final List<CsdnUserInfo> list = csdnUserInfoService.list();
        if (CollectionUtil.isNotEmpty(list)) {
215
            for (CsdnUserInfo csdnUserInfo : list) {
216 217
                // 构建参数
                String username = csdnUserInfo.getUserName();
218 219 220 221 222 223 224 225 226
                final CsdnStatusResp csdnStatusResp = singleArticle(username);
                final Integer code = csdnStatusResp.getCode();
                final String value = csdnStatusResp.getValue();
                if (code.equals(400)) {
                    log.info("用户{}:code={},{}", username, code, value);
                    break;
                }
                log.info("用户{}:{}", username, value);
            }
227
        }
228
    }
229
}