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");
|
}
|
}
|