CsdnCollectServiceImpl.java 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
package com.kwan.springbootkwan.service.impl;

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.CollectInfoQuery;
import com.kwan.springbootkwan.entity.resp.CollectResponse;
import com.kwan.springbootkwan.entity.resp.IsCollectResponse;
import com.kwan.springbootkwan.service.CsdnCollectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

16 17
import java.util.ArrayList;

18 19 20 21 22 23
@Slf4j
@Service
public class CsdnCollectServiceImpl implements CsdnCollectService {

    @Value("${csdn.cookie}")
    private String csdnCookie;
24 25
    @Value("${csdn.self_folder_id}")
    private Integer selfFolderId;
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    @Value("${csdn.self_user_name}")
    private String selfUserName;
    @Value("${csdn.url.is_collect_url}")
    private String isCollectUrl;
    @Value("${csdn.url.add_collect_url}")
    private String addCollectUrl;

    /**
     * 收藏满了
     */
    private static boolean COLLECT_IS_FULL = false;


    @Override
    public Boolean isCollect(String articleId) {
        if (COLLECT_IS_FULL) {
            return true;
        }
        HttpResponse response = HttpUtil.createGet(isCollectUrl)
                .header("Cookie", csdnCookie)
                .form("articleId", articleId)
                .execute();
        final String body = response.body();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            final int code = objectMapper.readValue(body, IsCollectResponse.class).code;
            final IsCollectResponse.CollectDataDetail data = objectMapper.readValue(body, IsCollectResponse.class).getData();
            if (code == 200) {
                final boolean status = data.status;
                if (status) {
56
                    log.info("文章{}已经收藏过", articleId);
57
                } else {
58
                    log.info("文章{}未收藏", articleId);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
                }
                return status;
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public Boolean collect(BusinessInfoResponse.ArticleData.Article article, String username) {
        if (COLLECT_IS_FULL) {
            return true;
        }
        final String urlInfo = article.getUrl();
        String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1);
        CollectResponse collectResponse = null;
        try {
            CollectInfoQuery collectInfoQuery = new CollectInfoQuery();
            collectInfoQuery.setSourceId(Integer.valueOf(articleId));
            collectInfoQuery.setFromType("PC");
            collectInfoQuery.setAuthor(username);
            collectInfoQuery.setDescription(article.getDescription());
            collectInfoQuery.setSource("blog");
83 84 85
            ArrayList<Integer> list = new ArrayList<>();
            list.add(selfFolderId);
            collectInfoQuery.setFolderIdList(list);
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
            collectInfoQuery.setTitle(article.getTitle());
            collectInfoQuery.setUrl(article.getUrl());
            collectInfoQuery.setUsername(selfUserName);
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonCollectInfo = objectMapper.writeValueAsString(collectInfoQuery);
            HttpResponse response = HttpUtil.createPost(addCollectUrl)
                    .header("Cookie", csdnCookie)
                    .header("Content-Type", "application/json")
                    .body(jsonCollectInfo)
                    .execute();
            final String body = response.body();
            collectResponse = objectMapper.readValue(body, CollectResponse.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        final Long code = collectResponse.code;
        if (code.equals(200)) {
103
            log.info("文章{}收藏成功", articleId);
104
        } else if (code.equals(400000101)) {
105
            log.info("收藏文章{}参数缺失", articleId);
106 107 108 109 110 111 112
        } else if (code.equals(400)) {
            COLLECT_IS_FULL = true;
            log.info("今日收藏次数已达上限!");
        }
        return true;
    }
}