CsdnCollectServiceImpl.java 5.2 KB
Newer Older
1 2 3 4 5 6
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;
7
import com.kwan.springbootkwan.entity.CsdnUserInfo;
8 9 10 11
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;
12
import com.kwan.springbootkwan.enums.CollectStatus;
13 14 15 16 17
import com.kwan.springbootkwan.service.CsdnCollectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

18
import java.util.ArrayList;
19
import java.util.List;
20

21 22 23 24 25 26
@Slf4j
@Service
public class CsdnCollectServiceImpl implements CsdnCollectService {

    @Value("${csdn.cookie}")
    private String csdnCookie;
27 28
    @Value("${csdn.self_folder_id}")
    private Integer selfFolderId;
29 30 31 32 33 34 35 36
    @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;

    @Override
37 38 39
    public Boolean isCollect(String articleId, CsdnUserInfo csdnUserInfo) {
        final Integer collectStatus = csdnUserInfo.getCollectStatus();
        if (CollectStatus.HAVE_ALREADY_COLLECT.getCode().equals(collectStatus) || CollectStatus.COLLECT_IS_FULL.getCode().equals(collectStatus)) {
40 41 42 43 44 45 46 47 48 49 50 51 52 53
            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) {
54
                    log.info("文章{}已经收藏过", articleId);
55
                    csdnUserInfo.setCommentStatus(CollectStatus.HAVE_ALREADY_COLLECT.getCode());
56
                } else {
57
                    log.info("文章{}未收藏", articleId);
58
                    csdnUserInfo.setCommentStatus(CollectStatus.UN_PROCESSED.getCode());
59 60 61 62 63 64 65 66 67 68
                }
                return status;
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
69 70 71 72
    public Boolean collect(BusinessInfoResponse.ArticleData.Article article, CsdnUserInfo csdnUserInfo) {
        final String userName = csdnUserInfo.getUserName();
        final Integer collectStatus = csdnUserInfo.getCollectStatus();
        if (CollectStatus.HAVE_ALREADY_COLLECT.getCode().equals(collectStatus) || CollectStatus.COLLECT_IS_FULL.getCode().equals(collectStatus)) {
73 74 75 76 77 78 79 80 81
            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");
82
            collectInfoQuery.setAuthor(userName);
83 84
            collectInfoQuery.setDescription(article.getDescription());
            collectInfoQuery.setSource("blog");
85
            List<Integer> list = new ArrayList<>();
86 87
            list.add(selfFolderId);
            collectInfoQuery.setFolderIdList(list);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
            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)) {
105
            log.info("文章{}收藏成功", articleId);
106
            csdnUserInfo.setCollectStatus(CollectStatus.COLLECT_SUCCESSFUL.getCode());
107
        } else if (code.equals(400000101)) {
108
            log.info("收藏文章{}参数缺失", articleId);
109
            csdnUserInfo.setCollectStatus(CollectStatus.MISSING_PARAMETER.getCode());
110 111
        } else if (code.equals(400)) {
            log.info("今日收藏次数已达上限!");
112
            csdnUserInfo.setCollectStatus(CollectStatus.COLLECT_IS_FULL.getCode());
113 114 115 116
        }
        return true;
    }
}