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


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