Guest User

Untitled

a guest
Jul 24th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. package imagedownloader;
  2.  
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import org.jsoup.Jsoup;
  6. import org.jsoup.nodes.Document;
  7. import java.util.ArrayList;
  8. import java.util.Arrays;
  9. import java.util.Scanner;
  10. import org.jsoup.Connection.Response;
  11. import org.jsoup.nodes.Element;
  12. import org.jsoup.select.Elements;
  13.  
  14. public class PictureDownloader {
  15.  
  16. static String search;
  17. static String base_url;
  18. static ArrayList<String> pictureList = new ArrayList<>();
  19. static int counter = 0;
  20.  
  21. //Gets tags, url, search for posts
  22. public static void Search() {
  23.  
  24. //WANTED TAGS
  25. String tag = "";
  26. //UNWANTED TAGS
  27. String bad_tags = "";
  28.  
  29. String bad_tag_first = bad_tags;
  30. String bad_tag = bad_tag_first.replaceAll(" ", " -");
  31.  
  32. ArrayList<String> tagList = new ArrayList<>();
  33.  
  34. search = "";
  35. base_url = "https://e621.net/post/index/";
  36. String real_bad_tags = "-" + bad_tag;
  37. String[] parts1 = tag.split(" ");
  38. if ("-".equals(real_bad_tags)) {
  39. real_bad_tags = "";
  40. }
  41. String[] parts2 = real_bad_tags.split(" ");
  42. tagList.addAll(Arrays.asList(parts1));
  43. tagList.addAll(Arrays.asList(parts2));
  44. for (String i : tagList) {
  45. search = search + i + "%20";
  46. }
  47. }
  48.  
  49. //gets all the links from the page(s)
  50. public static void getPosts(int page, int max_pages) throws Exception {
  51. while (page <= max_pages) {
  52. String url = base_url + String.valueOf(page) + "/" + search;
  53. final Document document = Jsoup.connect(url)
  54. .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36")
  55. .referrer("https://google.com")
  56. .get();
  57. for (Element link : document.select(".tooltip-thumb")) {
  58. final String relHref = link.attr("href");
  59. String href = "https://e621.net" + relHref;
  60. get_single_item_data(href);
  61. }
  62. page = page + 1;
  63. }
  64. }
  65.  
  66. //Gets the link of a singular picture
  67. public static void get_single_item_data(String item_url) throws Exception {
  68. Document doc = Jsoup.connect(item_url)
  69. .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36")
  70. .referrer("https://google.com")
  71. .get();
  72. Elements image = doc.select("[src]");
  73. String relHref = image.attr("data-sample_url");
  74. //adds to the linklist
  75. if (!relHref.contains(".swf")) {
  76. pictureList.add(relHref);
  77. }
  78. }
  79.  
  80. //Downloads a single picture
  81. public static void Download() throws IOException {
  82. saveImage(pictureList.get(PictureDownloader.counter), PictureDownloader.counter + ".png");
  83. PictureDownloader.counter = PictureDownloader.counter + 1;
  84. }
  85.  
  86. //Download + save the image function
  87. public static void saveImage(String imageUrl, String destinationFile) throws IOException {
  88. Response resultImageResponse = Jsoup.connect(imageUrl)
  89. .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36")
  90. .referrer("https://google.com")
  91. .maxBodySize(1024 * 1024 * 15) //15.7 MB
  92. .ignoreContentType(true).execute();
  93. FileOutputStream out = (new FileOutputStream(new java.io.File(destinationFile)));
  94. out.write(resultImageResponse.bodyAsBytes());
  95. out.close();
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment