Skip to content

Instantly share code, notes, and snippets.

@gandharva
Created April 2, 2014 09:16
Show Gist options
  • Select an option

  • Save gandharva/9930695 to your computer and use it in GitHub Desktop.

Select an option

Save gandharva/9930695 to your computer and use it in GitHub Desktop.
package io.leftshift.utilities;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class YoutubeVideoUrlExtrator {
public static final String CHARSET = "UTF-8";
public static final String ENDPOINT = "http://www.youtube.com/get_video_info?&video_id=";
public enum YoutubeFormatQuality {
FORMAT_3GP_QUALITY_240P(36), FORMAT_MP4_QUALITY_360P(18), FORMAT_MP4_QUALITY_720P(
22), FORMAT_MP4_QUALITY_1080P(37);
private int value;
private YoutubeFormatQuality(int value) {
this.value = value;
}
public String getValue() {
return Integer.toString(value);
}
}
/**
* Calculate the YouTube URL to load the video. Includes retrieving a token
* that YouTube requires to play the video.
*
* @param youTubeFormatQuality
* quality of the video. 17=low, 18=high
* @param youTubeVideoId
* the id of the video
* @return the url string that will retrieve the video
* @throws IOException
* @throws ClientProtocolException
* @throws UnsupportedEncodingException
*/
public static String calculateYouTubeUrl(
YoutubeFormatQuality youtubeFormatQuality, String youTubeVideoId)
throws IOException, ClientProtocolException,
UnsupportedEncodingException {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(ENDPOINT + youTubeVideoId);
HttpResponse response = null;
response = client.execute(get);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
String informationString = null;
response.getEntity().writeTo(byteArrayOutputStream);
informationString = new String(byteArrayOutputStream.toString(CHARSET));
String[] arguments = informationString.split("&");
Map<String, String> argumentsMap = new HashMap<String, String>();
for (int i = 0; i < arguments.length; i++) {
String[] argumentValueStringArray = arguments[i].split("=");
if (argumentValueStringArray != null) {
if (argumentValueStringArray.length >= 2) {
argumentsMap.put(argumentValueStringArray[0], URLDecoder
.decode(argumentValueStringArray[1], CHARSET));
}
}
}
String urlEncodeFormatStreamMap = argumentsMap
.get("url_encoded_fmt_stream_map");
if(urlEncodeFormatStreamMap == null){
return null;
}
String[] streamQueries = urlEncodeFormatStreamMap.split(",");
HashMap<String, String> urlMap = new HashMap<String, String>();
for (String streamQuery : streamQueries) {
HashMap<String, String> stream = getParameterMap(streamQuery);
String type = stream.get("type");
String urlString = stream.get("url");
String signature = stream.get("sig");
if (signature == null) {
if (!urlString.isEmpty()) {
String itag = stream.get("itag");
urlMap.put(itag, urlString);
}
} else {
if (!urlString.isEmpty() && !signature.isEmpty()) {
String url = urlString + "&signature=" + signature;
String itag = stream.get("itag");
urlMap.put(itag, url);
}
}
}
return getPreferedUrl(youtubeFormatQuality, urlMap);
}
private static HashMap<String, String> getParameterMap(String queryString)
throws UnsupportedEncodingException {
HashMap<String, String> map = new HashMap<String, String>();
String[] fields = queryString.split("&");
for (String field : fields) {
String[] pair = field.split("=");
if (pair.length == 2) {
String key = pair[0];
String value = null;
value = URLDecoder.decode(pair[1], CHARSET);
map.put(key, value);
}
}
return map;
}
private static String getPreferedUrl(
YoutubeFormatQuality youtubeFormatQuality,
HashMap<String, String> urlMap) {
String url = null;
switch (youtubeFormatQuality) {
case FORMAT_MP4_QUALITY_360P:
if (urlMap.containsKey(YoutubeFormatQuality.FORMAT_MP4_QUALITY_360P
.getValue())) {
url = urlMap.get(YoutubeFormatQuality.FORMAT_MP4_QUALITY_360P
.getValue());
} else if (urlMap
.containsKey(YoutubeFormatQuality.FORMAT_MP4_QUALITY_720P
.getValue())) {
url = urlMap.get(YoutubeFormatQuality.FORMAT_MP4_QUALITY_720P
.getValue());
} else if (urlMap
.containsKey(YoutubeFormatQuality.FORMAT_MP4_QUALITY_1080P
.getValue())) {
url = urlMap.get(YoutubeFormatQuality.FORMAT_MP4_QUALITY_1080P
.getValue());
} else if (urlMap
.containsKey(YoutubeFormatQuality.FORMAT_3GP_QUALITY_240P
.getValue())) {
url = urlMap.get(YoutubeFormatQuality.FORMAT_3GP_QUALITY_240P
.getValue());
}
break;
case FORMAT_MP4_QUALITY_720P:
if (urlMap.containsKey(YoutubeFormatQuality.FORMAT_MP4_QUALITY_720P
.getValue())) {
url = urlMap.get(YoutubeFormatQuality.FORMAT_MP4_QUALITY_720P
.getValue());
} else if (urlMap
.containsKey(YoutubeFormatQuality.FORMAT_MP4_QUALITY_1080P
.getValue())) {
url = urlMap.get(YoutubeFormatQuality.FORMAT_MP4_QUALITY_1080P
.getValue());
} else if (urlMap
.containsKey(YoutubeFormatQuality.FORMAT_MP4_QUALITY_360P
.getValue())) {
url = urlMap.get(YoutubeFormatQuality.FORMAT_MP4_QUALITY_360P
.getValue());
} else if (urlMap
.containsKey(YoutubeFormatQuality.FORMAT_3GP_QUALITY_240P
.getValue())) {
url = urlMap.get(YoutubeFormatQuality.FORMAT_3GP_QUALITY_240P
.getValue());
}
break;
case FORMAT_MP4_QUALITY_1080P:
if (urlMap
.containsKey(YoutubeFormatQuality.FORMAT_MP4_QUALITY_1080P
.getValue())) {
url = urlMap.get(YoutubeFormatQuality.FORMAT_MP4_QUALITY_1080P
.getValue());
} else if (urlMap
.containsKey(YoutubeFormatQuality.FORMAT_MP4_QUALITY_720P
.getValue())) {
url = urlMap.get(YoutubeFormatQuality.FORMAT_MP4_QUALITY_720P
.getValue());
} else if (urlMap
.containsKey(YoutubeFormatQuality.FORMAT_MP4_QUALITY_360P
.getValue())) {
url = urlMap.get(YoutubeFormatQuality.FORMAT_MP4_QUALITY_360P
.getValue());
} else if (urlMap
.containsKey(YoutubeFormatQuality.FORMAT_3GP_QUALITY_240P
.getValue())) {
url = urlMap.get(YoutubeFormatQuality.FORMAT_3GP_QUALITY_240P
.getValue());
}
break;
default:
url = urlMap.get(YoutubeFormatQuality.FORMAT_3GP_QUALITY_240P
.getValue());
break;
}
return url;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment