package com.yeshi.buwan.util.video.web;
|
|
import net.sf.json.JSONObject;
|
import org.yeshi.utils.StringUtil;
|
|
import java.io.UnsupportedEncodingException;
|
import java.net.URLDecoder;
|
import java.util.*;
|
|
public class TencentWebUtil {
|
|
final public static Map<String,Integer> CHANNEL_ID_MAP=new HashMap<>();
|
static{
|
CHANNEL_ID_MAP.put("电影",100173);
|
CHANNEL_ID_MAP.put("电视剧",100113);
|
CHANNEL_ID_MAP.put("动漫",100119);
|
}
|
|
public static class TencentWebVideoInfo {
|
private String playUrl;
|
private String id;
|
private String title;
|
private String picture;
|
private String duration;
|
private String tag;
|
private String epsodePubtime;
|
|
public String getEpsodePubtime() {
|
return epsodePubtime;
|
}
|
|
public void setEpsodePubtime(String epsodePubtime) {
|
this.epsodePubtime = epsodePubtime;
|
}
|
|
public String getPlayUrl() {
|
return playUrl;
|
}
|
|
public void setPlayUrl(String playUrl) {
|
this.playUrl = playUrl;
|
}
|
|
public String getId() {
|
return id;
|
}
|
|
public void setId(String id) {
|
this.id = id;
|
}
|
|
public String getTitle() {
|
return title;
|
}
|
|
public void setTitle(String title) {
|
this.title = title;
|
}
|
|
public String getPicture() {
|
return picture;
|
}
|
|
public void setPicture(String picture) {
|
this.picture = picture;
|
}
|
|
public String getDuration() {
|
return duration;
|
}
|
|
public void setDuration(String duration) {
|
this.duration = duration;
|
}
|
|
public String getTag() {
|
return tag;
|
}
|
|
public void setTag(String tag) {
|
this.tag = tag;
|
}
|
}
|
|
|
public static Map<String, String> parseParams(String url) {
|
//清除参数
|
String param = null;
|
if (url.indexOf("?") > -1) {
|
param = url.substring(url.indexOf("?"));
|
}
|
if (!url.contains("v.qq.com")) {
|
return null;
|
}
|
String[] params = param.split("&");
|
Map<String, String> map = new HashMap<>();
|
for (String p : params) {
|
map.put(p.split("=")[0], URLDecoder.decode(p.split("=")[1]));
|
}
|
return map;
|
}
|
|
|
/**
|
* 获取短视频列表
|
*
|
* @param channelId 100173:电影 100113:电视剧 100119:动漫
|
* @param pageIndex
|
* @return
|
* @throws Exception
|
*/
|
public static List<TencentWebVideoInfo> getVideoList(int channelId, int pageIndex, Integer areaId) throws Exception {
|
List<TencentWebVideoInfo> videoList=new ArrayList<>();
|
Map<String, String> headers = new HashMap<>();
|
headers.put("Content-Type", "application/json; charset=utf-8");
|
headers.put("Referer", "https://v.qq.com/");
|
String text =
|
"{\"page_context\":{\"page_index\":\"1\"},\"page_params\":{\"page_id\":\"channel_list_second_page\",\"page_type\":\"operation\",\"channel_id\":\"100173\",\"filter_params\":\"sort=75\",\"page\":\"1\",\"new_mark_label_enabled\":\"1\"},\"page_bypass_params\":{\"params\":{\"page_id\":\"channel_list_second_page\",\"page_type\":\"operation\",\"channel_id\":\"100173\",\"filter_params\":\"sort=75\",\"page\":\"1\",\"caller_id\":\"3000010\",\"platform_id\":\"2\",\"data_mode\":\"default\",\"user_mode\":\"default\"},\"scene\":\"operation\",\"abtest_bypass_id\":\"77fef11ab0ccd4ee\"}}";
|
JSONObject params=JSONObject.fromObject(text);
|
params.optJSONObject("page_context").put("page_index",pageIndex+"");
|
params.optJSONObject("page_params").put("channel_id",channelId+"");
|
params.optJSONObject("page_params").put("page",pageIndex+"");
|
if(areaId!=null) {
|
params.optJSONObject("page_params").put("filter_params", params.optJSONObject("page_params").get("filter_params") + "&iarea=" + areaId);
|
}
|
params.optJSONObject("page_bypass_params").optJSONObject("params").put("page",pageIndex+"");
|
params.optJSONObject("page_bypass_params").optJSONObject("params").put("channel_id",channelId+"");
|
|
String result = com.yeshi.buwan.util.HttpUtil.post("https://pbaccess.video.qq.com/trpc.vector_layout.page_view.PageService/getPage?video_appid=3000010", params.toString(), headers);
|
|
|
com.alibaba.fastjson.JSONObject resultJson = com.alibaba.fastjson.JSONObject.parseObject(result);
|
if (resultJson.getInteger("ret") == 0) {
|
|
com.alibaba.fastjson.JSONObject data = resultJson.getJSONObject("data");
|
com.alibaba.fastjson.JSONArray array = data.getJSONArray("CardList");
|
for (int i = 0; i < array.size(); i++) {
|
if (array.getJSONObject(i).getString("type").equalsIgnoreCase("channel_list_poster")) {
|
array = array.getJSONObject(i).getJSONObject("children_list").getJSONObject("list").getJSONArray("cards");
|
for (int j = 0; j < array.size(); j++) {
|
if (array.getJSONObject(j).getString("type").equalsIgnoreCase("channel_list_poster")) {
|
com.alibaba.fastjson.JSONObject item = array.getJSONObject(j).getJSONObject("params");
|
TencentWebVideoInfo video = parseListItem(item);
|
videoList.add(video);
|
}
|
}
|
break;
|
}
|
|
}
|
|
}
|
return videoList;
|
}
|
|
|
|
|
public static String getApiUrl(String webUrl, int page) {
|
Map<String, String> params = parseParams(webUrl);
|
int pageSize = 30;
|
params.put("append", "1");
|
params.put("listpage", page + "");
|
params.put("offset", (page - 1) * pageSize + "");
|
params.put("pagesize", pageSize + "");
|
String url = "https://v.qq.com/x/bu/pagesheet/list";
|
List<String> paramsList = new ArrayList<>();
|
for (Iterator<String> its = params.keySet().iterator(); its.hasNext(); ) {
|
String key = its.next();
|
String value = params.get(key);
|
try {
|
paramsList.add(key + "=" + URLDecoder.decode(value, "UTF-8"));
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
}
|
return url + "?" + StringUtil.concat(paramsList, "&");
|
}
|
|
private static TencentWebVideoInfo parseListItem(com.alibaba.fastjson.JSONObject item) {
|
TencentWebVideoInfo videoInfo = new TencentWebVideoInfo();
|
videoInfo.setId(item.getString("cid"));
|
videoInfo.setPicture(item.getString("new_pic_vt"));
|
videoInfo.setPlayUrl(String.format("https://v.qq.com/x/cover/%s.html", videoInfo.getId()));
|
videoInfo.setTitle(item.getString("title"));
|
videoInfo.setDuration("");
|
if (item.getInteger("type") != 1) {
|
videoInfo.setTag(item.getString("timelong"));
|
} else {
|
JSONObject imgTag = JSONObject.fromObject(item.getString("uni_imgtag"));
|
for (Object key : imgTag.keySet()) {
|
JSONObject imgTagItem = imgTag.optJSONObject(key.toString());
|
if (imgTagItem.optInt("id") == 28) {
|
videoInfo.setTag(imgTagItem.optString("text"));
|
}
|
}
|
if(videoInfo.getTag()==null){
|
videoInfo.setTag("9.0分");
|
}
|
}
|
videoInfo.setEpsodePubtime(item.getString("epsode_pubtime"));
|
return videoInfo;
|
}
|
|
|
public static void main(String[] args) throws Exception {
|
|
List<TencentWebVideoInfo> list = getVideoList(100173,0, 100028);
|
System.out.println(list.size());
|
|
}
|
}
|