Code:
public static void download(String url, String fileName) throws Exception {
String id = url.split("v=")[1].split("&")[0];
System.out.println(url + " " + id);
String nid = getDownloadId(id);
if (nid == null) {
throw new Exception("Invalud url");
}
url = "http://www.youtube.com/watch?v=" + id;
downloadStreamData(url, fileName);
}
/*
* returns the id of the video data
*/
private static String getDownloadId(String videoId) throws MalformedURLException, IOException {
String url = "http://www.youtube.com/watch?v=" + videoId;
URL tU = new URL(url);
HttpURLConnection conn = (HttpURLConnection) tU.openConnection();
InputStream ins = conn.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(ins));
String line;
StringBuffer content = new StringBuffer();
while ((line = rd.readLine()) != null) {
content.append(line);
}
String sContent = URLDecoder.decode(content.toString(), "UTF-8");
String[] tokens = sContent.split("&");
for (int i = 0; i < tokens.length - 1; i++) {
if (tokens[i] == null) {
continue;
}
String param1 = tokens[i].substring(0, tokens[i].indexOf("="));
String param2 = tokens[i].substring(param1.length() + 1);
if (param1.equals("token")) {
return param2;
}
}
return "";
}
/*
* this function will download the file after the stream is found
*/
private static void downloadStreamData(String url, String fileName) throws Exception {
URL tU = new URL(url);
HttpURLConnection conn = (HttpURLConnection) tU.openConnection();
String type = conn.getContentType();
InputStream ins = conn.getInputStream();
FileOutputStream fout = new FileOutputStream(new File(fileName));
byte[] outputByte = new byte[4096];
int bytesRead;
int length = conn.getContentLength();
int read = 0;
while ((bytesRead = ins.read(outputByte, 0, 4096)) != -1) {
read += bytesRead;
System.out.println(read + " out of " + length);
fout.write(outputByte, 0, bytesRead);
}
fout.flush();
fout.close();
}
I get this error on the console when I run it:
http://www.youtube.com/watch?v=jUEOWVj nIR8 jUEOWVjnIR8
Exception in thread "main" java.lang.Illeg alArgumentExcep tion: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "">"
at java.net.URLDec oder.decode(Unk nown Source)
at youtube.file.Yo uTube.getDownlo adId(YouTube.ja va:52)
at youtube.file.Yo uTube.download( YouTube.java:30 )
at youtube.file.Yo uTube.main(YouT ube.java:21)
Comment