admin
2024-09-05 ab35ac8b769b2d9816dffb33a64f2c6f7bd5dd6e
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.hxh.spring.test.video;
 
 
import com.yeshi.buwan.dao.VideoInfoDao;
import com.yeshi.buwan.dao.juhe.hanmi.HanmiShowDao;
import com.yeshi.buwan.dao.juhe.hanmi.HanmiShowEpisodeDao;
import com.yeshi.buwan.dao.juhe.hanmi.VideoHanmiMapDao;
import com.yeshi.buwan.dao.juhe.youku.InternetSearchVideoDao;
import com.yeshi.buwan.domain.ResourceVideo;
import com.yeshi.buwan.domain.video.InternetSearchVideo;
import com.yeshi.buwan.service.imp.ResourceVideoService;
import com.yeshi.buwan.service.imp.VideoInfoService;
import com.yeshi.buwan.service.imp.VideoService;
import com.yeshi.buwan.service.inter.juhe.HanmiService;
import com.yeshi.buwan.service.inter.juhe.InternetSearchVideoService;
import com.yeshi.buwan.videos.hanmi.HanmiApiUtil;
import com.yeshi.buwan.videos.hanmi.entity.HanmiShow;
import com.yeshi.buwan.videos.hanmi.entity.HanmiShowEpisode;
import com.yeshi.buwan.videos.hanmi.entity.VideoHanmiMap;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
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;
 
    final String MOVIE_URL = "https://www.wztaichuan.com/vod/show/area/%E9%9F%A9%E5%9B%BD/id/1/page/{页码}.html";
    final String SHOW_URL = "https://www.wztaichuan.com/vod/show/area/%E9%9F%A9%E5%9B%BD/id/3/page/{页码}.html";
    final String TV_URL = "https://www.wztaichuan.com/vod/type/id/5/page/{页码}.html";
 
    private  List<HanmiShow> updateShow(String listUrl,int page) throws Exception {
        List<HanmiShow> showList = HanmiApiUtil.parseDetailList(HanmiApiUtil.parseList(listUrl.replace("{页码}",page+"")));
        for (HanmiShow show : showList) {
            if(listUrl.contains("/id/1/")){
                show.setType("电影");
            }else if(listUrl.contains("/id/3/")){
                show.setType("综艺");
            }else if(listUrl.contains("/id/5/")){
                show.setType("电视剧");
            }
            hanmiService.saveShow(show);
        }
        return showList;
    }
 
 
 
    @Test
    public void updateLatest() throws Exception {
        String[] urls =new String[]{MOVIE_URL,SHOW_URL,TV_URL};
        for(String url: urls) {
            for (int i = 1; i < 20; i++) {
                System.out.printf(i + "==========================");
                // 电影 https://www.wztaichuan.com/vod/show/area/%E9%9F%A9%E5%9B%BD/id/1/page/2.html
                // 综艺 https://www.wztaichuan.com/vod/show/area/%E9%9F%A9%E5%9B%BD/id/3/page/2.html
                // 韩剧 https://www.wztaichuan.com/vod/type/id/5/page/2.html
                List<HanmiShow> showList =   updateShow(url,i);
                if (showList.size() < 10) {
                    break;
                }
            }
        }
    }
 
 
    @Test
    public void addHanJu() throws Exception {
        for (int i = 1; i < 30; i++) {
            List<HanmiShow> showList =   updateShow(SHOW_URL,i);
            if (showList.size() < 10) {
                break;
            }
        }
    }
 
    @Test
    public void addToVideo() throws IOException {
        long count = hanmiService.countAll();
        int pageSize = 100;
        int totalPage=(int) (count%pageSize ==0?count/pageSize:count/pageSize+1);
        for(int i=0;i<totalPage;i++) {
            List<HanmiShow> list = hanmiService.listAll(i + 1, pageSize);
            for(HanmiShow show:list){
                try {
                    show = hanmiService.getShowDetail(show.getId());
                    hanmiService.addToInternetSearch(show);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
 
    @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.addToInternetSearch(show);
        }
 
    }
 
    @Resource
    private VideoHanmiMapDao videoHanmiMapDao;
 
    @Resource
    private InternetSearchVideoDao internetSearchVideoDao;
 
    @Resource
    private ResourceVideoService resourceVideoService;
 
    @Resource
    private VideoInfoDao videoInfoDao;
 
    @Test
    public void update() {
        for (int i = 0; i < 100; i++) {
            List<HanmiShow> list = hanmiShowDao.listAll(i*100, 100);
            for (HanmiShow show : list) {
                try {
                    show = HanmiApiUtil.parseShowDetail(show);
                    if (show != null && show.getEpisodeList() != null) {
                        // 更新封面图片
                        VideoHanmiMap map =  videoHanmiMapDao.selectByShowId(show.getId());
                        if(map!=null){
                            InternetSearchVideo video =     internetSearchVideoDao.get(map.getVideoId());
                            if(video!=null){
                                InternetSearchVideo update = new InternetSearchVideo();
                                update.setId(video.getId());
                                update.setVpicture(show.getPicture());
                                internetSearchVideoDao.updateSelective(update);
                            }
                        }
                        Query query = new Query();
                        query.addCriteria(new Criteria().where("_id").is(show.getId()));
                        Update update = new Update();
                        update.set("tag", show.getTag());
                        hanmiShowDao.update(query, update);
                        query = new Query();
                        query.addCriteria(new Criteria().where("showId").is(show.getId()));
                        hanmiShowEpisodeDao.delete(query);
                        // 删除原来的数据
                        for (HanmiShowEpisode episode : show.getEpisodeList()) {
                            episode.setShowId(show.getId());
                            episode.setId(HanmiShowEpisode.createId(episode.getShowId(), episode.getTag()));
                            episode.setCreateTime(new Date());
                            hanmiShowEpisodeDao.save(episode);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
 
            }
        }
    }
 
 
    @Test
    public void deleteFromVideo(){
        // 清除韩迷数据
        List<ResourceVideo>  resourceVideos =   resourceVideoService.listResourceVideo(28L,1,100 );
        for(ResourceVideo rv:resourceVideos){
            List<ResourceVideo> resourceList =   resourceVideoService.getResourceList(rv.getVideo().getId());
            resourceVideoService.delete(rv.getVideo().getId(), rv.getResource().getId());
            if(resourceList.size()<2){
                // 删除视频
                videoInfoDao.delete(rv.getVideo());
            }
        }
    }
 
 
}