java获取优酷视频地址示例
import java.io.bufferedreader;
import java.io.inputstreamreader;
import java.net.url;
import java.util.regex.matcher;
import java.util.regex.pattern;
import net.sf.json.jsonarray;
import net.sf.json.jsonexception;
import net.sf.json.jsonobject;
public class videotools
{
public static void main(string[] args) throws exception
{
videotools manager = new videotools();
string jsonurl = manager.getvideourl("http://v.youku.com/v_show/id_xmtgznda5otmy.html");
system.out.println(jsonurl.split("#").length);
}
public string getvideourl(string htmlurl) throws jsonexception{
//正则表达式解析地址,取id
pattern p = pattern.compile(".*id_(\\w+)\\.html");
string u = htmlurl;
matcher m = p.matcher(u);
string id = "";
if (m.find()) {
id = m.group(1);
}
system.out.println(id);
// string id="xmtgznda5otmy";
string s = getcontent("http://v.youku.com/player/getplaylist/videoids/"+id);
jsonobject jsobj = new jsonobject();
jsobj =jsobj.fromobject(s);
jsonarray jsonarr = jsobj.getjsonarray("data");
system.out.println(jsobj);
jsonobject obj1 = jsonarr.getjsonobject(0);
string title= obj1.getstring("title");
double seed = obj1.getdouble("seed");
jsonobject obj2 = obj1.getjsonobject("streamfileids");
string mp4id = null;
string flvid = null;
string format = null;
try
{
mp4id = obj2.getstring("mp4");
format = "mp4";
} catch (jsonexception e)
{
system.out.println("没有mp4格式");
try
{
flvid = obj2.getstring("flv");
format = "flv";
} catch (jsonexception e1)
{
system.out.println("没有flv格式");
return "";
}
}
string realfileid = null;
if (format.equals("mp4"))
{
realfileid = getfileid(mp4id, seed);
} else
{
realfileid = getfileid(flvid, seed);
}
string idleft = realfileid.substring(0, 8);
string idright = realfileid.substring(10);
string sid = gensid();
jsonobject obj3 = obj1.getjsonobject("segs");
jsonarray mp4arr = obj3.getjsonarray(format);
string videourl="";
for (int i = 0; i < mp4arr.size(); i++)
{
jsonobject o = mp4arr.getjsonobject(i);
string k = o.getstring("k");
string url = "http://f.youku.com/player/getflvpath/sid/" + sid + "_" + string.format("%1$02x", i) + "/st/" + format
+ "/fileid/" + idleft + string.format("%1$02x", i) + idright + "?k=" + k;
system.out.println(url);
videourl+=url+"#";
}
return videourl;
}
private static string getfileid(string fileid, double seed)
{
string mixed = getfileidmixstring(seed);
string[] ids = fileid.split("\\*");
stringbuilder realid = new stringbuilder();
int idx;
for (int i = 0; i < ids.length; i++)
{
idx = integer.parseint(ids[i]);
realid.append(mixed.charat(idx));
}
return realid.tostring();
}
public static string gensid()
{
int i1 = (int) (1000 + math.floor(math.random() * 999));
int i2 = (int) (1000 + math.floor(math.random() * 9000));
return system.currenttimemillis() + "" + i1 + "" + i2;
}
private static string getfileidmixstring(double seed)
{
stringbuilder mixed = new stringbuilder();
stringbuilder source = new stringbuilder("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz/\\:._-1234567890");
int index, len = source.length();
for (int i = 0; i < len; ++i)
{
seed = (seed * 211 + 30031) % 65536;
index = (int) math.floor(seed / 65536 * source.length());
mixed.append(source.charat(index));
source.deletecharat(index);
}
return mixed.tostring();
}
public static string getcontent(string strurl) {
try {
url url = new url(strurl);
bufferedreader br = new bufferedreader(new inputstreamreader(
url.openstream()));
string s = "";
stringbuffer sb = new stringbuffer("");
while ((s = br.readline()) != null) {
sb.append(s);
}
br.close();
return sb.tostring();
} catch (exception e) {
return "error open url:" + strurl;
}
}
}
上一篇: Json读写本地文件实现代码