Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package imagedownloader;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Scanner;
- import org.jsoup.Connection.Response;
- import org.jsoup.nodes.Element;
- import org.jsoup.select.Elements;
- public class PictureDownloader {
- static String search;
- static String base_url;
- static ArrayList<String> pictureList = new ArrayList<>();
- static int counter = 0;
- //Gets tags, url, search for posts
- public static void Search() {
- //WANTED TAGS
- String tag = "";
- //UNWANTED TAGS
- String bad_tags = "";
- String bad_tag_first = bad_tags;
- String bad_tag = bad_tag_first.replaceAll(" ", " -");
- ArrayList<String> tagList = new ArrayList<>();
- search = "";
- base_url = "https://e621.net/post/index/";
- String real_bad_tags = "-" + bad_tag;
- String[] parts1 = tag.split(" ");
- if ("-".equals(real_bad_tags)) {
- real_bad_tags = "";
- }
- String[] parts2 = real_bad_tags.split(" ");
- tagList.addAll(Arrays.asList(parts1));
- tagList.addAll(Arrays.asList(parts2));
- for (String i : tagList) {
- search = search + i + "%20";
- }
- }
- //gets all the links from the page(s)
- public static void getPosts(int page, int max_pages) throws Exception {
- while (page <= max_pages) {
- String url = base_url + String.valueOf(page) + "/" + search;
- final Document document = Jsoup.connect(url)
- .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36")
- .referrer("https://google.com")
- .get();
- for (Element link : document.select(".tooltip-thumb")) {
- final String relHref = link.attr("href");
- String href = "https://e621.net" + relHref;
- get_single_item_data(href);
- }
- page = page + 1;
- }
- }
- //Gets the link of a singular picture
- public static void get_single_item_data(String item_url) throws Exception {
- Document doc = Jsoup.connect(item_url)
- .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36")
- .referrer("https://google.com")
- .get();
- Elements image = doc.select("[src]");
- String relHref = image.attr("data-sample_url");
- //adds to the linklist
- if (!relHref.contains(".swf")) {
- pictureList.add(relHref);
- }
- }
- //Downloads a single picture
- public static void Download() throws IOException {
- saveImage(pictureList.get(PictureDownloader.counter), PictureDownloader.counter + ".png");
- PictureDownloader.counter = PictureDownloader.counter + 1;
- }
- //Download + save the image function
- public static void saveImage(String imageUrl, String destinationFile) throws IOException {
- Response resultImageResponse = Jsoup.connect(imageUrl)
- .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36")
- .referrer("https://google.com")
- .maxBodySize(1024 * 1024 * 15) //15.7 MB
- .ignoreContentType(true).execute();
- FileOutputStream out = (new FileOutputStream(new java.io.File(destinationFile)));
- out.write(resultImageResponse.bodyAsBytes());
- out.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment