CsdnCollectServiceImpl.java 5.5 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.CsdnTripletDayInfo;
8
import com.kwan.springbootkwan.entity.CsdnUserInfo;
9 10 11 12
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;
13
import com.kwan.springbootkwan.enums.CollectStatus;
14 15 16 17 18
import com.kwan.springbootkwan.service.CsdnCollectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

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

    @Value("${csdn.cookie}")
    private String csdnCookie;
28 29
    @Value("${csdn.self_folder_id}")
    private Integer selfFolderId;
30 31 32 33 34 35 36 37
    @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
38 39 40
    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)) {
41 42 43 44 45 46 47 48 49 50 51 52 53 54
            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) {
55
                    log.info("文章{}已经收藏过", articleId);
56
                    csdnUserInfo.setCollectStatus(CollectStatus.HAVE_ALREADY_COLLECT.getCode());
57
                } else {
58
                    log.info("文章{}未收藏", articleId);
59
                    csdnUserInfo.setCollectStatus(CollectStatus.UN_PROCESSED.getCode());
60 61 62 63 64 65 66 67 68 69
                }
                return status;
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return true;
    }

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