CsdnArticleInfoServiceImpl.java 3.4 KB
Newer Older
1 2
package com.kwan.springbootkwan.service.impl;

3 4 5
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
6
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
7 8
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
9
import com.kwan.springbootkwan.entity.CsdnArticleInfo;
10 11
import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse;
import com.kwan.springbootkwan.mapper.CsdnArticleInfoMapper;
12
import com.kwan.springbootkwan.service.CsdnArticleInfoService;
13
import org.springframework.beans.factory.annotation.Value;
14 15
import org.springframework.stereotype.Service;

16 17
import java.util.List;

18 19 20 21 22 23 24
/**
 * csdn文章信息(CsdnArticleInfo)表服务实现类
 *
 * @author makejava
 * @since 2023-10-28 01:58:46
 */
@Service("csdnArticleInfoService")
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
public class CsdnArticleInfoServiceImpl extends ServiceImpl<CsdnArticleInfoMapper, CsdnArticleInfo> implements CsdnArticleInfoService {

    @Value("${csdn.cookie}")
    private String csdnCookie;
    @Value("${csdn.url.user_article_url}")
    private String url;

    @Override
    public List<BusinessInfoResponse.ArticleData.Article> getArticles(String username) {
        HttpResponse response = HttpUtil.createGet(url)
                .header("Cookie", csdnCookie)
                .form("page", 1)
                .form("size", 10)
//                .form("businessType", "blog")
//                .form("blogType", "ViewCount")
                .form("businessType", "lately")
                .form("noMore", false)
                .form("username", username)
                .execute();
        final String body = response.body();
        ObjectMapper objectMapper = new ObjectMapper();
        BusinessInfoResponse businessInfoResponse;
        List<BusinessInfoResponse.ArticleData.Article> list = null;
        try {
            businessInfoResponse = objectMapper.readValue(body, BusinessInfoResponse.class);
            final BusinessInfoResponse.ArticleData data = businessInfoResponse.getData();
            list = data.getList();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        return list;
    }
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
    @Override
    public List<BusinessInfoResponse.ArticleData.Article> getBlogs(String username) {
        HttpResponse response = HttpUtil.createGet(url)
                .header("Cookie", csdnCookie)
                .form("page", 1)
                .form("size", 10)
                .form("businessType", "blog")
//                .form("blogType", "ViewCount")
                .form("noMore", false)
                .form("username", username)
                .execute();
        final String body = response.body();
        ObjectMapper objectMapper = new ObjectMapper();
        BusinessInfoResponse businessInfoResponse;
        List<BusinessInfoResponse.ArticleData.Article> list = null;
        try {
            businessInfoResponse = objectMapper.readValue(body, BusinessInfoResponse.class);
            final BusinessInfoResponse.ArticleData data = businessInfoResponse.getData();
            list = data.getList();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        return list;
    }
88 89
}