数日で「WirtualnaPolskaSA」が「mp3.wp.pl」サービスを終了したので、そこから音楽をダウンロードしてハードドライブに保存することにしました。その約150k(私はすべてのところで必要ではありません)のファイルなので、これを自分で行うのは時間の無駄になります。特定のジャンルのすべてのIDをダウンロードして*.txtフォルダーに保存するプログラムを作成しました。ウェブサイトから音楽をダウンロードするには、彼らのサイトdownload_utId('id')からjavascriptメソッドを使用する必要があります。ここで、idは*.txtファイルからの1つの整数です。
- このメソッド(download_utId)をJavaで実行できますか?
- 私のコードをどのように最適化しますか?
- 他にアドバイスはありますか?
4.(編集)自分の名前でファイルを保存する方法は?「http://mp3.wp.pl/i/sciagnij?id=666&jakosc=hifi&streaming=0」をダウンロードする場合、このファイルのデフォルト名は「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();
}
}