admin
2021-12-24 b5e19564dcbf1b7ec12946209d74313479f9dfe1
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.hxh.spring.test.video;
 
 
import com.yeshi.buwan.dao.juhe.hanmi.HanmiShowDao;
import com.yeshi.buwan.dao.juhe.hanmi.HanmiShowEpisodeDao;
import com.yeshi.buwan.service.inter.juhe.HanmiService;
import com.yeshi.buwan.videos.hanmi.HanmiApiUtil;
import com.yeshi.buwan.videos.hanmi.entity.HanmiShow;
import com.yeshi.buwan.videos.hanmi.entity.HanmiShowEpisode;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
 
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.List;
 
@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试
@ContextConfiguration(locations = {"classpath:spring.xml"})
@WebAppConfiguration
public class HanmiTest {
 
    @Resource
    private HanmiService hanmiService;
 
    @Resource
    private HanmiShowEpisodeDao hanmiShowEpisodeDao;
 
    @Resource
    private HanmiShowDao hanmiShowDao;
 
    @Test
    public void addHanJu() throws Exception {
        for (int i = 1; i < 3; i++) {
            List<HanmiShow> showList = HanmiApiUtil.parseDetailList(HanmiApiUtil.parseList("https://www.hmtv.me/hanju/page/" + i));
            for (HanmiShow show : showList) {
                hanmiService.saveShow(show);
//                hanmiService.deleteByShowId(show.getId());
            }
        }
    }
 
    @Test
    public void addToVideo() throws IOException {
 
        for (int i = 1; i < 3; i++) {
            List<HanmiShow> showList = HanmiApiUtil.parseList("https://www.hmtv.me/hanju/page/" + i);
            Collections.reverse(showList);
            for (HanmiShow show : showList) {
                show.setId(show.getUrl().replace("/show/", "").trim());
                show = hanmiService.getShowDetail(show.getId());
                if (show != null)
                    hanmiService.addToVideoInfo(show);
            }
        }
    }
 
    @Test
    public void addSingleVideo() throws Exception {
        //解析数据
        String pageUrl="https://www.hmtv.me/search/?wd=%E6%88%90%E4%BA%BA%E7%BB%83%E4%B9%A0%E7%94%9F";
        List<HanmiShow> showList = HanmiApiUtil.parseDetailList(HanmiApiUtil.parseList(pageUrl));
        for (HanmiShow show : showList) {
            show.setType("韩剧");
            hanmiService.saveShow(show);
        }
        //添加数据
        for (HanmiShow show : showList) {
            show.setId(show.getUrl().split("/show/")[1].trim());
            show = hanmiService.getShowDetail(show.getId());
            show.setType("韩剧");
            if (show != null)
                hanmiService.addToVideoInfo(show);
        }
 
    }
 
    @Test
    public void update() {
        List<HanmiShow> list = hanmiShowDao.listAll(0, 100);
        for (HanmiShow show : list) {
            List<HanmiShowEpisode> epList = hanmiShowEpisodeDao.listByShowId(show.getId(), 0, 1);
            if (epList != null && epList.size() > 0) {
                try {
                    List<HanmiShowEpisode> episodeList = HanmiApiUtil.getShowEpisodesFromPlayUrl(epList.get(0).getPlayUrl());
                    if (episodeList != null)
                        for (HanmiShowEpisode episode : episodeList) {
                            episode.setShowId(show.getId());
                            episode.setId(HanmiShowEpisode.createId(episode.getShowId(), episode.getTag()));
                            episode.setCreateTime(new Date());
                            hanmiShowEpisodeDao.save(episode);
                        }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 
 
}