admin
2020-09-05 d3ebf5b103d4deebd6ffb75f4471a6fddab8d764
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package com.yeshi.buwan.iqiyi.util;
 
import com.yeshi.buwan.dao.VideoInfoDao;
import com.yeshi.buwan.dao.juhe.iqiyi.IqiyiAlbum2Dao;
import com.yeshi.buwan.dao.juhe.iqiyi.VideoIqiyi2Dao;
import com.yeshi.buwan.dao.video.AlbumVideoMapDao;
import com.yeshi.buwan.domain.*;
import com.yeshi.buwan.domain.entity.PlayUrl;
import com.yeshi.buwan.domain.video.AlbumVideoMap;
import com.yeshi.buwan.iqiyi.IqiYiNewAPI;
import com.yeshi.buwan.iqiyi.entity.IqiyiAlbum2;
import com.yeshi.buwan.iqiyi.entity.VideoIqiyi2;
import com.yeshi.buwan.iqiyi.vo.IqiyiAlbumListResult;
import com.yeshi.buwan.service.imp.CategoryVideoService;
import com.yeshi.buwan.service.imp.ResourceVideoService;
import com.yeshi.buwan.service.imp.VideoInfoService;
import com.yeshi.buwan.service.imp.VideoResourceService;
import com.yeshi.buwan.util.TimeUtil;
import com.yeshi.buwan.util.mq.CMQManager;
import com.yeshi.buwan.util.video.VideoConstant;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
@Component
public class IqiyiUtil2 {
 
 
    public final int PLAY_NONE = 0;// ²»Äܲ¥·Å
    public final int PLAY_HTML = 1;// Ìø×ªÒƶ¯¶ËÍøÒ³²¥·Å
    public final int PLAY_SWF = 2;// Ç¶Ì×ÍøÒ³²¥·Å
 
    private final static long RESOURCE_ID = 22L;
 
    @Resource
    private IqiyiAlbum2Dao iqiyiAlbum2Dao;
 
    @Resource
    private VideoIqiyi2Dao videoIqiyi2Dao;
 
    @Resource
    private VideoInfoDao videoInfoDao;
 
 
    @Resource
    private ResourceVideoService resourceVideoService;
 
    @Resource
    private VideoInfoService videoInfoService;
 
    @Resource
    private CategoryVideoService categoryVideoService;
 
    @Resource
    private VideoResourceService videoResourceService;
 
    @Resource
    private AlbumVideoMapDao albumVideoMapDao;
 
    private void saveAlbumAndVideo(List<IqiyiAlbum2> list) {
        if (list == null)
            return;
        for (IqiyiAlbum2 album : list) {
            if (album.getTvQipuIds() != null && album.getTvQipuIds().size() > 0) {
                int pageSize = 10;
                int page = album.getTvQipuIds().size() % pageSize == 0 ? album.getTvQipuIds().size() / pageSize : album.getTvQipuIds().size() / pageSize + 1;
                for (int p = 0; p < page; p++) {
                    int startIndex = p * pageSize;
                    int toIndex = (startIndex + pageSize) > album.getTvQipuIds().size() ? album.getTvQipuIds().size() : (startIndex + pageSize);
                    List<IqiyiAlbum2> detailList = IqiYiNewAPI.getAlbumOrVideoDetail(album.getTvQipuIds().subList(startIndex, toIndex));
                    if (detailList != null)
                        for (IqiyiAlbum2 video : detailList)
                            iqiyiAlbum2Dao.save(video);
                }
            }
            iqiyiAlbum2Dao.save(album);
        }
    }
 
    /**
     * Í¬²½ËùÓеÄר¼­ÓëÊÓÆµ
     *
     * @param categoryId
     * @param isAlbum
     */
    private void syncAlbumAndVideo(int categoryId, boolean isAlbum, Long minId) {
        IqiyiAlbumListResult result = IqiYiNewAPI.getAllAlbumAndVideoList(categoryId + "", minId, isAlbum, 10);
        if (result.getAlbum2List() != null)
            saveAlbumAndVideo(result.getAlbum2List());
        while (result.getMinId() != null) {
            result = IqiYiNewAPI.getAllAlbumAndVideoList(categoryId + "", result.getMinId(), isAlbum, 10);
            if (result.getAlbum2List() != null) {
                saveAlbumAndVideo(result.getAlbum2List());
            }
 
        }
    }
 
    //À­È¡ËùÓеĵçÓ°
    public void syncAllDianYing() {
        syncAlbumAndVideo(IqiYiNewAPI.TYPE_DIANYING, false, 255675400l);
    }
 
    //À­È¡ËùÓеĵçÊÓ¾ç
    public void syncAllDianShiJu() {
        syncAlbumAndVideo(IqiYiNewAPI.TYPE_DIANSHIJU, true, 206929801L);
    }
 
    //À­È¡ËùÓе͝Âþ
    public void syncAllDongMan() {
        syncAlbumAndVideo(IqiYiNewAPI.TYPE_DONGMAN, true, 217289601L);
    }
 
    //À­È¡ËùÓеÄ×ÛÒÕ
    public void syncAllZongYi() {
        syncAlbumAndVideo(IqiYiNewAPI.TYPE_ZONGYI, true, 246881501L);
    }
 
    //¸üÐÂר¼­
    public void updateAlbum(int categoryId, Date startTime, Date endTime) {
        IqiyiAlbumListResult result = IqiYiNewAPI.getUpdateAlbumList(categoryId + "", startTime, endTime, null, true, 10);
        if (result.getAlbum2List() != null)
            saveAlbumAndVideo(result.getAlbum2List());
        while (result.getMinId() != null) {
            result = IqiYiNewAPI.getUpdateAlbumList(categoryId + "", startTime, endTime, result.getMinId(), true, 10);
            if (result.getAlbum2List() != null)
                saveAlbumAndVideo(result.getAlbum2List());
        }
    }
 
    //¸üÐÂÊÓÆµ
    public void updateVideo(int categoryId, Date startTime, Date endTime) {
        IqiyiAlbumListResult result = IqiYiNewAPI.getUpdateVideoList(categoryId + "", startTime, endTime, null, true, 10);
        if (result.getAlbum2List() != null)
            saveAlbumAndVideo(result.getAlbum2List());
        while (result.getMinId() != null) {
            result = IqiYiNewAPI.getUpdateVideoList(categoryId + "", startTime, endTime, result.getMinId(), true, 10);
            if (result.getAlbum2List() != null)
                saveAlbumAndVideo(result.getAlbum2List());
        }
    }
 
    //½«×¨¼­Ìí¼Óµ½ÊÓÆµ
    @Transactional
    public void addToVideoInfo(IqiyiAlbum2 album) {
        if (album.getFeatureAlbumId() > 0L && album.getChannelId() != IqiYiNewAPI.TYPE_DIANYING) {
            return;
        }
 
        //Ö»»º´æÓÐЧµÄ
        if (album.getEffect() == 0)
            return;
 
        //Ö»»º´æÕýƬ
        if (album.getContentType() != 1)
            return;
 
        //×ÛÒÕ±ØÐëÒªÓÐÄÚÈÝ
        if (album.getChannelId() == 6) {
            if (album.getTvQipuIds().size() == 0)
                return;
        }
 
        VideoInfo newVideoInfo = convertAlbumToVideoInfo(album);
        VideoIqiyi2 videoIqiyi2 = videoIqiyi2Dao.selectByIqiyiId(album.getId());
        if (videoIqiyi2 != null) {//ÇþµÀÊÓÆµÒѾ­´æÔÚ
            VideoInfo videoInfo = videoInfoDao.find(VideoInfo.class, videoIqiyi2.getVideoId());
            if (videoInfo == null)
                return;
 
            if (newVideoInfo.getTag().equalsIgnoreCase(videoInfo.getTag()))
                return;
            newVideoInfo.setId(videoInfo.getId());
            newVideoInfo.setCreatetime(videoInfo.getCreatetime());
            //¸üÐÂÐÅÏ¢tagÐÅÏ¢
            videoInfo.setLatestHpicture(newVideoInfo.getLatestHpicture());
            videoInfo.setLatestVpicture(newVideoInfo.getLatestVpicture());
            videoInfo.setVideocount(newVideoInfo.getVideocount());
            videoInfo.setTag(newVideoInfo.getTag());
            videoInfo.setPicture(newVideoInfo.getPicture());
            videoInfo.setHpicture(newVideoInfo.getHpicture());
            videoInfo.setVpicture(newVideoInfo.getVpicture());
            videoInfo.setShow(newVideoInfo.getShow());
            videoInfoDao.update(videoInfo);
        } else {//ÊÓÆµ²»´æÔÚ
            //ÅжÏ2¸öÊÓÆµÊµÌåÊÇ·ñΪͬһÊÓÆµ
            VideoInfo oldVideo = videoInfoService.getExistSameVideo(newVideoInfo);
            if (oldVideo == null) {
                //Ìí¼ÓÊÓÆµ
                Serializable id = videoInfoDao.save(newVideoInfo);
                newVideoInfo.setId(id + "");
            } else {//ΪͬһÊÓÆµ
                //¸üÐÂtag
                newVideoInfo.setId(oldVideo.getId());
                oldVideo.setLatestHpicture(newVideoInfo.getLatestHpicture());
                oldVideo.setLatestVpicture(newVideoInfo.getLatestVpicture());
                oldVideo.setVideocount(newVideoInfo.getVideocount());
                oldVideo.setTag(newVideoInfo.getTag());
                oldVideo.setPicture(newVideoInfo.getPicture());
                oldVideo.setHpicture(newVideoInfo.getHpicture());
                oldVideo.setVpicture(newVideoInfo.getVpicture());
                oldVideo.setShow(newVideoInfo.getShow());
                videoInfoDao.update(oldVideo);
            }
 
            //¼ÓÈëר¼­ÊÓÆµÓ³Éä
            AlbumVideoMap map = new AlbumVideoMap();
            map.setCreateTime(new Date());
            map.setVideoId(newVideoInfo.getId());
            map.setRootVideoType(newVideoInfo.getVideoType().getId());
            map.setVideoCount(newVideoInfo.getVideocount());
            albumVideoMapDao.save(map);
 
            //¼ÓÈëÓ³Éä
            VideoIqiyi2 vi = new VideoIqiyi2();
            vi.setIqiyiId(album.getId());
            vi.setVideoId(Long.parseLong(newVideoInfo.getId()));
            videoIqiyi2Dao.save(vi);
 
 
            //Ìí¼ÓÊÓÆµÀ´Ô´Ó³Éä
            resourceVideoService.addVideoResource(newVideoInfo.getId(), RESOURCE_ID + "");
            //Ìí¼ÓÊÓÆµ·ÖÀàÓ³Éä
            categoryVideoService.addCategoryVideo(newVideoInfo.getId(), newVideoInfo.getVideoType().getId());
 
            CMQManager.getInstance().addSolrMsg(newVideoInfo.getId());
        }
 
 
    }
 
 
    //¸üÐÂ×î½üÒ»ÌìµÄµÄר¼­ÓëÊÓÆµ
    public void updateTodayAlbumAndVideo() {
        long now = System.currentTimeMillis();
        for (int i = 0; i < 8; i++) {
            Date endTime = new Date(now - i * 1000 * 60 * 60L * 3);
            Date startTime = new Date(endTime.getTime() - 1000 * 60 * 60L * 3);
 
            //¸üеçÓ°
            updateVideo(IqiYiNewAPI.TYPE_DIANYING, startTime, endTime);
            //¸üеçÊÓ¾ç
            updateAlbum(IqiYiNewAPI.TYPE_DIANSHIJU, startTime, endTime);
            updateVideo(IqiYiNewAPI.TYPE_DIANSHIJU, startTime, endTime);
 
            //¸üж¯Âþ
            updateAlbum(IqiYiNewAPI.TYPE_DONGMAN, startTime, endTime);
            updateVideo(IqiYiNewAPI.TYPE_DONGMAN, startTime, endTime);
 
            //¸üÐÂ×ÛÒÕ
            updateAlbum(IqiYiNewAPI.TYPE_ZONGYI, startTime, endTime);
            updateVideo(IqiYiNewAPI.TYPE_ZONGYI, startTime, endTime);
        }
    }
 
 
    public VideoInfo convertAlbumToVideoInfo(IqiyiAlbum2 album) {
        Calendar ca = Calendar.getInstance();
        ca.setTimeInMillis(TimeUtil.convertGernalTime(album.getPeriod(), "yyyyMMdd"));
        int day = ca.get(Calendar.DAY_OF_MONTH);
        int month = ca.get(Calendar.MONTH) + 1;
        int year = ca.get(Calendar.YEAR);
 
        VideoInfo video = new VideoInfo();
        video.setVideoType(new VideoType(IqiyiUtil.getType(album.getContentType())));
        video.setArea(album.getAreas());
        video.setBaseurl(album.getH5Url());
        video.setCanSave(false);
        video.setCommentCount(0);
        video.setCreatetime(System.currentTimeMillis());
        video.setDay(day + "");
        video.setDirector(album.getDirectors());
        video.setDuration(album.getDuration() + "");
        video.setFinish("1");
        video.setIntroduction(album.getDesc());
        video.setMainActor(album.getMainCharacters());
        video.setMonth(month + "");
        video.setName(album.getName());
        if (album.getLatestVideo() != null)
            video.setNowNumber(album.getLatestVideo().getOrder() + "");
        else
            video.setNowNumber("1");
        video.setOrderby("0" + "");
        video.setPicture(album.getImageUrl());
 
        video.setVpicture(IqiyiUtil.getVPicture(video.getPicture()));
        video.setHpicture(IqiyiUtil.getHPicture(video.getPicture()));
 
        if (album.getStatistics() != null)
            video.setScore(album.getStatistics().getScore() + "");
        video.setShare("0");
        video.setShow("1");
 
        video.setThirdType("1");
        video.setTotalNumber("0");
 
        video.setWatchCount(0 + "");
        video.setYear(year + "");
        video.setAdmin(new AdminInfo("1"));
        video.setContentType(album.getContentType());
        video.setCommentCount(0);
 
        String tag = getAlbumTag(album);
        video.setTag(tag);
 
        if (album.getLatestVideo() != null) {
            //²éѯ×î½üµÄͼƬ
            IqiyiAlbum2 album2 = iqiyiAlbum2Dao.get(album.getLatestVideo().getId());
            if (album2 != null) {
                video.setLatestHpicture(IqiyiUtil.getHPicture(album2.getImageUrl()));
                video.setLatestVpicture(IqiyiUtil.getVPicture(album2.getImageUrl()));
            }
        }
 
        if (album.getTvQipuIds() != null && album.getTvQipuIds().size() > 0)
            video.setVideocount(album.getTvQipuIds().size());
        else
            video.setVideocount(1);
 
        video.setVideoType(getVideoType(album));
        return video;
    }
 
    private static VideoType getVideoType(IqiyiAlbum2 album) {
        if (album.getChannelId() == IqiYiNewAPI.TYPE_DIANYING) {
            return new VideoType(VideoConstant.VIDEO_CATEGORY_DIANYING);
        } else if (album.getChannelId() == IqiYiNewAPI.TYPE_DIANSHIJU) {
            return new VideoType(VideoConstant.VIDEO_CATEGORY_DIANSHIJU);
        } else if (album.getChannelId() == IqiYiNewAPI.TYPE_DONGMAN) {
            return new VideoType(VideoConstant.VIDEO_CATEGORY_DONGMAN);
        } else if (album.getChannelId() == IqiYiNewAPI.TYPE_ZONGYI) {
            return new VideoType(VideoConstant.VIDEO_CATEGORY_ZONGYI);
        }
        return null;
    }
 
    private static String getAlbumTag(IqiyiAlbum2 album) {
        //µçӰΪÆÀ·Ö
        //µçÊÓ¾çºÍ¶¯ÂþΪ¸üе½¶àÉÙ¼¯
        //×ÛÒÕΪ¸üе½¶àÉÙÆÚ
 
        String tag = "";
        if (album.getChannelId() == IqiYiNewAPI.TYPE_DIANYING)
            if (album.getStatistics() != null)
                tag = "ÆÀ·Ö£º" + album.getStatistics().getScore();
            else if (album.getChannelId() == IqiYiNewAPI.TYPE_DIANSHIJU || album.getChannelId() == IqiYiNewAPI.TYPE_DONGMAN) {
                if (album.getLatestVideo() != null) {
                    if (album.getLatestVideo().getOrder() == album.getVideoCount())
                        tag = album.getVideoCount() + "¼¯È«";
                    else
                        tag = "¸üÐÂÖÁ" + album.getLatestVideo().getOrder() + "¼¯";
                }
                tag = album.getLatestVideo().getOrder() + "";
            } else if (album.getChannelId() == IqiYiNewAPI.TYPE_ZONGYI)
                if (album.getLatestVideo() != null) {
                    tag = "¸üÐÂÖÁ" + album.getLatestVideo().getPeriod() + "ÆÚ";
                }
        return tag;
    }
 
 
    public static VideoDetailInfo convertAlbumToVideoDetail(IqiyiAlbum2 album) {
        VideoDetailInfo vi = new VideoDetailInfo();
        vi.setAdmin(new AdminInfo("1"));
        vi.setId(album.getId());
        vi.setIntroduction(album.getDesc());
        vi.setName(album.getName());
        if (album.getChannelId() == IqiYiNewAPI.TYPE_DIANYING)
            vi.setTag(album.getName());
        else if (album.getChannelId() == IqiYiNewAPI.TYPE_DIANSHIJU || album.getChannelId() == IqiYiNewAPI.TYPE_DONGMAN)
            vi.setTag(album.getOrder() + "");
        else if (album.getChannelId() == IqiYiNewAPI.TYPE_ZONGYI)
            vi.setTag(album.getPeriod());
        vi.setType("album");
        return vi;
    }
 
 
    public int getShowType(String videoid) {
        VideoInfo videoInfo = videoInfoDao.find(VideoInfo.class, videoid);
        if (videoInfo.getVideoType() != null && (videoInfo.getVideoType().getId() == VideoConstant.VIDEO_CATEGORY_DIANSHIJU || videoInfo.getVideoType().getId() == VideoConstant.VIDEO_CATEGORY_DONGMAN))
            return 1;
        else
            return 2;
    }
 
 
    public PlayUrl getPlayUrl(String detailSystemId, int resourceId, String id, String videoId) {
        IqiyiAlbum2 album = iqiyiAlbum2Dao.get(Long.parseLong(id));
        VideoResource vr = videoResourceService.getResource(resourceId + "");
        int t = getPlayType(album);
        PlayUrl pu = new PlayUrl();
        pu.setParams("");
        pu.setPlayType(t);
        pu.setResource(vr);
 
        if (t == PLAY_NONE) {
            pu.setUrl("");
        } else if (t == PLAY_HTML) {
            pu.setUrl(album.getH5Url());
        }
        return pu;
    }
 
    public int getPlayType(IqiyiAlbum2 album) {
        int type = PLAY_NONE;
 
        if (album.getEffect() == 0)
            type = PLAY_NONE;
 
        for (IqiyiAlbum2.PlayControlsBean pc : album.getPlayControls()) {
            if (pc.getPlatformId() == 15)// Òƶ¯¶Ë
            {
                type = PLAY_HTML;
            }
        }
        return type;
    }
 
}