Advertisement
thatguyandrew1992

DamnDownloader7

Apr 27th, 2012
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.83 KB | None | 0 0
  1. //This program is used to essentially download every image from DamnLOL.com
  2. //By entering the first link of a damnlol 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. //DamnDownloader7 Fixes: Ability to stop if repeats are found
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10. import java.net.URL;
  11. import java.nio.channels.Channels;
  12. import java.nio.channels.ReadableByteChannel;
  13. import java.util.Scanner;
  14.  
  15.  
  16. public class DamnDownloader7 {
  17.  
  18.    
  19.     public static void main(String[] args) throws IOException {
  20.         System.out.println("About DamnDownloader:");
  21.         System.out.println("This program will essentially download every image from DamnLOL");
  22.         System.out.println("It will start at your link, then move to PREVIOUS pages");
  23.         System.out.println("Enter the DamnLOL URL to start with. Include http and www:");
  24.         Scanner userInput = new Scanner(System.in);
  25.         String theURL = userInput.next();
  26.         System.out.println("Stop the program when a duplicate is found? Yes or No");
  27.         String repeatQuit = userInput.next();
  28.         System.out.println("Press Ctrl + C to stop the program");
  29.        
  30.         URL link = new URL(theURL);//Created the first URL using the link above
  31.  
  32.         //Sets up the first filename
  33.         String damnLOLFront = "";
  34.         damnLOLFront = theURL.substring(23, theURL.length()-5);
  35.        
  36.         String nextURL = "";//Sets up nextURL
  37.         boolean finished = false;//Stops the program when the last page is reached
  38.         boolean startFinish = false;//Stops the program when the last page is reached
  39.         while(finished == false){
  40.            
  41.             //Stops the program when the last page is reached
  42.             if( nextURL.equals("http://www.damnlol.com/gonna-buy-my-girl-one-of-these-1.html")){//The comic ends at comic 15. This if statement will stop the program
  43.                 startFinish = true;
  44.             }
  45.            
  46.             //This section downloads the HTML page
  47.             ReadableByteChannel rbc = Channels.newChannel(link.openStream());//Gets the html page
  48.             FileOutputStream fos = new FileOutputStream("page.html");//Creates the output name of the html page to be saved to the computer
  49.             fos.getChannel().transferFrom(rbc, 0, 1 << 24);
  50.             Scanner sc = new Scanner(new FileReader("page.html"));//Takes the downloaded HTML page and sends it to a scanner
  51.            
  52.            
  53.                                                                                    
  54.            
  55.             //This section converts the html page to the string
  56.             String contents = "";
  57.             while(sc.hasNextLine() || sc.hasNext()){
  58.                 contents = contents + sc.nextLine() + "\n"; //Contents is the html page as a string!
  59.             }
  60.             sc.close();//Closes the scanner file
  61.            
  62.            
  63.            
  64.             //This finds the next url to scan!
  65.             Scanner sc2 = new Scanner(contents);//Takes the string version of the html page and sends it to a new scanner
  66.             int counter = 0;//The previous page's url is on line 8, so I use this int as a counter
  67.             while((sc2.hasNext() || sc2.hasNextLine()) && (counter != 8)){
  68.             nextURL = sc2.findWithinHorizon("http://www.damnlol.com/.*[.]html", 0);//This grabs the url and saves it to a string
  69.             theURL = nextURL;
  70.             counter += 1;//Adds one to the counter so it will stop at 8
  71.             }
  72.             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
  73.             //Ends next url!
  74.            
  75.            
  76.             //This section finds the image to download
  77.             sc2 = new Scanner(contents);//Sends the html to a scanner
  78.             String theImage = "";
  79.             int counter2 = 0;//I think that the image url is on line 2 so I need a counter
  80.             while((sc2.hasNext() || sc2.hasNextLine()) && (counter2 != 2)){
  81.             theImage = sc2.findWithinHorizon("http://www.damnlol.com/.*[.][jJpPgG][pPnNiI][eE]?[gGfF]", 0);//This finds the image's url and saves it to a string
  82.             counter2 += 1;//Adds one to my counter
  83.             }
  84.             //Ends finding image
  85.            
  86.            
  87.            
  88.             //This section determines the file extension
  89.                
  90.                 String imageType = "";
  91.                 if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][jJ][pP][gG]")){
  92.                     imageType = ".jpg";
  93.                 }
  94.                 if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][jJ][pP][eE][gG]")){
  95.                     imageType = ".jpeg";
  96.                 }
  97.                 if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][pP][nN][gG]")){
  98.                     imageType = ".png";
  99.                 }
  100.                 if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][gG][iI][Ff]")){
  101.                     imageType = ".gif";
  102.                 }      
  103.                
  104.            
  105.            
  106.            
  107.             //This section saves the image itself
  108.             URL link2 = new URL(theImage);//A new URL is created with the image's url that was found
  109.             String damnLOLLinkFinal = damnLOLFront + imageType;//The string that will be used to name the file
  110.            
  111.             //Skips the file if it exists
  112.             boolean exists = (new File(damnLOLLinkFinal)).exists();
  113.             if(exists){
  114.                 if(repeatQuit.equals("Yes") || repeatQuit.equals("yes")){
  115.                     System.out.println("Duplicate found. Program Stopped.");
  116.                     System.exit(0);
  117.                 }
  118.                 else{
  119.                     System.out.println(damnLOLLinkFinal + " already exists. Skipping.");
  120.                 }
  121.             }
  122.             else{
  123.                 ReadableByteChannel rbc2 = Channels.newChannel(link2.openStream());//Gets the image
  124.                 FileOutputStream fos2 = new FileOutputStream(damnLOLLinkFinal);//The output of the file name
  125.                 fos2.getChannel().transferFrom(rbc2, 0, 1 << 24);
  126.                 System.out.println("Image Saved: " + damnLOLLinkFinal);//Prints the name of the file that is saved
  127.                 //Ends saving the image
  128.             }
  129.            
  130.             //Sets up new file
  131.             damnLOLFront = nextURL.substring(23, nextURL.length()-5);
  132.            
  133.             //Quits program when at last page
  134.             if(startFinish == true){
  135.                 System.out.println("All images downloaded!");
  136.                 System.exit(0);
  137.             }
  138.            
  139.         }
  140.     }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement