Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
Thursday, December 24, 2015 0 comments

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;
 }
}
Saturday, December 19, 2015 0 comments

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
Friday, December 20, 2013 0 comments

Unique Random Algorithm

I want to make program that can generate random numbers. How should I do?
It can be done by using the function that already been provided by the library such as Random().

The thing comes out when we want unique random numbers. How should I do?
Step 1, generate random number and insert it into first element of an array.
Step 2, generate random number and check whether the random number is already exist in the array or not.
If the result is already exist, then repeat step 2.
If the result is not exist, then insert the random number to the next element of the array.

Here is the implementation in Java Programming Language:
import java.util.Random;

public class UniqueRandomAlgorithm {

    public static void main(String[] args) {
        printArray1D(generateUniqueRandom(50));
    }

    public static int[] generateUniqueRandom(int n) {
        int[] num = new int[n];
        Random a = new Random();
        int x = a.nextInt(n) + 1;
        num[0] = x;
        if (n > 1) {
            x = a.nextInt(n) + 1;
            int i = 1;
            while (i < n) {
                if (isSameNumber(num, x)) {
                    x = a.nextInt(n) + 1;
                } else {
                    num[i] = x;
                    i++;
                }
            }
        }
        return num;
    }

    public static boolean isSameNumber(int[] num, int x) {
        boolean result = false;
        for (int i = 0; i < num.length; i++) {
            if (num[i] == x) {
                result = true;
                break;
            }
        }
        return result;
    }

    public static void printArray1D(int[] num) {
        for (int i = 0; i < num.length; i++) {
            System.out.println("Number " + (i + 1) + " = " + num[i]);
        }
    }
}


Here is the implementation in C# Programming Language:
using System.IO;
using System;

class Program
{
    static void Main()
    {
        printArray1D(generateUniqRandom(50));
    }
    
    static int[] generateUniqRandom(int n) {
        int[] num = new int[n];
        Random a = new Random();
        int x = a.Next(n)+1;
        num[0] = x;
        if (n > 1) {
            x = a.Next(n)+1;
            int i = 1;
            while (i < n) {
                if (isSameNumber(num, x)==1) {
                    x = a.Next(n)+1;
                } else {
                    num[i] = x;
                    i++;
                }
            }
        }
        return num;
    }
    
    static int isSameNumber(int[] num, int x) {
        int result = 0;
        for (int i = 0; i < num.Length; i++) {
            if (num[i] == x) {
                result = 1;
                break;
            }
        }
        return result;
    }
    

    static void printArray1D(int[] num) {
        for (int i = 0; i < num.Length; i++) {
            Console.WriteLine("Number " + (i + 1) + " = " + num[i]);
        }
    }
}
 
;