package com.yeshi.buwan.iqiyi;
|
|
import com.google.gson.*;
|
import com.google.gson.reflect.TypeToken;
|
import com.yeshi.buwan.iqiyi.entity.IqiyiAlbum2;
|
import com.yeshi.buwan.iqiyi.vo.IqiyiAlbumListResult;
|
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;
|
|
import java.io.UnsupportedEncodingException;
|
import java.lang.reflect.Type;
|
import java.net.URLEncoder;
|
import java.util.*;
|
|
public class IqiYiNewAPI {
|
|
public final static int TYPE_DIANYING = 1;
|
public final static int TYPE_DIANSHIJU = 2;//电视剧
|
public final static int TYPE_ZONGYI = 6;//综艺
|
public final static int TYPE_DONGMAN = 4;//动漫
|
|
public static int TYPE_YULE = 7;//娱乐
|
public static int TYPE_KEJI = 30;//科技
|
public static int TYPE_YOUXI = 8;//游戏
|
public static int TYPE_YINYUE = 5;//音乐
|
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_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";
|
|
|
private static IqiyiAlbum2 parseAlbumData(JSONObject item) {
|
//数据预处理
|
try {
|
if (item.optJSONArray("tvQipuIds") == null) {
|
item.put("tvQipuIds", new ArrayList<>());
|
}
|
} catch (Exception e) {
|
item.put("tvQipuIds", new ArrayList<>());
|
}
|
|
try {
|
if (item.optJSONArray("threeCategories") == null) {
|
item.put("threeCategories", new ArrayList<>());
|
}
|
} catch (Exception e) {
|
item.put("threeCategories", new ArrayList<>());
|
}
|
|
try {
|
if (item.optJSONArray("contributors") == null) {
|
item.put("contributors", new ArrayList<>());
|
}
|
} catch (Exception e) {
|
item.put("contributors", new ArrayList<>());
|
}
|
|
try {
|
if (item.optJSONArray("playControls") == null) {
|
item.put("playControls", new ArrayList<>());
|
}
|
} catch (Exception e) {
|
item.put("playControls", new ArrayList<>());
|
}
|
|
return getGson().fromJson(item.toString(), IqiyiAlbum2.class);
|
}
|
|
/**
|
* 获取所有的专辑
|
*
|
* @param categoryId
|
* @param minId
|
* @param isAlbum
|
* @param pageSize
|
* @return
|
*/
|
public static IqiyiAlbumListResult getAllAlbumAndVideoList(String categoryId, Long minId, boolean isAlbum, Integer pageSize) {
|
Map<String, String> params = new HashMap<>();
|
params.put("categoryId", categoryId);
|
if (minId != null)
|
params.put("minId", minId + "");
|
if (pageSize != null)
|
params.put("pageSize", pageSize + "");
|
//0:专辑;1:单视频;默认专辑
|
if (isAlbum)
|
params.put("solo", "0");
|
else
|
params.put("solo", "1");
|
//0:无效;1 有效;默认有效
|
params.put("status", "1");
|
//0:免费;1:付费;
|
//params.put("isCharge", "");
|
|
|
String result = baseRquest("http://expand.video.iqiyi.com/c/mix/list.json", params);
|
System.out.println(result);
|
Gson gson = getGson();
|
|
IqiyiAlbumListResult albumResult = new IqiyiAlbumListResult();
|
|
|
if (!StringUtil.isNullOrEmpty(result)) {
|
JSONObject object = JSONObject.fromObject(result);
|
if (object != null) {
|
if ("A00000".equalsIgnoreCase(object.optString("code"))) {// 成功
|
List<IqiyiAlbum2> albumList = new ArrayList<>();
|
JSONArray array = object.optJSONArray("data");
|
for (int i = 0; i < array.size(); i++) {
|
albumList.add(parseAlbumData(array.optJSONObject(i)));
|
}
|
albumResult.setAlbum2List(albumList);
|
}
|
}
|
}
|
if (albumResult.getAlbum2List() != null && albumResult.getAlbum2List().size() > 0) {
|
Comparator<IqiyiAlbum2> cm = new Comparator<IqiyiAlbum2>() {
|
@Override
|
public int compare(IqiyiAlbum2 o1, IqiyiAlbum2 o2) {
|
return o1.getId() - o2.getId() >= 0 ? 1 : 0;
|
}
|
};
|
Collections.sort(albumResult.getAlbum2List(), cm);
|
albumResult.setMinId(albumResult.getAlbum2List().get(0).getId());
|
} else
|
albumResult.setMinId(null);
|
|
return albumResult;
|
}
|
|
|
/**
|
* 获取专辑或者视频详情
|
*
|
* @param id
|
* @return
|
*/
|
public static List<IqiyiAlbum2> getAlbumOrVideoDetail(List<Long> id) {
|
Map<String, String> params = new HashMap<>();
|
params.put("id", StringUtil.join(id, ","));
|
String result = baseRquest("http://expand.video.iqiyi.com/c/mixes/info.json", params);
|
System.out.println(result);
|
if (!StringUtil.isNullOrEmpty(result)) {
|
JSONObject object = JSONObject.fromObject(result);
|
if (object != null) {
|
if ("A00000".equalsIgnoreCase(object.optString("code"))) {// 成功
|
List<IqiyiAlbum2> albumList = new ArrayList<>();
|
JSONArray array = object.optJSONArray("data");
|
for (int i = 0; i < array.size(); i++) {
|
albumList.add(parseAlbumData(array.optJSONObject(i)));
|
}
|
return albumList;
|
}
|
}
|
}
|
return null;
|
}
|
|
public static IqiyiAlbum2 getAlbumOrVideoDetail(Long id) {
|
List<Long> ids = new ArrayList<>();
|
ids.add(id);
|
List<IqiyiAlbum2> list = getAlbumOrVideoDetail(ids);
|
|
if (list != null && list.size() > 0)
|
return list.get(0);
|
return null;
|
}
|
|
/**
|
* 获取更新的专辑
|
*
|
* @param categoryId
|
* @param startTime
|
* @param endTime
|
* @param minId
|
* @param isAlbum
|
* @param pageSize
|
* @return
|
*/
|
public static IqiyiAlbumListResult getUpdateAlbumList(String categoryId, Date startTime, Date endTime, Long minId, boolean isAlbum, Integer pageSize) {
|
Map<String, String> params = new HashMap<>();
|
params.put("categoryId", categoryId);
|
if (minId != null)
|
params.put("minId", minId + "");
|
if (pageSize != null)
|
params.put("pageSize", pageSize + "");
|
//0:专辑;1:单视频;默认专辑
|
if (isAlbum)
|
params.put("solo", "0");
|
else
|
params.put("solo", "1");
|
//0:无效;1 有效;默认有效
|
params.put("status", "1");
|
params.put("startTime", TimeUtil.getGernalTime(startTime.getTime(), "yyyyMMddHHmmss"));
|
params.put("endTime", TimeUtil.getGernalTime(endTime.getTime(), "yyyyMMddHHmmss"));
|
//0:免费;1:付费;
|
//params.put("isCharge", "");
|
|
|
String result = baseRquest("http://expand.video.iqiyi.com/c/mix/udlist.json", params);
|
System.out.println(result);
|
IqiyiAlbumListResult albumResult = new IqiyiAlbumListResult();
|
if (!StringUtil.isNullOrEmpty(result)) {
|
JSONObject object = JSONObject.fromObject(result);
|
if (object != null) {
|
if ("A00000".equalsIgnoreCase(object.optString("code"))) {// 成功
|
List<IqiyiAlbum2> albumList = new ArrayList<>();
|
JSONArray array = object.optJSONArray("data");
|
for (int i = 0; i < array.size(); i++) {
|
albumList.add(parseAlbumData(array.optJSONObject(i)));
|
}
|
albumResult.setAlbum2List(albumList);
|
}
|
}
|
}
|
if (albumResult.getAlbum2List() != null && albumResult.getAlbum2List().size() > 0) {
|
Comparator<IqiyiAlbum2> cm = new Comparator<IqiyiAlbum2>() {
|
@Override
|
public int compare(IqiyiAlbum2 o1, IqiyiAlbum2 o2) {
|
return o1.getId() - o2.getId() >= 0 ? 1 : 0;
|
}
|
};
|
Collections.sort(albumResult.getAlbum2List(), cm);
|
albumResult.setMinId(albumResult.getAlbum2List().get(0).getId());
|
} else
|
albumResult.setMinId(null);
|
|
return albumResult;
|
}
|
|
|
/**
|
* 获取更新的视频
|
*
|
* @param categoryId
|
* @param startTime
|
* @param endTime
|
* @param minId
|
* @param isAlbum
|
* @param pageSize
|
* @return
|
*/
|
public static IqiyiAlbumListResult getUpdateVideoList(String categoryId, Date startTime, Date endTime, Long minId, boolean isAlbum, Integer pageSize) {
|
Map<String, String> params = new HashMap<>();
|
params.put("categoryId", categoryId);
|
if (minId != null)
|
params.put("minId", minId + "");
|
if (pageSize != null)
|
params.put("pageSize", pageSize + "");
|
//0:专辑;1:单视频;默认专辑
|
if (isAlbum)
|
params.put("solo", "0");
|
else
|
params.put("solo", "1");
|
//0:无效;1 有效;默认有效
|
params.put("status", "1");
|
params.put("startTime", TimeUtil.getGernalTime(startTime.getTime(), "yyyyMMddHHmmss"));
|
params.put("endTime", TimeUtil.getGernalTime(endTime.getTime(), "yyyyMMddHHmmss"));
|
//0:免费;1:付费;
|
//params.put("isCharge", "");
|
|
|
String result = baseRquest("http://expand.video.iqiyi.com/c/episode/udlist.json", params);
|
System.out.println(result);
|
IqiyiAlbumListResult albumResult = new IqiyiAlbumListResult();
|
if (!StringUtil.isNullOrEmpty(result)) {
|
JSONObject object = JSONObject.fromObject(result);
|
if (object != null) {
|
if ("A00000".equalsIgnoreCase(object.optString("code"))) {// 成功
|
List<IqiyiAlbum2> albumList = new ArrayList<>();
|
JSONArray array = object.optJSONArray("data");
|
for (int i = 0; i < array.size(); i++) {
|
albumList.add(parseAlbumData(array.optJSONObject(i)));
|
}
|
albumResult.setAlbum2List(albumList);
|
}
|
}
|
}
|
if (albumResult.getAlbum2List() != null && albumResult.getAlbum2List().size() > 0) {
|
Comparator<IqiyiAlbum2> cm = new Comparator<IqiyiAlbum2>() {
|
@Override
|
public int compare(IqiyiAlbum2 o1, IqiyiAlbum2 o2) {
|
return o1.getId() - o2.getId() >= 0 ? 1 : 0;
|
}
|
};
|
Collections.sort(albumResult.getAlbum2List(), cm);
|
albumResult.setMinId(albumResult.getAlbum2List().get(0).getId());
|
} else
|
albumResult.setMinId(null);
|
|
return albumResult;
|
}
|
|
|
public static String getIdByUrl(String url) {
|
Map<String, String> params = new HashMap<>();
|
params.put("playurl", url);
|
params.put("rec", "0");
|
String result = baseRquest("http://expand.video.iqiyi.com/c/fb", params);
|
System.out.println(result);
|
if (!StringUtil.isNullOrEmpty(result)) {
|
JSONObject object = JSONObject.fromObject(result);
|
if (object != null) {
|
if ("A00000".equalsIgnoreCase(object.optString("code"))) {// 成功
|
List<IqiyiAlbum2> albumList = new ArrayList<>();
|
JSONObject obj = object.optJSONObject("data");
|
return obj.optString("qipuId");
|
}
|
}
|
}
|
return null;
|
}
|
|
public static String getAidByUrl(String url) {
|
Map<String, String> params = new HashMap<>();
|
params.put("playurl", url);
|
params.put("rec", "0");
|
String result = baseRquest("http://expand.video.iqiyi.com/c/fb", params);
|
System.out.println(result);
|
if (!StringUtil.isNullOrEmpty(result)) {
|
JSONObject object = JSONObject.fromObject(result);
|
if (object != null) {
|
if ("A00000".equalsIgnoreCase(object.optString("code"))) {// 成功
|
List<IqiyiAlbum2> albumList = new ArrayList<>();
|
JSONObject obj = object.optJSONObject("data");
|
return obj.optString("albumQipuId");
|
}
|
}
|
}
|
return null;
|
}
|
|
|
private static Gson gson = null;
|
|
static class DoubleTypeAdapter implements JsonDeserializer<Double> {
|
|
@Override
|
public Double deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
if (json == null || json.getAsString().equalsIgnoreCase("")) {
|
return 0.0;
|
} else
|
return Double.parseDouble(json.getAsString());
|
}
|
}
|
|
private static Gson getGson() {
|
if (gson == null)
|
gson = new GsonBuilder()
|
.registerTypeAdapter(double.class, new DoubleTypeAdapter())
|
.registerTypeAdapter(int.class, new JsonDeserializer<Integer>() {
|
@Override
|
public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
|
if (json == null || json.getAsString().equalsIgnoreCase("")) {
|
return 0;
|
} else
|
return json.getAsInt();
|
}
|
})
|
.registerTypeAdapter(IqiyiAlbum2.StatisticsBean.class, new JsonDeserializer<IqiyiAlbum2.StatisticsBean>() {
|
|
@Override
|
public IqiyiAlbum2.StatisticsBean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
String data = null;
|
if (json != null)
|
data = json.toString();
|
|
if (data == null || data.equalsIgnoreCase("")) {
|
return null;
|
} else
|
return new GsonBuilder().registerTypeAdapter(double.class, new DoubleTypeAdapter()).create().fromJson(data, IqiyiAlbum2.StatisticsBean.class);
|
}
|
})
|
.registerTypeAdapter(IqiyiAlbum2.VideoBean.class, new JsonDeserializer<IqiyiAlbum2.VideoBean>() {
|
|
@Override
|
public IqiyiAlbum2.VideoBean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
String data = json.toString();
|
if (json == null || data.replace("\"", "").equalsIgnoreCase("")) {
|
return null;
|
} else
|
return new Gson().fromJson(data, IqiyiAlbum2.VideoBean.class);
|
}
|
})
|
.registerTypeAdapter(IqiyiAlbum2.CopyrightOwnersBean.class, new JsonDeserializer<IqiyiAlbum2.CopyrightOwnersBean>() {
|
|
@Override
|
public IqiyiAlbum2.CopyrightOwnersBean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
String data = "";
|
try {
|
data = json.getAsJsonObject().toString();
|
} catch (Exception e) {
|
data = json.getAsString();
|
}
|
if (json == null || data.equalsIgnoreCase("")) {
|
return null;
|
} else
|
return new Gson().fromJson(data, IqiyiAlbum2.CopyrightOwnersBean.class);
|
}
|
})
|
.registerTypeAdapter(IqiyiAlbum2.PanoramaBean.class, new JsonDeserializer<IqiyiAlbum2.PanoramaBean>() {
|
|
@Override
|
public IqiyiAlbum2.PanoramaBean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
String data = json.toString();
|
if (json == null || data.replace("\"", "").equalsIgnoreCase("")) {
|
return null;
|
} else
|
return new Gson().fromJson(data, IqiyiAlbum2.PanoramaBean.class);
|
}
|
})
|
|
.create();
|
|
return gson;
|
}
|
|
|
public static String baseRquest(String url, Map<String, String> params) {
|
String baseUrl = url;
|
if (params != null) {
|
params.put("apiKey", KEY2);
|
baseUrl += "?";
|
for (Iterator<String> its = params.keySet().iterator(); its.hasNext(); ) {
|
String key = its.next();
|
String value = params.get(key);
|
try {
|
baseUrl += key + "=" + URLEncoder.encode(value, "UTF-8") + "&";
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
return HttpUtil.get(baseUrl, "ISO-8859-1");
|
}
|
|
|
}
|