package com.yeshi.buwan.videos.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.yeshi.buwan.videos.iqiyi.entity.IqiyiAlbum;
|
import com.yeshi.buwan.videos.iqiyi.entity.IqiyiChannel;
|
import com.yeshi.buwan.videos.iqiyi.entity.IqiyiVideoInfo;
|
import com.yeshi.buwan.videos.iqiyi.entity.PlayControls;
|
import com.yeshi.buwan.util.HttpUtil;
|
import com.yeshi.buwan.util.StringUtil;
|
import com.yeshi.buwan.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) {
|
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");
|
}
|
}
|