admin
2024-09-05 ab35ac8b769b2d9816dffb33a64f2c6f7bd5dd6e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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;
        }
    }
 
}