thatguyandrew1992

CHDownloader

Apr 25th, 2012
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.26 KB | None | 0 0
  1. //This program is used to essentially download every Cyanide and Happiness comic
  2. //By entering the first link of the comic's page, the program will save that image to your computer
  3. //then go to the previous page and download that picture
  4. //It will continue until you stop the program
  5. //v.1.0.0
  6. import java.io.FileOutputStream;
  7. import java.io.FileReader;
  8. import java.io.IOException;
  9. import java.net.URL;
  10. import java.nio.channels.Channels;
  11. import java.nio.channels.ReadableByteChannel;
  12. import java.util.Scanner;
  13.  
  14.  
  15. public class CHDownloader {
  16.  
  17.    
  18.     @SuppressWarnings("finally")
  19.     public static void main(String[] args) throws IOException {
  20.         System.out.println("About CHDownloader:");
  21.         System.out.println("This program will essentially download every comic from Cyanide and Happiness");
  22.         System.out.println("It will start at your link, then move to PREVIOUS pages");
  23.         System.out.println("Enter the Cyanide and Happiness comic number to start with (It's found in the URL. Ex 2776)");
  24.         Scanner userInput = new Scanner(System.in);
  25.         int theURL = userInput.nextInt();
  26.         System.out.println("Press Ctrl + C to stop the program");
  27.  
  28.        
  29.         URL link = new URL("http://www.explosm.net/comics/"+theURL+"/");//Created the first URL using the link above
  30.         boolean stop = false;
  31.         while( stop == false){
  32.             if( theURL == 14){//The comic ends at comic 15. This if statement will stop the program
  33.                 stop = true;
  34.                 System.out.println("All images downloaded");
  35.             }
  36.             try{
  37.             //This section downloads the HTML page
  38.             ReadableByteChannel rbc = Channels.newChannel(link.openStream());//Gets the html page
  39.             FileOutputStream fos = new FileOutputStream("page.html");//Creates the output name of the html page to be saved to the computer
  40.             fos.getChannel().transferFrom(rbc, 0, 1 << 24);
  41.             Scanner sc = new Scanner(new FileReader("page.html"));//Takes the downloaded HTML page and sends it to a scanner
  42.  
  43.  
  44.            
  45.            
  46.                                                                                    
  47.            
  48.             //This section converts the html page to the string
  49.             String contents = "";
  50.             while(sc.hasNextLine() || sc.hasNext()){
  51.                 contents = contents + sc.nextLine() + "\n"; //Contents is the html page as a string!
  52.             }
  53.             sc.close();//Closes the scanner file
  54.  
  55.            
  56.    
  57.             //This section finds the image to download
  58.             Scanner sc2 = new Scanner(contents);//Sends the html to a scanner
  59.             String theImage = "";
  60.             int counter2 = 0;//I think that the image url is on line 2 so I need a counter
  61.             while((sc2.hasNext() || sc2.hasNextLine()) && (counter2 != 1)){
  62.             theImage = sc2.findWithinHorizon("[h][t][t][p][:][/][/][w]?[w]?[w]?[.]?explosm.net/db/files/Comics/.*?[.][jJpPgG][pPnNiI][eE]?[gGfF]", 0);//This finds the image's url and saves it to a string
  63.             counter2 += 1;//Adds one to my counter
  64.             }
  65.             //Ends finding image
  66.            
  67.            
  68.            
  69.             //This section determines the file extension
  70.                 String imageType = "";
  71.                 if(theImage.matches(".*[.][jJ][pP][gG]")){
  72.                     imageType = ".jpg";
  73.                 }
  74.                 if(theImage.matches(".*[.][jJ][pP][eE][gG]")){
  75.                     imageType = ".jpeg";
  76.                 }
  77.                 if(theImage.matches(".*[.][pP][nN][gG]")){
  78.                     imageType = ".png";
  79.                 }
  80.                 if(theImage.matches(".*[.][gG][iI][Ff]")){
  81.                     imageType = ".gif";
  82.                 }
  83.            
  84.            
  85.            
  86.             //This section saves the image itself
  87.             URL link2 = new URL(theImage);//A new URL is created with the image's url that was found
  88.             String chLinkFinal = "CH-" + theURL + imageType;//The string that will be used to name the file
  89.    
  90.             ReadableByteChannel rbc2 = Channels.newChannel(link2.openStream());//Gets the image
  91.             FileOutputStream fos2 = new FileOutputStream(chLinkFinal);//The output of the file name
  92.             fos2.getChannel().transferFrom(rbc2, 0, 1 << 24);
  93.             System.out.println("Image Saved: " + chLinkFinal);//Prints the name of the file that is saved
  94.             theURL -= 1;//Decreases the link my 1 to capture the next image
  95.             link = new URL("http://www.explosm.net/comics/" + theURL + "/");//Sets up the next page to be read
  96.             }
  97.            
  98.             //Some comics using the decreasing number system don't exist. This catch loop skips them
  99.             catch(Exception e){
  100.                 theURL -=1;
  101.                 link = new URL("http://www.explosm.net/comics/" + theURL + "/");
  102.             }
  103.             //This finally block allows the program to continue past the catch block
  104.             finally{
  105.                 continue;
  106.             }
  107.         }
  108.     }
  109.  
  110. }
Add Comment
Please, Sign In to add comment