admin
2025-02-20 f537abe9f3646c739beaf15076246a2f71a347e9
src/main/java/com/yeshi/buwan/videos/bilibili/BilibiliApiUtil.java
@@ -1,8 +1,10 @@
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;
@@ -13,6 +15,10 @@
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 {
@@ -31,6 +37,31 @@
        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;
    }
@@ -60,6 +91,7 @@
            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;
@@ -77,33 +109,62 @@
    }
    private static String parsePageData(String url) throws ScriptException, NoSuchMethodException, IOException {
        Document doc = Jsoup.connect(url).userAgent("Dalvik/2.1.0 (Linux; U; Android 9; MI 8 Lite MIUI/V10.2.3.0.PDTCNXM)").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;
      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 IOException, ScriptException, NoSuchMethodException {
    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);
    }
        String[] urls = new String[]{
                "https://www.bilibili.com/bangumi/play/ss38129?theme=movie&from_spmid=666.7", "https://www.bilibili.com/bangumi/play/ss38611?theme=movie&from_spmid=666.7"
        };
        //3-记录片 5-电视剧  4-国漫 1-番剧  2-电影
        try {
            for (String url : urls) {
                BilibiliMediaInfo mediaInfo = parseMediaInfo(url);
                System.out.println("type:" + mediaInfo.getType());
            }
        } catch (Exception e) {
            e.printStackTrace();
    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;
        }
    }
}