//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 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 { 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("Your images will have the following names: chimage-0.jpg,chimage-1.jpg etc"); //System.out.println("Please enter your starting number. 0 or 1 is recommended:"); //int chLinkNumber = 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("http://www.explosm.net/comics/"+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 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][:][/][/]www.explosm.net/db/files/Comics/.*[jJpPgG][pPnNiI][eE]?[gGfF]", 0);//This finds the image's url and saves it to a string theImage = sc2.findWithinHorizon("[h][t][t][p][:][/][/]www.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 System.out.println(theImage); } //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; link = new URL("www.explosm.net/comics/"+theURL);//Sets up the next page to be read } } }