Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ng.dl;
- import java.io.*;
- import java.net.*;
- import java.util.ArrayList;
- import java.util.regex.*;
- public class ngdl {
- public static void main(String[] args) throws Exception {
- 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");
- else {
- ArrayList<String> links = new ArrayList<String>();
- ArrayList<String> titles = new ArrayList<String>();
- // on récupère le code source de la page
- System.out.println("Recuperation du code source de la page...");
- String src = getSourceCode("http://"+args[0]+".newgrounds.com/audio/");
- System.out.println("Code source récupéré.");
- // on récupère le nom de l'artiste (titre de la page)
- String artistName = "Unknown Artist";
- Pattern p0 = Pattern.compile("<title>.+'s Audio</title>");
- Matcher matcher0 = p0.matcher(src);
- matcher0.find();
- artistName = matcher0.group().replace("<title>","").replace("'s Audio</title>","");
- System.out.println("Nom de l'artiste : "+artistName);
- // on récupère les liens des fichiers audio à télécharger
- Pattern p = Pattern.compile("<a class=\"feature\" href=\"http://www.newgrounds.com/audio/listen/[0-9]+\">");
- Matcher matcher = p.matcher(src);
- while (matcher.find()) {
- String dlLink = matcher.group().replace("<a class=\"feature\" href=\"", "").replace("\">","").replace("listen","download");
- links.add(dlLink);
- //System.out.println("lien trouve : "+dlLink);
- }
- // on récupère les titres des fichiers audio à télécharger
- Pattern p2 = Pattern.compile("<span class=\"ftitle\">[^<]+</span>");
- Matcher matcher2 = p2.matcher(src);
- while (matcher2.find()) {
- String songName = matcher2.group().replace("<span class=\"ftitle\">","").replace("</span>", "");
- titles.add(songName);
- //System.out.println("titre trouve : "+songName);
- }
- System.out.println("Nb liens : "+links.size()+"\nNb titres : "+titles.size());
- for (int i = 0; i < links.size(); i++) {
- String fn = artistName+" - "+titles.get(i).split("- ")[titles.get(i).split("- ").length-1];
- fn = fn.replace("\\", "").replace("/", "").replace(":", "").replace("*", "").replace("<", "").replace(">", "").replace("|", "");
- System.out.println("Chanson "+(i+1)+"/"+links.size()+" : "+fn);
- if (!new File("Newgrounds Audio Portal/"+artistName+"/").exists()) {
- new File("Newgrounds Audio Portal/"+artistName+"/").mkdirs();
- }
- String filePath = "Newgrounds Audio Portal/"+artistName+"/"+fn+".mp3";
- saveFileFromURL(links.get(i), filePath);
- }
- }
- }
- public static void saveFileFromURL(String url_to_file, String path_of_file)
- {
- InputStream input = null;
- FileOutputStream writeFile = null;
- try
- {
- URL url = new URL(url_to_file);
- URLConnection connection = url.openConnection();
- int fileLength = connection.getContentLength();
- if (fileLength == -1) {
- System.out.println("Invalide URL or file.");
- } else {
- System.out.println("DL : "+url_to_file);
- input = connection.getInputStream();
- String fileName = path_of_file;
- writeFile = new FileOutputStream(fileName);
- byte[] buffer = new byte[1024];
- int read;
- while ((read = input.read(buffer)) > 0)
- writeFile.write(buffer, 0, read);
- writeFile.flush();
- System.out.println("OK : "+fileName+"\n------------------------------------------");
- }
- }
- catch (IOException e)
- {
- System.out.println("Error while trying to download the file.");
- e.printStackTrace();
- }
- finally
- {
- try
- {
- writeFile.close();
- input.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- public static String getSourceCode(String url) throws IOException {
- URL oracle = new URL(url);
- BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
- String inputLine, ret = "";
- while ((inputLine = in.readLine()) != null) ret += inputLine;
- in.close();
- return ret;
- }
- }
Add Comment
Please, Sign In to add comment