Advertisement
thatguyandrew1992

DamnDownloader5

Apr 23rd, 2012
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.36 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. //DamnDownloader5 Fixes: Adds support for jpeg extension
  6. import java.io.BufferedReader;
  7. import java.io.BufferedWriter;
  8. import java.io.FileOutputStream;
  9. import java.io.FileReader;
  10. import java.io.FileWriter;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.InputStreamReader;
  14. import java.net.MalformedURLException;
  15. import java.net.URL;
  16. import java.nio.channels.Channels;
  17. import java.nio.channels.ReadableByteChannel;
  18. import java.util.Scanner;
  19.  
  20.  
  21. public class DamnDownloader5 {
  22.  
  23.    
  24.     public static void main(String[] args) throws IOException {
  25.         System.out.println("About DamnDownloader5:");
  26.         System.out.println("This program will essentially download every image from DamnLOL");
  27.         System.out.println("It will start at your link, then move to PREVIOUS pages");
  28.         System.out.println("Enter the DamnLOL URL to start with. Include http and www:");
  29.         Scanner userInput = new Scanner(System.in);
  30.         String theURL = userInput.next();
  31.         System.out.println("Your images will have the following names: damnlolimage-0.jpg,damnlolimage-1.jpg etc");
  32.         System.out.println("Please enter your starting number. 0 or 1 is recommended:");
  33.         int damnLOLLinkNumber = userInput.nextInt();
  34.         System.out.println("Press Ctrl + C to stop the program");
  35.        
  36.         //int damnLOLLinkNumber = 0;//Used to name every image
  37.         //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.
  38.         URL link = new URL(theURL);//Created the first URL using the link above
  39.  
  40.         while(1>0){
  41.        
  42.             //This section downloads the HTML page
  43.             ReadableByteChannel rbc = Channels.newChannel(link.openStream());//Gets the html page
  44.             FileOutputStream fos = new FileOutputStream("page.html");//Creates the output name of the html page to be saved to the computer
  45.             fos.getChannel().transferFrom(rbc, 0, 1 << 24);
  46.             Scanner sc = new Scanner(new FileReader("page.html"));//Takes the downloaded HTML page and sends it to a scanner
  47.            
  48.            
  49.                                                                                    
  50.            
  51.             //This section converts the html page to the string
  52.             String contents = "";
  53.             while(sc.hasNextLine() || sc.hasNext()){
  54.                 contents = contents + sc.nextLine() + "\n"; //Contents is the html page as a string!
  55.             }
  56.             sc.close();//Closes the scanner file
  57.            
  58.            
  59.            
  60.             //This finds the next url to scan!
  61.             Scanner sc2 = new Scanner(contents);//Takes the string version of the html page and sends it to a new scanner
  62.             int counter = 0;//The previous page's url is on line 8, so I use this int as a counter
  63.             String nextURL = "";
  64.             while((sc2.hasNext() || sc2.hasNextLine()) && (counter != 8)){
  65.             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
  66.             counter += 1;//Adds one to the counter so it will stop at 8
  67.             }
  68.             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
  69.             //Ends next url!
  70.            
  71.            
  72.             //This section finds the image to download
  73.             sc2 = new Scanner(contents);//Sends the html to a scanner
  74.             String theImage = "";
  75.             int counter2 = 0;//I think that the image url is on line 2 so I need a counter
  76.             while((sc2.hasNext() || sc2.hasNextLine()) && (counter2 != 2)){
  77.             theImage = sc2.findWithinHorizon("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][jJpPgG][pPnNiI][eE]?[gGfF]", 0);//This finds the image's url and saves it to a string
  78.             counter2 += 1;//Adds one to my counter
  79.             }
  80.             //Ends finding image
  81.  
  82.            
  83.            
  84.            
  85.             //This section determines the file extension
  86.                 Scanner sc3 = new Scanner(theImage);
  87.                 String imageType = "";
  88.                 if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][jJ][pP][gG]")){
  89.                     imageType = ".jpg";
  90.                 }
  91.                 if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][jJ][pP][eE][gG]")){
  92.                     imageType = ".jpeg";
  93.                 }
  94.                 if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][pP][nN][gG]")){
  95.                     imageType = ".png";
  96.                 }
  97.                 if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][gG][iI][Ff]")){
  98.                     imageType = ".gif";
  99.                 }
  100.                
  101.            
  102.            
  103.            
  104.             //This section saves the image itself
  105.             URL link2 = new URL(theImage);//A new URL is created with the image's url that was found
  106.             String damnLOLLinkFinal = "damnlolimage-" + damnLOLLinkNumber + imageType;//The string that will be used to name the file
  107.    
  108.             ReadableByteChannel rbc2 = Channels.newChannel(link2.openStream());//Gets the image
  109.             FileOutputStream fos2 = new FileOutputStream(damnLOLLinkFinal);//The output of the file name
  110.             fos2.getChannel().transferFrom(rbc2, 0, 1 << 24);
  111.             System.out.println("Image Saved: " + damnLOLLinkFinal);//Prints the name of the file that is saved
  112.             damnLOLLinkNumber += 1;//Adds one so the next image will have a different name
  113.             //Ends saving the image
  114.        
  115.         }
  116.     }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement