CsdnServiceImpl.java 3.7 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.CsdnService;
13 14
import com.kwan.springbootkwan.service.CsdnUserInfoService;
import lombok.extern.slf4j.Slf4j;
15
import org.apache.commons.lang3.StringUtils;
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.beans.factory.annotation.Value;
18 19
import org.springframework.stereotype.Service;

20 21
import java.util.List;

22 23

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

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

40
    @Override
41
    public void singleArticle(String username) {
42
        HttpResponse response = HttpUtil.createGet(url)
43
                .header("Cookie", csdnCookie)
44
                .form("page", 1)
45
                .form("size", 5)
46 47 48 49 50 51
                .form("businessType", "lately")
                .form("noMore", false)
                .form("username", username)
                .execute();
        final String body = response.body();
        ObjectMapper objectMapper = new ObjectMapper();
52
        BusinessInfoResponse businessInfoResponse;
53
        try {
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
            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);
                    //评论
                    final Boolean comment = csdnCommentService.isComment(article);
                    if (!comment) {
                        csdnCommentService.comment(articleId);
74
                    }
75 76 77 78
                    //收藏
                    final Boolean collect = csdnCollectService.isCollect(articleId);
                    if (!collect) {
                        csdnCollectService.collect(article, username);
79 80 81
                    }
                }
            }
82

83 84 85
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
86 87 88 89 90 91
    }

    @Override
    public void multiTriplet() {
        final List<CsdnUserInfo> list = csdnUserInfoService.list();
        if (CollectionUtil.isNotEmpty(list)) {
92
            for (CsdnUserInfo csdnUserInfo : list) {
93
                singleArticle(csdnUserInfo.getUserName());
94
            }
95
        }
96
    }
97
}