//This program is used to essentially download every Cyanide and Happiness comic
//By entering the first link of the comic's 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
//v.1.0.0
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 CHDownloader {
@SuppressWarnings("finally")
public static void main(String[] args) throws IOException {
System.out.println("About CHDownloader:");
System.out.println("This program will essentially download every comic from Cyanide and Happiness");
System.out.println("It will start at your link, then move to PREVIOUS pages");
System.out.println("Enter the Cyanide and Happiness comic number to start with (It's found in the URL. Ex 2776)");
Scanner userInput = new Scanner(System.in);
int theURL = userInput.nextInt();
System.out.println("Press Ctrl + C to stop the program");
URL link = new URL("http://www.explosm.net/comics/"+theURL+"/");//Created the first URL using the link above
boolean stop = false;
while( stop == false){
if( theURL == 14){//The comic ends at comic 15. This if statement will stop the program
stop = true;
System.out.println("All images downloaded");
}
try{
//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 section finds the image to download
Scanner 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 != 1)){
theImage = sc2.findWithinHorizon("[h][t][t][p][:][/][/][w]?[w]?[w]?[.]?explosm.net/db/files/Comics/.*?[.][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(".*[.][jJ][pP][gG]")){
imageType = ".jpg";
}
if(theImage.matches(".*[.][jJ][pP][eE][gG]")){
imageType = ".jpeg";
}
if(theImage.matches(".*[.][pP][nN][gG]")){
imageType = ".png";
}
if(theImage.matches(".*[.][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 chLinkFinal = "CH-" + theURL + imageType;//The string that will be used to name the file
ReadableByteChannel rbc2 = Channels.newChannel(link2.openStream());//Gets the image
FileOutputStream fos2 = new FileOutputStream(chLinkFinal);//The output of the file name
fos2.getChannel().transferFrom(rbc2, 0, 1 << 24);
System.out.println("Image Saved: " + chLinkFinal);//Prints the name of the file that is saved
theURL -= 1;//Decreases the link my 1 to capture the next image
link = new URL("http://www.explosm.net/comics/" + theURL + "/");//Sets up the next page to be read
}
//Some comics using the decreasing number system don't exist. This catch loop skips them
catch(Exception e){
theURL -=1;
link = new URL("http://www.explosm.net/comics/" + theURL + "/");
}
//This finally block allows the program to continue past the catch block
finally{
continue;
}
}
}
}