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


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

    @Override
25 26 27 28
    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)) {
29 30 31 32 33 34 35 36 37 38
            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);
39
            csdnUserInfo.setLikeStatus(LikeStatus.HAVE_ALREADY_LIKED.getCode());
40 41 42
            return true;
        } else {
            log.info("文章{}未点过赞", articleId);
43
            csdnUserInfo.setLikeStatus(LikeStatus.UN_PROCESSED.getCode());
44 45
            return false;
        }
46
    }
47 48

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