Guest User

Newgrounds Audio Portal downloader

a guest
Oct 8th, 2011
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.90 KB | None | 0 0
  1. package ng.dl;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. import java.util.ArrayList;
  6. import java.util.regex.*;
  7.  
  8. public class ngdl {
  9.    
  10.     public static void main(String[] args) throws Exception {
  11.        
  12.         if (args.length != 1) System.out.println("Indiquez le pseudo de l'artiste à télécharger. par exemple, pour http://paragonx9.newgrounds.com/ il faut entrer 'paragonx9'.\nExemple d'utilisation : >java ngdl paragonx9");
  13.         else {
  14.            
  15.             ArrayList<String> links = new ArrayList<String>();
  16.             ArrayList<String> titles = new ArrayList<String>();
  17.            
  18.             // on récupère le code source de la page
  19.             System.out.println("Recuperation du code source de la page...");
  20.             String src = getSourceCode("http://"+args[0]+".newgrounds.com/audio/");
  21.             System.out.println("Code source récupéré.");
  22.            
  23.             // on récupère le nom de l'artiste (titre de la page)
  24.             String artistName = "Unknown Artist";
  25.             Pattern p0 = Pattern.compile("<title>.+'s Audio</title>");
  26.             Matcher matcher0 = p0.matcher(src);
  27.             matcher0.find();
  28.             artistName = matcher0.group().replace("<title>","").replace("'s Audio</title>","");
  29.             System.out.println("Nom de l'artiste : "+artistName);
  30.            
  31.             // on récupère les liens des fichiers audio à télécharger
  32.             Pattern p = Pattern.compile("<a class=\"feature\" href=\"http://www.newgrounds.com/audio/listen/[0-9]+\">");
  33.             Matcher matcher = p.matcher(src);
  34.             while (matcher.find()) {
  35.                 String dlLink = matcher.group().replace("<a class=\"feature\" href=\"", "").replace("\">","").replace("listen","download");
  36.                 links.add(dlLink);
  37.                 //System.out.println("lien trouve : "+dlLink);
  38.             }
  39.            
  40.             // on récupère les titres des fichiers audio à télécharger
  41.             Pattern p2 = Pattern.compile("<span class=\"ftitle\">[^<]+</span>");
  42.             Matcher matcher2 = p2.matcher(src);
  43.             while (matcher2.find()) {
  44.                 String songName = matcher2.group().replace("<span class=\"ftitle\">","").replace("</span>", "");
  45.                 titles.add(songName);
  46.                 //System.out.println("titre trouve : "+songName);
  47.             }
  48.            
  49.             System.out.println("Nb liens : "+links.size()+"\nNb titres : "+titles.size());
  50.            
  51.             for (int i = 0; i < links.size(); i++) {
  52.                 String fn = artistName+" - "+titles.get(i).split("- ")[titles.get(i).split("- ").length-1];
  53.                 fn = fn.replace("\\", "").replace("/", "").replace(":", "").replace("*", "").replace("<", "").replace(">", "").replace("|", "");
  54.                 System.out.println("Chanson "+(i+1)+"/"+links.size()+" : "+fn);
  55.                 if (!new File("Newgrounds Audio Portal/"+artistName+"/").exists()) {
  56.                     new File("Newgrounds Audio Portal/"+artistName+"/").mkdirs();
  57.                 }
  58.                 String filePath = "Newgrounds Audio Portal/"+artistName+"/"+fn+".mp3";
  59.                 saveFileFromURL(links.get(i), filePath);
  60.             }
  61.         }
  62.  
  63.     }
  64.    
  65.     public static void saveFileFromURL(String url_to_file, String path_of_file)
  66.     {
  67.         InputStream input = null;
  68.         FileOutputStream writeFile = null;
  69.  
  70.         try
  71.         {
  72.             URL url = new URL(url_to_file);
  73.             URLConnection connection = url.openConnection();
  74.             int fileLength = connection.getContentLength();
  75.  
  76.             if (fileLength == -1) {
  77.                 System.out.println("Invalide URL or file.");
  78.             } else {
  79.                    
  80.                 System.out.println("DL : "+url_to_file);
  81.                 input = connection.getInputStream();
  82.                 String fileName = path_of_file;
  83.                 writeFile = new FileOutputStream(fileName);
  84.                 byte[] buffer = new byte[1024];
  85.                 int read;
  86.    
  87.                 while ((read = input.read(buffer)) > 0)
  88.                     writeFile.write(buffer, 0, read);
  89.                 writeFile.flush();
  90.                
  91.                 System.out.println("OK : "+fileName+"\n------------------------------------------");
  92.                
  93.             }
  94.         }
  95.         catch (IOException e)
  96.         {
  97.             System.out.println("Error while trying to download the file.");
  98.             e.printStackTrace();
  99.         }
  100.         finally
  101.         {
  102.             try
  103.             {
  104.                 writeFile.close();
  105.                 input.close();
  106.             }
  107.             catch (IOException e)
  108.             {
  109.                 e.printStackTrace();
  110.             }
  111.         }
  112.     }
  113.  
  114.     public static String getSourceCode(String url) throws IOException {
  115.        
  116.         URL oracle = new URL(url);
  117.         BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
  118.  
  119.         String inputLine, ret = "";
  120.  
  121.         while ((inputLine = in.readLine()) != null) ret += inputLine;
  122.  
  123.         in.close();
  124.        
  125.         return ret;
  126.        
  127.     }
  128. }
  129.  
Add Comment
Please, Sign In to add comment