mkaplan

GoComics.com Downloader

Mar 26th, 2011
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.58 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.  
  8. /**
  9.  * @author Moshe Kaplan [email protected]
  10.  * @version 2011.03.26
  11.  * @since 1.6
  12.  *
  13.  *        The purpose of this class is to download every comic of a series from
  14.  *        gocomics.com. It accomplishes this by finding the comic and
  15.  *        downloading it, and then searching for a next button. When it finds a
  16.  *        next button, it will then return a string of a URL pointing to the
  17.  *        next comic.
  18.  */
  19. public class Downloader {
  20.  
  21.     /**
  22.      * A simple main class to test functionality.
  23.      *
  24.      * @param args [0] The first URL to download a comic from (oldest comic of the series, or the first one you're missing)
  25.      */
  26.     public static void main(String[] args)
  27.             throws java.net.MalformedURLException, java.io.IOException {
  28.         if (args.length < 1) {
  29.             System.out.println("You must put in a URL as a source");
  30.             System.exit(1);
  31.         }
  32.         String next = Downloader.getPage(args[0]);
  33.         while (!next.equals(""))
  34.             next = Downloader.getPage(next);
  35.     }
  36.  
  37.     /**
  38.      * Downloads a page, the image, and returns the URL of the next page
  39.      *
  40.      * Downloads page. Scans the page for the URL of the image. Parses out the
  41.      * date of the comic, and calls gifURLtoFile to download and save the
  42.      * picture. Scans the page for the URL of the Next button, and returns that
  43.      *
  44.      * @param surl A string representation of a URL to download
  45.      * @return The next page.
  46.      *
  47.      */
  48.     public static String getPage(String surl) {
  49.         try {
  50.             URL url = new URL(surl);
  51.             try {
  52.                 Scanner scan = new Scanner(url.openStream());
  53.                 while (scan.hasNextLine()) {
  54.                     String data = scan.nextLine();
  55.                     // now to find the URL of the image
  56.                     if (data.matches(".*http://imgsrv.gocomics.com.*")) {
  57.                         // extract URL
  58.                         int x = data.indexOf("src=\"");
  59.                         int y = data.indexOf("\"", x + 5);
  60.                         String fileURL = data.substring(x + 5, y);
  61.                         // extract title
  62.                         String title = url.getPath().substring(
  63.                                 url.getPath().indexOf("/", 1));
  64.                         System.out.println("Now Downloading: "
  65.                                 + title.replace("/", " "));
  66.                         title = title.replace("/", ""); // title is only the
  67.                                                         // numbers of the date
  68.                                                         // now - example:
  69.                                                         // 20070913
  70.                         // Save gif to file
  71.                         gifURLtoFile(new URL(fileURL), title);
  72.                     }
  73.                     // find URL for next page
  74.                     if (data.matches(".*>Next >></a></span></div>.*")) {
  75.                         // find last href=" , from there, till next "
  76.                         int x = data.lastIndexOf("href=");
  77.                         int y = data.indexOf("\"", x + 6);
  78.                         String nextURL = data.substring(x + 6, y); // partial
  79.                                                                     // URL
  80.                         nextURL = url.getProtocol() + "://" + url.getHost()
  81.                                 + nextURL;
  82.                         // optimization - save on Bandwidth - no need to DL the
  83.                         // rest of the page.
  84.                         return nextURL;
  85.                     }
  86.                 }
  87.  
  88.             } catch (java.io.IOException e) {
  89.                 System.out.println("Unable to connect to: " + surl);
  90.             }
  91.  
  92.         } catch (java.net.MalformedURLException e) {
  93.             System.out.println("Invalid URL");
  94.             System.out.println(surl);
  95.         }
  96.         return "";
  97.     }
  98.  
  99.     /**
  100.      * Saves a gif from a URL to a file
  101.      *
  102.      * @param url The URL of the gif picture
  103.      * @param filename The name to save the gif as.
  104.      */
  105.     public static void gifURLtoFile(URL url, String filename) {
  106.         try {
  107.             ImageIO.write(ImageIO.read(url), "gif", new File(filename + ".gif"));
  108.         } catch (Exception e) {
  109.             System.out.println("Error Saving file at: " + url + " as "
  110.                     + filename);
  111.         }
  112.     }
  113. }
Add Comment
Please, Sign In to add comment