package com.yeshi.buwan.tencent;
|
|
import com.google.gson.Gson;
|
import com.google.gson.reflect.TypeToken;
|
import com.yeshi.buwan.tencent.entity.TencentCoverInfo;
|
import net.sf.json.JSONObject;
|
import org.jsoup.Jsoup;
|
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Element;
|
import org.jsoup.select.Elements;
|
import org.yeshi.utils.HttpUtil;
|
|
import javax.script.Invocable;
|
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngineManager;
|
import javax.script.ScriptException;
|
import java.io.IOException;
|
import java.lang.reflect.Type;
|
import java.util.*;
|
|
public class TencentVideoUtil {
|
|
static ScriptEngine jsEngine = null;
|
|
// static {
|
// if (jdGoodsJs == null)
|
// jdGoodsJs = getJDGoodsJS();
|
// ScriptEngineManager manager = new ScriptEngineManager();
|
// jsEngine = manager.getEngineByName("javascript");
|
// try {
|
// jsEngine.eval(jdGoodsJs);
|
// } catch (ScriptException e) {
|
// e.printStackTrace();
|
// }
|
//
|
// if (jsEngine instanceof Invocable) {
|
// Invocable in = (Invocable) jsEngine;
|
// Object goodsId = in.invokeFunction("getGoodsId", link);
|
// if (goodsId != null)
|
// return goodsId.toString().trim();
|
// }
|
//
|
// }
|
|
|
private static List<String> objToArray(JSONObject root, String key) {
|
|
if (root.optJSONObject(key) == null) {
|
return null;
|
}
|
|
Gson gson = new Gson();
|
|
List<String> directors = new ArrayList<>();
|
if (root.optJSONObject(key).isArray()) {
|
Type type = new TypeToken<List<String>>() {
|
}.getType();
|
directors = gson.fromJson(root.optJSONArray(key).toString(), type);
|
} else {
|
JSONObject director = root.optJSONObject(key);
|
for (Iterator<String> its = director.keys(); its.hasNext(); ) {
|
String p = its.next();
|
directors.add(director.optString(p));
|
}
|
}
|
|
root.remove(key);
|
return directors;
|
}
|
|
public static TencentCoverInfo getCoverInfo(String url) throws Exception {
|
String script = null;
|
Document doc = Jsoup.connect(url).timeout(10000).userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36").get();
|
Elements els = doc.getElementsByTag("script");
|
for (int i = 0; i < els.size(); i++) {
|
if (els.get(i).html().indexOf("var COVER_INFO") > -1) {
|
script = els.get(i).html();
|
break;
|
}
|
}
|
|
script = script.replace("window.", "_window.");
|
script = " var _window={};" + "var document={getElementsByTagName:function(tag){return [''];}};" + script;
|
script += "\n function getCoverInfo(){return COVER_INFO }; function getVideoInfo(){return VIDEO_INFO}";
|
|
ScriptEngineManager manager = new ScriptEngineManager();
|
jsEngine = manager.getEngineByName("javascript");
|
try {
|
jsEngine.eval(script);
|
} catch (ScriptException e) {
|
e.printStackTrace();
|
}
|
|
Gson gson = new Gson();
|
if (jsEngine instanceof Invocable) {
|
Invocable in = (Invocable) jsEngine;
|
Object coverInfo = in.invokeFunction("getCoverInfo");
|
JSONObject root = JSONObject.fromObject(gson.toJson(coverInfo));
|
System.out.println(root.toString());
|
List<TencentCoverInfo.VipIdsBean> vipIdsBeans = new ArrayList<>();
|
|
if (root.optJSONObject("vip_ids").isArray()) {
|
Type type = new TypeToken<List<TencentCoverInfo.VipIdsBean>>() {
|
}.getType();
|
vipIdsBeans = gson.fromJson(root.optJSONArray("vip_ids").toString(), type);
|
} else {
|
JSONObject vipIds = root.optJSONObject("vip_ids");
|
for (Iterator<String> its = vipIds.keys(); its.hasNext(); ) {
|
String p = its.next();
|
TencentCoverInfo.VipIdsBean idsBean = gson.fromJson(vipIds.optJSONObject(p).toString(), TencentCoverInfo.VipIdsBean.class);
|
idsBean.setP(Integer.parseInt(p));
|
vipIdsBeans.add(idsBean);
|
}
|
}
|
root.remove("vip_ids");
|
|
try {
|
List<String> subTypes = objToArray(root, "subtype");
|
List<String> directors = objToArray(root, "director");
|
List<String> leadingActor = objToArray(root, "leading_actor");
|
List<String> subGenre = objToArray(root, "sub_genre");
|
|
|
TencentCoverInfo coverInfoBean = gson.fromJson(root.toString(), TencentCoverInfo.class);
|
coverInfoBean.setVip_ids(vipIdsBeans);
|
coverInfoBean.setSubtype(subTypes);
|
coverInfoBean.setDirector(directors);
|
coverInfoBean.setLeading_actor(leadingActor);
|
coverInfoBean.setSub_genre(subGenre);
|
return coverInfoBean;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
// System.out.println(coverInfoBean);
|
}
|
return null;
|
}
|
|
|
private static void parseList(String url) {
|
try {
|
Document document = Jsoup.connect(url).timeout(5000).get();
|
Elements els = document.getElementsByAttributeValue("class", "list_item");
|
for (int i = 0; i < els.size(); i++) {
|
String href = els.get(i).getElementsByTag("a").get(0).attr("href");
|
String img = els.get(i).getElementsByTag("img").get(0).attr("src");
|
img = img.startsWith("http") ? img : "http:" + img;
|
try {
|
TencentCoverInfo coverInfo = getCoverInfo(href);
|
System.out.println(coverInfo);
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
|
}
|
|
|
public static void main(String[] args) {
|
// try {
|
// TencentCoverInfo info = getCoverInfo("https://v.qq.com/x/cover/mzc00200fw94jce.html");
|
//
|
// info = getCoverInfo("https://v.qq.com/x/cover/mzc00200s0ii272/r0036xibgw2.html");
|
// info = getCoverInfo("https://v.qq.com/x/cover/ylgl3m6wo0sypou/w0036x9c5c7.html");
|
// info = getCoverInfo("https://v.qq.com/x/cover/ylgl3m6wo0sypou/w0036x9c5c7.html");
|
// //
|
// info = getCoverInfo("https://v.qq.com/x/cover/mzc00200tlv15ub.html");
|
// System.out.println(info);
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
|
// }
|
int page = 1;
|
parseList(String.format("https://v.qq.com/x/bu/pagesheet/list?_all=1&append=1&channel=cartoon&ipay=2&listpage=%s&offset=%s&pagesize=30&sort=18", page, (page - 1) * 30));
|
|
}
|
|
|
}
|