-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyCrawler.java
184 lines (175 loc) · 5.77 KB
/
MyCrawler.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
import java.io.FileWriter;
import java.io.IOException;
import java.util.Set;
import java.util.regex.Pattern;
import edu.uci.ics.crawler4j.crawler.Page;
import edu.uci.ics.crawler4j.crawler.WebCrawler;
import edu.uci.ics.crawler4j.parser.HtmlParseData;
import edu.uci.ics.crawler4j.url.WebURL;
public class MyCrawler extends WebCrawler {
private static final String COMMA_DELIMITER = ",";
private static final String NEW_LINE_SEPARATOR = "\n";
private static final String FETCH = "data/fetch_NewsSite.csv";
private static final String VISIT = "data/visit_NewsSite.csv";
private static final String TOTAL = "data/urls_NewsSite.csv";
private static FileWriter fileWriter_FETCH = null;
private static FileWriter fileWriter_VISIT = null;
private static FileWriter fileWriter_TOTAL = null;
@Override
public void onBeforeExit() {
// TODO Auto-generated method stub
if (fileWriter_FETCH != null) {
try {
fileWriter_FETCH.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fileWriter_VISIT != null) {
try {
fileWriter_VISIT.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fileWriter_TOTAL != null) {
try {
fileWriter_TOTAL.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
super.onBeforeExit();
}
@Override
public void onStart() {
// TODO Auto-generated method stub
try {
if (fileWriter_FETCH == null) {
fileWriter_FETCH = new FileWriter(FETCH);
}
if (fileWriter_VISIT == null) {
fileWriter_VISIT = new FileWriter(VISIT);
}
if (fileWriter_TOTAL == null) {
fileWriter_TOTAL = new FileWriter(TOTAL);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onStart();
}
private final static Pattern FILTERS_TEXT = Pattern.compile(".*(\\.(html|pdf|doc))$");
private final static Pattern FILTERS_IMAGE = Pattern.compile(".*(\\.(gif|jpeg|png))$");
private final static String root_http = "http://www.nytimes.com/";
private final static String root_https = "https://www.nytimes.com/";
/**
* This method receives two parameters. The first parameter is the page
* in which we have discovered this new url and the second parameter is
* the new url. You should implement this function to specify whether
* the given url should be crawled or not (based on your crawling logic).
* In this example, we are instructing the crawler to ignore urls that
* have css, js, git, ... extensions and to only accept urls that start
* with "http://www.viterbi.usc.edu/". In this case, we didn't need the
* referringPage parameter to make the decision.
*/
@Override
public boolean shouldVisit(Page referringPage, WebURL url) {
String href = url.getURL().toLowerCase().trim();
try {
String content = "";
if (href.startsWith(root_http) || href.startsWith(root_https)) {
content = href + COMMA_DELIMITER + "OK" + NEW_LINE_SEPARATOR;
} else {
content = href + COMMA_DELIMITER + "N_OK" + NEW_LINE_SEPARATOR;
}
fileWriter_TOTAL.write(content);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return isValid(href);
}
@Override
protected void handlePageStatusCode(WebURL webUrl, int statusCode,
String statusDescription) {
// TODO Auto-generated method stub
String href = webUrl.getURL().toLowerCase().trim();
if (!href.startsWith(root_http) && !href.startsWith(root_https)) {
href = root_http + href;
}
String details = href + COMMA_DELIMITER + statusCode + NEW_LINE_SEPARATOR;
try {
fileWriter_FETCH.write(details);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.handlePageStatusCode(webUrl, statusCode, statusDescription);
}
private boolean isValid(String href) {
if (href == null || href.length() == 0) {
return false;
}
if (href.equals(root_http) || href.equals(root_https)) {
return true;
}
if (href.startsWith(root_http) || href.startsWith(root_https)) {
if (FILTERS_TEXT.matcher(href).matches() || FILTERS_IMAGE.matcher(href).matches()) {
return true;
} else {
return false;
}
}
return false;
}
/**
* This function is called when a page is fetched and ready
* to be processed by your program.
*/
@Override
public void visit(Page page) {
String url = page.getWebURL().getURL().trim();
// System.out.println("URL: " + url);
int linkSize = 0;
if (page.getParseData() instanceof HtmlParseData) {
HtmlParseData htmlParseData = (HtmlParseData) page.getParseData();
// String text = htmlParseData.getText();
// String html = htmlParseData.getHtml();
Set<WebURL> links = htmlParseData.getOutgoingUrls();
// System.out.println("Status: " + page.getStatusCode());
// System.out.println("Text length: " + text.length());
// System.out.println("Html length: " + html.length());
// System.out.println("Number of outgoing links: " + links.size());
linkSize = links.size();
}
if (url == null || url.length() == 0) {
url = root_https;
}
if (!url.startsWith(root_http) && !url.startsWith(root_https)) {
url = root_http + url;
}
int pageSize = 0;
try {
if (page.getStatusCode() < 300 && page.getStatusCode() >= 200) {
pageSize = page.getContentData().length;
String details = url + COMMA_DELIMITER + pageSize
+ COMMA_DELIMITER + linkSize +
COMMA_DELIMITER;
String type = page.getContentType() == null ? "text/html" : page.getContentType();
if (type.startsWith("text/html")) {
type = "text/html";
}
details += type + NEW_LINE_SEPARATOR;
fileWriter_VISIT.write(details);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}