Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.net.URL;
- import java.io.PrintWriter;
- import java.io.File;
- import javax.imageio.ImageIO;
- import java.awt.image.BufferedImage;
- import java.util.HashSet;
- /**
- * @author Moshe Kaplan [email protected]
- * @version 2011.03.26
- * @since 1.6
- *
- * The purpose of this class is to download every picture on a thread
- */
- public class Downloader {
- /**
- * A simple main class to test functionality.
- *
- * @param args [0] The first URL to download a comic from (oldest comic of the series, or the first one you're missing)
- */
- public static void main(String[] args)
- throws java.net.MalformedURLException, java.io.IOException {
- if (args.length < 1) {
- System.out.println("You must put in a URL as a source");
- System.exit(1);
- }
- Downloader test = new Downloader();
- String next = test.getPage(args[0]);
- while (!next.equals(""))
- next = test.getPage(next);
- }
- /**
- * Downloads a page, the image, and returns the URL of the next page
- *
- * Downloads page. Scans the page for the URL of the image. Parses out the
- * date of the comic, and calls gifURLtoFile to download and save the
- * picture. Scans the page for the URL of the Next button, and returns that
- *
- * @param surl A string representation of a URL to download
- * @return The next page.
- *
- */
- public String getPage(String surl) {
- String nextURL = "";
- try {
- URL url = new URL(surl);
- try {
- Scanner scan = new Scanner(url.openStream());
- while (scan.hasNextLine()) {
- String data = scan.nextLine();
- // find URL for next page
- if (data.matches(".*title=\"Next Page.*")) {
- // find last href=" , from there, till next "
- int y = data.lastIndexOf("Next Page");
- int x = data.substring(0,y).lastIndexOf("href=");
- nextURL = data.substring(x+6);
- nextURL = nextURL.substring(0,nextURL.indexOf("\""));
- }
- // now to find the URL of the image
- while (data.matches(".*src=\".*\".*")) {
- //System.out.println(data);
- // extract URL
- int x = data.indexOf("src=\"");
- int y = data.indexOf("\"", x + 5);
- String fileURL = data.substring(x + 5, y);
- data = data.substring(y);
- if( fileURL.matches(".*fohguild.org/forums/.*") || fileURL.matches(".*fohguild.org/forums/.*") ||
- fileURL.matches(".*scorecardresearch.com.*")|| fileURL.matches(".*pixel.quantserve.com.*"))
- break;
- // extract format
- String format = fileURL.substring(fileURL.lastIndexOf(".") + 1);
- // Save picture to file
- gifURLtoFile(new URL(fileURL), number.toString(), format);
- }
- }
- } catch (java.io.IOException e) {
- System.out.println("Unable to connect to: " + surl);
- }
- } catch (java.net.MalformedURLException e) {
- System.out.println("Invalid URL");
- System.out.println(surl);
- }
- return nextURL;
- }
- /**
- * Saves a gif from a URL to a file
- *
- * @param url The URL of the gif picture
- * @param filename The name to save the gif as.
- */
- public void gifURLtoFile(URL url, String filename, String format) {
- try {
- BufferedImage temp = ImageIO.read(url);
- if(temp != null && !hashes.contains(url.hashCode()) ){
- System.out.println("Now Downloading: " + url + " as " + number);
- ImageIO.write(temp, format, new File(filename + "." + format));
- ++number;
- hashes.add(url.hashCode());
- }
- } catch (Exception e) {
- System.out.println("Error Saving file at: " + url + " as " + filename);
- }
- }
- private Integer number=0;
- private HashSet<Integer> hashes = new HashSet<Integer>();
- }
Advertisement
Add Comment
Please, Sign In to add comment