Saturday, December 19, 2015

Download Videos 1.0

Imagine when i open kissanime.to or kisscartoon.me website:


Output: text file contains links of the videos



Internet Download Manager is queuing the videos link



Download Java Project

Here is the implementation in Java Programming Language:
 
import java.io.File;
import java.io.IOException;
import java.util.List;

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
// set enviroment variable path to IDM.exe directory

public class IDMCommandLine {
 
 //get video url from kissanime.to or kisscartoon.me
 public static String seasonURL = "http://kisscartoon.me/Cartoon/The-Simpsons-Season-03";
 public static String filePath = "C:\\Users\\Stars\\Desktop\\TheSimpsonsSeason03.txt";
 public static String idmPath = "IDMan.exe";
 public static long waitingTime=5000;

 public static void main(String[] args) throws Exception {

  System.out.println("Start " + seasonURL);

  StringBuilder saveVideoURL = new StringBuilder();
  String[] episodesURL = getEpisodesURL().split(";");
  System.out.println("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+"-->"+videoURL);
    saveVideoURL.append(videoTitle);
    saveVideoURL.append(";");
    saveVideoURL.append(videoURL);
    saveVideoURL.append(";");
    saveVideoURL.append(episodesURL[i]);
    saveVideoURL.append("\n");
   }
  }

  File file = new File(filePath);
  FileUtils.writeStringToFile(file, saveVideoURL.toString().trim());

  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);
 }

 public static void sendURLtoIDM(String videoURL, String videoTitle) throws IOException, InterruptedException {
  // TODO Auto-generated method stub
  ProcessBuilder pb = new ProcessBuilder(idmPath, "/d", videoURL, "/f", videoTitle, "/a");
  Process process = pb.start();
  int returnValue = process.waitFor();
  if (returnValue == 0) {
   process.destroy();
  }
 }

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

 public static String getEpisodesURL() throws InterruptedException {
  // 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);

  StringBuilder sb = new StringBuilder();
  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();
  return sb == null ? null : sb.toString().trim();
 }

 public static String getVideoURL(String urlEpisode) throws InterruptedException {
  // 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);
  String urlVideo = null;
  List<webelement> results = driver.findElements(By.tagName("video"));
  for (WebElement we : results) {
   urlVideo = we.getAttribute("src");
  }
  driver.quit();
  return urlVideo;
 }
}
go to Download Video 1.1

0 comments:

Post a Comment

 
;