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
- //DamnDownloader7 Fixes: Ability to stop if repeats are found
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.IOException;
- import java.net.URL;
- import java.nio.channels.Channels;
- import java.nio.channels.ReadableByteChannel;
- import java.util.Scanner;
- public class DamnDownloader7 {
- public static void main(String[] args) throws IOException {
- System.out.println("About DamnDownloader:");
- System.out.println("This program will essentially download every image from DamnLOL");
- System.out.println("It will start at your link, then move to PREVIOUS pages");
- System.out.println("Enter the DamnLOL URL to start with. Include http and www:");
- Scanner userInput = new Scanner(System.in);
- String theURL = userInput.next();
- System.out.println("Stop the program when a duplicate is found? Yes or No");
- String repeatQuit = userInput.next();
- System.out.println("Press Ctrl + C to stop the program");
- URL link = new URL(theURL);//Created the first URL using the link above
- //Sets up the first filename
- String damnLOLFront = "";
- damnLOLFront = theURL.substring(23, theURL.length()-5);
- String nextURL = "";//Sets up nextURL
- boolean finished = false;//Stops the program when the last page is reached
- boolean startFinish = false;//Stops the program when the last page is reached
- while(finished == false){
- //Stops the program when the last page is reached
- 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
- startFinish = true;
- }
- //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
- while((sc2.hasNext() || sc2.hasNextLine()) && (counter != 8)){
- nextURL = sc2.findWithinHorizon("http://www.damnlol.com/.*[.]html", 0);//This grabs the url and saves it to a string
- theURL = nextURL;
- 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("http://www.damnlol.com/.*[.][jJpPgG][pPnNiI][eE]?[gGfF]", 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
- String imageType = "";
- if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][jJ][pP][gG]")){
- imageType = ".jpg";
- }
- if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][jJ][pP][eE][gG]")){
- imageType = ".jpeg";
- }
- if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][pP][nN][gG]")){
- imageType = ".png";
- }
- if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][gG][iI][Ff]")){
- 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 = damnLOLFront + imageType;//The string that will be used to name the file
- //Skips the file if it exists
- boolean exists = (new File(damnLOLLinkFinal)).exists();
- if(exists){
- if(repeatQuit.equals("Yes") || repeatQuit.equals("yes")){
- System.out.println("Duplicate found. Program Stopped.");
- System.exit(0);
- }
- else{
- System.out.println(damnLOLLinkFinal + " already exists. Skipping.");
- }
- }
- else{
- 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
- //Ends saving the image
- }
- //Sets up new file
- damnLOLFront = nextURL.substring(23, nextURL.length()-5);
- //Quits program when at last page
- if(startFinish == true){
- System.out.println("All images downloaded!");
- System.exit(0);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement