admin
2021-08-27 8fee151ffae0c3818694b7318583814bf92663e2
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
package com.yeshi.buwan.util.video.web;
 
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 org.yeshi.utils.StringUtil;
 
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;
 
public class TencentWebUtil {
 
    public static class TencentWebVideoInfo {
        private String playUrl;
        private String id;
        private String title;
        private String picture;
        private String duration;
 
        public String getPlayUrl() {
            return playUrl;
        }
 
        public void setPlayUrl(String playUrl) {
            this.playUrl = playUrl;
        }
 
        public String getId() {
            return id;
        }
 
        public void setId(String id) {
            this.id = id;
        }
 
        public String getTitle() {
            return title;
        }
 
        public void setTitle(String title) {
            this.title = title;
        }
 
        public String getPicture() {
            return picture;
        }
 
        public void setPicture(String picture) {
            this.picture = picture;
        }
 
        public String getDuration() {
            return duration;
        }
 
        public void setDuration(String duration) {
            this.duration = duration;
        }
    }
 
 
    public static Map<String, String> parseParams(String url) {
        //清除参数
        String param = null;
        if (url.indexOf("?") > -1) {
            param = url.substring(url.indexOf("?"));
        }
        if (!url.contains("v.qq.com")) {
            return null;
        }
        String[] params = param.split("&");
        Map<String, String> map = new HashMap<>();
        for (String p : params) {
            map.put(p.split("=")[0], URLDecoder.decode(p.split("=")[1]));
        }
        return map;
    }
 
 
    /**
     * 获取短视频列表
     *
     * @param params
     * @param page
     * @return
     * @throws Exception
     */
    public static List<TencentWebVideoInfo> getVideoList(Map<String, String> params, int page) throws Exception {
        if (params == null)
            throw new Exception("参数为空");
        int pageSize = 30;
        params.put("append", "1");
        params.put("listpage", page + "");
        params.put("offset", (page - 1) * pageSize + "");
        params.put("pagesize", pageSize + "");
 
        String url = "https://v.qq.com/x/bu/pagesheet/list";
        Map<String, String> headers = new HashMap<>();
        headers.put("referer", "https://v.qq.com/channel/ent");
        headers.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36");
        String result = HttpUtil.get(url, params, headers);
        Document document = Jsoup.parse(result);
        Elements els = document.getElementsByClass("list_item");
        return parseVideoList(els);
    }
 
 
    public static List<TencentWebVideoInfo> getVideoList(String url) throws Exception {
        Map<String, String> headers = new HashMap<>();
        headers.put("referer", "https://v.qq.com/channel/ent");
        headers.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36");
        String result = HttpUtil.get(url, new HashMap<>(), headers);
        Document document = Jsoup.parse(result);
        Elements els = document.getElementsByClass("list_item");
        return parseVideoList(els);
    }
 
    private static List<TencentWebVideoInfo> parseVideoList(Elements els) throws UnsupportedEncodingException {
        List<TencentWebVideoInfo> list = new ArrayList<>();
        for (int i = 0; i < els.size(); i++) {
            Element ele = els.get(i);
            String href = ele.getElementsByTag("a").get(0).attr("href");
            String id = ele.getElementsByTag("a").get(0).attr("data-float");
            String title = ele.getElementsByTag("a").get(0).attr("title");
            title = new String(title.getBytes("ISO-8859-1"), "UTF-8");
            String picture = ele.getElementsByTag("img").get(0).attr("src");
            picture = picture.startsWith("http") ? picture : "https:" + picture;
            String duration = null;
            try {
                duration = ele.getElementsByClass("figure_caption").get(0).ownText();
                duration = duration.trim();
                for (int j = 0; j < duration.length(); j++) {
                    char ca = duration.charAt(j);
                    if (!(ca >= 48 && ca < 59)) {
                        duration = null;
                        break;
                    }
                }
                System.out.println(duration);
            } catch (Exception e) {
            }
            TencentWebVideoInfo videoInfo = new TencentWebVideoInfo();
            videoInfo.setDuration(duration);
            videoInfo.setId(id);
            videoInfo.setPicture(picture);
            videoInfo.setPlayUrl(href);
            videoInfo.setTitle(title);
            list.add(videoInfo);
        }
        return list;
    }
 
    public static String getApiUrl(String webUrl, int page) {
        Map<String, String> params = parseParams(webUrl);
        int pageSize = 30;
        params.put("append", "1");
        params.put("listpage", page + "");
        params.put("offset", (page - 1) * pageSize + "");
        params.put("pagesize", pageSize + "");
        String url = "https://v.qq.com/x/bu/pagesheet/list";
        List<String> paramsList = new ArrayList<>();
        for (Iterator<String> its = params.keySet().iterator(); its.hasNext(); ) {
            String key = its.next();
            String value = params.get(key);
            try {
                paramsList.add(key + "=" + URLDecoder.decode(value, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return url + "?" + StringUtil.concat(paramsList, "&");
    }
 
 
    public static void main(String[] args) throws Exception {
        List<TencentWebVideoInfo> videoInfos = getVideoList(parseParams("https://v.qq.com/channel/ent?_all=1&channel=ent&iarea=2&itype=-1&listpage=1&sort=40"), 1);
        System.out.println(videoInfos);
    }
}