CsdnLikeServiceImpl.java 3.6 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
import com.kwan.springbootkwan.entity.resp.LikeResponse;
10
import com.kwan.springbootkwan.enums.LikeStatus;
11 12
import com.kwan.springbootkwan.service.CsdnLikeService;
import lombok.extern.slf4j.Slf4j;
13
import org.springframework.beans.factory.annotation.Value;
14 15 16 17 18 19
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class CsdnLikeServiceImpl implements CsdnLikeService {
20 21 22 23 24 25
    @Value("${csdn.cookie}")
    private String csdnCookie;
    @Value("${csdn.url.like_url}")
    private String url;

    @Override
26 27 28 29
    public Boolean isLike(String articleId, CsdnUserInfo csdnUserInfo) {
        final String userName = csdnUserInfo.getUserName();
        final Integer likeStatus = csdnUserInfo.getLikeStatus();
        if (LikeStatus.HAVE_ALREADY_LIKED.getCode().equals(likeStatus) || LikeStatus.LIKE_IS_FULL.getCode().equals(likeStatus)) {
30 31 32 33 34 35 36 37 38 39
            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);
40
            csdnUserInfo.setLikeStatus(LikeStatus.HAVE_ALREADY_LIKED.getCode());
41 42 43
            return true;
        } else {
            log.info("文章{}未点过赞", articleId);
44
            csdnUserInfo.setLikeStatus(LikeStatus.UN_PROCESSED.getCode());
45 46
            return false;
        }
47
    }
48 49

    @Override
50
    public Boolean like(String articleId, CsdnUserInfo csdnUserInfo, CsdnTripletDayInfo csdnTripletDayInfo) {
51 52
        final Integer likeStatus = csdnUserInfo.getLikeStatus();
        if (LikeStatus.HAVE_ALREADY_LIKED.getCode().equals(likeStatus) || LikeStatus.LIKE_IS_FULL.getCode().equals(likeStatus)) {
53 54 55
            return true;
        }
        HttpResponse response = HttpUtil.createPost(url)
56
                .header("Cookie", csdnCookie)
57 58 59 60 61
                .form("articleId", articleId)
                .execute();
        final String body = response.body();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
62 63
            final int code = objectMapper.readValue(body, LikeResponse.class).code;
            final LikeResponse.LikeDataDetail data = objectMapper.readValue(body, LikeResponse.class).getData();
64 65 66
            if (code == 200) {
                final boolean status = data.status;
                if (status) {
67
                    log.info("文章{}点赞成功", articleId);
68
                    csdnUserInfo.setLikeStatus(LikeStatus.LIKE_SUCCESSFUL.getCode());
69
                    csdnTripletDayInfo.setLikeNum(csdnTripletDayInfo.getLikeNum() + 1);
70
                } else {
71
                    log.info("文章{}点赞取消", articleId);
72
                    csdnUserInfo.setLikeStatus(LikeStatus.CANCEL_LIKES.getCode());
73 74 75 76
                }
                return status;
            } else if (code == 400) {
                log.info("今日点赞次数已达上限!");
77
                csdnUserInfo.setLikeStatus(LikeStatus.LIKE_IS_FULL.getCode());
78 79 80 81 82 83 84
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return true;
    }
}