CsdnCollectServiceImpl.java 4.1 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 25 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
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.google.common.collect.Lists;
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;

@Slf4j
@Service
public class CsdnCollectServiceImpl implements CsdnCollectService {

    @Value("${csdn.cookie}")
    private String csdnCookie;
    @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) {
                    log.info("已收藏");
                } else {
                    log.info("未收藏");
                }
                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");
            collectInfoQuery.setFolderIdList(Lists.newArrayList(7589042));
            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)) {
            log.info("收藏成功");
        } else if (code.equals(400000101)) {
            log.info("参数缺失");
        } else if (code.equals(400)) {
            COLLECT_IS_FULL = true;
            log.info("今日收藏次数已达上限!");
        }
        return true;
    }
}