mkaplan

Untitled

Apr 3rd, 2011
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.62 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.net.URL;
  3. import java.io.PrintWriter;
  4. import java.io.File;
  5. import javax.imageio.ImageIO;
  6. import java.awt.image.BufferedImage;
  7. import java.util.HashSet;
  8.  
  9. /**
  10.  * @author Moshe Kaplan [email protected]
  11.  * @version 2011.03.26
  12.  * @since 1.6
  13.  *
  14.  *      The purpose of this class is to download every picture on a thread
  15.  */
  16. public class Downloader {
  17.  
  18.     /**
  19.      * A simple main class to test functionality.
  20.      *
  21.      * @param args [0] The first URL to download a comic from (oldest comic of the series, or the first one you're missing)
  22.      */
  23.     public static void main(String[] args)
  24.             throws java.net.MalformedURLException, java.io.IOException {
  25.         if (args.length < 1) {
  26.             System.out.println("You must put in a URL as a source");
  27.             System.exit(1);
  28.         }
  29.         Downloader test = new Downloader();
  30.         String next = test.getPage(args[0]);
  31.         while (!next.equals(""))
  32.             next = test.getPage(next);
  33.     }
  34.  
  35.     /**
  36.      * Downloads a page, the image, and returns the URL of the next page
  37.      *
  38.      * Downloads page. Scans the page for the URL of the image. Parses out the
  39.      * date of the comic, and calls gifURLtoFile to download and save the
  40.      * picture. Scans the page for the URL of the Next button, and returns that
  41.      *
  42.      * @param surl A string representation of a URL to download
  43.      * @return The next page.
  44.      *
  45.      */
  46.     public String getPage(String surl) {
  47.         String nextURL = "";
  48.         try {
  49.             URL url = new URL(surl);
  50.             try {
  51.                 Scanner scan = new Scanner(url.openStream());
  52.                 while (scan.hasNextLine()) {
  53.                     String data = scan.nextLine();
  54.                     // find URL for next page
  55.                     if (data.matches(".*title=\"Next Page.*")) {
  56.                         // find last href=" , from there, till next "
  57.                         int y = data.lastIndexOf("Next Page");
  58.                         int x = data.substring(0,y).lastIndexOf("href=");
  59.                        
  60.                         nextURL = data.substring(x+6);
  61.                         nextURL = nextURL.substring(0,nextURL.indexOf("\""));
  62.                     }
  63.                     // now to find the URL of the image
  64.                     while (data.matches(".*src=\".*\".*")) {
  65.                         //System.out.println(data);
  66.                         // extract URL
  67.                         int x = data.indexOf("src=\"");
  68.                         int y = data.indexOf("\"", x + 5);
  69.                         String fileURL = data.substring(x + 5, y);
  70.                         data = data.substring(y);
  71.                         if( fileURL.matches(".*fohguild.org/forums/.*") || fileURL.matches(".*fohguild.org/forums/.*") ||
  72.                             fileURL.matches(".*scorecardresearch.com.*")|| fileURL.matches(".*pixel.quantserve.com.*"))
  73.                             break;
  74.                         // extract format
  75.                         String format = fileURL.substring(fileURL.lastIndexOf(".") + 1);
  76.                         // Save picture to file
  77.                         gifURLtoFile(new URL(fileURL), number.toString(), format);
  78.                        
  79.                     }
  80.                    
  81.                 }
  82.  
  83.             } catch (java.io.IOException e) {
  84.                 System.out.println("Unable to connect to: " + surl);
  85.             }
  86.  
  87.         } catch (java.net.MalformedURLException e) {
  88.             System.out.println("Invalid URL");
  89.             System.out.println(surl);
  90.         }
  91.         return nextURL;
  92.     }
  93.  
  94.     /**
  95.      * Saves a gif from a URL to a file
  96.      *
  97.      * @param url The URL of the gif picture
  98.      * @param filename The name to save the gif as.
  99.      */
  100.     public void gifURLtoFile(URL url, String filename, String format) {
  101.         try {
  102.             BufferedImage temp = ImageIO.read(url);
  103.             if(temp != null && !hashes.contains(url.hashCode()) ){
  104.                 System.out.println("Now Downloading: " + url + " as " + number);
  105.                 ImageIO.write(temp, format, new File(filename + "." + format));
  106.                 ++number;
  107.                 hashes.add(url.hashCode());
  108.             }
  109.         } catch (Exception e) {
  110.             System.out.println("Error Saving file at: " + url + " as " + filename);
  111.         }
  112.     }
  113.    
  114.     private Integer number=0;
  115.     private HashSet<Integer> hashes = new HashSet<Integer>();
  116.        
  117. }
Advertisement
Add Comment
Please, Sign In to add comment