Advertisement
Guest User

Untitled

a guest
Nov 30th, 2017
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.71 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.io.PrintStream;
  9. import java.net.URL;
  10. import org.jsoup.Connection;
  11. import org.jsoup.nodes.Document;
  12. import org.jsoup.nodes.Element;
  13.  
  14. public class Core
  15. {
  16.   private static String IMAGE_DESTINATION_FOLDER = ;
  17.  
  18.   public static class pathFile { public pathFile() {}
  19.    
  20.     public static void mainWrite() { try { File file = new File("path.txt");
  21.         file.createNewFile();
  22.         FileWriter fw = new FileWriter(file);
  23.         BufferedWriter bw = new BufferedWriter(fw);
  24.         if ((Core.Bundle.getPath() != null) && (Core.Bundle.getPath() != "")) {
  25.           bw.write(Core.Bundle.getPath());
  26.           bw.flush();
  27.           bw.close();
  28.           reading.setRead(Core.Bundle.getPath());
  29.         }
  30.         System.out.println(file.createNewFile());
  31.       } catch (IOException e) {
  32.         e.printStackTrace();
  33.       }
  34.     }
  35.    
  36.     public static void mainRead() {
  37.       try {
  38.         File file = new File("path.txt");
  39.         file.createNewFile();
  40.         java.io.FileReader fr = new java.io.FileReader(file);
  41.         BufferedReader br = new BufferedReader(fr);
  42.         String read; if ((read = br.readLine()) != null) {
  43.           Core.Bundle.setPath(read);
  44.           System.out.println(read);
  45.           Interface.t1.setText(read);
  46.         }
  47.         br.close();
  48.       } catch (IOException e) {
  49.         e.printStackTrace(); } }
  50.    
  51.     public static class reading { public static String read;
  52.      
  53.       public reading() {}
  54.      
  55.       public static String getRead() { Core.pathFile.mainWrite();
  56.         return read;
  57.       }
  58.      
  59.       public static void setRead(String reads) { read = reads; }
  60.     } }
  61.  
  62.   public Core() {}
  63.  
  64.   public static class Bundle { public static String path1;
  65.     public static String spath;
  66.    
  67.     public Bundle() {}
  68.    
  69.     public static String getPath() { return path1; }
  70.    
  71.     public static void setPath(String path) {
  72.       path1 = path;
  73.     }
  74.    
  75.     public static String getSPath() { return spath; }
  76.    
  77.     public static void setSPath(String spath1) {
  78.       spath = spath1;
  79.     }
  80.    
  81.     public static boolean getPage() { return page; }
  82.     public static String strURL;
  83.    
  84.     public static void setPage(boolean spage) { page = spage; }
  85.    
  86.     public static String getStrURL(Element imageElement) {
  87.       strURL = getPage() ? imageElement.attr("abs:href") : imageElement.attr("abs:src");
  88.       return strURL; }
  89.    
  90.     public static boolean page; }
  91.  
  92.   public static void makeFolder(String lines, int i) { String path = lines.substring(lines.lastIndexOf("/") + 1);
  93.     String[] pathf = path.split(".html");
  94.     String spath = pathf[0];
  95.     System.out.println("1");
  96.     Bundle.setSPath(spath);
  97.     System.out.println("2");
  98.     File theDir = new File(Bundle.getPath() + "/" + spath);
  99.     System.out.println("3");
  100.     if (!theDir.exists()) {
  101.       System.out.println("creating directory: " + theDir.getName());
  102.       boolean result = false;
  103.       try {
  104.         theDir.mkdir();
  105.         result = true;
  106.       }
  107.       catch (SecurityException localSecurityException) {}
  108.       if (result)
  109.         System.out.println("DIR created");
  110.     }
  111.   }
  112.  
  113.   public static void mainSoup(String strURL) throws IOException, InterruptedException {
  114.     System.setProperty("http.agent", "Mozilla/5.0");
  115.     Document document = org.jsoup.Jsoup.connect(strURL).timeout(100000).get();
  116.     org.jsoup.select.Elements imageElements = null;
  117.     if (Bundle.getPage() == true) {
  118.       imageElements = document.select("a[href$=.gif], a[href$=.jpg], a[href$=.png], a[href$=.bmp], a[href$=.webm]");
  119.     } else if (!Bundle.getPage()) {
  120.       imageElements = document.select("img");
  121.     }
  122.     for (Element imageElement : imageElements) {
  123.       String strImageURL = Bundle.getStrURL(imageElement);
  124.       downloadImage(strImageURL);
  125.     }
  126.   }
  127.  
  128.   private static void downloadImage(String strImageURL) { String strImageName = strImageURL.substring(strImageURL.lastIndexOf("/") + 1);
  129.     System.out.println("Saving: " + strImageName + ", from: " + strImageURL);
  130.     try {
  131.       URL urlImage = new URL(strImageURL);
  132.       InputStream in = urlImage.openStream();
  133.       byte[] buffer = new byte['က'];
  134.       int n = -1;
  135.       OutputStream os = new java.io.FileOutputStream(IMAGE_DESTINATION_FOLDER + "/" + Bundle.getSPath() + "/" + strImageName);
  136.       while ((n = in.read(buffer)) != -1) {
  137.         os.write(buffer, 0, n);
  138.       }
  139.       os.close();
  140.       System.out.println("Image saved");
  141.     } catch (IOException e) {
  142.       e.printStackTrace();
  143.     }
  144.   }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement