admin
2021-01-25 d182390205a9828bd1091b06fa712e028004c687
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package com.newvideo.youku;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.newvideo.youku.entity.Program;
import com.newvideo.youku.entity.ProgramVideo;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
public class YouKuApi {
 
    public static final String KEY = "758bef946d0050ef";// 758bef946d0050ef
    public static final String Secret = "e113a131d49e1f837402e2807f2daf58";
 
    static String URL_VIDEOCATEGORY = "https://openapi.youku.com/v2/schemas/video/category.json";
    static String URL_PROGRAMCATEGORY = "https://openapi.youku.com/v2/schemas/show/category.json";
    static String URL_VIDEO_DETAIL = "https://openapi.youku.com/v2/videos/show.json";// 获取视频详情
 
    static String URL_PAY_DETAIL = "https://openapi.youku.com/v2/shows/show_premium.json";// 节目付费详情
    static String URL_PROGRAMLIST = "https://openapi.youku.com/v2/shows/by_category.json";// 获取节目列表
    static String URL_PROGRAMVIDEO = "https://openapi.youku.com/v2/shows/videos.json";// 根据节目获取视频
    static String URL_VIDEOLIST = "https://openapi.youku.com/v2/videos/by_category.json";// 根据分类获取视频
    static String URL_PROGRAME_DETAIL = "https://openapi.youku.com/v2/shows/show.json";// 获取节目详情
    static String URL_PROGR = "https://openapi.youku.com/v2/shows/show.json";// 获取节目详情
    static String URL_ALBUMVIDEO = "https://openapi.youku.com/v2/playlists/videos.json";// 根据专辑获取视频
    static String URL_ALBUMDETAIL = "https://openapi.youku.com/v2/playlists/show.json";// 专辑详细信息
 
    /**
     * 视频类接口
     * 
     * @return
     */
 
    // 获取视频分类列表
    public static List<YouKuCategory> getVideoCategory() {
        String result = get(URL_VIDEOCATEGORY);
        JSONObject obj = JSONObject.fromObject(result);
        JSONArray array = obj.optJSONArray("categories");
        List<YouKuCategory> list = new ArrayList<YouKuCategory>();
        Gson gson = new GsonBuilder().setVersion(1.0).create();
        for (int i = 0; i < array.size(); i++) {
            YouKuCategory vc = gson.fromJson(array.optJSONObject(i).toString(), YouKuCategory.class);
            JSONArray ga = array.optJSONObject(i).optJSONArray("genres");
            List<YouKuCategory> genreList = new ArrayList<YouKuCategory>();
            for (int j = 0; j < ga.size(); j++) {
                genreList.add(gson.fromJson(ga.optJSONObject(j).toString(), YouKuCategory.class));
            }
            vc.setGenre(genreList);
            list.add(vc);
        }
        return list;
    }
 
    // 根据分类获取视频
    public static Map<String, Object> getVideoListByCategory(String cate, String genre, int page)
            throws UnsupportedEncodingException {
        String url = String.format(
                URL_VIDEOLIST + "?client_id=%s&category=%s&count=50&genre=%s&orderby=published&period=%s&page=" + page,
                KEY, URLEncoder.encode(cate, "UTF-8"), URLEncoder.encode(genre, "UTF-8"),
                cate.contains("资讯") ? "today" : "week");
        String result = get(url);
        JSONObject object = JSONObject.fromObject(result);
        int total = object.optInt("total");
        JSONArray array = object.optJSONArray("videos");
        Gson gson = new GsonBuilder().setVersion(1.0).create();
        List<ProgramVideo> list = new ArrayList<ProgramVideo>();
        for (int i = 0; i < array.size(); i++) {
            try {
                ProgramVideo pv = gson.fromJson(array.optJSONObject(i).toString(), ProgramVideo.class);
                list.add(pv);
            } catch (Exception e) {
            }
        }
        Map<String, Object> map = new HashMap<String, Object>();
 
        map.put("total", total);
        map.put("data", list);
        return map;
    }
 
    // 根据分类获取视频
    public static Map<String, Object> getZiXunVideoList(String cate, int page) throws UnsupportedEncodingException {
        String url = String.format(
                URL_VIDEOLIST + "?client_id=%s&category=%s&count=50&orderby=published&period=%s&page=" + page, KEY,
                URLEncoder.encode(cate, "UTF-8"), cate.contains("资讯") ? "today" : "month");
        String result = get(url);
        JSONObject object = JSONObject.fromObject(result);
        int total = object.optInt("total");
        JSONArray array = object.optJSONArray("videos");
        Gson gson = new GsonBuilder().setVersion(1.0).create();
        List<ProgramVideo> list = new ArrayList<ProgramVideo>();
        for (int i = 0; i < array.size(); i++) {
            try {
                ProgramVideo pv = gson.fromJson(array.optJSONObject(i).toString(), ProgramVideo.class);
                list.add(pv);
            } catch (Exception e) {
            }
        }
        Map<String, Object> map = new HashMap<String, Object>();
 
        map.put("total", total);
        map.put("data", list);
        return map;
    }
 
    // 获取视频详情
    public static ProgramVideo getVideoDetail(String id) {
        String url = String.format(URL_VIDEO_DETAIL + "?client_id=%s&video_id=%s", KEY, id);
        String result = get(url);
        Gson gson = new GsonBuilder().setVersion(1.0).create();
        ProgramVideo video = gson.fromJson(result, ProgramVideo.class);
        JSONObject obj = JSONObject.fromObject(result);
        JSONArray da = obj.optJSONArray("download_status");
        if (da != null) {
            String downloadL = "";
            for (int i = 0; i < da.size(); i++) {
                downloadL += da.optString(i) + ",";
            }
            if (downloadL.endsWith(","))
                downloadL = downloadL.substring(0, downloadL.length() - 1);
            video.setDownloadStatus(downloadL);
        }
 
        JSONArray ol = obj.optJSONArray("operation_limit");
        if (ol != null) {
            String oL = "";
            for (int i = 0; i < ol.size(); i++) {
                oL += da.optString(i) + ",";
            }
            if (oL.endsWith(","))
                oL = oL.substring(0, oL.length() - 1);
            video.setOperationLimit(oL);
        }
 
        return video;
    }
 
    public static boolean isBanQuan(String id) {
        String url = String.format(URL_VIDEO_DETAIL + "?client_id=%s&video_id=%s&ext=show", KEY, id);
        String result = get(url);
        JSONObject obj = JSONObject.fromObject(result);
        try {
            if ("正片".equalsIgnoreCase(obj.optJSONObject("show").optString("type")))
                return true;
            else
                return false;
        } catch (Exception e) {
            return false;
        }
    }
 
    /**
     * 节目类接口
     * 
     * @return
     */
 
    // 获取节目分类
    public static List<YouKuCategory> getProgramCategory() {
        String result = get(URL_PROGRAMCATEGORY);
        JSONObject obj = JSONObject.fromObject(result);
        JSONArray array = obj.optJSONArray("categories");
        List<YouKuCategory> list = new ArrayList<YouKuCategory>();
        Gson gson = new GsonBuilder().setVersion(1.0).create();
        for (int i = 0; i < array.size(); i++) {
            YouKuCategory vc = gson.fromJson(array.optJSONObject(i).toString(), YouKuCategory.class);
            list.add(vc);
        }
        return list;
    }
 
    // 获取节目列表
    public static Map<String, Object> getProgramList(String cName, String genre, int year, int page)
            throws UnsupportedEncodingException {
        Map<String, Object> map = new HashMap<String, Object>();
        String url = String.format(URL_PROGRAMLIST
                + "?client_id=%s&category=%s&paid=0&orderby=updated&page=%s&genre=%s&count=20&release_year=" + year,
                KEY, URLEncoder.encode(cName, "UTF-8"), page + "", URLEncoder.encode(genre, "UTF-8"));
        String result = get(url);
        JSONObject obj = JSONObject.fromObject(result);
        int total = obj.optInt("total");
        map.put("total", total);
        JSONArray array = obj.optJSONArray("shows");
        List<Program> list = new ArrayList<Program>();
        Gson gson = new GsonBuilder().setVersion(1.0).create();
        if (array != null)
            for (int i = 0; i < array.size(); i++) {
                Program pm = gson.fromJson(array.optJSONObject(i).toString(), Program.class);
                if (pm.getPaid() == 0)// 免费视频加入
                    list.add(pm);
            }
        else {
            System.out.println(result);
        }
 
        map.put("data", list);
        return map;
    }
 
    // 获取节目详情
    public static Program getProgramDetail(String id) throws UnsupportedEncodingException {
        Map<String, Object> map = new HashMap<String, Object>();
        String url = String.format(URL_PROGRAME_DETAIL + "?client_id=%s&show_id=%s", KEY, id);
        String result = get(url);
        JSONObject obj = JSONObject.fromObject(result);
        Gson gson = new GsonBuilder().setVersion(1.0).create();
        Program pm = gson.fromJson(obj.toString(), Program.class);
        pm.setAttrStr(obj.optJSONObject("attr") == null ? "" : obj.optJSONObject("attr").toString());
        return pm;
    }
 
    // 获取节目的视频
    public static Map<String, Object> getProgramVideo(String id, int page) {
        Map<String, Object> map = new HashMap<String, Object>();
        String url = String.format(URL_PROGRAMVIDEO + "?client_id=%s&show_id=%s&page=%s", KEY, id, page + "");
        String result = get(url);
        JSONObject object = JSONObject.fromObject(result);
        int total = object.optInt("total");
        JSONArray array = object.optJSONArray("videos");
        Gson gson = new GsonBuilder().setVersion(1.0).create();
        List<ProgramVideo> list = new ArrayList<ProgramVideo>();
        for (int i = 0; i < array.size(); i++) {
            ProgramVideo pv = gson.fromJson(array.optJSONObject(i).toString(), ProgramVideo.class);
            // if ("normal".equalsIgnoreCase(pv.getState()))
            list.add(pv);
        }
        map.put("count", total);
        map.put("data", list);
        return map;
    }
 
    // 获取节目的视频
    public static Map<String, Object> getProgramVideoV3(String id, int page) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, String> paramsMap = new HashMap<String, String>();
        // 系统参数
        paramsMap.put("action", "youku.content.video.byprogram.get");
        paramsMap.put("client_id", KEY);
        paramsMap.put("timestamp", System.currentTimeMillis() / 1000 + "");
        paramsMap.put("version", "3.0");
 
        // try {
        // paramsMap.put("sign",
        // YouKuSign.signApiRequest(paramsMap, Secret, "md5"));
        // } catch (Exception e) {
        //
        // }
        Iterator<String> its = paramsMap.keySet().iterator();
        JSONObject obj = new JSONObject();
        while (its.hasNext()) {
            String key = its.next();
            obj.put(key, paramsMap.get(key));
        }
 
        paramsMap.put("q", "show_id:" + id);
        paramsMap.put("page", page + "");
        paramsMap.put("show_videotype", "1");
 
        try {
            obj.put("sign", YouKuSign.signApiRequest(paramsMap, Secret, "md5"));
        } catch (Exception e) {
 
        }
 
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("opensysparams", obj);
        param.put("q", "show_id:" + id);
        param.put("page", page + "");
        param.put("show_videotype", "1");
 
        param.put("opensysparams", obj.toString());
 
        String result = post("http://openapi.youku.com/router/rest.json", param);
        // 业务参数
        System.out.println(result);
 
        JSONObject object = JSONObject.fromObject(result);
        int total = object.optInt("total");
        JSONArray array = object.optJSONArray("videos");
        Gson gson = new GsonBuilder().setVersion(1.0).create();
        List<ProgramVideo> list = new ArrayList<ProgramVideo>();
        for (int i = 0; i < array.size(); i++) {
            ProgramVideo pv = gson.fromJson(array.optJSONObject(i).toString(), ProgramVideo.class);
            // if ("normal".equalsIgnoreCase(pv.getState()))
            list.add(pv);
        }
        map.put("count", total);
        map.put("data", list);
        return map;
    }
 
    // 获取节目付费信息
 
    public static int getProgramNotPayCount(String showid) {
        String url = String.format(URL_PAY_DETAIL + "?client_id=%s&show_ids=%s", KEY, showid);
        String result = get(url);
        JSONObject object = JSONObject.fromObject(result);
        JSONArray shows = object.optJSONArray("shows");
        if (shows.size() > 0) {
            JSONObject obj = shows.optJSONObject(0);
            if (obj.optInt("paid") > 0) {
                return obj.optJSONObject("premium").optInt("try_episodes");
            }
        }
 
        return Integer.MAX_VALUE;
 
    }
 
    // 根据分类获取专辑
    public static void getAlbumListByCategory(String cateName, int page) throws UnsupportedEncodingException {
        String url = String.format(
                "https://openapi.youku.com/v2/playlists/by_category.json?client_id=%s&category=%s&page=%s", KEY,
                URLEncoder.encode(cateName, "UTF-8"), page);
        String result = get(url);
        JSONObject obj = JSONObject.fromObject(result);
    }
 
    // 获取专辑详情
    public static void getAlbumDetail(String aid) {
        String url = String.format(URL_ALBUMDETAIL + "?client_id=%s&playlist_id=%s", KEY, aid);
        String result = get(url);
        JSONObject obj = JSONObject.fromObject(result);
    }
 
    // 根据专辑获取视频
    public static void getVideoListByAlbum(String aid, int page) {
        String url = String.format(URL_ALBUMVIDEO + "?client_id=%s&playlist_id=%s&page=%s", KEY, aid, page);
        String result = get(url);
        JSONObject obj = JSONObject.fromObject(result);
    }
 
    @SuppressWarnings("deprecation")
    public static String get(String url) {
        HttpClient client = new HttpClient();
 
        // client.getHostConfiguration().setProxy("192.168.1.122", 8888);
 
        GetMethod getMethod = new GetMethod(url);
        try {
            client.executeMethod(getMethod);
            return getMethod.getResponseBodyAsString();
        } catch (Exception e) {
        }
        return "";
    }
 
    public static List<String> getMusicIdList(String genre, int page) {
        String url = String.format("http://list.youku.com/category/show/c_95_mt_%s_s_1_d_1_p_%s.html", genre,
                page + "");
        List<String> idList = new ArrayList<String>();
        try {
            Document doc = Jsoup.connect(url).timeout(60 * 1000).get();
            Element root = doc.getElementsByAttributeValue("class", "yk-row").get(0);
            Elements items = root.getElementsByAttributeValue("class", "yk-col4");
            for (int i = 0; i < items.size(); i++) {
                String u = items.get(i).getElementsByTag("a").get(0).attr("href");
                idList.add(u.split("/id_")[1].split(".html")[0]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return idList;
    }
 
    public static List<ProgramVideo> getMusicVideoList(String genre, int page) {
        List<ProgramVideo> videoList = new ArrayList<ProgramVideo>();
        List<String> ids = getMusicIdList(genre, page);
        for (String id : ids) {
            videoList.add(getVideoDetail(id));
        }
        return videoList;
    }
 
    public static String post(String url, Map<String, Object> params) {
 
        HttpClient client = new HttpClient();
 
        // client.getHostConfiguration().setProxy("192.168.1.122", 8888);
 
        PostMethod pm = new PostMethod(url);
        try {
            NameValuePair[] values = new NameValuePair[params.keySet().size()];
            Iterator<String> its = params.keySet().iterator();
            int i = 0;
            while (its.hasNext()) {
 
                String key = its.next();
                NameValuePair pa = new NameValuePair(key, params.get(key).toString());
                values[i++] = pa;
            }
 
            pm.setRequestBody(values);
 
            client.executeMethod(pm);
            return pm.getResponseBodyAsString();
        } catch (Exception e) {
        }
        return "";
 
    }
 
}