5

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();
    }
}
USER_
  • 63
  • 4
  • 7
    You'll probably find it easier to reverse engineer the JS and reimplement it in Java then to create a suitable environment for it to run in. – Quentin Dec 19 '12 at 14:17
  • 5
    This is all perfectly legal of course. ;) – Peter Lawrey Dec 19 '12 at 14:24
  • ofcource it is, he's only downloading tracks he legally has the rights to, there is no piracy on the internet. – G-Man Dec 19 '12 at 14:51
  • @Peter Lawrey YES, it's all legal - look in regulations of this page – USER_ Dec 19 '12 at 15:21
  • @Quentin UR GENIUS!, i decompiled this .swf and i saw this: if (_arg1 != null){navigateToURL(new URLRequest((("http://mp3.wp.pl/i/sciagnij?id=" + _arg1) + "&jakosc=hifi&streaming=0")), "_self");}; – USER_ Dec 19 '12 at 15:46

1 Answers1

1

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

d.raev
  • 9,216
  • 8
  • 58
  • 79
  • 1st of all: Use simple methods, dont make ur life harder ;) but anyway thanks for respond. – USER_ Dec 19 '12 at 16:22