admin
2020-10-10 81db7b3b070c003e6f5f0d1c757ab30b6f42c944
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
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.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.ArrayList;
import java.util.Date;
import java.util.List;
 
@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.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)
                            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, 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());
        }
    }
 
 
    //更新最近一天的的专辑与视频
    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 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.getLatestVideo().getOrder() == album.getVideoCount())
                    tag = album.getVideoCount() + "集全";
                else
                    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 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;
    }
 
 
}