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

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

21 22
import java.util.List;

23 24

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

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

43
    @Override
44
    public void singleArticle(String username) {
45
        HttpResponse response = HttpUtil.createGet(url)
46
                .header("Cookie", csdnCookie)
47
                .form("page", 1)
48
                .form("size", 5)
49 50 51 52 53 54
                .form("businessType", "lately")
                .form("noMore", false)
                .form("username", username)
                .execute();
        final String body = response.body();
        ObjectMapper objectMapper = new ObjectMapper();
55
        BusinessInfoResponse businessInfoResponse;
56
        try {
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
            businessInfoResponse = objectMapper.readValue(body, BusinessInfoResponse.class);
            final BusinessInfoResponse.ArticleData data = businessInfoResponse.getData();
            final List<BusinessInfoResponse.ArticleData.Article> list = data.getList();
            if (CollectionUtil.isNotEmpty(list)) {
                final int size = list.size();
                if (size < numOfArticlesPerPerson) {
                    numOfArticlesPerPerson = size;
                }
                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);
73 74 75 76 77
                    //点赞
                    final Boolean isLike = csdnLikeService.isLike(articleId, username);
                    if (!isLike) {
                        csdnLikeService.like(articleId);
                    }
78 79 80 81
                    //评论
                    final Boolean comment = csdnCommentService.isComment(article);
                    if (!comment) {
                        csdnCommentService.comment(articleId);
82
                    }
83 84 85 86
                    //收藏
                    final Boolean collect = csdnCollectService.isCollect(articleId);
                    if (!collect) {
                        csdnCollectService.collect(article, username);
87 88 89
                    }
                }
            }
90

91 92 93
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
94 95 96 97 98 99
    }

    @Override
    public void multiTriplet() {
        final List<CsdnUserInfo> list = csdnUserInfoService.list();
        if (CollectionUtil.isNotEmpty(list)) {
100
            for (CsdnUserInfo csdnUserInfo : list) {
101
                singleArticle(csdnUserInfo.getUserName());
102
            }
103
        }
104
        log.info("全部三连完成");
105
    }
106
}