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; } }