Guest User

rafaltonie

a guest
Dec 14th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.97 KB | None | 0 0
  1. package rafaltonie;
  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.nio.file.Files;
  21. import java.nio.file.Path;
  22. import java.nio.file.Paths;
  23. import java.util.ArrayList;
  24. import java.util.Base64;
  25. import java.util.List;
  26. import java.util.concurrent.TimeUnit;
  27. import java.util.stream.Collectors;
  28.  
  29. public class Downloader {
  30.     private final static Logger log = Logger.getLogger(Downloader.class);
  31.  
  32.     private static String usernamePassword = "";
  33.     private static String mainUrl = "";
  34.     private static String mainPage = "";
  35.     private static String desktopVideo = "C:\\Users\\...\\Desktop\\video2\\";
  36.  
  37.     public static void main(String[] args) throws IOException, InterruptedException {
  38.         download();
  39.  
  40.     }
  41.  
  42.     private static void download() throws IOException, InterruptedException {
  43.         List<AdressAndImage> subPages = getSubPages();
  44.  
  45.         int waitingCount = 1;
  46.         for(int i = 14; i < subPages.size() ; i++){
  47.             log.info(i);
  48.             try {
  49.                 String subPage = subPages.get(i).adress;
  50.                 int folderNumber = i+1;
  51.                 String subfolderBaseName = subFolderName(subPage);
  52.  
  53.                 String subFolderName = folderNumber + "_"+ subfolderBaseName + "\\";
  54.                 log.info(subPages.get(i).image);
  55.                 if(subPages.get(i).image != null){
  56.                     downloadFile(subFolderName, subPages.get(i).image);
  57.                 }
  58.                 if(subPage.startsWith("movie")){
  59.                     log.info(subFolderName);
  60.                     List<String> subSubPages = getSubSubPages(subPage);
  61.                     TimeUnit.SECONDS.sleep(0);
  62.                     waitingCount = 1;
  63.                     for(String subSubPage: subSubPages){
  64.                         log.info((folderNumber) + "_____" + subSubPage);
  65.                         TimeUnit.SECONDS.sleep(0);
  66.                         String fileAdress = getFileAdress(subSubPage);
  67.                         downloadFile(subFolderName, fileAdress);
  68.                     }
  69.                 }else{
  70.                     log.info(subFolderName);
  71.                     downloadFile(subFolderName, subPage);
  72.                 }
  73.  
  74.             } catch (IOException e) {
  75.                 log.error("###############################################################");
  76.                 log.error("###############################################################");
  77.                 log.error("last i = " + i );
  78.                 log.error("waiting " + waitingCount++);
  79.                 TimeUnit.SECONDS.sleep(30);
  80.                 i--;
  81.             }
  82.         }
  83.     }
  84.  
  85.     private static boolean isSubfolderExists(String subfolderBaseName) throws IOException {
  86.         List<Path> files = Files.walk(Paths.get(desktopVideo)).collect(Collectors.toList());
  87.         for(Path f:files){
  88.             if(f.toString().contains(subfolderBaseName)){
  89.                 return true;
  90.             }
  91.         }
  92.         return false;
  93.     }
  94.  
  95.     private static List<AdressAndImage> getSubPages() throws IOException {
  96.         Document doc = getDocumentFromPage(mainUrl + mainPage);
  97.         Elements elements = doc.select("a[href$=.php]").select("a[href^=movie], a[href^=video]");
  98.         List<AdressAndImage> subPages = new ArrayList<>();
  99.         for(Element e:elements){
  100.             AdressAndImage adressAndImage = new AdressAndImage();
  101.             adressAndImage.adress = e.attr("href");
  102.             if(e.children().size() > 0 ){
  103.                 adressAndImage.image = e.child(0).attr("src");
  104.             }
  105.             subPages.add(adressAndImage);
  106.         }
  107.         return subPages;
  108.     }
  109.  
  110.     private static List<String> getSubSubPages(String subSiteName) throws IOException {
  111.         Document doc = getDocumentFromPage(mainUrl+subSiteName);
  112.         Elements elements = doc.select("a[href$=.php]").select("a[href^=video]");
  113.         List<String> subPages = new ArrayList<>();
  114.         for(Element e:elements){
  115.             if(e.elementSiblingIndex() == 0 && e.getElementsByTag("img").size() > 0){
  116.                 subPages.add(e.attr("href"));
  117.             }
  118.         }
  119.         return subPages;
  120.     }
  121.  
  122.     /**
  123.      * @return true if downloaded, false if already exits
  124.      */
  125.     private static boolean downloadFile(String subFolderName, String fileAdress) throws IOException {
  126.         String fileUri = mainUrl + fileAdress;
  127.         fileAdress = fileAdress.replaceFirst("video", subFolderName.split("_")[0]);
  128.         if(fileAdress.contains("/")){//if jpg make filename shorter  and without '/'
  129.             fileAdress = "_"+fileAdress.split("/")[2];
  130.         }
  131.  
  132.         File targetFile = new File(desktopVideo+subFolderName+fileAdress);
  133.         if(!targetFile.exists()){
  134.             FileUtils.copyInputStreamToFile(getFile(fileUri), targetFile);
  135.             return true;
  136.         }
  137.         return false;
  138.     }
  139.  
  140.     private static String subFolderName(String subUrl){
  141.         return subUrl.substring(6, subUrl.length() - 4);
  142.     }
  143.  
  144.     private static InputStream getFile(String uri) throws IOException {
  145.         HttpEntity entity = getPageWithAutorization(uri);
  146.         return entity.getContent();
  147.     }
  148.     private static String getFileAdress(String subSubPage) throws IOException {
  149.         Document doc = getDocumentFromPage(mainUrl+subSubPage);
  150.         Elements elements = doc.select("a[id=player]");
  151.         return elements.get(0).attr("href");
  152.     }
  153.  
  154.     private static HttpEntity getPageWithAutorization(String uri) throws IOException {
  155.         String encoding = Base64.getEncoder().encodeToString((usernamePassword).getBytes());
  156.         HttpPost httppost = new HttpPost(uri);
  157.         httppost.setHeader("Authorization", "Basic " + encoding);
  158.         log.info("executing request " + httppost.getRequestLine());
  159.  
  160.         DefaultHttpClient httpclient = new DefaultHttpClient();
  161.         HttpResponse response = httpclient.execute(httppost);
  162.         return response.getEntity();
  163.     }
  164.  
  165.     private static Document getDocumentFromPage(String pageAdress) throws IOException {
  166.         HttpEntity entity = getPageWithAutorization(pageAdress);
  167.         String result = IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8);
  168.         return Jsoup.parse(result);
  169.     }
  170.  
  171.  
  172.     static class AdressAndImage {
  173.         String adress;
  174.         String image;
  175.         @Override
  176.         public String toString() {
  177.             return "AdressAndImage{" +
  178.                     "adress='" + adress + '\'' +
  179.                     ", image='" + image + '\'' +
  180.                     '}';
  181.         }
  182.     }
  183.  
  184. }
Add Comment
Please, Sign In to add comment