package com.yeshi.buwan.util.video.web;
|
|
import com.yeshi.buwan.util.StringUtil;
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
import org.yeshi.utils.HttpUtil;
|
|
import java.net.URLEncoder;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
public class IqiyiWebUtil {
|
|
/**
|
* 网页列表中的请求参数
|
*/
|
private static class IqiyiWebVideoListParams {
|
private int channelId;
|
private int dataType;
|
private int mode;
|
private String threeCategoryId;
|
|
public int getChannelId() {
|
return channelId;
|
}
|
|
public void setChannelId(int channelId) {
|
this.channelId = channelId;
|
}
|
|
public int getDataType() {
|
return dataType;
|
}
|
|
public void setDataType(int dataType) {
|
this.dataType = dataType;
|
}
|
|
public int getMode() {
|
return mode;
|
}
|
|
public void setMode(int mode) {
|
this.mode = mode;
|
}
|
|
public String getThreeCategoryId() {
|
return threeCategoryId;
|
}
|
|
public void setThreeCategoryId(String threeCategoryId) {
|
this.threeCategoryId = threeCategoryId;
|
}
|
}
|
|
|
public static IqiyiWebVideoListParams parseParams(String url) {
|
//清除参数
|
if (url.indexOf("?") > -1) {
|
url = url.substring(0, url.indexOf("?"));
|
}
|
url = url.replace("https://", "").replace("http://", "");
|
String[] urls = url.split("/");
|
String host = urls[0];
|
if (!host.contains(".iqiyi.com")) {
|
return null;
|
}
|
IqiyiWebVideoListParams params = new IqiyiWebVideoListParams();
|
List<String> threeCategoryIds = new ArrayList<>();
|
String channelId = urls[2];
|
String otherParams = urls[3];
|
String[] sts = otherParams.split("-------------");
|
if (sts.length > 1) {
|
String threeCategoryId = sts[0];
|
if (!StringUtil.isNullOrEmpty(threeCategoryId)) {
|
String[] categorys = threeCategoryId.split("-");
|
for (String cate : categorys) {
|
threeCategoryIds.add(cate + ";must");
|
}
|
}
|
sts = sts[1].split("-");
|
String mode = sts[0];
|
String dataType = sts[2];
|
params.setMode(Integer.parseInt(mode));
|
params.setDataType(Integer.parseInt(dataType));
|
}
|
if (threeCategoryIds.size() > 0) {
|
params.setThreeCategoryId(org.yeshi.utils.StringUtil.concat(threeCategoryIds, ","));
|
}
|
params.setChannelId(Integer.parseInt(channelId));
|
return params;
|
}
|
|
|
public static List<Long> getVideoTvidList(IqiyiWebVideoListParams params, int page) throws Exception {
|
if (params == null)
|
return null;
|
String url = "https://pcw-api.iqiyi.com/search/recommend/list?";
|
List<String> paramsList = new ArrayList<>();
|
paramsList.add("channel_id=" + params.getChannelId());
|
paramsList.add("data_type=" + params.getDataType());
|
paramsList.add("mode=" + params.getMode());
|
paramsList.add("page_id=" + page);
|
paramsList.add("ret_num=48");
|
if (params.getThreeCategoryId() != null) {
|
paramsList.add("three_category_id=" + URLEncoder.encode(params.getThreeCategoryId()));
|
}
|
|
url += org.yeshi.utils.StringUtil.concat(paramsList, "&");
|
Map<String, String> headers = new HashMap<>();
|
headers.put("referer", "https://list.iqiyi.com/");
|
headers.put("origin", "https://list.iqiyi.com");
|
headers.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36");
|
String result = HttpUtil.get(url, new HashMap<>(), headers);
|
JSONObject resultJSON = JSONObject.fromObject(result);
|
|
List<Long> tvIds = new ArrayList<>();
|
if (resultJSON.optString("code").equalsIgnoreCase("A00000")) {
|
JSONObject data = resultJSON.optJSONObject("data");
|
JSONArray list = data.optJSONArray("list");
|
for (int i = 0; i < list.size(); i++) {
|
String tvId = list.optJSONObject(i).optString("tvId");
|
tvIds.add(Long.parseLong(tvId));
|
}
|
} else
|
throw new Exception(resultJSON.optString("data"));
|
return tvIds;
|
}
|
|
|
public static void main(String[] args) throws Exception {
|
List<Long> list = getVideoTvidList(parseParams("https://list.iqiyi.com/www/7/186-------------24-1-2-iqiyi--.html?vfrm=pcw_yule&vfrmblk=C&vfrmrst="), 1);
|
System.out.println(list);
|
}
|
}
|