CsdnServiceImpl.java 5.2 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
import com.kwan.springbootkwan.entity.resp.BusinessInfoResponse;
11
import com.kwan.springbootkwan.enums.CommentStatus;
12 13
import com.kwan.springbootkwan.service.CsdnCollectService;
import com.kwan.springbootkwan.service.CsdnCommentService;
14
import com.kwan.springbootkwan.service.CsdnLikeService;
15
import com.kwan.springbootkwan.service.CsdnService;
16 17
import com.kwan.springbootkwan.service.CsdnUserInfoService;
import lombok.extern.slf4j.Slf4j;
18
import org.apache.commons.lang3.StringUtils;
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.beans.factory.annotation.Value;
21 22
import org.springframework.stereotype.Service;

23 24
import java.util.List;

25 26

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

30 31 32 33 34 35
    @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;
36 37
    @Autowired
    private CsdnUserInfoService csdnUserInfoService;
38 39 40 41
    @Autowired
    private CsdnCollectService csdnCollectService;
    @Autowired
    private CsdnCommentService csdnCommentService;
42 43
    @Autowired
    private CsdnLikeService csdnLikeService;
44

45
    @Override
46 47
    public void singleArticle(CsdnUserInfo csdnUserInfo) {
        final String username = csdnUserInfo.getUserName();
48 49 50 51 52 53 54 55
        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)) {
56
                csdnUserInfo.setArticleType(type);
57 58 59 60 61
                continue;
            }
            final String urlInfo = article.getUrl();
            String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1);
            //点赞
62
            final Boolean isLike = csdnLikeService.isLike(articleId, csdnUserInfo);
63
            if (!isLike) {
64
                csdnLikeService.like(articleId, csdnUserInfo);
65
            }
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
            //查看已经评论总数量
            QueryWrapper<CsdnUserInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("is_delete", 0);
            wrapper.eq("comment_status", 9);
            final List<CsdnUserInfo> comments = csdnUserInfoService.list(wrapper);
            if (CollectionUtil.isNotEmpty(comments)) {
                if (comments.size() >= 49) {
                    csdnUserInfo.setCommentStatus(CommentStatus.COMMENT_NUM_49.getCode());
                } else {
                    //评论
                    final Boolean comment = csdnCommentService.isComment(article, csdnUserInfo);
                    if (!comment) {
                        csdnCommentService.comment(articleId, csdnUserInfo);
                    }
                }
81 82
            }
            //收藏
83
            final Boolean collect = csdnCollectService.isCollect(articleId, csdnUserInfo);
84
            if (!collect) {
85
                csdnCollectService.collect(article, csdnUserInfo);
86 87
            }
        }
88
        csdnUserInfoService.updateById(csdnUserInfo);
89 90 91
    }

    private List<BusinessInfoResponse.ArticleData.Article> getArticles(String username) {
92
        HttpResponse response = HttpUtil.createGet(url)
93
                .header("Cookie", csdnCookie)
94
                .form("page", 1)
95
                .form("size", 5)
96 97 98 99 100 101
                .form("businessType", "lately")
                .form("noMore", false)
                .form("username", username)
                .execute();
        final String body = response.body();
        ObjectMapper objectMapper = new ObjectMapper();
102
        BusinessInfoResponse businessInfoResponse;
103
        List<BusinessInfoResponse.ArticleData.Article> list = null;
104
        try {
105 106
            businessInfoResponse = objectMapper.readValue(body, BusinessInfoResponse.class);
            final BusinessInfoResponse.ArticleData data = businessInfoResponse.getData();
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
            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() {
123 124
        QueryWrapper<CsdnUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", 0);
125
        wrapper.orderByDesc("user_weight");
126
        final List<CsdnUserInfo> list = csdnUserInfoService.list(wrapper);
127 128
        if (CollectionUtil.isNotEmpty(list)) {
            for (CsdnUserInfo csdnUserInfo : list) {
129
                singleArticle(csdnUserInfo);
130 131 132 133
            }
        }
        log.info("全部三连完成");
    }
134
}