admin
2021-01-04 aa6ef62aef83e277d4171df1d9f0803f91738216
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
package com.newvideo.iqiyi;
 
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.newvideo.iqiyi.entity.IqiyiAlbum;
import com.newvideo.iqiyi.entity.IqiyiChannel;
import com.newvideo.iqiyi.entity.IqiyiVideoInfo;
import com.newvideo.iqiyi.entity.PlayControls;
import com.newvideo.util.HttpUtil;
import com.newvideo.util.StringUtil;
import com.newvideo.util.TimeUtil;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
public class IqiYiAPI {
    public static int TYPE_YULE = 7;
    public static int TYPE_KEJI = 30;
    public static int TYPE_ZONGYI = 6;
    public static int TYPE_YOUXI = 8;
    public static int TYPE_DIANSHIJU = 2;
    public static int TYPE_YINYUE = 5;
    public static int TYPE_DONGMAN = 4;
    public static int TYPE_TIYU = 17;
    public static int TYPE_JUNSHI = 28;
    public static int TYPE_SHAOER = 15;
    public static int TYPE_QICHE = 26;
    public static int TYPE_ZIXUN = 25;
    public static int TYPE_DIANYING = 1;
    public static int TYPE_SHISAHNG = 13;
    public static int TYPE_CAIJING = 24;
    public static int TYPE_JIAOYU = 12;
    public static int TYPE_GAOXIAO = 22;
    public static int TYPE_PIANHUA = 10;
    public static int TYPE_SHENGHUO = 21;
 
    public final static String KEY1 = "4e62e7a3642246a8b7f2422bed4117ce";// 短视频KEY
    public final static String KEY2 = "4e62e7a3642246a8b7f2422bed4117ce";// 电影电视剧动漫Key
 
    // 频道列表
    public static String channel_list = "http://expand.video.iqiyi.com/c/channel/list.json";
 
    // 专辑列表
    public static String album_list = "http://expand.video.iqiyi.com/c/mix/udlist.json";
 
    // 专辑详情
    public static String album_detail = "http://expand.video.iqiyi.com/c/mix/info.json";
 
    // 视频信息
    public static String video_info = "http://expand.video.iqiyi.com/c/mix/info.json";
 
    // 根据更新时间段查询视频接口
    public static String update_info = "http://expand.video.iqiyi.com/api/video/list.json";
 
    public static String search_album = "http://expand.video.iqiyi.com/api/search/list.json";
 
    // 获取频道列表
    public static List<IqiyiChannel> getChannelList() {
        String url = channel_list + "?apiKey=" + KEY1;
        String result = get(url);
        Gson gson = new GsonBuilder().setVersion(1).create();
        if (!StringUtil.isNullOrEmpty(result)) {
            JSONObject object = JSONObject.fromObject(result);
            if (object != null) {
                if ("A00000".equalsIgnoreCase(object.optString("code"))) {// 成功
                    Type listType = new TypeToken<List<IqiyiChannel>>() {
                    }.getType();
                    return gson.fromJson(object.optJSONArray("data").toString(), listType);
                }
            }
        }
        return null;
    }
 
    /**
     * 获取专辑列表
     * 
     * @param categoryId
     * @param scode
     * @param pageNo
     * @param pageSize
     * @param coopal
     * @return
     */
    public static Map<String, Object> getAlbumList(String categoryId, String scode, int pageNo, int pageSize,
            int coopal) {
        Map<String, Object> map = new HashMap<String, Object>();
 
        String url = String.format(
                "http://expand.video.iqiyi.com/api/album/list.json"
                        + "?apiKey=%s&categoryId=%s&scode=%s&pageNo=%d&pageSize=%d&%s&isCharge=0",
                KEY1, categoryId, scode, pageNo, pageSize, coopal == -1 ? "" : "coopal=" + coopal);
        String result = get(url);
        Gson gson = new GsonBuilder().setVersion(1).create();
        if (!StringUtil.isNullOrEmpty(result)) {
            JSONObject object = JSONObject.fromObject(result);
            if (object != null) {
                if ("A00000".equalsIgnoreCase(object.optString("code"))) {// 成功
                    map.put("count", object.optInt("total"));
                    Type listType = new TypeToken<List<IqiyiAlbum>>() {
                    }.getType();
                    map.put("data", gson.fromJson(object.optJSONArray("data").toString(), listType));
                }
            }
        }
        return map;
    }
 
    public static int getAlbumListCount(String categoryId, String scode, int pageNo, int pageSize, int coopal) {
        String url = String.format(album_list + "?apiKey=%s&categoryId=%s&scode=%s&pageNo=%d&pageSize=%d&%s&isCharge=0",
                KEY1, categoryId, scode, pageNo, pageSize, coopal == -1 ? "" : "coopal=" + coopal);
        String result = get(url);
        if (!StringUtil.isNullOrEmpty(result)) {
            JSONObject object = JSONObject.fromObject(result);
            if (object != null) {
                if ("A00000".equalsIgnoreCase(object.optString("code"))) {// 成功
                    return object.optInt("total");
                }
            }
        }
        return 0;
    }
 
    private static IqiyiAlbum parseAlbumDetail(String str) {
        Gson gson = new GsonBuilder().setVersion(1).create();
        JSONObject data = JSONObject.fromObject(str);
        IqiyiAlbum qa = gson.fromJson(data.toString(), IqiyiAlbum.class);
        qa.setThreeCtgs(data.optJSONArray("threeCategories") + "");
        qa.setTvIds(data.optJSONArray("tvQipuIds") + "");
        qa.setUpDown(data.optJSONArray("upDown") + "");
        qa.setCreditList(data.optJSONArray("creditList") + "");
        if (data.optJSONObject("statistics") != null)
            qa.setScore(data.optJSONObject("statistics").optString("score"));
        if (!data.optBoolean("sourceAlbum"))
            qa.setOwnerAlbumId(-1);
 
        if (StringUtil.isNullOrEmpty(qa.getTvIds())) {
            JSONArray array = new JSONArray();
            array.add(qa.getAlbumId());
            qa.setTvIds(array.toString());
        }
        return qa;
 
    }
 
    /**
     * 获取专辑详情
     * 
     * @param albumid
     * @return
     */
    public static IqiyiAlbum getAlbumDetail(String albumid) {
        IqiyiAlbum qa = null;
        String url = String.format(album_detail + "?apiKey=%s&qipuId=%s", KEY1, albumid);
        String result = get(url);
        JSONObject object = JSONObject.fromObject(result);
        if (object != null) {
            if ("A00000".equalsIgnoreCase(object.optString("code"))) {// 成功
                JSONObject data = object.optJSONObject("data");
                if (StringUtil.isNullOrEmpty(data.optString("videoCount")))
                    data.put("videoCount", 1);
                qa = parseAlbumDetail(data.toString());
            }
        }
        return qa;
    }
 
    /**
     * 获取视频详情
     * 
     * @param tvid
     * @return
     */
 
    public static IqiyiVideoInfo getVideoInfo(String tvid) {
        String url = String.format(video_info + "?apiKey=%s&qipuId=%s", KEY1, tvid);
        Gson gson = new GsonBuilder().setVersion(1).create();
        String result = get(url);
        JSONObject object = JSONObject.fromObject(result);
        if (object != null) {
            if ("A00000".equalsIgnoreCase(object.optString("code"))) {// 成功
                JSONObject data = object.optJSONObject("data");
                IqiyiVideoInfo videoInfo = gson.fromJson(data.toString(), IqiyiVideoInfo.class);
                videoInfo.setTvYear(TimeUtil.getGernalTime(
                        TimeUtil.convertGernalTime(data.optString("updateTime"), "yyyy-MM-dd HH:mm:ss"), "yyyyMMdd"));
                if (!StringUtil.isNullOrEmpty(data.optString("period")))
                    videoInfo.setTvYear(data.optString("period"));
 
                videoInfo.setPlaycontrols(data.optJSONArray("playControls").toString());
                Type type = new TypeToken<List<PlayControls>>() {
                }.getType();
                List<PlayControls> list = new Gson().fromJson(videoInfo.getPlaycontrols(), type);
                videoInfo.setPlayControls(list);
                return videoInfo;
            }
        }
        return null;
    }
 
    public static Map<String, Object> updateAlbum(String startTime, String endTime, Long minId, int pageSize,
            int categoryId, int shortVideo) {
        Map<String, Object> map = new HashMap<String, Object>();
 
        String url = "";
 
        if (minId == null)
            url = String.format(album_list + "?apiKey=%s&startTime=%s&endTime=%s&status=1&pageSize=%s&categoryId=%s",
                    KEY1, startTime, endTime, pageSize, categoryId);
        else
            url = String.format(
                    album_list + "?apiKey=%s&startTime=%s&endTime=%s&minId=%s&status=1&pageSize=%s&categoryId=%s", KEY1,
                    startTime, endTime, minId + "", pageSize, categoryId);
        if (shortVideo == 1) {
            url += "&solo=" + shortVideo;
        }
 
        String result = get(url);
 
        if (!StringUtil.isNullOrEmpty(result)) {
            JSONObject object = JSONObject.fromObject(result);
            if (object != null) {
                if ("A00000".equalsIgnoreCase(object.optString("code"))) {// 成功
                    map.put("count", object.optInt("total"));
                    JSONArray array = object.optJSONArray("data");
                    List<IqiyiAlbum> list = new ArrayList<>();
                    for (int i = 0; i < array.size(); i++) {
                        String albumId = array.optJSONObject(i).optString("featureAlbumId");
                        if (shortVideo == 1) {
                            IqiyiAlbum album = getAlbumDetail(albumId);
                            if (album != null)
                            { if(StringUtil.isNullOrEmpty(album.getCreatedTime()))
                                album.setCreatedTime(array.optJSONObject(i).optString("initialIssueTime"));
                                list.add(album);
                            }
                            } else {
                            IqiyiAlbum album = parseAlbumDetail(array.optJSONObject(i).toString());
                            if (album != null)
                                list.add(album);
                        }
                    }
                    map.put("data", list);
                }
            }
        }
        return map;
    }
 
    public static Map<String, Object> searchAlbum(String key) {
        Map<String, Object> map = new HashMap<String, Object>();
 
        String keyStr = "";
        try {
            keyStr = java.net.URLEncoder.encode(key, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        String url = String.format(search_album + "?apiKey=%s&keyWord=%s", KEY1, keyStr);
        String result = get(url);
        Gson gson = new GsonBuilder().setVersion(1).create();
        if (!StringUtil.isNullOrEmpty(result)) {
            JSONObject object = JSONObject.fromObject(result);
            if (object != null) {
                if ("A00000".equalsIgnoreCase(object.optString("code"))) {// 成功
                    map.put("count", object.optInt("total"));
                    Type listType = new TypeToken<List<IqiyiAlbum>>() {
                    }.getType();
                    map.put("data", gson.fromJson(object.optJSONArray("data").toString(), listType));
                }
            }
        }
        return map;
    }
 
    public static String get(String url) {
        return HttpUtil.get(url, "ISO-8859-1");
    }
}