-
Notifications
You must be signed in to change notification settings - Fork 119
/
BBC.java
338 lines (328 loc) · 15.2 KB
/
BBC.java
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* APDPlat - Application Product Development Platform
* Copyright (c) 2013, 杨尚川, yang-shangchuan@qq.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.apdplat.extractor.html.demo;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by ysc on 10/21/15.
* 下载BBC Learning English在线课程
*/
public class BBC {
private static int count = 0;
public static void main(String[] args) {
String path = "/Users/ysc/百度云同步盘/BBC/";
//***
//Courses
//***
download("english-you-need", 30, path);
download("towards-advanced", 30, path);
download("lower-intermediate", 30, path);
download("intermediate", 30, path);
download("upper-intermediate", 30, path);
download("shakespeare", 30, path);
download("emw", 17, path);
//***
//Features
//***
download("http://www.bbc.co.uk/learningenglish/english/features/english-at-university",
"English at University",
path);
download("http://www.bbc.co.uk/learningenglish/english/features/english-at-work",
"English at Work (New)",
path);
download("http://www.bbc.co.uk/learningenglish/english/features/news-report",
"News Report",
path);
download("http://www.bbc.co.uk/learningenglish/english/features/the-english-we-speak",
"The English We Speak",
path);
download("http://www.bbc.co.uk/learningenglish/english/features/lingohack",
"Lingohack",
path);
download("http://www.bbc.co.uk/learningenglish/english/features/6-minute-english",
"6 Minute English",
path);
download("http://www.bbc.co.uk/worldservice/learningenglish/grammar/pron/sounds/",
"The sounds of English",
path,
true,
true);
download("http://www.bbc.co.uk/learningenglish/english/features/drama",
"Dramas from BBC Learning English",
path);
download("http://www.bbc.co.uk/learningenglish/english/features/witn",
"Words in the News",
path);
//***
//archived version
//***
//General & Business English
download("http://www.bbc.co.uk/worldservice/learningenglish/general/sixminute/",
"6 Minute English Archived",
path);
download("http://www.bbc.co.uk/worldservice/learningenglish/general/englishatwork/",
"English at Work",
path);
download("http://www.bbc.co.uk/worldservice/learningenglish/general/expressenglish/",
"Express English",
path);
download("http://www.bbc.co.uk/worldservice/learningenglish/general/talkaboutenglish/",
"Talk about English",
path);
download("http://www.bbc.co.uk/worldservice/learningenglish/business/talkingbusiness/",
"Talking Business",
path);
download("http://www.bbc.co.uk/worldservice/learningenglish/business/wab/",
"Working Abroad",
path);
download("http://www.bbc.co.uk/worldservice/learningenglish/work/handy/",
"Handy Guide",
path);
//Grammar, Vocabulary & Pronunciation
download("http://www.bbc.co.uk/worldservice/learningenglish/language/wordsinthenews/",
"Words in the News Archived",
path);
download("http://www.bbc.co.uk/worldservice/learningenglish/language/theenglishwespeak/",
"The English We Speak Archived",
path);
download("http://www.bbc.co.uk/worldservice/learningenglish/language/theteacher/",
"The Teacher",
path);
download("http://www.bbc.co.uk/worldservice/learningenglish/language/newsextra/",
"News English Extra",
path);
download("http://www.bbc.co.uk/worldservice/learningenglish/language/newsaboutbritain/",
"News about Britain",
path);
download("http://www.bbc.co.uk/worldservice/learningenglish/language/askaboutenglish/",
"Ask about English",
path);
download("http://www.bbc.co.uk/worldservice/learningenglish/language/uptodate/",
"Keep your English up to date",
path);
download("http://www.bbc.co.uk/worldservice/learningenglish/language/faceup/",
"Face Up to Phrasals",
path);
//Talking Sport
download("http://www.bbc.co.uk/worldservice/learningenglish/talkingsport/",
"Talking Sport",
path);
//Specials
download("http://www.bbc.co.uk/worldservice/learningenglish/specials/",
"Specials",
path);
System.out.println("total file count: " + count);
}
public static void download(String entranceURL, String type, String path){
download(entranceURL, type, path, false, false);
}
/**
* 下载课程
* @param entranceURL 课程入口页面
* @param type 课程类型
* @param path 保存到本地的路径
* @param containEntrance 是否下载入口页面上的课程
* @param justOriginalName 是否使用原来的文件名称保存文件
*/
public static void download(String entranceURL, String type, String path, boolean containEntrance, boolean justOriginalName){
int timeout = 300000;
if(!entranceURL.endsWith("/")){
entranceURL += "/";
}
Set<String> urls = new HashSet<>();
boolean ok = false;
int limit=0;
while (!ok && (limit++) < 3) {
try {
System.out.println("【"+type+"】*** connect " + entranceURL);
for (Element element : Jsoup.connect(entranceURL).timeout(timeout).get().select("a")) {
String href = element.attr("href").trim();
if (!href.startsWith("http")) {
if(href.startsWith("/")){
href = "http://www.bbc.co.uk" + href;
}else{
href = entranceURL + href;
}
}
if (href.startsWith(entranceURL) && (!href.equals(entranceURL) || containEntrance) ) {
urls.add(href);
}
}
ok = true;
} catch (Exception e) {
System.out.println(e.getMessage() + " retry...");
}
}
AtomicInteger i = new AtomicInteger(1);
Set<String> resources = new HashSet<>();
urls.stream().sorted().forEach(url -> {
boolean success = false;
int times=0;
while (!success && (times++) < 3) {
try {
System.out.println(i.get() + "、connect " + url);
for (Element element : Jsoup.connect(url).timeout(timeout).get().select("a")) {
String href = element.attr("href").trim();
//只下载mp3、mp4、wav和pdf文件
if (href.endsWith(".mp3") || href.endsWith(".wav") || href.endsWith(".mp4") || href.endsWith(".pdf")) {
if (!href.startsWith("http")) {
if(href.startsWith("/")){
href = "http://www.bbc.co.uk" + href;
}else{
String[] attr = url.split("/");
href = url.substring(0, url.length()-attr[attr.length-1].length()) + href;
}
}
resources.add(href);
}
}
i.incrementAndGet();
success = true;
} catch (Exception e) {
System.out.println(e.getMessage() + " retry...");
}
}
});
AtomicInteger j = new AtomicInteger(1);
count += resources.size();
resources.stream().sorted().forEach(resource -> {
boolean success = false;
int times=0;
while (!success && (times++) < 3) {
try {
//提取文件名称
String[] attr = resource.split("/");
String fileName = attr[attr.length - 2] + "_" + attr[attr.length - 1].replace(attr[attr.length - 2], "");
if(attr[attr.length - 1].endsWith(attr[attr.length - 2])){
fileName = attr[attr.length - 1];
}
fileName = fileName.replace("_download", "");
if(justOriginalName){
fileName = attr[attr.length - 1];
}
System.out.println(resources.size() + "/" + j.get() + "、find resource: " + resource);
//确保本地路径存储
Path dir = Paths.get(path, type);
if (!Files.exists(dir)) {
//不存在则新建
dir.toFile().mkdirs();
}
//保存文件的完整本地路径
Path out = Paths.get(path, type, fileName);
//如果文件存在则表示之前已经下载过,本次不用下载
//因为BBC的访问不稳定,所以可能需要执行程序多次才能完整下载完毕,所以这里要处理已存在文件的问题
if (!Files.exists(out)) {
//下载文件
Connection.Response response = Jsoup.connect(resource).maxBodySize(0).ignoreContentType(true).timeout(timeout).execute();
//将文件保存到本地
Files.write(out, response.bodyAsBytes());
System.out.println(resources.size() + "/" + j.get() + "、save resource to: " + out);
} else {
System.out.println(resources.size() + "/" + j.get() + "、resource exist, don't need to download");
}
j.incrementAndGet();
success = true;
} catch (Exception e) {
System.out.println(e.getMessage() + " retry...");
}
}
});
}
/**
* BBC Learning English在线课程类型:
* 1、lower-intermediate http://www.bbc.co.uk/learningenglish/english/course/lower-intermediate
* 2、intermediate http://www.bbc.co.uk/learningenglish/english/course/intermediate
* 3、emw http://www.bbc.co.uk/learningenglish/english/course/emw
* @param type 课程类型
* @param unitCount 课数
* @param path 保存到本地的路径
*/
public static void download(String type, int unitCount, String path) {
int timeout = 300000;
Set<String> hrefs = new HashSet<>();
System.out.println("【"+type+"】*** starting... ");
for(int i=1; i<=unitCount; i++) {
int times=0;
boolean success = false;
while (!success && (times++) < 3) {
try {
String url = "http://www.bbc.co.uk/learningenglish/english/course/" + type + "/unit-" + i + "/downloads";
System.out.println("unit " + i + "、connect " + url);
for (Element element : Jsoup.connect(url).timeout(timeout).get().select("a")) {
String href = element.attr("href").trim();
//只下载mp3、mp4、wav和pdf文件
if (href.endsWith(".mp3") || href.endsWith(".wav") || href.endsWith(".mp4") || href.endsWith(".pdf")) {
hrefs.add(href);
}
}
success = true;
} catch (Exception e) {
System.out.println(e.getMessage()+" retry...");
}
}
}
AtomicInteger i = new AtomicInteger(1);
count += hrefs.size();
hrefs.stream().sorted().forEach(href -> {
int times=0;
boolean success = false;
while (!success && (times++) < 3) {
try {
//提取文件名称
String[] attr = href.split("/");
String fileName = attr[attr.length - 2] + "_" + attr[attr.length - 1].replace(attr[attr.length - 2], "");
if(attr[attr.length - 1].endsWith(attr[attr.length - 2])){
fileName = attr[attr.length - 1];
}
fileName = fileName.replace("_download", "");
System.out.println(hrefs.size() + "/" + i.get() + "、find resource: " + href);
//确保本地路径存储
Path dir = Paths.get(path, type);
if (!Files.exists(dir)) {
//不存在则新建
dir.toFile().mkdirs();
}
//保存文件的完整本地路径
Path out = Paths.get(path, type, fileName);
//如果文件存在则表示之前已经下载过,本次不用下载
//因为BBC的访问不稳定,所以可能需要执行程序多次才能完整下载完毕,所以这里要处理已存在文件的问题
if (!Files.exists(out)) {
//下载文件
Connection.Response response = Jsoup.connect(href).maxBodySize(0).ignoreContentType(true).timeout(timeout).execute();
//将文件保存到本地
Files.write(out, response.bodyAsBytes());
System.out.println(hrefs.size() + "/" + i.get() + "、save resource to: " + out);
} else {
System.out.println(hrefs.size() + "/" + i.get() + "、resource exist, don't need to download");
}
i.incrementAndGet();
success = true;
} catch (Exception e) {
System.out.println(e.getMessage()+" retry...");
}
}
});
}
}