Guest User

Untitled

a guest
Mar 17th, 2015
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1.     public class Youtube_Downloader {
  2.        private static FileOutputStream fos = null;
  3.       /******************************************************************************************/
  4.       public static void main(String[] args) throws Exception {
  5.             String s = null;
  6.             File logFile = new File("ytlog.txt");
  7.             fos = new FileOutputStream(logFile, true); //true = append
  8.             try {
  9.                 Process p = Runtime.getRuntime().exec("ps -ef ");
  10.                 BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
  11.                 BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  12.                 boolean youtube_dl_running_flag = false;
  13.                 while ((s = stdInput.readLine()) != null) {
  14.                     if(s.indexOf("youtube-dl") > -1) {
  15.                         log(s);
  16.                         youtube_dl_running_flag = true;
  17.                         System.exit(-1);
  18.                     }
  19.                 }
  20.                 if(youtube_dl_running_flag == false) {
  21.                     log("youtube-dl is not running.  launching now...");
  22.                     Process p2 = Runtime.getRuntime().exec("youtube-dl https://www.youtube.com/watch?v=0SZw8jpfAk0");
  23.                     log("Process launched...");
  24.                     BufferedReader stdInput2 = new BufferedReader(new InputStreamReader(p2.getInputStream()));
  25.                     BufferedReader stdError2 = new BufferedReader(new InputStreamReader(p2.getErrorStream()));
  26.                     int exitVal = p2.waitFor();
  27.                     log("WaitFor() complete.  exitVal=" + exitVal );
  28.                     while ((s = stdInput2.readLine()) != null) {
  29.                         System.out.println(s);
  30.                     }
  31.                     while ((s = stdError2.readLine()) != null) {
  32.                         System.out.println(s);
  33.                     }
  34.                 }
  35.             } catch (IOException e) {
  36.                 e.printStackTrace();
  37.                 System.exit(-1);
  38.             } finally {
  39.                 fos.close();
  40.             }
  41.       }
  42.       /******************************************************************************************/
  43.       public static void log(Object obj) throws Exception {
  44.          fos.write((new Date() + " : " + obj.toString() + "\r\n").getBytes());
  45.       }
  46.     }
Advertisement
Add Comment
Please, Sign In to add comment