All pastes #959799 Raw Edit

Untitled

public text v1 · immutable
#959799 ·published 2008-03-27 18:20 UTC
rendered paste body
package com.youtube;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DownloadVideo {

    // Constants
    private static final String WATCH_URL = "http://www.youtube.com/watch?v=";
    private static String downloadUrl = "http://www.youtube.com/get_video?video_id=";
    // Instance members
    private String video_id = null;
    private String t = null;

    public DownloadVideo(String video_id) {
        this.video_id = video_id;
    }

    public void downloadPage() throws IOException {
        URL u = new URL(WATCH_URL + video_id);
        URLConnection conn = u.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(false);
        conn.connect();

        // What we will need for the loop
        Pattern p = Pattern.compile("\"t\":\\s\"[a-zA-Z\\-_0-9]+\"");
        Matcher m = null;
        // Get input reader and read the T string
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String tmp = null;
        // Don't process the first couple of lines, to make it faster
        int idx = 0;
        while (idx < 40) {
            idx++;
            in.readLine(); // Don't do anything with the read line
        }
        // Real loop that will parse the T variable from the JSON txt
        while ((tmp = in.readLine().trim()) != null) {
            if (tmp.contains("\"t\":")) {
                m = p.matcher(tmp);
                while (m.find()) {
                    String[] arr = m.group().split("\\s");
                    // This will parse the T value from the found string
                    this.t = arr[1].split("\"")[1];
                    break;
                }
                break;
            }
        }
        in.close();
    }

    // This is what the download link is going to look like
    // http://www.youtube.com/get_video?video_id=$VIDEO_ID &t=$T
    public void downloadVideo(String targetDir) throws IOException {
        File f = new File(targetDir + video_id);
        URL u = new URL("http://www.youtube.com/get_video?video_id=" + video_id + "&t=" + t);
        URLConnection conn = u.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(false);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));
        BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
        int buff = 0;
        // Read the video into the file
        while ((buff = bis.read()) != -1) {
            bos.write(buff);
        }
        bos.close();
        bis.close();
        System.out.println("Done");
    }
}