Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run JavaScript method in java?(I need to create program to download about 10k tracks) [closed]

In few days "Wirtualna Polska SA" closes "mp3.wp.pl" service, so I decided to download some music from there and save on my hard drive. Its about 150k(I dont need all btw.) of files so doing this by myself would be waste of time. I've create a program whose download all ids of specific genre and saves it into *.txt folder. To download music from website I need to use javascript method from theirs site download_utId('id'), where id is one int from *.txt file

  1. Can I run this method(download_utId) in java?
  2. How would you optimalize my code?
  3. Any other advices?

4.(EDIT.) How to save file with his own name? When u want to download "http://mp3.wp.pl/i/sciagnij?id=666&jakosc=hifi&streaming=0" default name of this file would be "Dub_Brother-Dance_Mission"


import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

public class Test {

    public static int numberOfPages = 1;

    public ArrayList<String> downloadPage(String page, String findAString, String endString){
        ArrayList<String> listOfIDs = new ArrayList<String>();
        URL url;
        InputStream is = null;
        DataInputStream dis;
        String line;

        try {
            url = new URL(page);
            is = url.openStream();  // throws an IOException
            dis = new DataInputStream(new BufferedInputStream(is));

            while ((line = dis.readLine()) != null) {
                if(line.contains("Liczba znalezionych utworów:")){
                    numberOfPages = numberOfPages(new Integer(line.substring(line.indexOf("<b>") + 3, line.lastIndexOf("</b>. Po"))));
                }
                if(line.contains(findAString)){
                    listOfIDs.add((line.substring(line.indexOf(findAString)+15, line.lastIndexOf(endString))));
                }
            }
        } catch (MalformedURLException mue) {
            mue.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException ioe) {
                // nothing to see here
            }
        }
        return listOfIDs;
    }

    public int numberOfPages(int numberOfSongs){
        return numberOfSongs - numberOfSongs/10*10 >= 5 ? numberOfSongs/10+1 : numberOfSongs/10;
    }

    public void addToFile(){
        ArrayList<String> hehe = new ArrayList<String>();
        for(int i = 0; i < numberOfPages; i++){
            String link = "http://mp3.wp.pl/p/strefa/utwory/E9,nazwa," + i + "0,+0,+0,+0.html";
            hehe.addAll(downloadPage(link, "download_utId('", "'"));
            System.out.print(link);
        }
        FileWriter writer = null;
        try {
            writer = new FileWriter("c:/output.txt");
            for(String str: hehe) {
                writer.write(str + " ");
            }
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        Test t = new Test();
        t.addToFile();
    }
}
like image 348
USER_ Avatar asked Nov 13 '22 14:11

USER_


1 Answers

There is a nice tool Rhino Utilities not sure if it is wort installing it but it is easy to use.

like image 175
d.raev Avatar answered Nov 15 '22 04:11

d.raev