-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDouban.java
More file actions
226 lines (210 loc) · 5.93 KB
/
Douban.java
File metadata and controls
226 lines (210 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
/**
* 一个下载豆瓣音乐的脚本。直接输入列表url即可
*
* @author [email protected]
*
*/
public class Douban {
private InputStream getInputStream(String url)
throws MalformedURLException, IOException {
URLConnection con = new URL(url).openConnection();
con.setConnectTimeout(5000);
con.setReadTimeout(30000);
con.addRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; rv:10.0) Gecko/20100101 Firefox/10.0");
con.addRequestProperty("Referer", " http://douban.fm");
return con.getInputStream();
}
private String getString(String url) {
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
InputStream in = null;
try {
in = getInputStream(url);
br = new BufferedReader(new InputStreamReader(in, "utf-8"));
String line = null;
while ((line = br.readLine()) != null)
sb.append(line).append("\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
if (in != null)
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
/**
* 下载一个mp3(二进制)文件到本地文件
*
* @param url
* @param fn
*/
private void downloadMp3(String url, String fn) {
BufferedReader br = null;
InputStream in = null;
OutputStream out = null;
try {
System.out.println("0%\t" + url + "-->" + fn);
in = getInputStream(url);
/* for a cache* */
ByteArrayOutputStream ba = new ByteArrayOutputStream();
int b = -1;
while ((b = in.read()) != -1)
ba.write(b);
out = new FileOutputStream(new File(fn));
out.write(ba.toByteArray());
ba.close();
System.out.println("100%\t" + url + "-->" + fn);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
if (in != null)
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
if (out != null)
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void printUsage() {
System.out
.println("a script to download mp3 from douban.fm\njava Douban -t<threadnum> -d<dir to save mp3> -c<maxcount> -h<show the help> -u<music list>");
System.exit(0);
}
public static void main(String[] args) {
int thread = 20;
String _dir = "E:\\豆瓣音乐\\欧美\\";
int maxcount = 1000;
String _url = "http://douban.fm/j/mine/playlist?type=n&channel=2&from=mainsite&r=a09822fe44";
for (String arg : args) {
if (arg.startsWith("-t")) {
try {
thread = Integer.parseInt(arg.substring(2));
} catch (Exception e) {
System.err.println("thread must be a number,eg,-t20");
printUsage();
}
} else if (arg.startsWith("-d")) {
_dir = arg.substring(2);
if (!new File(_dir).isDirectory()) {
System.err.println(_dir + " must be a directory,check it");
printUsage();
}
} else if (arg.startsWith("-c")) {
try {
maxcount = Integer.parseInt(arg.substring(2));
} catch (Exception e) {
System.err.println("maxcount be a number,eg,-c1000");
printUsage();
}
} else if (arg.startsWith("-u")) {
_url = arg.substring(2);
} else
printUsage();
}
final String dir = _dir;
final String url = _url;
final Douban douban = new Douban();
final HashSet<String> urlCache = new HashSet<String>();
/* easy way to parse JSON* */
ScriptEngineManager sem = new ScriptEngineManager();
final ScriptEngine se = sem.getEngineByName("javascript");
ExecutorService e = Executors.newFixedThreadPool(thread);
for (int i = 0; i < maxcount; i++) {
final int j = i;
e.execute(new Runnable() {
@Override
public void run() {
System.out.println("Thread-" + j + " starting....");
String pl = douban.getString(url);
String script = null;
try {
HashMap<String, String> songs = new HashMap<String, String>();
se.put("songs", songs);
script = "var temp=eval("
+ pl.trim()
+ ");\ntemp=temp.song;\nif(typeof(temp)!=undefined)\nfor(var i in temp)\nsongs.put(temp[i].url,temp[i].artist+'-'+temp[i].title+'.mp3')";
se.eval(script);
System.out.println("Thread-" + j + " songs: "
+ songs.size());
for (String songurl : songs.keySet()) {
if (!urlCache.contains(songurl)) {
synchronized (urlCache) {
urlCache.add(songurl);
}
douban.downloadMp3(
songurl,
dir
+ songs.get(songurl)
.replace('\\', ' ')
.replace('/', ' ')
.replace(':', ' ')
.replace('*', ' ')
.replace('?', ' ')
.replace('"', ' ')
.replace('<', ' ')
.replace('|', ' ')
.replace('>', ' '));
} else
System.err.println(songurl + " ingnore!");
}
} catch (ScriptException e) {
e.printStackTrace();
System.err.println(script);
}
System.out.println("Thread-" + j + " stoped....");
}
});
}
e.shutdown();
try {
e.awaitTermination(30000 * maxcount, TimeUnit.SECONDS);
e.shutdownNow();
} catch (InterruptedException e1) {
e1.printStackTrace();
e.shutdownNow();
}
System.out.println("good finished!");
}
}