package com.vennersistemas.vplus.youtube;
import com.vennersistemas.vplus.web.SimpleHttpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
public class YtAdapter
{
public static String visitorInfo1Live = "fbIFj8E_0rw";
public static int quality = 22;
public static Map getInfo(String videoId) throws IOException
{
Map params = SimpleHttpClient.getParameters(
SimpleHttpClient.getContent("http://www.youtube.com/get_video_info?video_id=" +
videoId, null));
return decodeParams(params);
}
private static Map decodeParams(Map params) throws UnsupportedEncodingException {
String str = (String)params.get("url_encoded_fmt_stream_map");
if (str != null) {
String[] tokens = str.split(",");
List list = new ArrayList();
for (String token : tokens) {
list.add(URLDecoder.decode(token.split("=")[1], "UTF-8"));
}
params.put("url_encoded_fmt_stream_map", list);
}
str = (String)params.get("fmt_list");
if (str != null) {
String[] tokens = str.split(",");
List list = new ArrayList();
for (String token : tokens) {
list.add(token.split("/"));
}
params.put("fmt_list", list);
}
return params;
}
public static String getVideoUrl(String videoId) throws IOException {
String strUrl = "http://www.youtube.com/watch?v=" + videoId;
HttpURLConnection connection = SimpleHttpClient.getConnection(strUrl);
connection.addRequestProperty("Cookie", "VISITOR_INFO1_LIVE=" + visitorInfo1Live);
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
int pos = line.indexOf("'PLAYER_CONFIG': {");
int pos2 = line.lastIndexOf("},");
if (pos < 0) continue;
try {
br.close();
}
catch (Exception localException) {
}
line = line.substring(pos + "'PLAYER_CONFIG': ".length());
try
{
br.close();
}
catch (Exception localException1)
{
}
}
if (line == null) return null;
JSONObject obj = (JSONObject)((JSONObject)JSONValue.parse(line)).get("args");
if (obj == null) {
return null;
}
int pos = -1;
int k = 0;
System.out.println(obj.get("fmt_list"));
for (String fmt : ((String)obj.get("fmt_list")).split(",")) {
System.out.println(fmt);
if (Integer.parseInt(fmt.split("/")[0]) <= quality) {
pos = k;
break;
}
k++;
}
System.out.println(obj.get("url_encoded_fmt_stream_map"));
if (pos >= 0) {
return URLDecoder.decode(((String)obj.get("url_encoded_fmt_stream_map")).split(",")[pos].split("=")[1], "UTF-8");
}
return null;
}
}