admin
2021-03-25 f13ed98e1de0ec7b85ed179212cc095f63480eed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.yeshi.buwan.job.video;
 
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yeshi.buwan.dao.juhe.pptv.PPTVSeriesDao;
import com.yeshi.buwan.dao.juhe.pptv.VideoPPTVMapDao;
import com.yeshi.buwan.pptv.PPTVApiUtil;
import com.yeshi.buwan.pptv.entity.PPTVSeries;
import com.yeshi.buwan.service.inter.juhe.PPTVService;
import com.yeshi.buwan.service.inter.juhe.YouKuService;
import com.yeshi.buwan.util.StringUtil;
import com.yeshi.buwan.youku.YouKuApiUtil;
import com.yeshi.buwan.youku.entity.YouKuShowDetail;
import com.yeshi.buwan.youku.entity.YouKuShowSimple;
import com.yeshi.buwan.youku.entity.YouKuVideo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.List;
 
@Component
public class YouKuVideoUpdate {
 
    private final static Logger logger = LoggerFactory.getLogger(YouKuVideoUpdate.class);
 
    @Resource
    private YouKuService youKuService;
 
 
    private YouKuShowDetail getShowDetail(String showId) throws Exception {
 
        YouKuShowDetail detail = YouKuApiUtil.getShowDetail(showId);
        int pageSize = 100;
        YouKuApiUtil.ListResultDTO resultDTO = YouKuApiUtil.getVideoList(showId, 1, pageSize);
        if (resultDTO == null)
            throw new Exception("视频列表获取出错");
        List<YouKuVideo> videoList = resultDTO.getList();
        if (videoList.size() < resultDTO.getTotal()) {
            int totalPage = resultDTO.getTotal() % pageSize == 0 ? resultDTO.getTotal() / pageSize : resultDTO.getTotal() / pageSize + 1;
            for (int i = 1; i < totalPage; i++) {
                resultDTO = YouKuApiUtil.getVideoList(showId, i + 1, pageSize);
                if (resultDTO != null)
                    videoList.addAll(resultDTO.getList());
            }
        }
 
        detail.setVideoList(videoList);
        return detail;
    }
 
    private void updateCategory(String categoryName) throws Exception {
        YouKuApiUtil.ListResultDTO dto = YouKuApiUtil.getShowListByCategory(categoryName, 1, 20);
        if (dto != null) {
            int pageSize = 100;
            int totalPage = dto.getTotal() % pageSize == 0 ? dto.getTotal() / pageSize : dto.getTotal() / pageSize + 1;
            totalPage = totalPage > 20 ? 20 : totalPage;
            for (int i = 0; i < totalPage; i++) {
                dto = YouKuApiUtil.getShowListByCategory(categoryName, i + 1, pageSize);
                for (YouKuShowSimple simple : (List<YouKuShowSimple>) dto.getList()) {
                    System.out.println(simple.getName() + ":" + simple.getId());
                    try {
                        YouKuShowDetail detail = getShowDetail(simple.getId());
                        youKuService.save(detail);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
 
    /**
     * 更新最近几天的视频
     *
     * @param param
     * @return
     * @throws Exception
     */
    @XxlJob("video-update-youku-updateVideo")
    public ReturnT<String> updateLatestVideo(String param) throws Exception {
 
        String[] types = new String[]{
                "电影", "电视剧", "动漫", "综艺"
        };
        if (!StringUtil.isNullOrEmpty(param)) {
            if (param.length() < 10) {
                updateCategory(param);
            } else {
                String[] ids = param.split(",");
                for (String id : ids) {
                    YouKuShowDetail detail = getShowDetail(id);
                    youKuService.save(detail);
                }
            }
        } else {
            for (String type : types) {
                updateCategory(type);
            }
        }
        return ReturnT.SUCCESS;
    }
 
}