Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

CHDownloader

By: thatguyandrew1992 on Apr 24th, 2012  |  syntax: Java  |  size: 5.03 KB  |  hits: 48  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. import java.io.FileOutputStream;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.net.URL;
  9. import java.nio.channels.Channels;
  10. import java.nio.channels.ReadableByteChannel;
  11. import java.util.Scanner;
  12.  
  13.  
  14. public class CHDownloader {
  15.  
  16.        
  17.         public static void main(String[] args) throws IOException {
  18.                 System.out.println("About CHDownloader:");
  19.                 System.out.println("This program will essentially download every comic from Cyanide and Happiness");
  20.                 System.out.println("It will start at your link, then move to PREVIOUS pages");
  21.                 System.out.println("Enter the Cyanide and Happiness comic number to start with It's found in the URL. Ex 2776");
  22.                 Scanner userInput = new Scanner(System.in);
  23.                 int theURL = userInput.nextInt();
  24.                 //System.out.println("Your images will have the following names: chimage-0.jpg,chimage-1.jpg etc");
  25.                 //System.out.println("Please enter your starting number. 0 or 1 is recommended:");
  26.                 //int chLinkNumber = userInput.nextInt();
  27.                 System.out.println("Press Ctrl + C to stop the program");
  28.                
  29.                 //int damnLOLLinkNumber = 0;//Used to name every image
  30.                 //String theURL = "http://www.damnlol.com/i-shall-call-him-18574.html";//The first page the program will start at. It will go to every PREVIOUS page.
  31.                 URL link = new URL("http://www.explosm.net/comics/"+theURL+"/");//Created the first URL using the link above
  32.  
  33.                 while(1>0){
  34.                
  35.                         //This section downloads the HTML page
  36.                     ReadableByteChannel rbc = Channels.newChannel(link.openStream());//Gets the html page
  37.                     FileOutputStream fos = new FileOutputStream("page.html");//Creates the output name of the html page to be saved to the computer
  38.                     fos.getChannel().transferFrom(rbc, 0, 1 << 24);
  39.                     Scanner sc = new Scanner(new FileReader("page.html"));//Takes the downloaded HTML page and sends it to a scanner
  40.  
  41.                    
  42.                    
  43.                                                                                                                                                                
  44.                    
  45.                     //This section converts the html page to the string
  46.                     String contents = "";
  47.                         while(sc.hasNextLine() || sc.hasNext()){
  48.                                 contents = contents + sc.nextLine() + "\n"; //Contents is the html page as a string!
  49.                         }
  50.                         sc.close();//Closes the scanner file
  51.                        
  52.                        
  53.                         //This finds the next url to scan!
  54.                         /*Scanner sc2 = new Scanner(contents);//Takes the string version of the html page and sends it to a new scanner
  55.                         int counter = 0;//The previous page's url is on line 8, so I use this int as a counter
  56.                         String nextURL = "";
  57.                         while((sc2.hasNext() || sc2.hasNextLine()) && (counter != 8)){
  58.                         nextURL = sc2.findWithinHorizon("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][h][t][m][l]", 0);//This grabs the url and saves it to a string
  59.                         counter += 1;//Adds one to the counter so it will stop at 8
  60.                         }
  61.                         link = new URL(nextURL);//This now sets link as the newly found url so when the program starts back at the top it will now download this HTML page
  62.                         //Ends next url!*/
  63.                        
  64.                        
  65.                         //This section finds the image to download
  66.                         Scanner sc2 = new Scanner(contents);//Sends the html to a scanner
  67.                         String theImage = "";
  68.                         int counter2 = 0;//I think that the image url is on line 2 so I need a counter
  69.                         while((sc2.hasNext() || sc2.hasNextLine()) && (counter2 != 1)){
  70.                         //theImage = sc2.findWithinHorizon("[h][t][t][p][:][/][/]www.explosm.net/db/files/Comics/.*[jJpPgG][pPnNiI][eE]?[gGfF]", 0);//This finds the image's url and saves it to a string
  71.                         theImage = sc2.findWithinHorizon("[h][t][t][p][:][/][/]www.explosm.net/db/files/Comics/.*[.][jJpPgG][pPnNiI][eE]?[gGfF]", 0);//This finds the image's url and saves it to a string
  72.                         counter2 += 1;//Adds one to my counter
  73.                         System.out.println(theImage);
  74.                         }
  75.                         //Ends finding image
  76.  
  77.                        
  78.                        
  79.                        
  80.                         //This section determines the file extension
  81.                                 String imageType = "";
  82.                                 if(theImage.matches(".*[.][jJ][pP][gG]")){
  83.                                         imageType = ".jpg";
  84.                                 }
  85.                                 if(theImage.matches(".*[.][jJ][pP][eE][gG]")){
  86.                                         imageType = ".jpeg";
  87.                                 }
  88.                                 if(theImage.matches(".*[.][pP][nN][gG]")){
  89.                                         imageType = ".png";
  90.                                 }
  91.                                 if(theImage.matches(".*[.][gG][iI][Ff]")){
  92.                                         imageType = ".gif";
  93.                                 }
  94.                                
  95.                        
  96.                        
  97.                        
  98.                         //This section saves the image itself
  99.                         URL link2 = new URL(theImage);//A new URL is created with the image's url that was found
  100.                         String chLinkFinal = "CH-" + theURL + imageType;//The string that will be used to name the file
  101.        
  102.                     ReadableByteChannel rbc2 = Channels.newChannel(link2.openStream());//Gets the image
  103.                     FileOutputStream fos2 = new FileOutputStream(chLinkFinal);//The output of the file name
  104.                     fos2.getChannel().transferFrom(rbc2, 0, 1 << 24);
  105.                     System.out.println("Image Saved: " + chLinkFinal);//Prints the name of the file that is saved
  106.                     theURL -= 1;
  107.                     link = new URL("www.explosm.net/comics/"+theURL);//Sets up the next page to be read
  108.                
  109.                 }
  110.         }
  111.  
  112. }