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==");
|
}
|
|
}
|