admin
2021-02-06 cad915058c3c53bf328a8ae9ca9bc7de099caba7
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
package com.yeshi.buwan.util.video;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
 
import com.yeshi.buwan.util.StringUtil;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
/**
 * �ſ���Ƶ����
 * 
 * @author Administrator
 * 
 */
public class YouKuVideoParser {
 
    static Map<String, String> videoType = null;
    static {
        videoType = new HashMap<String, String>();
        videoType.put("hd3", "flv");
        videoType.put("hd2", "flv");
        videoType.put("mp4", "mp4");
        videoType.put("flvhd", "flv");
        videoType.put("flv", "flv");
        videoType.put("3gphd", "3gp");
    }
 
    private String httpGet(String url) {
        HttpClient client = new HttpClient();
        GetMethod getMethod = new GetMethod(url);
        try {
            client.executeMethod(getMethod);
            return getMethod.getResponseBodyAsString();
        } catch (HttpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }
 
    public Map<String, String> getRealUrl(String vid) {
        Map<String, String> map = new HashMap<String, String>();
        String result = httpGet("http://play.youku.com/play/get.json?vid="
                + vid + "&ct=12");
        JSONObject root =JSONObject.fromObject(result);
        JSONObject data = root.optJSONObject("data");
        String ip = data.optJSONObject("security").optString("ip");
        String encrypt = data.optJSONObject("security").optString(
                "encrypt_string");
        JSONArray array = data.optJSONArray("stream");
        for (int i = 0; i < array.size(); i++) {
            JSONObject obj = array.optJSONObject(i);
            String type = obj.optString("stream_type");
            String sid = new String();
            String token = new String();
            String newEp = new String();
            try {
                Map<String, String> m = getEp(vid, encrypt, newEp, token, sid);
                System.out
                        .println("http://pl.youku.com/playlist/m3u8?ctype=12&ep="
                                + m.get("ep")
                                + "&ev=1&keyframe=1&oip="
                                + ip
                                + "&sid="
                                + m.get("sid")
                                + "&token="
                                + m.get("token")
                                + "&type="
                                + videoType.get(type) + "&vid=" + vid + "");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
 
        }
        return map;
    }
 
    private String myEncoder(String a, byte[] c, boolean isToBase64)
            throws UnsupportedEncodingException {
        String result = "";
        List<Byte> bytesR = new ArrayList<Byte>();
        int f = 0, h = 0, q = 0;
        int[] b = new int[256];
        for (int i = 0; i < 256; i++)
            b[i] = i;
        while (h < 256) {
            f = (f + b[h] + a.charAt(h % a.length())) % 256;
            int temp = b[h];
            b[h] = b[f];
            b[f] = temp;
            h++;
        }
        f = 0;
        h = 0;
        q = 0;
        while (q < c.length) {
            h = (h + 1) % 256;
            f = (f + b[h]) % 256;
            int temp = b[h];
            b[h] = b[f];
            b[f] = temp;
            byte[] bytes = new byte[] { (byte) (c[q] ^ b[(b[h] + b[f]) % 256]) };
            bytesR.add(bytes[0]);
 
            result += (new String(bytes, "ASCII"));
            // System.Text.ASCIIEncoding.ASCII.GetString(bytes);
            q++;
        }
        if (isToBase64) {
            Byte[] byteR = bytesR.toArray(new Byte[bytesR.size()]);
            byte[] byteRb = new byte[byteR.length];
            for (int i = 0; i < byteR.length; i++)
                byteRb[i] = byteR[i];
            result = StringUtil.getBase64FromByte(byteRb);// Convert.ToBase64String(byteR);
        }
        return result;
    }
 
    public Map<String, String> getEp(String vid, String ep, String pNew,
            String token, String sid) throws UnsupportedEncodingException {
        Map<String, String> map = new HashMap<String, String>();
        String template1 = "becaf9be";
        String template2 = "bf7e5f01";
        byte[] bytes = StringUtil.getFromBase64Byte(ep);
        ep = new String(bytes, "ASCII");// System.Text.ASCIIEncoding.ASCII.GetString(bytes);
        String temp = myEncoder(template1, bytes, false);
        String[] part = temp.split("_");
        sid = part[0];// 241273717793612e7b085
        token = part[1];
        String whole = sid + "_" + vid + "_" + token;
        byte[] newbytes = whole.getBytes("ASCII");// System.Text.ASCIIEncoding.ASCII.GetBytes(whole);
        String epNew = myEncoder(template2, newbytes, true);
        System.out.println("EP:" + URLEncoder.encode(epNew));
        pNew = URLEncoder.encode(epNew);
        System.out.println("SID:" + sid);
        System.out.println("TOKEN:" + token);
        map.put("sid", sid);
        map.put("token", token);
        map.put("ep", pNew);
        return map;
    }
 
    public static void main(String[] args) {
        new YouKuVideoParser().getRealUrl("XMTQ2Mjk1Mjc3Mg==");
    }
 
}