Thursday, December 24, 2015

Download Videos 1.1

Refer to previous version program Download Video 1.0, I made some changes:
1. Add GUI so that it will look nicer
2. Add method for checking internet connectivity
3. Add try catch to all methods
4. No need to set path for Internet Download Manager(IDM), just put the path on the Input Dialog
5. Have the executable program (.exe)


Here is the input:




Here is the process:



And for the output is still the same with previous version: have a text file output and links are queuing to IDM.

Download Executable Program (.exe)

Here is the implementation in Java Programming Language:
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

// create "firefox.exe -p" profile
// and set browser firefox about:config :
// permissions.default.image=2 (disable images)
// media.autoplay.enabled=false
// media.mediasource.enabled=false
// and set never remember history or cache

public class IDMCommandLine {

 public static String seasonURL;
 public static String filePath;
 public static String idmPath;
 public static long waitingTime;
 public static String urlTestInetConn = "www.google.com";
 public static int portTestInetConn = 80;
 public static JLabel label;

 public static void main(String[] args) {
  try {
   if (testInternetConnection(urlTestInetConn, portTestInetConn)) {
    getInputDialogShowProgressBarDoBackgroundProcess();
   }
  } catch (Exception ex) {
   System.out.println("Error from main");
   JOptionPane.showMessageDialog(null, "Error from main");
   ex.printStackTrace();
   System.exit(0);
  }

 }

 public static boolean testInternetConnection(String site, int port) {
  boolean returnValue = false;
  Socket sock = new Socket();
  try {
   InetSocketAddress addr = new InetSocketAddress(site, port);
   sock.connect(addr, 3000);
   returnValue = true;
  } catch (Exception ex) {
   returnValue = false;
   ex.printStackTrace();
   System.out.println("Make sure you have an internet connection.");
   JOptionPane.showMessageDialog(null, "Make sure you have an internet connection.");
  } finally {
   try {
    sock.close();
   } catch (IOException e) {
   }

  }
  return returnValue;
 }

 public static void getInputDialogShowProgressBarDoBackgroundProcess() {
  try {
   // show input dialog for season url, file path,
   // idm path and waiting time
   seasonURL = JOptionPane.showInputDialog(null,
     "Input Season URL =\nExample: http://kisscartoon.me/Cartoon/The-Simpsons-Season-03",
     "http://kisscartoon.me/Cartoon/The-Simpsons-Season-03");
   filePath = JOptionPane.showInputDialog(null,
     "Input file text output destination =\nExample: C:\\Users\\Stars\\Desktop\\TheSimpsonsSeason03.txt",
     "C:\\Users\\Stars\\Desktop\\TheSimpsonsSeason03.txt");
   idmPath = JOptionPane.showInputDialog(null,
     "Input Internet Download Manager's path =\nExample: C:\\Program Files (x86)\\Internet Download Manager\\IDMan.exe",
     "C:\\Program Files (x86)\\Internet Download Manager\\IDMan.exe");
   String time = JOptionPane.showInputDialog(null, "Input waiting time in miliseconds=\nExample: 5000",
     "5000");
   waitingTime = Long.parseLong(time);

   // call frame for progress bar
   final JFrame frame = new JFrame("Download Video");
   final JProgressBar progressBar = new JProgressBar();
   progressBar.setIndeterminate(true);

   progressBar.setPreferredSize(new Dimension(500, 20));
   final JPanel contentPane = new JPanel();
   // top,left,bottom,right
   contentPane.setBorder(BorderFactory.createEmptyBorder(10, 100, 10, 100));
   contentPane.setLayout(new BorderLayout());
   label = new JLabel("Processing...");
   contentPane.add(label, BorderLayout.NORTH);
   contentPane.add(progressBar, BorderLayout.CENTER);
   frame.setContentPane(contentPane);
   frame.pack();
   frame.setLocationRelativeTo(null);
   frame.setVisible(true);
   frame.setResizable(false);
   frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent we) {
     // exit process when click close icon
     System.exit(0);
    }
   });

   // start background prosess
   Runnable runnable = new Runnable() {
    public void run() {

     // call main process
     process(seasonURL, filePath, idmPath, waitingTime);

     SwingUtilities.invokeLater(new Runnable() {
      public void run() {
       frame.setVisible(false);
      }
     });

    }
   };

   new Thread(runnable).start();
  } catch (Exception ex) {
   System.out.println("Error from getInputDialogShowProgressBarDoBackgroundProcess");
   JOptionPane.showMessageDialog(null, "Error from getInputDialogShowProgressBarDoBackgroundProcess");
   ex.printStackTrace();
   System.exit(0);
  }
 }

 public static void changeLabel(final String s) {
  try {
   SwingUtilities.invokeLater(new Runnable() {
    public void run() {
     label.setText("<html>" + s + "</html>");

    }
   });
  } catch (Exception ex) {
   System.out.println("Error from changeLabel");
   JOptionPane.showMessageDialog(null, "Error from changeLabel");
   ex.printStackTrace();
   System.exit(0);
  }
 }

 public static void process(String seasonURL, String filePath, String idmPath, long waitingTime) {
  try {
   System.out.println("Start " + seasonURL);
   changeLabel("Start " + seasonURL);
   // get episodes and videos url
   StringBuilder saveVideoURL = new StringBuilder();
   String[] episodesURL = getEpisodesURL().split(";");
   System.out.println("Total episodes = " + episodesURL.length);
   changeLabel("Total episodes = " + episodesURL.length);
   for (int i = episodesURL.length - 1; i >= 0; i--) {
    if (episodesURL[i] != null) {
     String videoURL = getVideoURL(episodesURL[i]);
     String videoTitle = getVideoTitle(episodesURL[i]);
     System.out.println(videoTitle + "\n" + videoURL);
     changeLabel(videoTitle + "<br>" + videoURL);
     saveVideoURL.append(videoTitle);
     saveVideoURL.append(";");
     saveVideoURL.append(videoURL);
     saveVideoURL.append(";");
     saveVideoURL.append(episodesURL[i]);
     saveVideoURL.append("\n");
    }
   }

   // write file to text
   File file = new File(filePath);
   FileUtils.writeStringToFile(file, saveVideoURL.toString().trim());

   // send link to IDM
   File file1 = FileUtils.getFile(filePath);
   LineIterator iter = FileUtils.lineIterator(file1);
   while (iter.hasNext()) {
    String[] lines = iter.next().split(";");
    String title = lines[0];
    String url = lines[1];
    // String referer = lines[2];

    if (!url.equals("null")) {
     sendURLtoIDM(url, title);
    }
   }
   System.out.println("Finish " + seasonURL);
   changeLabel("Finish " + seasonURL);

  } catch (Exception ex) {
   System.out.println("Error from process");
   JOptionPane.showMessageDialog(null, "Error from process");
   ex.printStackTrace();
   System.exit(0);
  }
 }

 public static void sendURLtoIDM(String videoURL, String videoTitle) {
  try {
   ProcessBuilder pb = new ProcessBuilder(idmPath, "/d", videoURL, "/f", videoTitle, "/a");
   Process process = pb.start();
   int returnValue = process.waitFor();
   if (returnValue == 0) {
    process.destroy();
   }
  } catch (Exception ex) {
   System.out.println("Error from sendURLtoIDM");
   JOptionPane.showMessageDialog(null, "Error from sendURLtoIDM");
   ex.printStackTrace();
   System.exit(0);
  }
 }

 public static String getVideoTitle(String url) {
  String titleString = null;
  try {
   if (url != null && !url.equals("")) {
    String[] titles = url.trim().replace("-", "_").replace("?", ",").split(",")[0].split("/");
    titleString = titles[titles.length - 2].concat("_").concat(titles[titles.length - 1]).concat(".mp4");
   }

  } catch (Exception ex) {
   System.out.println("Error from getVideoTitle");
   JOptionPane.showMessageDialog(null, "Error from getVideoTitle");
   ex.printStackTrace();
   System.exit(0);
  }
  return titleString;
 }

 public static String getEpisodesURL() {
  StringBuilder sb = new StringBuilder();

  try {
   // get saved profile
   ProfilesIni profile = new ProfilesIni();
   FirefoxProfile myprofile = profile.getProfile("MyProfile");

   // load the firefox profile
   WebDriver driver = new FirefoxDriver(myprofile);
   // open URL
   driver.get(seasonURL);
   // wait page loading
   Thread.sleep(waitingTime);

   List<WebElement> tables = driver.findElements(By.tagName("table"));
   for (WebElement table : tables) {
    List<WebElement> trs = table.findElements(By.tagName("tr"));
    for (WebElement tr : trs) {
     List<WebElement> tds = tr.findElements(By.tagName("td"));
     for (WebElement td : tds) {
      List<WebElement> as = td.findElements(By.tagName("a"));
      for (int i = 0; i < as.size(); i++) {
       sb.append(as.get(i).getAttribute("href"));
       sb.append(";");
      }
     }
    }
   }
   driver.quit();
   if (sb == null || sb.toString().trim().equals("")) {
    sb = null;
    System.out.println("Could not find any episodes");
    JOptionPane.showMessageDialog(null, "Could not find any episodes");
    System.exit(0);
   }

  } catch (Exception ex) {
   System.out.println("Error from getEpisodesURL");
   JOptionPane.showMessageDialog(null, "Error from getEpisodesURL");
   ex.printStackTrace();
   System.exit(0);
  }
  return sb == null ? null : sb.toString().trim();
 }

 public static String getVideoURL(String urlEpisode) {
  String urlVideo = null;
  try {
   // get saved profile
   ProfilesIni profile = new ProfilesIni();
   FirefoxProfile myprofile = profile.getProfile("MyProfile");

   // load the firefox profile
   WebDriver driver = new FirefoxDriver(myprofile);
   // open URL
   driver.get(urlEpisode);
   // wait page loading
   Thread.sleep(waitingTime);

   List<WebElement> results = driver.findElements(By.tagName("video"));
   for (WebElement we : results) {
    urlVideo = we.getAttribute("src");
   }
   driver.quit();
  } catch (Exception ex) {
   System.out.println("Error from getVideoURL");
   JOptionPane.showMessageDialog(null, "Error from getVideoURL");
   ex.printStackTrace();
   System.exit(0);
  }
  return urlVideo;
 }
}

0 comments:

Post a Comment

 
;