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;
- /**
- * @author Moshe Kaplan [email protected]
- * @version 2011.03.26
- * @since 1.6
- *
- * The purpose of this class is to download every comic of a series from
- * gocomics.com. It accomplishes this by finding the comic and
- * downloading it, and then searching for a next button. When it finds a
- * next button, it will then return a string of a URL pointing to the
- * next comic.
- */
- 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);
- }
- String next = Downloader.getPage(args[0]);
- while (!next.equals(""))
- next = Downloader.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 static String getPage(String surl) {
- try {
- URL url = new URL(surl);
- try {
- Scanner scan = new Scanner(url.openStream());
- while (scan.hasNextLine()) {
- String data = scan.nextLine();
- // now to find the URL of the image
- if (data.matches(".*http://imgsrv.gocomics.com.*")) {
- // extract URL
- int x = data.indexOf("src=\"");
- int y = data.indexOf("\"", x + 5);
- String fileURL = data.substring(x + 5, y);
- // extract title
- String title = url.getPath().substring(
- url.getPath().indexOf("/", 1));
- System.out.println("Now Downloading: "
- + title.replace("/", " "));
- title = title.replace("/", ""); // title is only the
- // numbers of the date
- // now - example:
- // 20070913
- // Save gif to file
- gifURLtoFile(new URL(fileURL), title);
- }
- // find URL for next page
- if (data.matches(".*>Next >></a></span></div>.*")) {
- // find last href=" , from there, till next "
- int x = data.lastIndexOf("href=");
- int y = data.indexOf("\"", x + 6);
- String nextURL = data.substring(x + 6, y); // partial
- // URL
- nextURL = url.getProtocol() + "://" + url.getHost()
- + nextURL;
- // optimization - save on Bandwidth - no need to DL the
- // rest of the page.
- return nextURL;
- }
- }
- } 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 "";
- }
- /**
- * 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 static void gifURLtoFile(URL url, String filename) {
- try {
- ImageIO.write(ImageIO.read(url), "gif", new File(filename + ".gif"));
- } catch (Exception e) {
- System.out.println("Error Saving file at: " + url + " as "
- + filename);
- }
- }
- }
Add Comment
Please, Sign In to add comment