//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
//DamnDownloader5 Fixes: Adds support for jpeg extension
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 DamnDownloader5 {
public static void main(String[] args) throws IOException {
System.out.println("About DamnDownloader5:");
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("Your images will have the following names: damnlolimage-0.jpg,damnlolimage-1.jpg etc");
System.out.println("Please enter your starting number. 0 or 1 is recommended:");
int damnLOLLinkNumber = userInput.nextInt();
System.out.println("Press Ctrl + C to stop the program");
//int damnLOLLinkNumber = 0;//Used to name every image
//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.
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][/].*[.][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
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][/].*[.][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 = "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
}
}
}