thatguyandrew1992

DamnLOL Downloader

Apr 21st, 2012
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileOutputStream;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.net.MalformedURLException;
  10. import java.net.URL;
  11. import java.nio.channels.Channels;
  12. import java.nio.channels.ReadableByteChannel;
  13. import java.util.Scanner;
  14.  
  15.  
  16. public class DamnDownloader {
  17.  
  18.     /**
  19.      * @param args
  20.      * @throws IOException
  21.      */
  22.     public static void main(String[] args) throws IOException {
  23.         int damnLOLLinkNumber = 0;
  24.         String theURL = "http://www.damnlol.com/true-friendship-18482.html";
  25.        
  26.        
  27.         URL link = new URL(theURL);
  28.  
  29.         while(1>0){
  30.        
  31.             ReadableByteChannel rbc = Channels.newChannel(link.openStream());
  32.             FileOutputStream fos = new FileOutputStream("link.html");
  33.             fos.getChannel().transferFrom(rbc, 0, 1 << 24);
  34.             System.out.println("File saved!");
  35.            
  36.             Scanner sc = new Scanner(new FileReader("link.html"));
  37.            
  38.            
  39.                                                                                    
  40.            
  41.            
  42.             String contents = "";
  43.             while(sc.hasNextLine() || sc.hasNext()){
  44.                 contents = contents + sc.nextLine() + "\n"; //Contents is the html page as a string!
  45.             }
  46.             sc.close();
  47.            
  48.             //This finds the next url to scan!
  49.             Scanner sc2 = new Scanner(contents);
  50.             int counter = 0;
  51.             String nextURL = "";
  52.             while((sc2.hasNext() || sc2.hasNextLine()) && (counter != 8)){
  53.             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);
  54.             //System.out.println(nextURL);
  55.             counter += 1;
  56.             }
  57.             link = new URL(nextURL);
  58.             //Ends next url!
  59.            
  60.            
  61.             //Finds the image to download
  62.             sc2 = new Scanner(contents);
  63.             String theImage = "";
  64.             int counter2 = 0;
  65.             while((sc2.hasNext() || sc2.hasNextLine()) && (counter2 != 2)){
  66.             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);
  67.             //System.out.println(theImage);
  68.             counter2 += 1;
  69.             }
  70.             //Ends finding image
  71.            
  72.             //Finds the image type
  73.                 Scanner sc3 = new Scanner(theImage);
  74.                 String imageType = "";
  75.                 if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][j][p][g]")){
  76.                     imageType = ".jpg";
  77.                 }
  78.                 if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][p][n][g]")){
  79.                     imageType = ".png";
  80.                 }
  81.                 if(theImage.matches("[h][t][t][p][:][/][/][w][w][w][.][d][a][m][n][l][o][l][.][c][o][m][/].*[.][g][i][f]")){
  82.                     imageType = ".gif";
  83.                 }
  84.                 /*else{
  85.                     imageType = ".jpg";
  86.                     System.out.println("Image type not found!");
  87.                 }*/
  88.                
  89.            
  90.            
  91.            
  92.             //Saves the image
  93.             URL link2 = new URL(theImage);
  94.             String damnLOLLink = "damnlolimage-";
  95.             String damnLOLLinkFinal = damnLOLLink + damnLOLLinkNumber + imageType;
  96.    
  97.             ReadableByteChannel rbc2 = Channels.newChannel(link2.openStream());
  98.             FileOutputStream fos2 = new FileOutputStream(damnLOLLinkFinal);
  99.             fos2.getChannel().transferFrom(rbc2, 0, 1 << 24);
  100.             System.out.println("File saved!");
  101.             damnLOLLinkNumber += 1;
  102.             //Ends saving the image
  103.        
  104.         }
  105.        
  106.         //Writing the file
  107.         /*BufferedWriter bw = new BufferedWriter ( new FileWriter("test.txt"));
  108.         bw.write(contents);
  109.         bw.close();*/
  110.         //End Writing the file
  111.     }
  112.  
  113. }
Add Comment
Please, Sign In to add comment