admin
2021-03-24 c44a4296e8913080bff7ca0ee1b65356ed34ec3d
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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));
 
    }
 
 
}