-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuperFastDemo.java
62 lines (48 loc) · 1.72 KB
/
SuperFastDemo.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
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
class SuperFastDemo {
private int numRequests = 0;
public SuperFastDemo() {
}
private String stripWikipediaTitlePrefixAndSuffix(ParsedPage page) {
String title = page.getTitle().substring("Wikipedia -".length());
int pos = title.lastIndexOf(" - Wikipedia");
if (pos >= 0) {
title = title.substring(0, pos);
}
return title;
}
private Document fetch(String url) throws IOException {
Document doc = Jsoup.connect(url).get();
numRequests += 1;
return doc;
}
public Document fetchRandomWikipediaArticle() throws IOException {
return fetch("https://en.wikipedia.org/wiki/Special:Random");
}
public boolean testRandomArticle(String theTitle) {
if (theTitle.startsWith("The")) {
return false;
}
String firstChar = theTitle.substring(0, 1);
return "AEIOUSTLNRHD".contains(firstChar);
}
public void gatherArticleStats(int numArticles) {
for (int counter = 0; counter < numArticles; counter++) {
try {
Document doc = fetchRandomWikipediaArticle();
ParsedPage article = new ParsedPage(doc);
String cleanTitle = stripWikipediaTitlePrefixAndSuffix(article);
if (testRandomArticle(cleanTitle)) {
System.out.println(cleanTitle);
} else {
System.out.println("Skipping #" + numRequests);
}
Thread.sleep(500);
} catch (Exception e) {
System.out.println("Uh oh: " + e);
}
}
}
}