CsdnServiceImpl.java 4.8 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
    public void singleArticle(String username) {
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        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)) {
                continue;
            }
            final String urlInfo = article.getUrl();
            String articleId = urlInfo.substring(urlInfo.lastIndexOf("/") + 1);
            //点赞
            final Boolean isLike = csdnLikeService.isLike(articleId, username);
            if (!isLike) {
                csdnLikeService.like(articleId);
            }
            //评论
            final Boolean comment = csdnCommentService.isComment(article);
            if (!comment) {
                csdnCommentService.comment(articleId);
            }
            //收藏
            final Boolean collect = csdnCollectService.isCollect(articleId);
            if (!collect) {
                csdnCollectService.collect(article, username);
            }
        }
    }

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


//    /**
//     * 多人三连非数据库
//     */
//    @Override
//    public void multiTriplet() {
//        String[] userNames = new String[]{"user1", "user2", "user3", "xxxx"};
//        for (String name : userNames) {
//            singleArticle(name);
//        }
//        log.info("全部三连完成");
//    }
133
}