admin
2021-02-06 e2c6372f29ae0a93d9f672ffad4613581ba3e201
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
package com.newvideo.iqiyi;
 
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
 
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.util.HttpUtil;
import com.newvideo.util.StringUtil;
 
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/api/category/list.json";
 
    // 专辑列表
    public static String album_list = "http://expand.video.iqiyi.com/api/album/list.json";
 
    // 专辑详情
    public static String album_detail = "http://expand.video.iqiyi.com/api/album/info.json";
 
    // 视频信息
    public static String video_info = "http://expand.video.iqiyi.com/api/video/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(album_list + "?apiKey=%s&categoryId=%s&scode=%s&pageNo=%d&pageSize=%d&%s&isCharge=0&contentType=1",
                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;
    }
 
    /**
     * 获取专辑详情
     * 
     * @param albumid
     * @return
     */
    public static IqiyiAlbum getAlbumDetail(String albumid) {
 
        IqiyiAlbum qa = null;
        String url = String.format(album_detail + "?apiKey=%s&albumId=%s", KEY1, albumid);
        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");
                qa = gson.fromJson(data.toString(), IqiyiAlbum.class);
                qa.setThreeCtgs(data.optJSONArray("threeCtgs") + "");
                qa.setTvIds(data.optJSONArray("tvIds") + "");
                qa.setUpDown(data.optJSONArray("upDown") + "");
                qa.setCreditList(data.optJSONArray("creditList") + "");
            }
        }
        return qa;
    }
 
    /**
     * 获取视频详情
     * 
     * @param tvid
     * @return
     */
 
    public static IqiyiVideoInfo getVideoInfo(String tvid) {
        String url = String.format(video_info + "?apiKey=%s&tvId=%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");
                return gson.fromJson(data.toString(), IqiyiVideoInfo.class);
            }
        }
        return null;
    }
 
    public static Map<String, Object> updateAlbum(String startTime, String endTime, int pageNo, int pageSize,
            int categoryId) {
        Map<String, Object> map = new HashMap<String, Object>();
        String url = String.format(
                album_list
                        + "?apiKey=%s&startTime=%s&endTime=%s&status=1&pageNo=%s&pageSize=%s&categoryId=%s",//&contentType=1
                KEY1, startTime, endTime, pageNo, pageSize, categoryId);
        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<IqiyiAlbum>>() {
                    }.getType();
                    map.put("count", object.optInt("total"));
                    try {
                        map.put("data", gson.fromJson(object.optJSONArray("data").toString(), listType));
                    } catch (Exception e) {
                        System.out.println(categoryId);
                        e.printStackTrace();
                    }
 
                }
            }
        }
        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");
    }
}