Advertisement
thatguyandrew1992

DamnDownloader2

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