CsdnServiceImpl.java 4.6 KB
Newer Older
1 2
package com.kwan.springbootkwan.service.impl;

3
import cn.hutool.core.collection.CollectionUtil;
4 5
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
6
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
7 8
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
9
import com.kwan.springbootkwan.entity.CsdnUserInfo;
10 11 12
import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse;
import com.kwan.springbootkwan.service.CsdnCollectService;
import com.kwan.springbootkwan.service.CsdnCommentService;
13
import com.kwan.springbootkwan.service.CsdnLikeService;
14
import com.kwan.springbootkwan.service.CsdnService;
15 16
import com.kwan.springbootkwan.service.CsdnUserInfoService;
import lombok.extern.slf4j.Slf4j;
17
import org.apache.commons.lang3.StringUtils;
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.beans.factory.annotation.Value;
20 21
import org.springframework.stereotype.Service;

22 23
import java.util.List;

24 25

@Slf4j
26 27 28
@Service
public class CsdnServiceImpl implements CsdnService {

29 30 31 32 33 34
    @Value("${csdn.cookie}")
    private String csdnCookie;
    @Value("${csdn.num_of_articles_per_person}")
    private Integer numOfArticlesPerPerson;
    @Value("${csdn.url.user_article_url}")
    private String url;
35 36
    @Autowired
    private CsdnUserInfoService csdnUserInfoService;
37 38 39 40
    @Autowired
    private CsdnCollectService csdnCollectService;
    @Autowired
    private CsdnCommentService csdnCommentService;
41 42
    @Autowired
    private CsdnLikeService csdnLikeService;
43

44
    @Override
45 46
    public void singleArticle(CsdnUserInfo csdnUserInfo) {
        final String username = csdnUserInfo.getUserName();
47 48 49 50 51 52 53 54
        List<BusinessInfoResponse.ArticleData.Article> list = this.getArticles(username);
        if (list == null) {
            return;
        }
        for (int i = 0; i < numOfArticlesPerPerson; i++) {
            final BusinessInfoResponse.ArticleData.Article article = list.get(i);
            final String type = article.getType();
            if (!StringUtils.equals("blog", type)) {
55
                csdnUserInfo.setArticleType(type);
56 57 58 59 60
                continue;
            }
            final String urlInfo = article.getUrl();
            String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1);
            //点赞
61
            final Boolean isLike = csdnLikeService.isLike(articleId, csdnUserInfo);
62
            if (!isLike) {
63
                csdnLikeService.like(articleId, csdnUserInfo);
64 65
            }
            //评论
66
            final Boolean comment = csdnCommentService.isComment(article, csdnUserInfo);
67
            if (!comment) {
68
                csdnCommentService.comment(articleId, csdnUserInfo);
69 70
            }
            //收藏
71
            final Boolean collect = csdnCollectService.isCollect(articleId, csdnUserInfo);
72
            if (!collect) {
73
                csdnCollectService.collect(article, csdnUserInfo);
74 75
            }
        }
76
        csdnUserInfoService.updateById(csdnUserInfo);
77 78 79
    }

    private List<BusinessInfoResponse.ArticleData.Article> getArticles(String username) {
80
        HttpResponse response = HttpUtil.createGet(url)
81
                .header("Cookie", csdnCookie)
82
                .form("page", 1)
83
                .form("size", 5)
84 85 86 87 88 89
                .form("businessType", "lately")
                .form("noMore", false)
                .form("username", username)
                .execute();
        final String body = response.body();
        ObjectMapper objectMapper = new ObjectMapper();
90
        BusinessInfoResponse businessInfoResponse;
91
        List<BusinessInfoResponse.ArticleData.Article> list = null;
92
        try {
93 94
            businessInfoResponse = objectMapper.readValue(body, BusinessInfoResponse.class);
            final BusinessInfoResponse.ArticleData data = businessInfoResponse.getData();
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
            list = data.getList();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        final int size = list.size();
        if (size < numOfArticlesPerPerson) {
            numOfArticlesPerPerson = size;
        }
        return list;
    }

    @Override
    public void multiTriplet() {
111 112
        QueryWrapper<CsdnUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", 0);
113
        wrapper.orderByDesc("user_weight");
114
        final List<CsdnUserInfo> list = csdnUserInfoService.list(wrapper);
115 116
        if (CollectionUtil.isNotEmpty(list)) {
            for (CsdnUserInfo csdnUserInfo : list) {
117
                singleArticle(csdnUserInfo);
118 119 120 121
            }
        }
        log.info("全部三连完成");
    }
122
}