GenuineSounds

Phapit Downloader

Oct 12th, 2014
355
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. package com.genuineminecraft.phapit;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.net.URL;
  7. import java.nio.channels.Channels;
  8. import java.nio.channels.ReadableByteChannel;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. import org.jsoup.Jsoup;
  13. import org.jsoup.nodes.Document;
  14. import org.jsoup.nodes.Element;
  15. import org.jsoup.select.Elements;
  16.  
  17. public class Main {
  18.  
  19.     private static File currentDir;
  20.  
  21.     private static List<Runnable> runners = new ArrayList<>();
  22.  
  23.     public static void main(String[] args) {
  24.         currentDir = new File("downloads");
  25.         currentDir.mkdirs();
  26.         // 149790 is currently the highest pic.
  27.         for (int i = 0; i < 150000; i++)
  28.             runners.add(createRunner(i));
  29.         runners.stream().parallel().forEach(Runnable::run);
  30.     }
  31.  
  32.     public static Runnable createRunner(int i) {
  33.         return () -> {
  34.             File file = new File(currentDir, i + ".jpg");
  35.             if (file.exists())
  36.                 return;
  37.             try {
  38.                 Document doc = Jsoup.connect("http://phapit.com/image/phap" + i).get();
  39.                 Elements imgs = doc.select("img");
  40.                 String image = "";
  41.                 String tags = "";
  42.                 for (Element element : imgs) {
  43.                     if (!element.attr("src").contains("http://images.phapit.com/pic/"))
  44.                         continue;
  45.                     image = element.attr("src");
  46.                     tags = element.attr("alt");
  47.                     break;
  48.                 }
  49.                 if (image.isEmpty() || tags.toLowerCase().contains("child") || tags.toLowerCase().contains("delete"))
  50.                     return;
  51.                 saveFile(i, image);
  52.             }
  53.             catch (IOException e) {}
  54.         };
  55.     }
  56.  
  57.     public static void saveFile(int i, String url) {
  58.         File file = new File(currentDir, i + ".jpg");
  59.         if (file.exists())
  60.             return;
  61.         try (ReadableByteChannel rbc = Channels.newChannel(new URL(url).openStream()); FileOutputStream fos = new FileOutputStream(file);) {
  62.             file.createNewFile();
  63.             fos.getChannel().transferFrom(rbc, 0, 1 << 24);
  64.         }
  65.         catch (Exception e) {}
  66.     }
  67. }
Comments
  • User was banned
Add Comment
Please, Sign In to add comment