Guest User

jezaraf

a guest
Dec 14th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package jezaraf;
  2.  
  3.  
  4. import org.apache.commons.io.FileUtils;
  5. import org.apache.commons.io.IOUtils;
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.client.methods.HttpPost;
  9. import org.apache.http.impl.client.DefaultHttpClient;
  10. import org.apache.log4j.Logger;
  11. import org.jsoup.Jsoup;
  12. import org.jsoup.nodes.Document;
  13. import org.jsoup.nodes.Element;
  14. import org.jsoup.select.Elements;
  15.  
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.nio.charset.StandardCharsets;
  20. import java.util.ArrayList;
  21. import java.util.Base64;
  22. import java.util.List;
  23.  
  24. public class Downloader {
  25.     private final static Logger log = Logger.getLogger(Downloader.class);
  26.    
  27.     private static String username = "";
  28.     private static String password = "";
  29.     private static String baseUrl = "";
  30.     private static String desktopVideo = "C:\\Users\\...\\Desktop\\video\\";
  31.    
  32.     public static void main(String[] args) throws IOException {
  33.         List<String> subPages = getSubPages();
  34.         for(int i = 0 ; i < subPages.size() ; i++){
  35.             String subPage = subPages.get(i);
  36.             String subFolderName = (i+1) + "_"+ subFolderName(subPage) + "\\";
  37.             //log.info(subPage);
  38.             if(subPage.startsWith("movie")){
  39.                 log.info(subFolderName);
  40.                 List<String> subSubPages = getSubSubPages(subPages.get(i));
  41.                 for(String subSubPage: subSubPages){
  42.                     log.info(subSubPage);
  43.                     downloadFile(subFolderName, subSubPage);
  44.                 }
  45.             }else{
  46.                 log.info(subFolderName);
  47.                 downloadFile(subFolderName, subPage);
  48.             }
  49.         }
  50.     }
  51.  
  52.     private static void downloadFile(String subFolderName, String subSubPage) throws IOException {
  53.         String fileName = getFileAdress(subSubPage);
  54.         String fileUri = baseUrl + fileName;
  55.         log.info(fileUri);
  56.         File targetFile = new File(desktopVideo+subFolderName+fileName);
  57.         if(!targetFile.exists()){
  58.             FileUtils.copyInputStreamToFile(getFile(fileUri), targetFile);
  59.         }
  60.     }
  61.  
  62.     private static String subFolderName(String subUrl){
  63.         return subUrl.substring(6, subUrl.length() - 4);
  64.     }
  65.  
  66.     private static InputStream getFile(String mpgAdress) throws IOException {
  67.         String encoding = Base64.getEncoder().encodeToString((username+":"+password).getBytes());
  68.         HttpPost httppost = new HttpPost(mpgAdress);
  69.         httppost.setHeader("Authorization", "Basic " + encoding);
  70.         log.info("executing request " + httppost.getRequestLine());
  71.  
  72.         DefaultHttpClient httpclient = new DefaultHttpClient();
  73.         HttpResponse response = httpclient.execute(httppost);
  74.         HttpEntity entity = response.getEntity();
  75.         return entity.getContent();
  76.     }
  77.     private static String getFileAdress(String subSubPage) throws IOException {
  78.         String uri = baseUrl + subSubPage;
  79.         String encoding = Base64.getEncoder().encodeToString((username+":"+password).getBytes());
  80.         HttpPost httppost = new HttpPost(uri);
  81.         httppost.setHeader("Authorization", "Basic " + encoding);
  82.  
  83.         log.info("executing request " + httppost.getRequestLine());
  84.  
  85.         DefaultHttpClient httpclient = new DefaultHttpClient();
  86.         HttpResponse response = httpclient.execute(httppost);
  87.         HttpEntity entity = response.getEntity();
  88.         String result = IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8);
  89.         //log.info(result);
  90.         Document doc = Jsoup.parse(result);
  91.         Elements elements = doc.select("a[id=player]");
  92.         //log.info(elements);
  93.         return elements.get(0).attr("href");
  94.     }
  95.     private static List<String> getSubPages() throws IOException {
  96.         String uri = baseUrl + "GT_video.php";
  97.         String encoding = Base64.getEncoder().encodeToString((username+":"+password).getBytes());
  98.         HttpPost httppost = new HttpPost(uri);
  99.         httppost.setHeader("Authorization", "Basic " + encoding);
  100.  
  101.         log.info("executing request " + httppost.getRequestLine());
  102.  
  103.         DefaultHttpClient httpclient = new DefaultHttpClient();
  104.         HttpResponse response = httpclient.execute(httppost);
  105.         HttpEntity entity = response.getEntity();
  106.         String result = IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8);
  107.         //log.info(result);
  108.         Document doc = Jsoup.parse(result);
  109.         Elements elements = doc.select("a[href$=.php]").select("a[href^=movie], a[href^=video]");
  110.         List<String> subPages = new ArrayList<>();
  111.         for(Element e:elements){
  112.             if(e.firstElementSibling() == null){
  113.                 //log.info(e);
  114.                 subPages.add(e.attr("href"));
  115.             }
  116.         }
  117.         return subPages;
  118.     }
  119.     private static List<String> getSubSubPages(String subSiteName) throws IOException {
  120.         String uri = baseUrl + subSiteName;
  121.         String encoding = Base64.getEncoder().encodeToString((username+":"+password).getBytes());
  122.  
  123.         HttpPost httppost = new HttpPost(uri);
  124.         httppost.setHeader("Authorization", "Basic " + encoding);
  125.  
  126.         DefaultHttpClient httpclient = new DefaultHttpClient();
  127.         HttpResponse response = httpclient.execute(httppost);
  128.         HttpEntity entity = response.getEntity();
  129.         String result = IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8);
  130.         //log.info(result);
  131.         Document doc = Jsoup.parse(result);
  132.         Elements elements = doc.select("a[href$=.php]").select("a[href^=video]");
  133.         List<String> subPages = new ArrayList<>();
  134.         for(Element e:elements){
  135.             if(e.firstElementSibling() == null && e.getElementsByTag("img").size() > 0){
  136.                 //log.info(e);
  137.                 subPages.add(e.attr("href"));
  138.             }
  139.         }
  140.         return subPages;
  141.     }
  142. }
Add Comment
Please, Sign In to add comment