package com.yeshi.buwan.videos.bilibili;
|
|
import com.google.gson.Gson;
|
import com.yeshi.buwan.util.HttpUtil;
|
import com.yeshi.buwan.videos.bilibili.entity.BilibiliMediaInfo;
|
import com.yeshi.buwan.videos.bilibili.entity.BilibiliVideo;
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
import org.jsoup.Jsoup;
|
import org.jsoup.nodes.Document;
|
import org.jsoup.select.Elements;
|
|
import javax.script.Invocable;
|
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngineManager;
|
import javax.script.ScriptException;
|
import java.io.IOException;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
public class BilibiliApiUtil {
|
|
public static BilibiliVideo parseShortVideo(String url) throws Exception {
|
String result = parsePageData(url);
|
System.out.println(result);
|
JSONObject root = JSONObject.fromObject(result);
|
BilibiliVideo video = parseVideo(root);
|
return video;
|
}
|
|
|
public static BilibiliMediaInfo parseMediaInfo(String url) throws Exception {
|
String result = parsePageData(url);
|
System.out.println(result);
|
JSONObject root = JSONObject.fromObject(result);
|
BilibiliMediaInfo mediaInfo = parseMedia(root);
|
return mediaInfo;
|
}
|
|
/**
|
* 网页链接:https://www.bilibili.com/guochuang/index/
|
* @param type
|
* @param order 3-追番人数
|
* @param page 0-最近更新
|
* @return
|
*/
|
public static MediaUrlResult getMediaList(int type, int order, int page) {
|
String url = String.format("https://api.bilibili.com/pgc/season/index/result?style_id=-1&producer_id=-1&release_date=-1&season_status=-1&order=%s&st=%s&sort=0&page=%s&season_type=%s&pagesize=20&type=1", order, type, page, type);
|
String result = HttpUtil.get(url);
|
JSONObject resultJSON = JSONObject.fromObject(result);
|
if (resultJSON.optInt("code") == 0) {
|
JSONObject data = resultJSON.optJSONObject("data");
|
int total = data.optInt("total");
|
JSONArray list = data.optJSONArray("list");
|
List<String> urlList = new ArrayList<>();
|
for (int i = 0; i < list.size(); i++) {
|
JSONObject item = list.optJSONObject(i);
|
urlList.add(item.optString("link"));
|
}
|
return new MediaUrlResult(total, urlList);
|
}
|
return null;
|
}
|
|
|
private static String getPageData(String scriptContent) throws ScriptException, NoSuchMethodException {
|
String script = "var _window={};var _document={scripts:[{parentNode:{removeChild:function(){}}}]};" + scriptContent.replace("window.", "_window.").replace("document.", "_document.");
|
script += ";function getData(){return JSON.stringify(_window.__INITIAL_STATE__);}";
|
ScriptEngineManager manager = new ScriptEngineManager();
|
ScriptEngine 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 data = in.invokeFunction("getData");
|
return data.toString();
|
}
|
return null;
|
}
|
|
private static BilibiliMediaInfo parseMedia(JSONObject root) {
|
if (root.optJSONObject("mediaInfo") != null) {
|
JSONObject mediaJson = root.optJSONObject("mediaInfo");
|
mediaJson.put("mediaRating", root.optJSONObject("mediaRating"));
|
mediaJson.put("newestEp", root.optJSONObject("newestEp"));
|
mediaJson.put("epList", root.optJSONArray("epList"));
|
mediaJson.put("pubInfo", root.optJSONObject("pubInfo"));
|
return new Gson().fromJson(mediaJson.toString(), BilibiliMediaInfo.class);
|
}
|
return null;
|
}
|
|
private static BilibiliVideo parseVideo(JSONObject root) {
|
if (root.optJSONObject("video") != null) {
|
JSONObject videoJson = root.optJSONObject("video");
|
if (videoJson != null) {
|
videoJson = videoJson.optJSONObject("viewInfo");
|
}
|
return new Gson().fromJson(videoJson.toString(), BilibiliVideo.class);
|
}
|
return null;
|
}
|
|
private static String parsePageData(String url) throws ScriptException, NoSuchMethodException, IOException {
|
Map<String,String> headers=new HashMap<>();
|
headers.put("User-Agent","Dalvik/2.1.0 (Linux; U; Android 9; MI 8 Lite MIUI/V10.2.3.0.PDTCNXM)");
|
String result_str = org.yeshi.utils.HttpUtil.get(url,new HashMap<>(),headers);
|
int start_index = result_str.indexOf("window.__INITIAL_STATE__=");
|
result_str= result_str.substring(start_index);
|
int endIndex = result_str.indexOf( "</script>");
|
|
String script = result_str.substring(0,endIndex);
|
String result = getPageData(script);
|
return result;
|
|
//
|
// Document doc = Jsoup.connect(url).userAgent("").timeout(30000).get();
|
// Elements els = doc.getElementsByTag("script");
|
// for (int i = 0; i < els.size(); i++) {
|
// if (els.get(i).html().indexOf("window.__INITIAL_STATE__=") > -1) {
|
// String script = els.get(i).html();
|
// System.out.println(script);
|
// String result = getPageData(script);
|
// return result;
|
// }
|
// }
|
// return null;
|
}
|
|
|
public static void main(String[] args) throws Exception {
|
MediaUrlResult result = getMediaList(BilibiliUtil.TYPE_GUOMAN, 3, 1);
|
// parseMediaInfo("https://www.bilibili.com/bangumi/play/ss28747");
|
// System.out.println(result);
|
}
|
|
public static class MediaUrlResult {
|
private int total;
|
private List<String> result;
|
|
public MediaUrlResult(int total, List<String> result) {
|
this.total = total;
|
this.result = result;
|
}
|
|
public int getTotal() {
|
return total;
|
}
|
|
public void setTotal(int total) {
|
this.total = total;
|
}
|
|
public List<String> getResult() {
|
return result;
|
}
|
|
public void setResult(List<String> result) {
|
this.result = result;
|
}
|
}
|
|
}
|