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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.yeshi.buwan.videos.youku;
 
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.buwan.videos.youku.entity.YouKuShowDetail;
import com.yeshi.buwan.videos.youku.entity.YouKuShowSimple;
import com.yeshi.buwan.videos.youku.entity.YouKuVideo;
import org.json.JSONArray;
import org.json.JSONObject;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.StringUtil;
 
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URLEncoder;
import java.util.*;
 
public class YouKuApiUtil {
 
    private final static String APP_ID = "758bef946d0050ef";
    private final static String APP_SECRET = "e113a131d49e1f837402e2807f2daf58";
 
 
    private static String request(String action, Map<String, String> ps) {
        Map<String, String> params = new HashMap<>();
        params.put("action", action);
        params.put("client_id", APP_ID);
        params.put("format", "json");
        params.put("timestamp", System.currentTimeMillis() / 1000 + "");
        params.put("version", "3.0");
        params.put("format", "json");
        params.putAll(ps);
 
 
        List<String> list = new ArrayList<>();
        for (Iterator<String> its = params.keySet().iterator(); its.hasNext(); ) {
            String key = its.next();
            try {
                list.add(key + URLEncoder.encode(params.get(key), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
 
        Collections.sort(list);
 
        String sign = StringUtil.Md5(StringUtil.concat(list, "") + APP_SECRET);
        params.put("sign", sign);
        ps.put("opensysparams", new JSONObject(params).toString());
 
        return HttpUtil.get("https://openapi.youku.com/router/rest.json", ps, new HashMap<>());
    }
 
    public static void getSearchRank(String channel, int count) {
        Map<String, String> params = new HashMap<>();
        params.put("limit", count + "");
        if (channel != null) {
            params.put("channel", channel);
        }
        System.out.println(request("youku.search.keyword.rankinglist", params));
    }
 
    /**
     * 获取节目详情
     *
     * @param id
     * @return
     */
    public static YouKuShowDetail getShowDetail(String id) {
        String url = String.format("https://openapi.youku.com/v2/shows/show.json?client_id=%s&show_id=%s", APP_ID, id);
        String result = HttpUtil.get(url);
        return new Gson().fromJson(result, YouKuShowDetail.class);
    }
 
    /**
     * 根据分类拉取数据
     *
     * @param category
     * @param page
     */
    public static ListResultDTO getShowListByCategory(String category, int page, int pageSize) {
 
        try {
            String url = String.format("https://openapi.youku.com/v2/shows/by_category.json?client_id=%s&paid=1&category=%s&page=%s&count=" + pageSize, APP_ID, URLEncoder.encode(category, "UTF-8"), page);
            String result = HttpUtil.get(url);
            net.sf.json.JSONObject resultJson = net.sf.json.JSONObject.fromObject(result);
            int total = resultJson.optInt("total");
            Type type = new TypeToken<List<YouKuShowSimple>>() {
            }.getType();
            List<YouKuShowSimple> list = new Gson().fromJson(resultJson.optJSONArray("shows").toString(), type);
            return new ListResultDTO(list, total);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
 
        return null;
    }
 
    /**
     * @return com.yeshi.buwan.videos.youku.YouKuApiUtil.ListResultDTO
     * @author hxh
     * @description 获取热门的专辑
     * @date 15:26 2024/3/21
     * @param: category
     * @param: page
     * @param: pageSize
     **/
    public static ListResultDTO getHotShowListByCategory(String category, int page, int pageSize) throws UnsupportedEncodingException {
 
        String seesion = "{\"subIndex\":96,\"trackInfo\":{\"parentdrawerid\":\"34441\"},\"spmA\":\"a2h05\",\"level\":2,\"spmC\":\"drawer3\",\"spmB\":\"8165803_SHAIXUAN_ALL\",\"index\":1,\"pageName\":\"page_channelmain_SHAIXUAN_ALL\",\"scene\":\"search_component_paging\",\"scmB\":\"manual\",\"path\":\"EP782932\",\"scmA\":\"20140719\",\"scmC\":\"34441\",\"from\":\"SHAIXUAN\",\"id\":227939,\"category\":\"电视剧\"}";
        net.sf.json.JSONObject sessionJSON = net.sf.json.JSONObject.fromObject(seesion);
        sessionJSON.put("subIndex", (page - 1) * pageSize);
        sessionJSON.put("category", category);
        String url = String.format("https://www.youku.com/category/data?session=%s&params=%s&pageNo=%s", URLEncoder.encode(sessionJSON.toString(), "UTF-8"), URLEncoder.encode("{\"type\":\"" + category + "\"}", "UTF-8"), page);
        Map<String,String> headers=new HashMap<>();
        headers.put("Accept","text/javascript, text/html, application/xml, text/xml, */*");
        headers.put("Bx-V","2.5.11");
        headers.put("Content-Type","application/x-www-form-urlencoded");
        headers.put("Referer","https://www.youku.com/channel/webtv/list?");
 
        headers.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36");
        headers.put("Sec-Ch-Ua","\"Chromium\";v=\"122\", \"Not(A:Brand\";v=\"24\", \"Google Chrome\";v=\"122\"");
        headers.put("Sec-Ch-Ua-Mobile","?0");
        headers.put("Sec-Ch-Ua-Platform","\"Windows\"");
        headers.put("Sec-Fetch-Dest","empty");
        headers.put("Sec-Fetch-Mode","cors");
        headers.put("Sec-Fetch-Site","same-origin");
 
        String result = HttpUtil.get(url,new HashMap<>(),headers);
        net.sf.json.JSONObject resultJSON =    net.sf.json.JSONObject.fromObject(result);
        List<YouKuShowSimple> list =new ArrayList<>();
        if(resultJSON.optBoolean("success")){
            net.sf.json.JSONArray array = resultJSON.optJSONObject("data").optJSONObject("filterData").optJSONArray("listData");
            for(int i=0;i<array.size();i++){
                net.sf.json.JSONObject item =  array.optJSONObject(i);
                YouKuShowSimple show=new YouKuShowSimple();
                show.setPoster(item.optString("img"));
                show.setName(item.optString("title"));
                show.setLink(item.optString("videoLink"));
                URI uri=URI.create(show.getLink());
                show.setId(uri.getQuery().replace("s=","").trim());
                list.add(show);
            }
        }
        return new ListResultDTO(list, 100);
    }
 
    public static ListResultDTO getVideoList(String showId, int page, int pageSize) {
        Map<String, String> params = new HashMap<>();
        params.put("show_id", showId);
        params.put("show_videotype", "正片");
        params.put("page", page + "");
        params.put("pageLength", pageSize + "");
        String result = YouKuApiUtil.request("youku.api.video.byprogram.get", params);
        System.out.println(result);
        net.sf.json.JSONObject resultJson = net.sf.json.JSONObject.fromObject(result);
        int total = resultJson.optInt("total");
        Type type = new TypeToken<List<YouKuVideo>>() {
        }.getType();
 
        List<YouKuVideo> videoList = new Gson().fromJson(resultJson.optJSONArray("videos").toString(), type);
        if (videoList != null)
            for (YouKuVideo video : videoList) {
                video.setShowId(showId);
            }
        return new ListResultDTO(videoList, total);
    }
 
 
    public static void main(String[] args) throws  Exception{
 
        getSearchRank("电视剧",10);
//        ListResultDTO dto = getShowListByCategory("电视剧",1,20);
//        System.out.printf(dto.toString());
//        getHotShowListByCategory("电视剧",1,24);
 
//        YouKuShowDetail detail = getShowDetail("ffaac0420f0042b9b1e1");
//        System.out.println(detail);
//        getVideoList("0a17e614d7e311e68ce4", 1, 20);
 
//        ListResultDTO resultDTO = getShowListByCategory("电视剧", 1);
 
//        System.out.println(resultDTO);
    }
 
 
    public static class ListResultDTO {
 
        private List list;
        private int total;
 
        public ListResultDTO(List list, int total) {
            this.list = list;
            this.total = total;
        }
 
        public List getList() {
            return list;
        }
 
        public void setList(List list) {
            this.list = list;
        }
 
        public int getTotal() {
            return total;
        }
 
        public void setTotal(int total) {
            this.total = total;
        }
    }
 
 
}