Java get cool video address sample _java

Source: Internet
Author: User

Copy Code code as follows:

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{
The regular expression resolves the address, and takes an 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 ("No MP4 format");
Try
{
Flvid = obj2.getstring ("flv");
format = "flv";
catch (Jsonexception E1)
{
SYSTEM.OUT.PRINTLN ("No FLV format");
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;
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.