admin
2025-02-20 f537abe9f3646c739beaf15076246a2f71a347e9
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
package com.yeshi.buwan.util.video.web;
 
import com.yeshi.buwan.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.yeshi.utils.HttpUtil;
 
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class IqiyiWebUtil {
 
    /**
     * 网页列表中的请求参数
     */
    private static class IqiyiWebVideoListParams {
        private int channelId;
        private int dataType;
        private int mode;
        private String threeCategoryId;
 
        public int getChannelId() {
            return channelId;
        }
 
        public void setChannelId(int channelId) {
            this.channelId = channelId;
        }
 
        public int getDataType() {
            return dataType;
        }
 
        public void setDataType(int dataType) {
            this.dataType = dataType;
        }
 
        public int getMode() {
            return mode;
        }
 
        public void setMode(int mode) {
            this.mode = mode;
        }
 
        public String getThreeCategoryId() {
            return threeCategoryId;
        }
 
        public void setThreeCategoryId(String threeCategoryId) {
            this.threeCategoryId = threeCategoryId;
        }
    }
 
 
    public static IqiyiWebVideoListParams parseParams(String url) {
        //清除参数
        if (url.indexOf("?") > -1) {
            url = url.substring(0, url.indexOf("?"));
        }
        url = url.replace("https://", "").replace("http://", "");
        String[] urls = url.split("/");
        String host = urls[0];
        if (!host.contains(".iqiyi.com")) {
            return null;
        }
        IqiyiWebVideoListParams params = new IqiyiWebVideoListParams();
        List<String> threeCategoryIds = new ArrayList<>();
        String channelId = urls[2];
        String otherParams = urls[3];
        String[] sts = otherParams.split("-------------");
        if (sts.length > 1) {
            String threeCategoryId = sts[0];
            if (!StringUtil.isNullOrEmpty(threeCategoryId)) {
                String[] categorys = threeCategoryId.split("-");
                for (String cate : categorys) {
                    threeCategoryIds.add(cate + ";must");
                }
            }
            sts = sts[1].split("-");
            String mode = sts[0];
            String dataType = sts[2];
            params.setMode(Integer.parseInt(mode));
            params.setDataType(Integer.parseInt(dataType));
        }
        if (threeCategoryIds.size() > 0) {
            params.setThreeCategoryId(org.yeshi.utils.StringUtil.concat(threeCategoryIds, ","));
        }
        params.setChannelId(Integer.parseInt(channelId));
        return params;
    }
 
 
    public static List<Long> getVideoTvidList(IqiyiWebVideoListParams params, int page) throws Exception {
        if (params == null)
            return null;
        String url = "https://pcw-api.iqiyi.com/search/recommend/list?";
        List<String> paramsList = new ArrayList<>();
        paramsList.add("channel_id=" + params.getChannelId());
        paramsList.add("data_type=" + params.getDataType());
        paramsList.add("mode=" + params.getMode());
        paramsList.add("page_id=" + page);
        paramsList.add("ret_num=48");
        if (params.getThreeCategoryId() != null) {
            paramsList.add("three_category_id=" + URLEncoder.encode(params.getThreeCategoryId()));
        }
 
        url += org.yeshi.utils.StringUtil.concat(paramsList, "&");
        Map<String, String> headers = new HashMap<>();
        headers.put("referer", "https://list.iqiyi.com/");
        headers.put("origin", "https://list.iqiyi.com");
        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);
        JSONObject resultJSON = JSONObject.fromObject(result);
 
        List<Long> tvIds = new ArrayList<>();
        if (resultJSON.optString("code").equalsIgnoreCase("A00000")) {
            JSONObject data = resultJSON.optJSONObject("data");
            JSONArray list = data.optJSONArray("list");
            for (int i = 0; i < list.size(); i++) {
                String tvId = list.optJSONObject(i).optString("tvId");
                tvIds.add(Long.parseLong(tvId));
            }
        } else
            throw new Exception(resultJSON.optString("data"));
        return tvIds;
    }
 
 
    public static void main(String[] args) throws Exception {
        List<Long> list = getVideoTvidList(parseParams("https://list.iqiyi.com/www/7/186-------------24-1-2-iqiyi--.html?vfrm=pcw_yule&vfrmblk=C&vfrmrst="), 1);
        System.out.println(list);
    }
}