admin
2020-10-26 89e370bfdda29ac8a8f7080a18dc09a6ddc75c09
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
package com.yeshi.buwan.iqiyi.util;
 
import com.yeshi.buwan.domain.AdminInfo;
import com.yeshi.buwan.domain.VideoDetailInfo;
import com.yeshi.buwan.domain.VideoType;
import com.yeshi.buwan.iqiyi.IqiYiNewAPI;
import com.yeshi.buwan.iqiyi.entity.IqiyiAlbum2;
import com.yeshi.buwan.iqiyi.vo.IqiyiAlbumListResult;
import com.yeshi.buwan.service.inter.juhe.Iqiyi2Service;
import com.yeshi.buwan.util.StringUtil;
import com.yeshi.buwan.util.TimeUtil;
import com.yeshi.buwan.util.mq.CMQManager;
import com.yeshi.buwan.util.video.VideoConstant;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
 
@Component
public class IqiyiUtil2 {
    public final static int PLAY_NONE = 0;// 不能播放
    public final static int PLAY_HTML = 1;// 跳转移动端网页播放
    public final static int PLAY_SWF = 2;// 嵌套网页播放
 
    public final static int RESOURCE_ID = 22;
    public final static String RESOURCE_NAME = "爱奇艺.";
    @Resource
    private Iqiyi2Service iqiyi2Service;
 
    private void saveAlbumAndVideo(List<IqiyiAlbum2> list) {
        if (list == null)
            return;
        for (IqiyiAlbum2 album : list) {
            if (album.getContentType() != 1)
                continue;
            if (!albumIsValid(album)) {//如果是无效的专辑就需要删除
                iqiyi2Service.deleteByAid(album.getId());
                continue;
            }
            if (album.getTvQipuIds() != null && album.getTvQipuIds().size() > 0) {
                //查询最新的一集是否缓存
                boolean needSaveVideos = true;
                if (album.getLatestVideo() != null) {
                    if (iqiyi2Service.countById(album.getLatestVideo().getId()) > 0L)
                        needSaveVideos = false;
                }
 
 
                if (needSaveVideos) {
                    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<Long> tvQipuIds = new ArrayList<>();
                        tvQipuIds.addAll(album.getTvQipuIds().subList(startIndex, toIndex));
 
                        //查询是否已经存在了
                        List<IqiyiAlbum2> existList = iqiyi2Service.listByIds(tvQipuIds);
                        Set<Long> sets = new HashSet<>();
                        if (existList != null && existList.size() > 0) {
                            for (IqiyiAlbum2 a : existList)
                                sets.add(a.getId());
 
                            //删除已经存在的
                            for (int i = 0; i < tvQipuIds.size(); i++) {
                                if (sets.contains(tvQipuIds.get(i))) {
                                    tvQipuIds.remove(i--);
                                }
                            }
                        }
 
                        if (tvQipuIds.size() > 0) {
                            List<IqiyiAlbum2> detailList = IqiYiNewAPI.getAlbumOrVideoDetail(tvQipuIds);
                            if (detailList != null)
                                for (IqiyiAlbum2 video : detailList)
                                    iqiyi2Service.saveIqiyiAlbum(video);
                        }
                    }
                }
            }
            iqiyi2Service.saveIqiyiAlbum(album);
            CMQManager.getInstance().addIqiyiAlbumUpdateMsg(album.getId());
        }
    }
 
    /**
     * 同步所有的专辑与视频
     *
     * @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());
            }
        }
    }
 
 
    /**
     * 根据专辑ID查询
     *
     * @param aid
     */
    public void syncByAid(Long aid) {
        List<Long> aidList = new ArrayList<>();
        aidList.add(aid);
        List<IqiyiAlbum2> album2List = IqiYiNewAPI.getAlbumOrVideoDetail(aidList);
        saveAlbumAndVideo(album2List);
    }
 
    //拉取所有的电影
    public void syncAllDianYing() {
        syncAlbumAndVideo(IqiYiNewAPI.TYPE_DIANYING, false, null);
    }
 
    //拉取所有的电视剧
    public void syncAllDianShiJu() {
        syncAlbumAndVideo(IqiYiNewAPI.TYPE_DIANSHIJU, true, null);
    }
 
    //拉取所有的动漫
    public void syncAllDongMan() {
        syncAlbumAndVideo(IqiYiNewAPI.TYPE_DONGMAN, true, 217289601L);
    }
 
    //拉取所有的综艺
    public void syncAllZongYi() {
        syncAlbumAndVideo(IqiYiNewAPI.TYPE_ZONGYI, true, 246881501L);
    }
 
    //更新专辑
    public void updateAlbum(int categoryId, boolean isAlbum, Date startTime, Date endTime) {
        IqiyiAlbumListResult result = IqiYiNewAPI.getUpdateAlbumList(categoryId + "", startTime, endTime, null, isAlbum, 10);
        if (result.getAlbum2List() != null)
            saveAlbumAndVideo(result.getAlbum2List());
        while (result.getMinId() != null) {
            result = IqiYiNewAPI.getUpdateAlbumList(categoryId + "", startTime, endTime, result.getMinId(), isAlbum, 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());
        }
    }
 
 
    //更新最近一天的的专辑与视频
    public void updateTodayAlbumAndVideo(int channelId) {
        long now = System.currentTimeMillis();
        for (int i = 0; i < 8; i++) {
            Date endTime = new Date(now - 1000 * 60 * 60L * 3 * i);
            Date startTime = new Date(endTime.getTime() - 1000 * 60 * 60L * 3);
 
            switch (channelId) {
                case IqiYiNewAPI.TYPE_DIANYING:
                    updateAlbum(channelId, true, startTime, endTime);
                    break;
                case IqiYiNewAPI.TYPE_DIANSHIJU:
                case IqiYiNewAPI.TYPE_DONGMAN:
                case IqiYiNewAPI.TYPE_ZONGYI:
                    updateAlbum(channelId, true, startTime, endTime);
                    break;
            }
        }
    }
 
 
    public 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;
    }
 
    public 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.isSourceAlbum()) {//专辑
                    tag = "更新至" + TimeUtil.getGernalTime(TimeUtil.convertGernalTime(album.getLatestVideo().getPeriod(), "yyyyMMdd"), "yyyy-MM-dd") + "期";
                } else {
                    if (album.getLatestVideo().getOrder() == album.getVideoCount())
                        tag = album.getVideoCount() + "集全";
                    else
                        tag = "更新至" + album.getLatestVideo().getOrder() + "集";
                }
            }
        } else if (album.getChannelId() == IqiYiNewAPI.TYPE_ZONGYI)
            if (album.getLatestVideo() != null) {
                tag = "更新至" + TimeUtil.getGernalTime(TimeUtil.convertGernalTime(album.getLatestVideo().getPeriod(), "yyyyMMdd"), "yyyy-MM-dd") + "期";
            }
        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.setExtraId(album.getId() + "");
        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) {
            if (album.isSourceAlbum())
                vi.setTag(album.getPeriod() + "\n" + album.getSubTitle());
            else
                vi.setTag(album.getOrder() + "");
        } else if (album.getChannelId() == IqiYiNewAPI.TYPE_ZONGYI)
            vi.setTag(album.getPeriod() + "\n" + album.getSubTitle());
        vi.setType("album");
        return vi;
    }
 
 
    public static 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;
    }
 
 
    /**
     * 通过列表链接获取爱奇艺中的视频
     *
     * @param url
     * @return
     */
    public static List<String> getAlbumUrlsFromUrl(String url) {
        List<String> list = new ArrayList<>();
        try {
            Document doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36").timeout(60000).get();
            Element root = doc.getElementsByAttributeValue("class", "qy-mod-ul").get(0);
            Elements items = root.getElementsByAttributeValue("class", "qy-mod-li");
            for (int i = 0; i < items.size(); i++) {
                String href = items.get(i).getElementsByTag("a").get(0).attr("href");
                if (href.startsWith("//"))
                    href = "http:" + href;
                list.add(href);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
 
    /**
     * 专辑是否有效
     *
     * @param album2
     * @return
     */
    public static boolean albumIsValid(IqiyiAlbum2 album2) {
        if (album2.getEffect() != 1)
            return false;
 
        if (album2.isSupportDrm())
            return false;
 
        List<IqiyiAlbum2.PlayControlsBean> playControls = album2.getPlayControls();
        if (playControls == null)
            return false;
        for (IqiyiAlbum2.PlayControlsBean bean : playControls) {
            if (bean.getPlatformId() == 15 && bean.getAvailableStatus() == 1)
                return true;
        }
        return false;
    }
 
 
}