Advertisement
thatguyandrew1992

DamnDownloader3

Apr 22nd, 2012
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.59 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. //DamnDownloader3 Fixes: Adds support for capitalized extensions - PNG,JPG,GIF
  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 DamnDownloader3 {
  22.  
  23.     /**
  24.      * @param args
  25.      * @throws IOException
  26.      */
  27.     public static void main(String[] args) throws IOException {
  28.         int damnLOLLinkNumber = 0;//Used to name every image
  29.  
  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(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.             //This section converts the html page to the string
  45.             String contents = "";
  46.             while(sc.hasNextLine() || sc.hasNext()){
  47.                 contents = contents + sc.nextLine() + "\n"; //Contents is the html page as a string!
  48.             }
  49.             sc.close();//Closes the scanner file
  50.            
  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.             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 != 2)){
  70.             theImage = sc2.findWithinHorizon("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][jJpPgG][pPnNiI][gGfF]", 0);//This finds the image's url and saves it to a string
  71.             counter2 += 1;//Adds one to my counter
  72.             }
  73.             //Ends finding image
  74.  
  75.            
  76.            
  77.            
  78.             //This section determines the file extension
  79.                 Scanner sc3 = new Scanner(theImage);
  80.                 String imageType = "";
  81.                 if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][jJ][pP][gG]")){
  82.                     imageType = ".jpg";
  83.                 }
  84.                 if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][pP][nN][gG]")){
  85.                     imageType = ".png";
  86.                 }
  87.                 if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][gG][iI][Ff]")){
  88.                     imageType = ".gif";
  89.                 }
  90.                
  91.            
  92.            
  93.            
  94.             //This section saves the image itself
  95.             URL link2 = new URL(theImage);//A new URL is created with the image's url that was found
  96.             String damnLOLLinkFinal = "damnlolimage-" + damnLOLLinkNumber + imageType;//The string that will be used to name the file
  97.    
  98.             ReadableByteChannel rbc2 = Channels.newChannel(link2.openStream());//Gets the image
  99.             FileOutputStream fos2 = new FileOutputStream(damnLOLLinkFinal);//The output of the file name
  100.             fos2.getChannel().transferFrom(rbc2, 0, 1 << 24);
  101.             System.out.println("Image saved: " + damnLOLLinkFinal);//Prints the name of the file that is saved
  102.             damnLOLLinkNumber += 1;//Adds one so the next image will have a different name
  103.             //Ends saving the image
  104.        
  105.         }
  106.     }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement