Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This program is used to essentially download every image from DamnLOL.com
- //By entering the first link of a damnlol page, the program will save that image to your computer
- //then go to the previous page and download that picture
- //It will continue until you stop the program
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.nio.channels.Channels;
- import java.nio.channels.ReadableByteChannel;
- import java.util.Scanner;
- public class DamnDownloader2 {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- int damnLOLLinkNumber = 0;//Used to name every image
- 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.
- URL link = new URL(theURL);//Created the first URL using the link above
- while(1>0){
- //This section downloads the HTML page
- ReadableByteChannel rbc = Channels.newChannel(link.openStream());//Gets the html page
- FileOutputStream fos = new FileOutputStream("page.html");//Creates the output name of the html page to be saved to the computer
- fos.getChannel().transferFrom(rbc, 0, 1 << 24);
- Scanner sc = new Scanner(new FileReader("page.html"));//Takes the downloaded HTML page and sends it to a scanner
- //This section converts the html page to the string
- String contents = "";
- while(sc.hasNextLine() || sc.hasNext()){
- contents = contents + sc.nextLine() + "\n"; //Contents is the html page as a string!
- }
- sc.close();//Closes the scanner file
- //This finds the next url to scan!
- Scanner sc2 = new Scanner(contents);//Takes the string version of the html page and sends it to a new scanner
- int counter = 0;//The previous page's url is on line 8, so I use this int as a counter
- String nextURL = "";
- while((sc2.hasNext() || sc2.hasNextLine()) && (counter != 8)){
- 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
- counter += 1;//Adds one to the counter so it will stop at 8
- }
- 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
- //Ends next url!
- //This section finds the image to download
- sc2 = new Scanner(contents);//Sends the html to a scanner
- String theImage = "";
- int counter2 = 0;//I think that the image url is on line 2 so I need a counter
- while((sc2.hasNext() || sc2.hasNextLine()) && (counter2 != 2)){
- 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
- counter2 += 1;//Adds one to my counter
- }
- //Ends finding image
- //This section determines the file extension
- Scanner sc3 = new Scanner(theImage);
- String imageType = "";
- if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][j][p][g]")){
- imageType = ".jpg";
- }
- if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][p][n][g]")){
- imageType = ".png";
- }
- if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][g][i][f]")){
- imageType = ".gif";
- }
- //This section saves the image itself
- URL link2 = new URL(theImage);//A new URL is created with the image's url that was found
- String damnLOLLinkFinal = "damnlolimage-" + damnLOLLinkNumber + imageType;//The string that will be used to name the file
- ReadableByteChannel rbc2 = Channels.newChannel(link2.openStream());//Gets the image
- FileOutputStream fos2 = new FileOutputStream(damnLOLLinkFinal);//The output of the file name
- fos2.getChannel().transferFrom(rbc2, 0, 1 << 24);
- System.out.println("Image saved: " + damnLOLLinkFinal);//Prints the name of the file that is saved
- damnLOLLinkNumber += 1;//Adds one so the next image will have a different name
- //Ends saving the image
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement