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.LikeResponse; import com.kwan.springbootkwan.service.CsdnLikeService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Slf4j @Service public class CsdnLikeServiceImpl implements CsdnLikeService { @Value("${csdn.cookie}") private String csdnCookie; @Value("${csdn.url.like_url}") private String url; /** * 点赞满了 */ private static boolean LIKE_IS_FULL = false; @Override public Boolean isLike(String articleId, String userName) { if (LIKE_IS_FULL) { return true; } String url = "https://blog.csdn.net/" + userName + "/article/details/" + articleId; HttpResponse response = HttpUtil.createGet(url) .header("Cookie", csdnCookie) .form("articleId", articleId) .execute(); final String body = response.body(); if (body.contains("isLikeStatus = true")) { log.info("文章{}已经点过赞", articleId); return true; } else { log.info("文章{}未点过赞", articleId); return false; } } @Override public Boolean like(String articleId) { if (LIKE_IS_FULL) { return true; } HttpResponse response = HttpUtil.createPost(url) .header("Cookie", csdnCookie) .form("articleId", articleId) .execute(); final String body = response.body(); ObjectMapper objectMapper = new ObjectMapper(); try { final int code = objectMapper.readValue(body, LikeResponse.class).code; final LikeResponse.LikeDataDetail data = objectMapper.readValue(body, LikeResponse.class).getData(); if (code == 200) { final boolean status = data.status; if (status) { log.info("文章{}点赞成功", articleId); } else { log.info("文章{}点赞取消", articleId); } return status; } else if (code == 400) { LIKE_IS_FULL = true; log.info("今日点赞次数已达上限!"); } } catch (JsonProcessingException e) { e.printStackTrace(); } return true; } }